def __init__(self, instance):
        self.instance = instance
        KSEPanel.__init__(self)
        self.butAddAtlas = Button("Create atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butImportAtlas = Button("Import atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAnimation = Button("Add animation", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAtlas.connect("clicked", self.instance.addAtlasCb)
        self.butImportAtlas.connect("clicked", self.instance.importAtlasCb)

        pixbufcol = gtk.TreeViewColumn("Icon")
        pixbufcol.set_expand(False)
        self.listview.append_column(pixbufcol)
        pixcell = gtk.CellRendererPixbuf()
        pixcell.props.xpad = 6
        pixbufcol.pack_start(pixcell)
        pixbufcol.add_attribute(pixcell, 'pixbuf', 1)

        self.selectedSprite = None
        self.width = 32
        self.height = 32
        self._addSpinButton("Base Width : ", self._widthChangedCb)
        self._addSpinButton("Base Height : ", self._heightChangedCb)
        self.spinX = self._addSpinButton("Selected X : ", self._selectedXChangedCb)
        self.spinY = self._addSpinButton("Selected Y : ", self._selectedYChangedCb)
        self.spinWidth = self._addSpinButton("Selected Width : ", self._selectedWidthChangedCb)
        self.spinHeight = self._addSpinButton("Selected Height : ", self._selectedHeightChangedCb)

        self.show_all()
    def __init__(self, instance):
        KSEPanel.__init__(self)
        self.instance = instance
        self.butAddAtlas = Button("Add atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAnimation = Button("Add animation", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddResource = Button("Add resource image", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAtlas.connect("clicked", self.instance.addAtlasCb)
        self.butAddResource.connect("clicked", self._addResourcesCb)

        self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str)
        self.listview = gtk.TreeView()
        self.listview.set_model(self.model)
        scrolledWindow = gtk.ScrolledWindow()
        scrolledWindow.add_with_viewport(self.listview)
        self.pack_start(scrolledWindow, True, True, 0)
        resources = gtk.TreeViewColumn("Resources")
        cell = gtk.CellRendererText()
        resources.pack_start(cell, True)
        resources.add_attribute(cell, "text", 0)
        self.listview.append_column(resources)

        pixbufcol = gtk.TreeViewColumn("Icon")
        pixbufcol.set_expand(False)
        self.listview.append_column(pixbufcol)
        pixcell = gtk.CellRendererPixbuf()
        pixcell.props.xpad = 6
        pixbufcol.pack_start(pixcell)
        pixbufcol.add_attribute(pixcell, 'pixbuf', 1)

        self.listview.connect("row-activated", self.instance.rowActivatedCb, self.model)
        self.width = 32
        self.height = 32
        self._addSpinButton("width : ", self._widthChangedCb)
        self._addSpinButton("height : ", self._heightChangedCb)

        self.show_all()
 def __init__(self):
     gtk.VBox.__init__(self, False, 0)
     self.show()
     self.set_size_request(125, -1)
     self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str)
     self.listview = gtk.TreeView()
     self.listview.set_model(self.model)
     scrolledWindow = gtk.ScrolledWindow()
     scrolledWindow.add_with_viewport(self.listview)
     self.pack_end(scrolledWindow, True, True, 0)
     resources = gtk.TreeViewColumn("Resources")
     cell = gtk.CellRendererText()
     resources.pack_start(cell, True)
     resources.add_attribute(cell, "text", 0)
     self.listview.append_column(resources)
     self.listview.connect("row-activated", self.instance.rowActivatedCb, self.model)
     self.butAddResource = Button("Add resource", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
     self.butAddResource.connect("clicked", self._addResourcesCb)
Example #4
0
class Editor:
    # tools
    button_text_box = Button(DIM_TOOLS, (0, POS_TY), C.XLIGHT_GREY,'TextBox')
    button_button = Button(DIM_TOOLS, (0, POS_TY+80), C.XLIGHT_GREY,'Button')
    button_input_text = Button(DIM_TOOLS, (0, POS_TY+160), C.XLIGHT_GREY,'InputText')
    button_cadre = Button(DIM_TOOLS, (0, POS_TY+240), C.XLIGHT_GREY,'Cadre')
    button_done = Button(DIM_BDONE, (2800, 1400), C.LIGHT_BLUE,'Done',font=Font.f(30))
    drag_surf = None
    objs = []
    state = 'running'

    # set attr here for on_resize method
    window = None
    TOP_LEFT     = [0,0]
    TOP_RIGHT    = [0,0]
    BOTTOM_LEFT  = [0,0]
    BOTTOM_RIGHT = [0,0]

    current_selected = None

    @classmethod
    def set_window(cls, window):
        cls.window = window
        # don't rescale window.dim to have the original dimension stored (for on_resize)
        dim = window.dim
        cls.cadre = Cadre(dim, POS_WIN, C.WHITE)
        cls.TOP_LEFT     = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
        cls.TOP_RIGHT    = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
        cls.BOTTOM_LEFT  = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
        cls.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )

    @staticmethod
    def on_resize(factor):
        if Editor.window:
            dim = Editor.window.dim
            Editor.TOP_LEFT     = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
            Editor.TOP_RIGHT    = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
            Editor.BOTTOM_LEFT  = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
            Editor.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )

    @classmethod
    def create_obj(cls, pos, objtype):
        gui_obj = GuiObj(pos, C.WHITE, objtype)
        cls.objs.append(gui_obj)
        cls.current_selected = gui_obj
        Settings.set_obj(gui_obj)

    @classmethod
    def check_deselect(cls):
        if cls.current_selected:
            if cls.cadre.on_it() and not cls.current_selected.as_change_dim:
                if not cls.current_selected.changing_dim and not cls.current_selected.on_it():
                    return True
        return False

    @classmethod
    def check_done(cls):
        for gobj in cls.objs:
            if not gobj.name:
                return
        return True

    @classmethod
    def str_gobj(cls, gobj):
        '''
        Name, objtype, dim, pos, text, color, font
        '''
        string = gobj.name + '\n'
        string += gobj.objtype + '\n'
        string += f'{gobj.dim[0]} {gobj.dim[1]}\n'
        pos = gobj.get_real_pos()
        string += f'{pos[0]} {pos[1]}\n'
        string += gobj.input_text.content + '\n'
        string += str(gobj.color) + '\n' # can be either str or list
        string += str(gobj.input_text.font['size'])
        return string

    @classmethod
    def create_savefile(cls):
        # create file
        with open(cls.window.name+'.pygui','w') as file:
            sep = '\n'+SEPARATOR+'\n'
            # first write name, dim of window
            global_info = f'{cls.window.name} {cls.window.dim[0]} {cls.window.dim[1]}'
            file.write(global_info)
            file.write(sep)
            for gobj in cls.objs:
                file.write(cls.str_gobj(gobj))
                file.write(sep)

    @classmethod
    def display(cls):
        cls.cadre.display()
        cls.button_text_box.display()
        cls.button_button.display()
        cls.button_input_text.display()
        cls.button_cadre.display()
        cls.button_done.display()
        if cls.drag_surf:
            cls.drag_surf.run()
        
        for gui_obj in cls.objs:
            gui_obj.display()
    
    @classmethod
    def react_events(cls, events, pressed):
        pos = pygame.mouse.get_pos()
        
        Settings.run(events, pressed)

        # first check for changing dim
        for gobj in cls.objs:
            gobj.react_events(events, pressed)
        
        # check if click anywhere -> deselect gui obj
        for event in events:
            if event.type == pygame.MOUSEBUTTONUP:
                if cls.check_deselect():
                    cls.current_selected.selected = False
                    cls.current_selected = None
                    Settings.deselect()

        # then check for new selected
        for gobj in cls.objs:
            if gobj.pushed(events):
                if not cls.current_selected:
                    cls.current_selected = gobj
                    gobj.selected = True  
                    Settings.set_obj(gobj)
        
        # check for gui obj to remove
        if pressed[pygame.K_DELETE]:
            if cls.current_selected:
                cls.objs.remove(cls.current_selected)
                cls.current_selected = None
                Settings.deselect()

        # check for deselect by enter
        if pressed[pygame.K_RETURN]:
            if cls.current_selected:
                cls.current_selected.selected = False
                cls.current_selected = None
                Settings.deselect()

        # last check for new gui obj
        if cls.drag_surf:
            for event in events:
                if event.type == pygame.MOUSEBUTTONUP:
                    # set new obj on window
                    cls.create_obj(pos,cls.drag_surf.objtype)
                    # reset drag_surf
                    cls.drag_surf = None
        
        if cls.button_text_box.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'TextBox')
        elif cls.button_button.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Button')
        elif cls.button_input_text.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'InputText')
        elif cls.button_cadre.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Cadre')
        
        # end edition
        if cls.button_done.pushed(events):
            cls.create_savefile()
            cls.state = 'done'
