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

Source Code for Module horizons.gui.tabs.residentialtabs

  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 horizons.command.uioptions import SetTaxSetting 
 23  from horizons.component.namedcomponent import NamedComponent 
 24  from horizons.constants import SETTLER 
 25  from horizons.gui.util import create_resource_icon, get_happiness_icon_and_helptext 
 26  from horizons.i18n import gettext_lazy as LazyT 
 27  from horizons.messaging import SettlerUpdate 
 28  from horizons.util.loaders.actionsetloader import ActionSetLoader 
 29  from horizons.util.python.callback import Callback 
 30   
 31  from .overviewtab import OverviewTab 
 32   
 33   
34 -class SettlerOverviewTab(OverviewTab):
35 widget = 'overview_settler.xml' 36 helptext = LazyT("Settler overview") 37
38 - def init_widget(self):
39 super().init_widget() 40 name = self.instance.settlement.get_component(NamedComponent).name 41 self.widget.findChild(name="headline").text = name 42 setup_tax_slider(self.widget.child_finder('tax_slider'), 43 self.widget.child_finder('tax_val_label'), 44 self.instance.settlement, 45 self.instance.level) 46 47 taxes = self.instance.settlement.tax_settings[self.instance.level] 48 self.widget.child_finder('tax_val_label').text = str(taxes) 49 action_set = ActionSetLoader.get_set(self.instance._action_set_id) 50 action_gfx = list(action_set.items())[0][1] 51 image = list(action_gfx[45].keys())[0] 52 self.widget.findChild(name="building_image").image = image
53
54 - def on_settler_level_change(self, message):
55 assert isinstance(message, SettlerUpdate) 56 setup_tax_slider(self.widget.child_finder('tax_slider'), 57 self.widget.child_finder('tax_val_label'), 58 self.instance.settlement, 59 message.level) 60 taxes = self.instance.settlement.tax_settings[self.instance.level] 61 self.widget.child_finder('tax_val_label').text = str(taxes) 62 imgs = list(ActionSetLoader.get_set(self.instance._action_set_id).items())[0][1] 63 self.widget.findChild(name="building_image").image = list(imgs[45].keys())[0]
64
65 - def show(self):
66 super().show() 67 SettlerUpdate.subscribe(self.on_settler_level_change, sender=self.instance)
68
69 - def hide(self):
70 SettlerUpdate.discard(self.on_settler_level_change, sender=self.instance) 71 super().hide()
72
73 - def refresh(self):
74 image, helptext = get_happiness_icon_and_helptext(self.instance.happiness, self.instance.session) 75 self.widget.child_finder('happiness_label').image = image 76 self.widget.child_finder('happiness_label').helptext = helptext 77 self.widget.child_finder('happiness').progress = self.instance.happiness 78 self.widget.child_finder('inhabitants').text = "{}/{}".format( 79 self.instance.inhabitants, 80 self.instance.inhabitants_max) 81 self.widget.child_finder('taxes').text = str(self.instance.last_tax_payed) 82 self.update_consumed_res() 83 name = self.instance.settlement.get_component(NamedComponent).name 84 self.widget.findChild(name="headline").text = name 85 events = { 86 'headline': Callback(self.instance.session.ingame_gui.show_change_name_dialog, 87 self.instance.settlement) 88 } 89 self.widget.mapEvents(events) 90 super().refresh()
91
92 - def update_consumed_res(self):
93 """Updates the container that displays the needed resources of the settler""" 94 container = self.widget.findChild(name="needed_res") 95 # remove icons from the container 96 container.removeAllChildren() 97 98 # create new ones 99 resources = self.instance.get_currently_not_consumed_resources() 100 for res in resources: 101 icon = create_resource_icon(res, self.instance.session.db) 102 icon.max_size = icon.min_size = icon.size = (32, 32) 103 container.addChild(icon) 104 105 container.adaptLayout()
106 107
108 -def setup_tax_slider(slider, val_label, settlement, level):
109 """Set up a slider to work as tax slider""" 110 step_count = int((SETTLER.TAX_SETTINGS_MAX - SETTLER.TAX_SETTINGS_MIN) / SETTLER.TAX_SETTINGS_STEP) 111 slider.steps = [SETTLER.TAX_SETTINGS_MIN + SETTLER.TAX_SETTINGS_STEP * i for i in 112 range(step_count)] 113 slider.value = settlement.tax_settings[level] 114 def on_slider_change(): 115 val_label.text = str(slider.value) 116 if settlement.tax_settings[level] != slider.value: 117 SetTaxSetting(settlement, level, slider.value).execute(settlement.session)
118 slider.capture(on_slider_change) 119