Package horizons :: Package gui :: Package tabs :: Module buildrelatedtab
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.tabs.buildrelatedtab

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife.extensions.pychan.widgets import Icon 
 23   
 24  from horizons.component.selectablecomponent import SelectableComponent 
 25  from horizons.entities import Entities 
 26  from horizons.gui.util import load_uh_widget 
 27  from horizons.gui.widgets.imagebutton import ImageButton 
 28  from horizons.i18n import gettext_lazy as LazyT 
 29  from horizons.util.python.callback import Callback 
 30   
 31  from .overviewtab import OverviewTab 
 32   
 33   
34 -class BuildRelatedTab(OverviewTab):
35 """ 36 Adds a special tab to each production building with at least one entry in 37 the table related_buildings. This tab acts as modified build menu tab and 38 only displays those buildings actually related to the selected building. 39 Examples: tree for lumberjack; pavilion, school, etc. for inhabitants. 40 """ 41 widget = 'related_buildings.xml' 42 icon_path = 'icons/tabwidget/production/related' 43 helptext = LazyT("Build related buildings") 44 template_gui_xml = 'related_buildings_container.xml' 45
46 - def refresh(self):
47 """ 48 This function is called by the TabWidget to redraw the widget. 49 """ 50 # remove old data 51 parent_container = self.widget.child_finder('related_buildings') 52 while parent_container.children: 53 parent_container.removeChild(parent_container.children[0]) 54 55 # load all related buildings from DB 56 building_ids = self.instance.session.db.get_related_building_ids_for_menu(self.instance.id) 57 sorted_ids = sorted([(b, Entities.buildings[b].settler_level) for b in building_ids], key=lambda x: x[1]) 58 container = self.__get_new_container() 59 for i, (building_id, level) in enumerate(sorted_ids): 60 if level > self.instance.owner.settler_level: 61 break 62 63 button = self._create_build_buttons(building_id, container) 64 # check whether to start new line (currently only 4 fit per line) 65 if i and i % 4 == 0: 66 parent_container.addChild(container) 67 container = self.__get_new_container() 68 container.findChild(name="build_button_container").addChild(button) 69 button_bg = Icon(image="content/gui/images/buttons/buildmenu_button_bg.png") 70 container.findChild(name="build_button_bg_container").addChild(button_bg) 71 # Still need to add last container 72 parent_container.addChild(container) 73 super().refresh()
74
75 - def __get_new_container(self):
76 """ 77 Loads a background container xml file. Returns the loaded widget. 78 """ 79 gui = load_uh_widget(self.template_gui_xml) 80 return gui.findChild(name="buildings_container")
81
82 - def _create_build_buttons(self, building_id, container):
83 # {{mode}} in double braces because it is replaced as a second step 84 building_type = Entities.buildings[building_id] 85 build_button = ImageButton(name="build{id}".format(id=building_id), helptext=building_type.get_tooltip()) 86 build_button.path = "icons/buildmenu/{id:03d}".format(id=building_id) 87 build_button.capture(Callback(self.build_related, building_id)) 88 return build_button
89
100