Example #5
0
    for row, new_row in zip(window.window, new_window.window):
        for cell, new_cell in zip(row, new_row):
            neighbours = cell.get_neighbours(window)
            alive = count_alive_neighbours(neighbours)
            if cell.state == 0 and alive == 3:
                new_cell.state = 1
            elif cell.state == 1 and alive in [2, 3]:
                new_cell.state = 1
            elif cell.state == 1:
                new_cell.state = 0
    return new_window


generation = 1
window = Window(500, 500)
gen_button = Button(510, 40, 180, 40)
active_cells_button = Button(510, 100, 180, 40)
start_button = Button(550, 160, 100, 40, True)
stop_button = Button(550, 210, 100, 40, True)
save_button = Button(550, 270, 100, 40, True)
read_button = Button(550, 320, 100, 40, True)


def draw_interface(screen, window, generation):
    gen_button.draw(screen, 'Generation: {}'.format(generation))
    active_cells_button.draw(
        screen, 'Active cells: {}'.format(window.count_active_cells()))
    start_button.draw(screen, ' START')
    stop_button.draw(screen, '  STOP')
    save_button.draw(screen, '  SAVE')
    read_button.draw(screen, '  READ')
Example #6
0
class PromMenu:
    def __init__(self, player, pos):

        if player.color == 'white':
            queen_img = white_queen
            bishop_img = white_bishop
            rock_img = white_rock
            knight_img = white_knight
        else:
            queen_img = black_queen
            bishop_img = black_bishop
            rock_img = black_rock
            knight_img = black_knight

        self.button_queen = Button(DIM_PBUTTON, (pos[0] + 50, pos[1]),
                                   surface=queen_img,
                                   surf_font_color=C.LIGHT_GREY)
        self.button_bishop = Button(DIM_PBUTTON, (pos[0] + 300, pos[1]),
                                    surface=bishop_img,
                                    surf_font_color=C.LIGHT_GREY)
        self.button_rock = Button(DIM_PBUTTON, (pos[0] + 50, pos[1] + 250),
                                  surface=rock_img,
                                  surf_font_color=C.LIGHT_GREY)
        self.button_knight = Button(DIM_PBUTTON, (pos[0] + 300, pos[1] + 250),
                                    surface=knight_img,
                                    surf_font_color=C.LIGHT_GREY)
        self.state = 'wait'

    def react_events(self, events, pressed):
        if self.button_knight.pushed(events):
            self.state = 'done'
            self.piece_name = 'knight'
        if self.button_queen.pushed(events):
            self.state = 'done'
            self.piece_name = 'queen'
        if self.button_rock.pushed(events):
            self.state = 'done'
            self.piece_name = 'rock'
        if self.button_bishop.pushed(events):
            self.state = 'done'
            self.piece_name = 'bishop'

    def display(self):
        self.button_queen.display()
        self.button_bishop.display()
        self.button_rock.display()
        self.button_knight.display()
