def _init_tab_buttons(self): """Add enough tabbuttons for all widgets.""" def on_tab_removal(tabwidget): # called when a tab is being removed (via weakref since tabs shouldn't have references to the parent tabwidget) # If one tab is removed, the whole tabwidget will die.. # This is easy usually the desired behavior. if tabwidget(): tabwidget().on_remove() # Load buttons for index, tab in enumerate(self._tabs): # don't add a reference to the tab.add_remove_listener(Callback(on_tab_removal, weakref.ref(self))) container = Container(name="container_{}".format(index)) background = Icon(name="bg_{}".format(index)) button = ImageButton(name=str(index), size=(50, 50)) if self.current_tab is tab: background.image = tab.button_background_image_active button.path = tab.path_active else: background.image = tab.button_background_image button.path = tab.path button.capture(Callback(self.show_tab, index)) if hasattr(tab, 'helptext') and tab.helptext: button.helptext = tab.helptext container.size = (50, 52) container.addChild(background) container.addChild(button) self.content.addChild(container) self.widget.size = (54, 55 * len(self._tabs)) self.widget.adaptLayout() self._apply_layout_hack()
def build_ship_info(self, index, ship, prodline): size = (260, 90) widget = Container(name='showcase_%s' % index, position=(0, 20 + index * 90), min_size=size, max_size=size, size=size) bg_icon = Icon(image='content/gui/images/background/square_80.png', name='bg_%s' % index) widget.addChild(bg_icon) image = 'content/gui/images/objects/ships/76/{unit_id}.png'.format( unit_id=ship) helptext = self.instance.session.db.get_ship_tooltip(ship) unit_icon = Icon(image=image, name='icon_%s' % index, position=(2, 2), helptext=helptext) widget.addChild(unit_icon) # if not buildable, this returns string with reason why to be displayed as helptext #ship_unbuildable = self.is_ship_unbuildable(ship) ship_unbuildable = False if not ship_unbuildable: button = OkButton(position=(60, 50), name='ok_%s' % index, helptext=_('Build this ship!')) button.capture(Callback(self.start_production, prodline)) else: button = CancelButton(position=(60, 50), name='ok_%s' % index, helptext=ship_unbuildable) widget.addChild(button) # Get production line info production = self.producer.create_production_line(prodline) # consumed == negative, reverse to sort in *ascending* order: costs = sorted(production.consumed_res.iteritems(), key=itemgetter(1)) for i, (res, amount) in enumerate(costs): xoffset = 103 + (i % 2) * 55 yoffset = 20 + (i // 2) * 20 icon = create_resource_icon(res, self.instance.session.db) icon.max_size = icon.min_size = icon.size = (16, 16) icon.position = (xoffset, yoffset) label = Label(name='cost_%s_%s' % (index, i)) if res == RES.GOLD: label.text = unicode(-amount) else: label.text = u'{amount:02}t'.format(amount=-amount) label.position = (22 + xoffset, yoffset) widget.addChild(icon) widget.addChild(label) return widget
def __init__(self): available_images = glob.glob('content/gui/images/background/mainmenu/bg_*.png') self.bg_images = deque(available_images) latest_bg = horizons.globals.fife.get_uh_setting("LatestBackground") try: # If we know the current background from an earlier session, # show all other available ones before picking that one again. self.bg_images.remove(latest_bg) self.bg_images.append(latest_bg) except ValueError: pass (res_width, res_height) = horizons.globals.fife.get_fife_setting('ScreenResolution').split('x') self._black_box = Container() self._black_box.size = int(res_width), int(res_height) self._black_box.base_color = (0, 0, 0, 255) self._image = Icon(position_technique='center:center') self.rotate_image()
def create_resource_selection_dialog(on_click, inventory, db, widget='select_trade_resource.xml', res_filter=None, amount_per_line=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 @param amount_per_line: how many resource icons per line. Default: try to fit layout """ from horizons.gui.widgets.imagefillstatusbutton import ImageFillStatusButton dummy_icon_path = "icons/resources/none_gray" dlg = load_uh_widget(widget) icon_size = ImageFillStatusButton.ICON_SIZE # used for dummy button cell_size = ImageFillStatusButton.CELL_SIZE button_width = cell_size[0] vbox = dlg.findChild(name="resources") amount_per_line = amount_per_line or vbox.width // button_width # Add the zero element to the beginning that allows to remove the currently # sold/bought resource: resources = [0] + db.get_res(only_tradeable=True) current_hbox = HBox(name="hbox_0", padding=0) index = 1 for res_id in resources: # don't show resources that are already in the list if res_filter is not None and not res_filter(res_id): continue # on click: add this res cb = Callback(on_click, res_id) # create button (dummy one or real one) if res_id == 0 or inventory is None: reset_button = ImageButton(max_size=icon_size, name="resource_icon_00") reset_button.path = dummy_icon_path button = Container(size=cell_size) # add the "No Resource" image to the container, positioned in the top left button.addChild(reset_button) # capture a mouse click on the container. It's possible to click on the # image itself or into the empty area (below or to the right of the image) button.capture(cb, event_name="mouseClicked") button.name = "resource_{:d}".format(res_id) else: amount = inventory[res_id] filled = int(inventory[res_id] / inventory.get_limit(res_id) * 100) button = ImageFillStatusButton.init_for_res(db, res_id, amount=amount, filled=filled, uncached=True, use_inactive_icon=False, showprice=True) button.capture(cb, event_name="mouseClicked") button.name = "resource_{:d}".format(res_id) current_hbox.addChild(button) if index % amount_per_line == 0: vbox.addChild(current_hbox) box_id = index // amount_per_line current_hbox = HBox(name="hbox_{}".format(box_id), padding=0) index += 1 vbox.addChild(current_hbox) vbox.adaptLayout() return dlg