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

Source Code for Module horizons.gui.tabs.mainsquaretabs

  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 functools import partial 
 23   
 24  from fife.extensions.pychan.widgets import Label 
 25   
 26  from horizons.command.uioptions import SetSettlementUpgradePermissions 
 27  from horizons.component.collectingcomponent import CollectingComponent 
 28  from horizons.component.namedcomponent import NamedComponent 
 29  from horizons.component.selectablecomponent import SelectableComponent 
 30  from horizons.component.tradepostcomponent import TradePostComponent 
 31  from horizons.constants import BUILDINGS, TIER 
 32  from horizons.gui.widgets.productionoverview import ProductionOverview 
 33  from horizons.i18n import gettext as T, gettext_lazy as LazyT 
 34  from horizons.messaging import PlayerLevelUpgrade, UpgradePermissionsChanged 
 35  from horizons.util.python.callback import Callback 
 36   
 37  from .overviewtab import OverviewTab 
 38  from .residentialtabs import setup_tax_slider 
39 40 41 -class MainSquareTab(OverviewTab):
42 """Tab for main square. Refreshes when one building on the settlement changes""" 43 @property
44 - def settlement(self):
45 return self.instance.settlement
46
47 - def show(self):
48 super().show() 49 PlayerLevelUpgrade.subscribe(self.on_player_level_upgrade) 50 # update self when a building of the settlement changes. 51 for building in self.settlement.buildings: 52 if not building.has_change_listener(self._schedule_refresh): 53 building.add_change_listener(self._schedule_refresh)
54
55 - def hide(self):
60
61 - def on_player_level_upgrade(self, message):
62 self.hide() 63 self.instance.get_component(SelectableComponent).show_menu(jump_to_tabclass=type(self))
64
65 66 -class AccountTab(MainSquareTab):
67 """Display basic income and expenses of a settlement""" 68 widget = 'tab_account.xml' 69 icon_path = 'icons/tabwidget/warehouse/account' 70 helptext = LazyT("Account") 71
72 - def init_widget(self):
73 super().init_widget() 74 self.widget.mapEvents({ 75 'show_production_overview/mouseClicked': self.show_production_overview, 76 }) 77 78 # FIXME having to access the WindowManager this way is pretty ugly 79 self._windows = self.instance.session.ingame_gui.windows 80 self.prod_overview = ProductionOverview(self._windows, self.settlement) 81 82 self.widget.child_finder('headline').text = self.settlement.get_component(NamedComponent).name 83 self.widget.child_finder('headline').helptext = T('Click to change the name of your settlement') 84 85 path = 'icons/widgets/cityinfo/settlement_{}'.format(self.settlement.owner.color.name) 86 self.widget.child_finder('show_production_overview').path = path
87
88 - def show_production_overview(self):
89 self._windows.toggle(self.prod_overview)
90
91 - def refresh(self):
92 super().refresh() 93 self.refresh_collector_utilization() 94 taxes = self.settlement.cumulative_taxes 95 running_costs = self.settlement.cumulative_running_costs 96 buy_expenses = self.settlement.get_component(TradePostComponent).buy_expenses 97 sell_income = self.settlement.get_component(TradePostComponent).sell_income 98 balance = self.settlement.balance 99 sign = '+' if balance >= 0 else '-' 100 self.widget.child_finder('taxes').text = str(taxes) 101 self.widget.child_finder('running_costs').text = str(running_costs) 102 self.widget.child_finder('buying').text = str(buy_expenses) 103 self.widget.child_finder('sale').text = str(sell_income) 104 self.widget.child_finder('balance').text = str(sign + ' ' + str(abs(balance))) 105 self.widget.child_finder('headline').text = self.settlement.get_component(NamedComponent).name 106 rename = Callback(self.instance.session.ingame_gui.show_change_name_dialog, self.settlement) 107 self.widget.mapEvents({'headline': rename})
108
110 if self.instance.has_component(CollectingComponent): 111 utilization = int(round(self.instance.get_collector_utilization() * 100)) 112 utilization = str(utilization) + '%' 113 else: 114 utilization = '---' 115 self.widget.findChild(name="collector_utilization").text = utilization
116
117 118 -class MainSquareOverviewTab(AccountTab):
119 helptext = LazyT('Main square overview') 120
121 - def init_widget(self):
122 super().init_widget() 123 self.widget.child_finder('headline').text = self.settlement.get_component(NamedComponent).name 124 self.widget.child_finder('headline').helptext = T('Click to change the name of your settlement')
125
126 127 -class MainSquareSettlerLevelTab(MainSquareTab):
128 widget = "mainsquare_inhabitants.xml" 129 # overwrite in subclass 130 LEVEL = None # type: int 131
132 - def __init__(self, instance):
133 self.max_inhabitants = instance.session.db.get_tier_inhabitants_max(self.__class__.LEVEL) 134 self.min_inhabitants = instance.session.db.get_tier_inhabitants_min(self.__class__.LEVEL) 135 self.helptext = LazyT(instance.session.db.get_settler_name(self.__class__.LEVEL)) 136 137 icon_path = 'icons/tabwidget/mainsquare/inhabitants{tier}'.format(tier=self.__class__.LEVEL) 138 super().__init__(instance=instance, icon_path=icon_path)
139
140 - def init_widget(self):
141 super().init_widget() 142 slider = self.widget.child_finder('tax_slider') 143 val_label = self.widget.child_finder('tax_val_label') 144 setup_tax_slider(slider, val_label, self.settlement, self.__class__.LEVEL) 145 self.widget.child_finder('tax_val_label').text = str(self.settlement.tax_settings[self.__class__.LEVEL]) 146 self.widget.child_finder('headline').text = T(self.instance.session.db.get_settler_name(self.__class__.LEVEL)) 147 148 if self.__class__.LEVEL == TIER.CURRENT_MAX: 149 # highest currently playable tier => upgrades not possible 150 upgrades_label = self.widget.child_finder('upgrades_lbl') 151 upgrades_label.text = T("Upgrade not possible:") 152 upgrades_button = self.widget.child_finder('allow_upgrades') 153 upgrades_button.set_inactive() 154 upgrades_button.helptext = T("This is the highest playable tier for now!")
155 156 @classmethod
157 - def shown_for(cls, instance):
158 return instance.owner.settler_level >= cls.LEVEL
159
160 - def show(self):
163
164 - def hide(self):
167
168 - def _get_last_tax_paid(self):
169 houses = self.settlement.buildings_by_id[BUILDINGS.RESIDENTIAL] 170 return sum([building.last_tax_payed for building in houses if building.level == self.__class__.LEVEL])
171
172 - def _get_resident_counts(self):
173 result = {} 174 for building in self.settlement.buildings_by_id[BUILDINGS.RESIDENTIAL]: 175 if building.level == self.__class__.LEVEL: 176 if building.inhabitants not in result: 177 result[building.inhabitants] = 0 178 result[building.inhabitants] += 1 179 return result
180
181 - def refresh_via_message(self, message):
182 # message bus requires parameter, refresh() doesn't allow parameter 183 # TODO: find general solution 184 self.refresh()
185
186 - def refresh(self):
187 self.widget.mapEvents({ 188 'allow_upgrades/mouseClicked': self.toggle_upgrades, 189 }) 190 191 # refresh taxes 192 self.widget.child_finder('taxes').text = str(self._get_last_tax_paid()) 193 194 # refresh upgrade permissions 195 upgrades_button = self.widget.child_finder('allow_upgrades') 196 # The currently highest playable tier cannot allow upgrades. 197 if self.__class__.LEVEL < TIER.CURRENT_MAX: 198 if self.settlement.upgrade_permissions[self.__class__.LEVEL]: 199 upgrades_button.set_active() 200 upgrades_button.helptext = T("Don't allow upgrades") 201 else: 202 upgrades_button.set_inactive() 203 upgrades_button.helptext = T('Allow upgrades') 204 205 # refresh residents per house info 206 resident_counts = self._get_resident_counts() 207 houses = 0 208 residents = 0 209 container = self.widget.child_finder('residents_per_house_table') 210 space_per_label = container.size[0] // 6 211 for number in range(self.min_inhabitants, self.max_inhabitants + 1): 212 column = number - (self.min_inhabitants - 1 if self.min_inhabitants > 0 else 0) 213 house_count = resident_counts.get(number, 0) 214 houses += house_count 215 residents += house_count * number 216 position_x = (space_per_label * (column - 1)) + 10 217 if not container.findChild(name="resident_" + str(column)): 218 label = Label(name="resident_" + str(column), position=(position_x, 0), text=str(number)) 219 container.addChild(label) 220 count_label = Label(name="resident_count_" + str(column), position=(position_x - 1, 20), text=str(house_count)) 221 container.addChild(count_label) 222 else: 223 container.findChild(name="resident_" + str(column)).text = str(number) 224 container.findChild(name="resident_count_" + str(column)).text = str(house_count) 225 226 sad = self.instance.session.db.get_lower_happiness_limit() 227 happy = self.instance.session.db.get_upper_happiness_limit() 228 inhabitants = partial(self.settlement.get_residentials_of_lvl_for_happiness, 229 self.__class__.LEVEL) 230 self.widget.child_finder('sad_amount').text = str(inhabitants(max_happiness=sad)) 231 self.widget.child_finder('avg_amount').text = str(inhabitants(sad, happy)) 232 self.widget.child_finder('happy_amount').text = str(inhabitants(happy)) 233 234 # refresh the summary 235 self.widget.child_finder('house_count').text = str(houses) 236 self.widget.child_finder('resident_count').text = str(residents) 237 238 self.widget.adaptLayout() 239 super().refresh()
240
241 - def toggle_upgrades(self):
242 SetSettlementUpgradePermissions(self.settlement, self.__class__.LEVEL, not self.settlement.upgrade_permissions[self.__class__.LEVEL]).execute(self.settlement.session)
243
244 245 -class MainSquareSailorsTab(MainSquareSettlerLevelTab):
246 LEVEL = TIER.SAILORS
247
248 249 -class MainSquarePioneersTab(MainSquareSettlerLevelTab):
250 LEVEL = TIER.PIONEERS
251
252 253 -class MainSquareSettlersTab(MainSquareSettlerLevelTab):
254 LEVEL = TIER.SETTLERS
255
256 257 -class MainSquareCitizensTab(MainSquareSettlerLevelTab):
258 LEVEL = TIER.CITIZENS
259
260 261 -class MainSquareMerchantsTab(MainSquareSettlerLevelTab):
262 LEVEL = TIER.MERCHANTS
263