Example #7
0
class ColorRange:
    '''
    Graphical interface to choose colors

    Advise: choose a dimension with a 9/5 ration

    Selected color stored in: chosen_color

    '''
    def __init__(self, dim, pos):

        dim_cr = (round(dim[0] * 5 / 9), dim[1])
        dim_bar = (round(dim[0] * 1 / 18), dim[1])
        dim_ptr = (round(dim[0] / 90), round(dim[0] / 90))
        dim_view = (round(dim[0] * 5 / 18), dim[1])

        pos_ptr = (round(pos[0] + dim_cr[0] / 2),
                   round(pos[1] + dim_cr[1] / 2))
        pos_bar = (pos[0] + dim_cr[0] + dim_bar[0], pos[1])
        pos_view = (pos[0] + dim_cr[0] + 3 * dim_bar[0], pos[1])

        self.cadre = Cadre(dim, pos, set_transparent=True)
        # create color range img
        self.arr_color = get_color_range([255, 0, 0])
        self.color_range = Button(dim_cr,
                                  pos,
                                  surface=self.arr_color,
                                  highlight=False)

        self.pointer = Form(dim_ptr, pos_ptr, C.LIGHT_GREY)
        self.bar_pointer = Form((dim_bar[0], dim_ptr[0]), pos_bar,
                                C.LIGHT_GREY)

        self.arr_bar = create_color_bar(150)
        self.color_bar = Button(dim_bar,
                                pos_bar,
                                surface=self.arr_bar,
                                highlight=False)

        self.color_view = Form(dim_view, pos_view)

        self.range_active = False
        self.bar_active = False
        self.chosen_color = None

    def react_events(self, events, pressed):
        if self.color_range.pushed(events):
            self.range_active = not self.range_active  # activate/deactivate color selection

        if self.color_bar.pushed(events):
            self.bar_active = not self.bar_active

        # update pointer position
        if self.range_active:
            mouse_pos = Interface.mouse_pos
            # check that pointer is still on surf
            if self.color_range.on_it():
                self.pointer.set_pos(mouse_pos, center=True)
            else:
                self.range_active = False

        # update bar pointer position
        if self.bar_active:
            mouse_pos = Interface.mouse_pos
            # check that pointer is still on surf
            if self.color_bar.on_it():
                self.bar_pointer.set_pos(
                    (self.bar_pointer.pos[0], mouse_pos[1]))
            else:
                self.bar_active = False
                # set last color range
                self.set_color_range()

    def set_color_range(self):
        '''Create a new color range based on the current bar color'''
        # first get base_color
        y = self.bar_pointer.pos[1] - self.cadre.pos[1]
        y = int(y * 6 * 256 / self.cadre.dim[1])

        base_color = self.arr_bar[0, y, :]

        # create a new color range
        self.arr_color = get_color_range(base_color)
        self.color_range.set_surf(surface=self.arr_color)

    @set_color_range_deco
    def check_set_color_range(self):
        '''Use a delayer to avoid freaking out the cpu for no reason (creating the color range is quite heavy)'''
        if self.bar_active:
            self.set_color_range()
            return True

    def set_view_color(self):
        '''
        Get the selected color in color range, get the correct pixel of arr_color and set color_view's color
        '''
        # get color
        x = self.pointer.pos[0] - self.cadre.pos[0]
        x = int(x * 256 / self.color_range.dim[0])
        y = self.pointer.pos[1] - self.cadre.pos[1]
        y = int(y * 256 / self.color_range.dim[1])

        if x < 0:
            x = 0
        if y < 0:
            y = 0

        color = self.arr_color[x, y, :]
        # set new color
        self.color_view.set_color(color)
        # store color
        self.chosen_color = list(color)

    def display(self):
        # update colors
        self.set_view_color()
        self.check_set_color_range()
        # display everything
        self.color_range.display()
        self.color_bar.display()
        self.pointer.display()
        self.bar_pointer.display()
        self.color_view.display()
        self.cadre.display()
