def _init_tabs(self): """Add enough tabbuttons for all widgets.""" # Load buttons for index, tab in enumerate(self._tabs): tab.add_remove_listener(self.hide) container = pychan.Container() background = pychan.Icon() background.name = "bg_%s" % index button = TooltipButton() button.name = index if self.current_tab is tab: background.image = tab.button_background_image_active button.up_image = tab.button_active_image else: background.image = tab.button_background_image button.up_image = tab.button_up_image button.down_image = tab.button_down_image button.hover_image = tab.button_hover_image button.is_focusable = False button.size = (50, 50) button.capture(Callback(self._show_tab, index)) if hasattr(tab, "tooltip") and tab.tooltip is not None: button.tooltip = unicode(tab.tooltip) container.size = background.size container.addChild(background) container.addChild(button) self.content.addChild(container) self.widget.size = (50, 55 * len(self._tabs)) self.widget.adaptLayout()
def _init_tabs(self): """Add enough tabbuttons for all widgets.""" # Load buttons for index, tab in enumerate(self._tabs): container = pychan.Container() background = pychan.Icon() background.image = tab.button_background_image button = TooltipButton() button.name = index if self.current_tab is tab: button.up_image = tab.button_active_image else: button.up_image = tab.button_up_image button.down_image = tab.button_down_image button.hover_image = tab.button_hover_image button.size = (50, 50) button.capture(pychan.tools.callbackWithArguments(self._show_tab, index)) if hasattr(tab, 'tooltip') and tab.tooltip is not None: button.tooltip = unicode(tab.tooltip) container.size = background.size container.addChild(background) container.addChild(button) self.content.addChild(container) self.widget.size = (50, 200*len(self._tabs)) self.widget.adaptLayout()
def show_resource_menu(self, slot_id): self.resources = load_xml_translated('select_trade_resource.xml') self.resources.position = self.widget.position button_width = 50 vbox = self.resources.findChild(name="resources") current_hbox = pychan.widgets.HBox(padding = 2) index = 1 resources = horizons.main.db.get_res_id_and_icon(True) # Add the zero element to the beginning that allows to remove the currently sold/bought resource for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources): if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list: continue # don't show resources that are already in the list button = TooltipButton(size=(50, 50)) button.up_image, button.down_image, button.hover_image = icon, icon, icon if res_id == 0: button.tooltip = u"" else: button.tooltip = horizons.main.db.get_res_name(res_id) button.capture(Callback(self.add_resource, res_id, slot_id)) current_hbox.addChild(button) if index % (vbox.width/(button_width)) == 0 and index is not 0: vbox.addChild(current_hbox) current_hbox = pychan.widgets.HBox(padding=0) index += 1 vbox.addChild(current_hbox) vbox.adaptLayout() self.resources.addChild(vbox) self.resources.stylize('headline') self.hide() self.resources.show() self.settlement.session.ingame_gui.minimap_to_front()
def _init_tabs(self): """Add enough tabbuttons for all widgets.""" # Load buttons for index, tab in enumerate(self._tabs): container = pychan.Container() background = pychan.Icon() background.image = tab.button_background_image button = TooltipButton() button.name = index if self.current_tab is tab: button.up_image = tab.button_active_image else: button.up_image = tab.button_up_image button.down_image = tab.button_down_image button.hover_image = tab.button_hover_image button.is_focusable = False button.size = (50, 50) button.capture(Callback(self._show_tab, index)) if hasattr(tab, 'tooltip') and tab.tooltip is not None: button.tooltip = unicode(tab.tooltip) container.size = background.size container.addChild(background) container.addChild(button) self.content.addChild(container) self.widget.size = (50, 200 * len(self._tabs)) self.widget.adaptLayout()
def show_resource_menu(self, slot_id): self.resources = load_xml_translated('select_trade_resource.xml') self.resources.position = self.widget.position button_width = 50 vbox = self.resources.findChild(name="resources") amount_per_line = vbox.width / button_width current_hbox = pychan.widgets.HBox(name="hbox_0") index = 1 resources = horizons.main.db.get_res_id_and_icon(True) # Add the zero element to the beginning that allows to remove the currently sold/bought resource for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources): if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list: continue # don't show resources that are already in the list button = TooltipButton( size=(button_width, button_width), \ name="resource_icon_%02d" % res_id ) button.up_image, button.down_image, button.hover_image = icon, icon, icon if res_id == 0: button.tooltip = u"" else: button.tooltip = horizons.main.db.get_res_name(res_id) button.capture(Callback(self.add_resource, res_id, slot_id)) current_hbox.addChild(button) if index % amount_per_line == 0 and index is not 0: vbox.addChild(current_hbox) current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line) ) index += 1 # current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment vbox.addChild(current_hbox) vbox.adaptLayout() self.hide() # hides tab that invoked the selection widget self.resources.show() # show selection widget, still display old tab icons self.settlement.session.ingame_gui.minimap_to_front()
def show_resource_menu(self, slot_id): self.resources = load_xml_translated("select_trade_resource.xml") self.resources.position = self.widget.position button_width = 50 vbox = self.resources.findChild(name="resources") amount_per_line = vbox.width / button_width current_hbox = pychan.widgets.HBox(name="hbox_0") index = 1 resources = horizons.main.db.get_res_id_and_icon(True) # Add the zero element to the beginning that allows to remove the currently sold/bought resource for (res_id, icon) in [(0, self.dummy_icon_path)] + list(resources): if res_id in self.settlement.buy_list or res_id in self.settlement.sell_list: continue # don't show resources that are already in the list button = TooltipButton(size=(button_width, button_width), name="resource_icon_%02d" % res_id) button.up_image, button.down_image, button.hover_image = icon, icon, icon if res_id == 0: button.tooltip = u"" else: button.tooltip = horizons.main.db.get_res_name(res_id) button.capture(Callback(self.add_resource, res_id, slot_id)) current_hbox.addChild(button) if index % amount_per_line == 0 and index is not 0: vbox.addChild(current_hbox) current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line)) index += 1 # current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment vbox.addChild(current_hbox) vbox.adaptLayout() self.hide() # hides tab that invoked the selection widget self.resources.show() # show selection widget, still display old tab icons self.settlement.session.ingame_gui.minimap_to_front()
def create_resource_selection_dialog(on_click, inventory, db, widget='select_trade_resource.xml', res_filter=None): """Returns a container containing resource icons @param on_click: called with resource id as parameter on clicks @param inventory: to determine fill status of resource slots @param db: main db instance @param widget: which xml file to use as a template. Default: tabwidget. Required since the resource bar also uses this code (no tabs there though). @param res_filter: callback to decide which icons to use. Default: show all """ from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton from horizons.gui.widgets.tooltip import TooltipButton dummy_icon_path = "content/gui/icons/resources/none_gray.png" dlg = load_uh_widget(widget) button_width = ImageFillStatusButton.DEFAULT_BUTTON_SIZE[0] # used for dummy button vbox = dlg.findChild(name="resources") amount_per_line = vbox.width / button_width current_hbox = pychan.widgets.HBox(name="hbox_0", padding=0) index = 1 resources = db.get_res(True) # Add the zero element to the beginning that allows to remove the currently # sold/bought resource for res_id in [0] + list(resources): # don't show resources that are already in the list if res_filter is not None and not res_filter(res_id): continue # create button (dummy one or real one) if res_id == 0: button = TooltipButton( size=(button_width, button_width), name="resource_icon_00") button.up_image, button.down_image, button.hover_image = (dummy_icon_path,)*3 else: amount = inventory[res_id] filled = int(float(inventory[res_id]) / float(inventory.get_limit(res_id)) * 100.0) button = ImageFillStatusButton.init_for_res(db, res_id, amount=amount, filled=filled, use_inactive_icon=False) # on click: add this res cb = Callback(on_click, res_id) if hasattr(button, "button"): # for imagefillstatusbuttons button.button.capture( cb ) else: button.capture( cb ) current_hbox.addChild(button) if index % amount_per_line == 0 and index is not 0: vbox.addChild(current_hbox) current_hbox = pychan.widgets.HBox(name="hbox_%s" % (index / amount_per_line), padding=0) index += 1 # current_hbox.addSpacer(pychan.widgets.layout.Spacer) #TODO: proper alignment vbox.addChild(current_hbox) vbox.adaptLayout() return dlg
def show_resource_menu(self, slot, entry): position = self.widgets.index(entry) if self.resource_menu_shown: self.hide_resource_menu() self.resource_menu_shown = True vbox = self._gui.findChild(name="resources") label = self._gui.findChild(name="select_res_label") label.text = unicode("Select Resources") #hardcoded for 5 works better than vbox.width / button_width amount_per_line = 5 current_hbox = widgets.HBox() index = 1 for res_id in self.icon_for_resource: if res_id in self.instance.route.waypoints[position]['resource_list'] and\ slot.findChild(name='button').up_image.source is not self.icon_for_resource[res_id]: continue button = TooltipButton(size=(50,50)) icon = self.icon_for_resource[res_id] button.up_image, button.down_image, button.hover_image = icon, icon, icon button.capture(Callback(self.add_resource, slot, res_id, entry)) if res_id != 0: button.tooltip = horizons.main.db.get_res_name(res_id) current_hbox.addChild(button) if index > amount_per_line: index -= amount_per_line vbox.addChild(current_hbox) current_hbox = widgets.HBox() index += 1 vbox.addChild(current_hbox) self.hide() self.show()
def show_resource_menu(self, slot, entry): position = self.widgets.index(entry) if self.resource_menu_shown: self.hide_resource_menu() self.resource_menu_shown = True vbox = self._gui.findChild(name="resources") lbl = widgets.Label(name='select_res_label', text=_('Select a resource:')) vbox.addChild( lbl ) scrollarea = widgets.ScrollArea() res_box = widgets.VBox() scrollarea.addChild(res_box) vbox.addChild(scrollarea) # TODO: use create_resource_selection_dialog from util/gui.py #hardcoded for 5 works better than vbox.width / button_width amount_per_line = 5 current_hbox = widgets.HBox(max_size="326,46") index = 1 settlement = entry.settlement() inventory = settlement.get_component(StorageComponent).inventory if settlement else None from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton for res_id in sorted(self.icon_for_resource): if res_id in self.instance.route.waypoints[position]['resource_list'] and \ slot.findChild(name='button').up_image.source is not self.icon_for_resource[res_id]: continue cb = Callback(self.add_resource, slot, res_id, entry) if res_id == 0 or inventory is None: # no fillbar e.g. on dead settlement (shouldn't happen) or dummy slot button = TooltipButton(size=(46,46)) icon = self.icon_for_resource[res_id] button.up_image, button.down_image, button.hover_image = icon, icon, icon button.capture(cb) else: # button with fillbar amount = inventory[res_id] filled = int(float(inventory[res_id]) / float(inventory.get_limit(res_id)) * 100.0) button = ImageFillStatusButton.init_for_res(self.session.db, res_id, amount=amount, filled=filled, use_inactive_icon=False) button.button.capture(cb) current_hbox.addChild(button) if index >= amount_per_line: index -= amount_per_line res_box.addChild(current_hbox) current_hbox = widgets.HBox(max_size="326,26") index += 1 current_hbox.addSpacer(widgets.Spacer()) #TODO this spacer does absolutely nothing. res_box.addChild(current_hbox) self._gui.adaptLayout() scrollarea.max_width = 316 scrollarea.width = 316 vbox.max_width = 316 vbox.width = 316