def _reset_gold_gui(self): if self.gold_gui is not None: self.gold_gui.hide() self.gold_gui = load_uh_widget(self.__class__.GOLD_ENTRY_GUI_FILE, style=self.__class__.STYLE) self.gold_gui.child_finder = PychanChildFinder(self.gold_gui) # set appropriate icon self.gold_gui.findChild(name="res_icon").image = get_res_icon(RES.GOLD_ID)[4] # the 32 one
def set_inventory_instance(self, instance, keep_construction_mode=False, force_update=False): """Display different inventory. May change resources that are displayed""" if self.current_instance() is instance and not self.construction_mode and not force_update: return # caller is drunk yet again if self.construction_mode and not keep_construction_mode: # stop construction mode, immediately update view, which will be a normal view self.close_construction_mode(update_slots=False) # reconstruct general gui # remove old gui for i in self.gui: i.hide() self._hide_resource_selection_dialog() self.gui = [] inv = self._get_current_inventory() if inv is not None: inv.remove_change_listener(self._update_resources) if instance in (None, self): # show nothing instead self.current_instance = weakref.ref(self) # can't weakref to None self._do_show_dummy = False # don't save dummy value return self.current_instance = weakref.ref(instance) # construct new slots (fill values later) initial_offset = 93 offset = 52 resources = self._get_current_resources() addition = [-1] if self._do_show_dummy or not resources else [] # add dummy at end for adding stuff for i, res in enumerate( resources + addition ): entry = load_uh_widget(self.ENTRY_GUI_FILE, style=self.__class__.STYLE) entry.findChild(name="entry").position = (initial_offset + offset * i, 17) background_icon = entry.findChild(name="background_icon") background_icon.add_entered_callback( Callback(self._show_resource_selection_dialog, i) ) if res != -1: helptext = self.session.db.get_res_name(res) icon = entry.findChild(name="res_icon") icon.num = i icon.image = get_res_icon(res)[2] # the 24 one icon.capture(self._on_res_slot_click, event_name = 'mouseClicked') else: helptext = _("Click to add a new slot") entry.show() # this will not be filled as the other res background_icon.helptext = helptext self.gui.append(entry) # show it just when values are entered, this appeases pychan # fill values inv = self._get_current_inventory() # update on all changes as well as now inv.add_change_listener(self._update_resources, call_listener_now=True)
def get_res_id_and_icon(self, only_tradeable=False, only_inventory=False): """Returns a list of all resources and the matching icons. @param only_tradeable: return only those you can trade. @param only_inventory: return only those displayed in inventories. @return: list of tuples: (resource ids, resource icon)""" sql = "SELECT id FROM resource WHERE id " if only_tradeable: sql += " AND tradeable = 1 " if only_inventory: sql += " AND shown_in_inventory = 1 " query = self.cached_query(sql) return [(query[res][0], get_res_icon(query[res][0])[0]) for res in xrange(len(query))]
def __init__(self, player, resource_id, amount, gold, **kwargs): super(TradeHistoryItem, self).__init__(**kwargs) self.widget = load_uh_widget('trade_history_item.xml') self.addChild(self.widget) self.findChild(name='player_emblem').background_color = player.color self.findChild(name='player_name').text = player.name gold_amount_label = self.findChild(name='gold_amount') gold_amount_label.text = u'{gold:+5d}'.format(gold=gold) gold_icon = self.findChild(name='gold_icon') gold_icon.image = get_res_icon(RES.GOLD_ID)[3] gold_icon.helptext = player.session.db.get_res_name(RES.GOLD_ID) resource_amount_label = self.findChild(name='resource_amount') resource_amount_label.text = u'{amount:+5d}'.format(amount=amount) resource_icon = self.findChild(name='resource_icon') resource_icon.image = get_res_icon(resource_id)[3] resource_icon.helptext = player.session.db.get_res_name(resource_id) self.size = self.widget.size
def update(self): self.removeAllChildren() weapons_added = False if hasattr(self.instance, 'get_weapon_storage'): storage = self.instance.get_weapon_storage() for weapon, amount in storage: weapons_added = True icon_image = get_res_icon(weapon)[2] icon_tooltip = self.instance.session.db.get_res_name(weapon)+': '+str(amount) icon = TooltipIcon(image = icon_image, tooltip = icon_tooltip) self.addChild(icon) if not weapons_added: icon_image = "content/gui/icons/resources/none.png" icon = TooltipIcon(image = icon_image, tooltip = _("none")) self.addChild(icon)
def init_for_res(cls, db, res, amount=0, filled=0, use_inactive_icon=True, uncached=False): """Inites the button to display the icons for res @param db: dbreader to get info about res icon. @param res: resource id @param amount: int amount of res (used to decide inactiveness and as text) @param filled: percent of fill status (values are ints in [0, 100]) @param use_inactive_icon: wheter to use inactive icon if amount == 0 @param uncached: force no cache. see __init__() @return: ImageFillStatusButton instance""" icons = get_res_icon(res) icon, icon_disabled = icons[0], icons[1] if not use_inactive_icon: icon_disabled = icon helptext = db.get_res_name(res) return cls(up_image=icon_disabled if amount == 0 else icon, down_image=icon_disabled if amount == 0 else icon, hover_image=icon_disabled if amount == 0 else icon, text=str(amount), helptext=helptext, size=cls.DEFAULT_BUTTON_SIZE, res_id = res, filled = filled, uncached = uncached, opaque=False)
def add_resource(self, res_id, slot_id, value=None): """ Adds a resource to the specified slot @param res_id: int - resource id @param slot: int - slot number of the slot that is to be set """ self.log.debug("BuySellTab add_resource() resid: %s; slot_id %s; value: %s", \ res_id, slot_id, value) keep_hint = False if self.resources is not None: # Hide resource menu self.resources.hide() self.show() if res_id != 0: # new res self._set_hint( _("Set to buy or sell by clicking on that label, then adjust the amount via the slider to the right.") ) else: self._set_hint( u"" ) keep_hint = True slot = self.slots[slot_id] slider = slot.findChild(name="slider") if value is None: # use current slider value if player provided no input value = int(slider.value) else: # set slider to value entered by the player slider.value = float(value) if slot.action is "sell": if slot.res is not None: # slot has been in use before, delete old value self.remove_sell_from_settlement(slot.res) if res_id != 0: self.add_sell_to_settlement(res_id, value, slot.id) else: if slot.action is "buy" and slot.res is not None: self.remove_buy_from_settlement(slot.res) if res_id != 0: self.add_buy_to_settlement(res_id, value, slot.id) button = slot.findChild(name="button") fillbar = slot.findChild(name="fillbar") # reset slot value for new res if res_id == 0: button.up_image, button.down_image, button.hover_image = [ self.dummy_icon_path ] * 3 button.helptext = u"" slot.findChild(name="amount").text = u"" slot.findChild(name="slider").value = 0.0 slot.res = None slider.capture(None) # hide fillbar by setting position icon = slot.findChild(name="icon") fillbar.position = (icon.width - fillbar.width - 1, icon.height) button = slot.findChild(name="buysell") button.up_image = None button.hover_image = None else: icons = get_res_icon(res_id) button.up_image = icons[0] button.down_image = icons[0] button.hover_image = icons[1] # disabled icon button.helptext = self.session.db.get_res_name(res_id) slot.res = res_id # use some python magic to assign a res attribute to the slot to # save which res_id it stores slider.capture(Callback(self.slider_adjust, res_id, slot.id)) slot.findChild(name="amount").text = unicode(value)+"t" icon = slot.findChild(name="icon") inventory = self.tradepost.get_inventory() filled = float(inventory[res_id]) / inventory.get_limit(res_id) fillbar.position = (icon.width - fillbar.width - 1, icon.height - int(icon.height*filled)) # show buy action by default (set to sell and toggle) slot.action = "sell" self.toggle_buysell(slot_id, keep_hint=keep_hint) slot.adaptLayout()
def add_resource(self, res_id, slot_id, value=None, dont_use_commands=False): """ Adds a resource to the specified slot @param res_id: int - resource id @param slot: int - slot number of the slot that is to be set """ self.log.debug("BuySellTab add_resource() resid: %s; slot_id %s; value: %s", \ res_id, slot_id, value) if self.resources is not None: # Hide resource menu self.resources.hide() self.show() slot = self.slots[slot_id] slider = slot.findChild(name="slider") if value is None: # use current slider value if player provided no input value = int(slider.getValue()) else: # set slider to value entered by the player slider.setValue(float(value)) if slot.action is "sell": if slot.res is not None: # slot has been in use before, delete old value if dont_use_commands: # dont_use_commands is true if called by __init__ self.settlement.remove_from_sell_list(slot.res) else: RemoveFromSellList(self.settlement, slot.res).execute(self.settlement.session) if res_id != 0: self.add_sell_to_settlement(res_id, value, slot.id, dont_use_commands) else: if slot.action is "buy" and slot.res is not None: if dont_use_commands: # dont_use_commands is true if called by __init__ self.settlement.remove_from_buy_list(slot.res) else: RemoveFromBuyList(self.settlement, slot.res).execute(self.settlement.session) if res_id != 0: self.add_buy_to_settlement(res_id, value, slot.id, dont_use_commands) button = slot.findChild(name="button") fillbar = slot.findChild(name="fillbar") if res_id == 0: button.up_image, button.down_image, button.hover_image = [ self.dummy_icon_path ] * 3 button.tooltip = u"" slot.findChild(name="amount").text = u"" slot.res = None slider.capture(None) # hide fillbar by setting position icon = slot.findChild(name="icon") fillbar.position = (icon.width - fillbar.width - 1, icon.height) else: icons = get_res_icon(res_id) button.up_image = icons[0] button.down_image = icons[0] button.hover_image = icons[1] # disabled icon button.tooltip = self.settlement.session.db.get_res_name(res_id) slot.res = res_id # use some python magic to assign a res attribute to the slot to # save which res_id it stores slider.capture(Callback(self.slider_adjust, res_id, slot.id)) slot.findChild(name="amount").text = unicode(value)+"t" icon = slot.findChild(name="icon") inventory = self.settlement.inventory filled = float(inventory[res_id]) / inventory.get_limit(res_id) fillbar.position = (icon.width - fillbar.width - 1, icon.height - int(icon.height*filled)) slot.adaptLayout()
def refresh(self): """This function is called by the TabWidget to redraw the widget.""" super(BoatbuilderTab, self).refresh() progress = self.instance.get_production_progress() self.widget.findChild(name='progress').progress = progress*100 self.widget.findChild(name='BB_progress_perc').text = unicode(math.floor(progress*100))+u"%" main_container = self.widget.findChild(name="BB_main_tab") container_active = self.widget.findChild(name="container_active") container_inactive = self.widget.findChild(name="container_inactive") progress_container = main_container.findChild(name="BB_progress_container") # a boatbuilder is considered active here, if he build sth, no matter if it's paused if self.instance.get_production_lines(): # TODO: fill in actual values here # remove other container, but save it if container_inactive is not None: main_container.container_inactive = container_inactive main_container.removeChild( container_inactive ) if container_active is None: main_container.insertChildBefore( main_container.container_active, progress_container) container_active = main_container.container_active container_active.findChild(name="headline_BB_builtship_label").text = _("Fishing boat") container_active.findChild(name="BB_cur_ship_icon").tooltip = "Used by: Fisher\nStorage: 1 slot of 1t\nSpeed: average\nHealth: 40" button_active = container_active.findChild(name="toggle_active_active") button_inactive = container_active.findChild(name="toggle_active_inactive") if not self.instance.is_active(): # if production is paused # remove active button, if it's there, and save a reference to it if button_active is not None: container_active.button_active = button_active container_active.removeChild( button_active ) # restore inactive button, if it isn't in the gui if button_inactive is None: # insert at the end container_active.insertChild(container_active.button_inactive, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_inactive' : Callback(self.instance.set_active, active=True) }) # TODO: make this button do sth else: # remove inactive button, if it's there, and save a reference to it if button_inactive is not None: container_active.button_inactive = button_inactive container_active.removeChild( button_inactive ) # restore active button, if it isn't in the gui if button_active is None: # insert at the end container_active.insertChild(container_active.button_active, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_active' : Callback(self.instance.set_active, active=False) }) upgrades_box = container_active.findChild(name="BB_upgrades_box") for child in upgrades_box.children[:]: upgrades_box.removeChild(child) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ love") ) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ affection") ) # no upgrades in 2010.1 release ---^ upgrades_box.stylize('menu_black') # TODO: make this button do sth # Update needed resources production = self.instance._get_productions()[0] still_needed_res = production.get_consumed_resources() i = 1 needed_res_container = self.widget.findChild(name="BB_needed_resources_container") main_container.findChild(name="BB_needed_res_label").text = _('Resources still needed:') for res, amount in still_needed_res.iteritems(): assert i <= 3, "Only 3 still needed res for ships are currently supported" icon = get_res_icon(res)[3] needed_res_container.findChild(name="BB_needed_res_icon_"+str(i)).image = icon needed_res_container.findChild(name="BB_needed_res_lbl_"+str(i)).text = unicode(-1*amount)+u't' # -1 make them positive i += 1 # TODO: cancel building button # print "Cancelbutton search.." cancel_button = self.widget.findChild(name="BB_cancel_button") # print "Found:", cancel_button cancel_button.capture(self.instance.cancel_all_productions, event_name="mouseClicked") # print cancel_button.isCaptured() else: # display sth when nothing is produced # remove other container, but save it if container_active is not None: main_container.container_active = container_active main_container.removeChild( container_active ) if container_inactive is None: main_container.insertChildBefore( main_container.container_inactive, progress_container) container_inactive = main_container.container_inactive self.widget.adaptLayout()
def refresh(self): """This function is called by the TabWidget to redraw the widget.""" super(BoatbuilderTab, self).refresh() main_container = self.widget.findChild(name="BB_main_tab") container_active = main_container.findChild(name="container_active") container_inactive = main_container.findChild(name="container_inactive") progress_container = main_container.findChild(name="BB_progress_container") cancel_container = main_container.findChild(name="BB_cancel_container") needed_res_container = self.widget.findChild(name="BB_needed_resources_container") # a boatbuilder is considered active here, if he build sth, no matter if it's paused production_lines = self.instance.get_production_lines() if production_lines: if cancel_container is None: main_container.addChild(main_container.cancel_container) cancel_container = main_container.cancel_container if needed_res_container is None: main_container.insertChildBefore(main_container.needed_res_container, cancel_container) needed_res_container = main_container.needed_res_container # Set progress if progress_container is None: main_container.insertChildBefore( main_container.progress_container, self.widget.findChild(name="BB_needed_resources_container")) progress_container = main_container.progress_container progress = self.instance.get_production_progress() self.widget.findChild(name='progress').progress = progress*100 self.widget.findChild(name='BB_progress_perc').text = unicode(math.floor(progress*100))+u"%" # remove other container, but save it if container_inactive is not None: main_container.container_inactive = container_inactive main_container.removeChild( container_inactive ) if container_active is None: main_container.insertChildBefore( main_container.container_active, progress_container) container_active = main_container.container_active produced_unit_id = self.instance._get_production(production_lines[0]).get_produced_units().keys()[0] (name,) = self.instance.session.db("SELECT name FROM unit WHERE id = ?", produced_unit_id)[0] container_active.findChild(name="headline_BB_builtship_label").text = unicode(name) container_active.findChild(name="BB_cur_ship_icon").tooltip = "Storage: 4 slots, 120t \nHealth: 100" container_active.findChild(name="BB_cur_ship_icon").image = "content/gui/images/objects/ships/116/%s.png" % (produced_unit_id) button_active = container_active.findChild(name="toggle_active_active") button_inactive = container_active.findChild(name="toggle_active_inactive") if not self.instance.is_active(): # if production is paused # remove active button, if it's there, and save a reference to it if button_active is not None: container_active.button_active = button_active container_active.removeChild( button_active ) # restore inactive button, if it isn't in the gui if button_inactive is None: # insert at the end container_active.insertChild(container_active.button_inactive, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_inactive' : Callback(self.instance.set_active, active=True) }) # TODO: make this button do sth else: # remove inactive button, if it's there, and save a reference to it if button_inactive is not None: container_active.button_inactive = button_inactive container_active.removeChild( button_inactive ) # restore active button, if it isn't in the gui if button_active is None: # insert at the end container_active.insertChild(container_active.button_active, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_active' : Callback(self.instance.set_active, active=False) }) upgrades_box = container_active.findChild(name="BB_upgrades_box") for child in upgrades_box.children[:]: upgrades_box.removeChild(child) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ love") ) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ affection") ) # no upgrades in 2010.1 release ---^ upgrades_box.stylize('menu_black') # Update needed resources production = self.instance._get_productions()[0] still_needed_res = production.get_consumed_resources() # Now sort! still_needed_res = sorted(still_needed_res.iteritems(), key=operator.itemgetter(1)) main_container.findChild(name="BB_needed_res_label").text = _('Resources still needed:') i = 0 for res, amount in still_needed_res: if amount == 0: continue # Don't show res that are not really needed anymore assert i <= 3, "Only 3 still needed res for ships are currently supported" icon = get_res_icon(res)[3] needed_res_container.findChild(name="BB_needed_res_icon_"+str(i+1)).image = icon needed_res_container.findChild(name="BB_needed_res_lbl_"+str(i+1)).text = unicode(-1*amount)+u't' # -1 make them positive i += 1 if i >= 3: break for j in xrange(i, 3): # these are not filled by a resource, so we need to make it invisible needed_res_container.findChild(name="BB_needed_res_icon_"+str(j+1)).image = None needed_res_container.findChild(name="BB_needed_res_lbl_"+str(j+1)).text = u"" # TODO: cancel building button # print "Cancelbutton search.." cancel_button = self.widget.findChild(name="BB_cancel_button") # print "Found:", cancel_button cancel_button.capture(self.instance.cancel_all_productions, event_name="mouseClicked") # print cancel_button.isCaptured() else: # display sth when nothing is produced # remove other container, but save it if container_active is not None: main_container.container_active = container_active main_container.removeChild( container_active ) if container_inactive is None: main_container.insertChildBefore( main_container.container_inactive, progress_container) container_inactive = main_container.container_inactive if progress_container is not None: main_container.progress_container = progress_container main_container.removeChild(progress_container) if needed_res_container is not None: main_container.needed_res_container = needed_res_container main_container.removeChild(needed_res_container) if cancel_container is not None: main_container.cancel_container = cancel_container main_container.removeChild(cancel_container) self.widget.adaptLayout()
def refresh(self): """This function is called by the TabWidget to redraw the widget.""" super(BoatbuilderTab, self).refresh() main_container = self.widget.findChild(name="BB_main_tab") container_active = main_container.findChild(name="container_active") container_inactive = main_container.findChild(name="container_inactive") progress_container = main_container.findChild(name="BB_progress_container") cancel_container = main_container.findChild(name="BB_cancel_container") needed_res_container = self.widget.findChild(name="BB_needed_resources_container") # a boatbuilder is considered active here if it build sth, no matter if it's paused production_lines = self.producer.get_production_lines() if production_lines: if cancel_container is None: main_container.addChild(main_container.cancel_container) cancel_container = main_container.cancel_container if needed_res_container is None: main_container.insertChildBefore(main_container.needed_res_container, cancel_container) needed_res_container = main_container.needed_res_container # Set progress if progress_container is None: main_container.insertChildBefore( main_container.progress_container, self.widget.findChild(name="BB_needed_resources_container")) progress_container = main_container.progress_container progress = self.producer.get_production_progress() self.widget.findChild(name='progress').progress = progress*100 self.widget.findChild(name='BB_progress_perc').text = unicode(math.floor(progress*100))+u"%" # remove other container, but save it if container_inactive is not None: main_container.container_inactive = container_inactive main_container.removeChild( container_inactive ) if container_active is None: main_container.insertChildBefore( main_container.container_active, progress_container) container_active = main_container.container_active # Update boatbuilder queue queue = self.producer.get_unit_production_queue() queue_container = container_active.findChild(name="queue_container") queue_container.removeAllChildren() for i in enumerate(queue): place_in_queue, unit_type = i image = self.__class__.SHIP_THUMBNAIL.format(type_id=unit_type) #xgettext:python-format helptext = _(u"{ship} (place in queue: {place})").format( ship=self.instance.session.db.get_unit_type_name(unit_type), place=place_in_queue+1 ) # people don't count properly, always starting at 1.. icon_name = "queue_elem_"+str(place_in_queue) icon = Icon(name=icon_name, image=image, helptext=helptext) icon.capture( Callback(RemoveFromQueue(self.producer, place_in_queue).execute, self.instance.session), event_name="mouseClicked" ) queue_container.addChild( icon ) # Set built ship info produced_unit_id = self.producer._get_production(production_lines[0]).get_produced_units().keys()[0] produced_unit_id = self.producer._get_production(production_lines[0]).get_produced_units().keys()[0] (name,) = self.instance.session.db.cached_query("SELECT name FROM unit WHERE id = ?", produced_unit_id)[0] container_active.findChild(name="headline_BB_builtship_label").text = _(name) container_active.findChild(name="BB_cur_ship_icon").helptext = "Storage: 4 slots, 120t \nHealth: 100" container_active.findChild(name="BB_cur_ship_icon").image = "content/gui/images/objects/ships/116/%s.png" % (produced_unit_id) button_active = container_active.findChild(name="toggle_active_active") button_inactive = container_active.findChild(name="toggle_active_inactive") if not self.producer.is_active(): # if production is paused # remove active button, if it's there, and save a reference to it if button_active is not None: container_active.button_active = button_active container_active.removeChild( button_active ) # restore inactive button, if it isn't in the gui if button_inactive is None: # insert at the end container_active.insertChild(container_active.button_inactive, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_inactive' : Callback(self.producer.set_active, active=True) }) # TODO: make this button do sth else: # remove inactive button, if it's there, and save a reference to it if button_inactive is not None: container_active.button_inactive = button_inactive container_active.removeChild( button_inactive ) # restore active button, if it isn't in the gui if button_active is None: # insert at the end container_active.insertChild(container_active.button_active, \ len(container_active.children)) container_active.mapEvents({ 'toggle_active_active' : Callback(self.producer.set_active, active=False) }) upgrades_box = container_active.findChild(name="BB_upgrades_box") for child in upgrades_box.children[:]: upgrades_box.removeChild(child) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ love") ) # upgrades_box.addChild( pychan.widgets.Label(text=u"+ affection") ) # no upgrades in 2010.1 release ---^ upgrades_box.stylize('menu_black') # Update needed resources production = self.producer.get_productions()[0] still_needed_res = production.get_consumed_resources() # Now sort! still_needed_res = sorted(still_needed_res.iteritems(), key=operator.itemgetter(1)) main_container.findChild(name="BB_needed_res_label").text = _('Resources still needed:') i = 0 for res, amount in still_needed_res: if amount == 0: continue # Don't show res that are not really needed anymore assert i <= 3, "Only 3 still needed res for ships are currently supported" icon = get_res_icon(res)[3] needed_res_container.findChild(name="BB_needed_res_icon_"+str(i+1)).image = icon needed_res_container.findChild(name="BB_needed_res_lbl_"+str(i+1)).text = unicode(-1*amount)+u't' # -1 makes them positive i += 1 if i >= 3: break for j in xrange(i, 3): # these are not filled by a resource, so we need to make it invisible needed_res_container.findChild(name="BB_needed_res_icon_"+str(j+1)).image = None needed_res_container.findChild(name="BB_needed_res_lbl_"+str(j+1)).text = u"" cancel_button = self.widget.findChild(name="BB_cancel_button") cancel_button.capture( Callback(CancelCurrentProduction(self.producer).execute, self.instance.session), event_name="mouseClicked" ) else: # display sth when nothing is produced # remove other container, but save it if container_active is not None: main_container.container_active = container_active main_container.removeChild( container_active ) if container_inactive is None: main_container.insertChildBefore( main_container.container_inactive, progress_container) container_inactive = main_container.container_inactive if progress_container is not None: main_container.progress_container = progress_container main_container.removeChild(progress_container) if needed_res_container is not None: main_container.needed_res_container = needed_res_container main_container.removeChild(needed_res_container) if cancel_container is not None: main_container.cancel_container = cancel_container main_container.removeChild(cancel_container) self.widget.adaptLayout()