class GraphicsPanel(KSEPanel):
    """
    This panel contains a treeview listing the image sources for the opened
    project. The model of this treeview is a list of strings :
    [0] -> Name of the resource.
    [1] -> Path of the resource.
    Width and height attributes describe the desired size of the next merge,
    and can be modified from the UI.
    """
    #TODO : handle Aspect Ratio
    #TODO : use gstreamer discoverer to display metadata about the files.
    def __init__(self, instance):
        KSEPanel.__init__(self)
        self.instance = instance
        self.butAddAtlas = Button("Add atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAnimation = Button("Add animation", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddResource = Button("Add resource image", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAtlas.connect("clicked", self.instance.addAtlasCb)
        self.butAddResource.connect("clicked", self._addResourcesCb)

        self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str)
        self.listview = gtk.TreeView()
        self.listview.set_model(self.model)
        scrolledWindow = gtk.ScrolledWindow()
        scrolledWindow.add_with_viewport(self.listview)
        self.pack_start(scrolledWindow, True, True, 0)
        resources = gtk.TreeViewColumn("Resources")
        cell = gtk.CellRendererText()
        resources.pack_start(cell, True)
        resources.add_attribute(cell, "text", 0)
        self.listview.append_column(resources)

        pixbufcol = gtk.TreeViewColumn("Icon")
        pixbufcol.set_expand(False)
        self.listview.append_column(pixbufcol)
        pixcell = gtk.CellRendererPixbuf()
        pixcell.props.xpad = 6
        pixbufcol.pack_start(pixcell)
        pixbufcol.add_attribute(pixcell, 'pixbuf', 1)

        self.listview.connect("row-activated", self.instance.rowActivatedCb, self.model)
        self.width = 32
        self.height = 32
        self._addSpinButton("width : ", self._widthChangedCb)
        self._addSpinButton("height : ", self._heightChangedCb)

        self.show_all()

    def loadProjectResources(self, node):
        """
        @param node: resources/graphics/resources xml Node
        """
        for elem in node.findall("resource"):
            pixbuf = gtk.gdk.pixbuf_new_from_file(elem.attrib["path"])
            pixbuf = pixbuf.scale_simple(64, 64, gtk.gdk.INTERP_BILINEAR)
            self.model.append([elem.attrib["name"], pixbuf, elem.attrib["path"]])

    #INTERNAL

    def _addSpinButton(self, text, function):
        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label(text), False, False, 0)
        adj = gtk.Adjustment(32, 1.0, 102400.0, 1.0, 5.0, 0.0)
        spinbutton = gtk.SpinButton(adj, 0, 0)
        spinbutton.set_wrap(True)
        spinbutton.show()
        spinbutton.connect("value_changed", function)
        hbox.pack_start(spinbutton, True, True, 0)
        hbox.show_all()
        self.pack_end(hbox, False, False, 0)

    def _widthChangedCb(self, spinner):
        self.width = spinner.get_value()

    def _heightChangedCb(self, spinner):
        self.height = spinner.get_value()

    def _addResourcesCb(self, unused):
        uris = None
        chooser = gtk.FileChooserDialog(title = "Choose location", action = gtk.FILE_CHOOSER_ACTION_OPEN,
                                        buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                                   gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
        chooser.set_select_multiple(True)
        try:
            pw = PreviewWidget(self.instance.app)
            chooser.set_preview_widget(pw)
            chooser.set_use_preview_label(False)
            chooser.connect('update-preview', pw.add_preview_request)
        except AttributeError:
            print "We must set the pythonpath"
        if chooser.run() == gtk.RESPONSE_ACCEPT:
            uris = chooser.get_filenames()
        chooser.destroy()
        if uris:
            self.instance.addImagesToResources(uris)
            for uri in uris:
                pixbuf = gtk.gdk.pixbuf_new_from_file(uri)
                pixbuf = pixbuf.scale_simple(64, 64, gtk.gdk.INTERP_BILINEAR)
                self.model.append([get_name_from_uri(uri), pixbuf, uri])
class GraphicsPanel(KSEPanel):
    """
    This panel contains a treeview listing the image sources for the opened
    project. The model of this treeview is a list of strings :
    [0] -> Name of the resource.
    [1] -> Path of the resource.
    Width and height attributes describe the desired size of the next merge,
    and can be modified from the UI.
    """
    #TODO : handle Aspect Ratio
    #TODO : use gstreamer discoverer to display metadata about the files.
    def __init__(self, instance):
        self.instance = instance
        KSEPanel.__init__(self)
        self.butAddAtlas = Button("Create atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butImportAtlas = Button("Import atlas", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAnimation = Button("Add animation", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddAtlas.connect("clicked", self.instance.addAtlasCb)
        self.butImportAtlas.connect("clicked", self.instance.importAtlasCb)

        pixbufcol = gtk.TreeViewColumn("Icon")
        pixbufcol.set_expand(False)
        self.listview.append_column(pixbufcol)
        pixcell = gtk.CellRendererPixbuf()
        pixcell.props.xpad = 6
        pixbufcol.pack_start(pixcell)
        pixbufcol.add_attribute(pixcell, 'pixbuf', 1)

        self.selectedSprite = None
        self.width = 32
        self.height = 32
        self._addSpinButton("Base Width : ", self._widthChangedCb)
        self._addSpinButton("Base Height : ", self._heightChangedCb)
        self.spinX = self._addSpinButton("Selected X : ", self._selectedXChangedCb)
        self.spinY = self._addSpinButton("Selected Y : ", self._selectedYChangedCb)
        self.spinWidth = self._addSpinButton("Selected Width : ", self._selectedWidthChangedCb)
        self.spinHeight = self._addSpinButton("Selected Height : ", self._selectedHeightChangedCb)

        self.show_all()
        
    def updateSelectedSprite(self, selected, graphic):
        self.selectedSprite = selected
        self.graphic = graphic
        
        if selected is not None:
            self.spinX.set_value(selected.texturex)
            self.spinY.set_value(selected.texturey)
            self.spinWidth.set_value(selected.texturew)
            self.spinHeight.set_value(selected.textureh)


    #INTERNAL
    
    def _selectedXChangedCb(self, spinner):
        if self.selectedSprite is not None:
            self.selectedSprite.texturex = spinner.get_value()
            self.graphic.refreshDisplay()
            
    def _selectedYChangedCb(self, spinner):
        if self.selectedSprite is not None:
            self.selectedSprite.texturey = spinner.get_value()
            self.graphic.refreshDisplay()

    def _selectedWidthChangedCb(self, spinner):
        if self.selectedSprite is not None:
            self.selectedSprite.texturew = spinner.get_value()
            self.graphic.refreshDisplay()

    def _selectedHeightChangedCb(self, spinner):
        if self.selectedSprite is not None:
            self.selectedSprite.textureh = spinner.get_value()
            self.graphic.refreshDisplay()

    def _addSpinButton(self, text, function):
        hbox = gtk.HBox()
        hbox.pack_start(gtk.Label(text), False, False, 0)
        adj = gtk.Adjustment(32, 0, 102400.0, 1.0, 5.0, 0.0)
        spinbutton = gtk.SpinButton(adj, 0, 0)
        spinbutton.set_wrap(True)
        spinbutton.show()
        spinbutton.connect("value_changed", function)
        hbox.pack_end(spinbutton, True, True, 0)
        hbox.show_all()
        self.pack_start(hbox, False, False, 0)
        spinbutton.connect("button-press-event", self._buttonPressedCb)
        return spinbutton

    def _widthChangedCb(self, spinner):
        self.width = spinner.get_value()

    def _heightChangedCb(self, spinner):
        self.height = spinner.get_value()

    def _addResourcesCb(self, unused):
        uris = None
        chooser = gtk.FileChooserDialog(title = "Choose location", action = gtk.FILE_CHOOSER_ACTION_OPEN,
                                        buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                                   gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
        chooser.set_select_multiple(True)
        try:
            if gstSet:
                pw = PreviewWidget(self.instance.app)
                chooser.set_preview_widget(pw)
                chooser.set_use_preview_label(False)
                chooser.connect('update-preview', pw.add_preview_request)
        except AttributeError:
            print "We must set the pythonpath"
        if chooser.run() == gtk.RESPONSE_ACCEPT:
            uris = chooser.get_filenames()
        chooser.destroy()
        if uris:
            self.instance.addToResources(uris)
            for uri in uris:
                pixbuf = gtk.gdk.pixbuf_new_from_file(uri)
                pixbuf = pixbuf.scale_simple(64, 64, gtk.gdk.INTERP_BILINEAR)
                self.model.append([get_name_from_uri(uri), pixbuf, uri])

    def _buttonPressedCb(self, widget, event):
        widget.grab_focus()
class KSEPanel(gtk.VBox):
    """ 
    Contained by Sections.
    Panels are on the right-hand side of the UI. They allow addition and
    deletion of resources in their respective sections (for example the
    graphic panel allows adding atlases or resources to these atlases).
    Content will obviously depend on the section.
    """
    def __init__(self):
        gtk.VBox.__init__(self, False, 0)
        self.show()
        self.set_size_request(125, -1)
        self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str)
        self.listview = gtk.TreeView()
        self.listview.set_model(self.model)
        scrolledWindow = gtk.ScrolledWindow()
        scrolledWindow.add_with_viewport(self.listview)
        self.pack_end(scrolledWindow, True, True, 0)
        resources = gtk.TreeViewColumn("Resources")
        cell = gtk.CellRendererText()
        resources.pack_start(cell, True)
        resources.add_attribute(cell, "text", 0)
        self.listview.append_column(resources)
        self.listview.connect("row-activated", self.instance.rowActivatedCb, self.model)
        self.butAddResource = Button("Add resource", self, Button.PACKSTART, show=True, imageLink=IMAGE_ADD)
        self.butAddResource.connect("clicked", self._addResourcesCb)

    def loadProjectResources(self, node):
        """
        @param node: resources/graphics/resources xml Node
        """
        for elem in node.findall("resource"):
            try:
                pixbuf = gtk.gdk.pixbuf_new_from_file(elem.attrib["path"])
                pixbuf = pixbuf.scale_simple(64, 64, gtk.gdk.INTERP_BILINEAR)
                self.model.append([elem.attrib["name"], pixbuf, elem.attrib["path"]])
            except glib.GError:
                self.model.append([elem.attrib["name"], None, elem.attrib["path"]])

    def _addResourcesCb(self, unused):
        uris = None
        chooser = gtk.FileChooserDialog(title = "Choose location", action = gtk.FILE_CHOOSER_ACTION_OPEN,
                                        buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                                   gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
        chooser.set_select_multiple(True)
        try:
            pw = PreviewWidget(self.instance.app)
            chooser.set_preview_widget(pw)
            chooser.set_use_preview_label(False)
            chooser.connect('update-preview', pw.add_preview_request)
        except AttributeError:
            print "We must set the pythonpath"
        if chooser.run() == gtk.RESPONSE_ACCEPT:
            uris = chooser.get_filenames()
        chooser.destroy()
        if uris:
            self.instance.addToResources(uris)
            for uri in uris:
                try:
                    pixbuf = gtk.gdk.pixbuf_new_from_file(uri)
                    pixbuf = pixbuf.scale_simple(64, 64, gtk.gdk.INTERP_BILINEAR)
                    self.model.append([get_name_from_uri(uri), pixbuf, uri])
                except glib.GError:
                    self.model.append([get_name_from_uri(uri), None, uri])
                    pass