Package horizons :: Package gui :: Package widgets :: Module unitoverview
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.widgets.unitoverview

  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 Container, HBox, Icon 
 23   
 24  from horizons.command.unit import SetStance 
 25  from horizons.component.healthcomponent import HealthComponent 
 26  from horizons.component.stancecomponent import DEFAULT_STANCES 
 27  from horizons.extscheduler import ExtScheduler 
 28  from horizons.gui.util import get_res_icon_path, load_uh_widget 
 29  from horizons.i18n import gettext as T 
 30  from horizons.util.python.callback import Callback 
 31   
 32   
33 -class StanceWidget(Container):
34 """Widget used for setting up the stance for one instance"""
35 - def __init__(self, **kwargs):
36 super().__init__(size=(245, 50), **kwargs) 37 widget = load_uh_widget('stancewidget.xml') 38 self.addChild(widget) 39 ExtScheduler().add_new_object(self.refresh, self, run_in=0.3, loops=-1)
40
41 - def init(self, instance):
42 self.instance = instance 43 self.toggle_stance() 44 events = {i.NAME: Callback(self.set_stance, i) for i in DEFAULT_STANCES} 45 self.mapEvents(events)
46
47 - def beforeShow(self):
48 super().beforeShow() 49 ExtScheduler().rem_all_classinst_calls(self) 50 ExtScheduler().add_new_object(self.refresh, self, run_in=1, loops=-1)
51
52 - def refresh(self):
53 if not self.isVisible(): 54 # refresh not needed 55 ExtScheduler().rem_all_classinst_calls(self) 56 return 57 self.toggle_stance()
58
59 - def remove(self, caller=None):
60 """Removes instance ref""" 61 ExtScheduler().rem_all_classinst_calls(self) 62 self.mapEvents({}) 63 self.instance = None
64
65 - def set_stance(self, stance):
66 SetStance(self.instance, stance).execute(self.instance.session) 67 self.toggle_stance()
68
69 - def toggle_stance(self):
70 for stance in DEFAULT_STANCES: 71 self.findChild(name=stance.NAME).set_inactive() 72 self.findChild(name=self.instance.stance.NAME).set_active()
73 74
75 -class HealthWidget(Container):
76 """Widget that shows a health bar for an unit"""
77 - def __init__(self, **kwargs):
78 super().__init__(size=(50, 25), **kwargs) 79 widget = load_uh_widget('healthwidget.xml') 80 self.addChild(widget)
81
82 - def init(self, instance):
83 self.instance = instance 84 self.draw_health() 85 health_component = self.instance.get_component(HealthComponent) 86 if not health_component.has_damage_dealt_listener(self.draw_health): 87 health_component.add_damage_dealt_listener(self.draw_health)
88
89 - def draw_health(self, caller=None):
90 health_component = self.instance.get_component(HealthComponent) 91 max_health = int(health_component.max_health) 92 health = int(health_component.health) 93 self.findChild(name='health_label').text = "{health}/{max_health}".format(health=health, max_health=max_health) 94 self.findChild(name='health_bar').progress = int(health * 100. / max_health)
95
96 - def remove(self, caller=None):
97 health_component = self.instance.get_component(HealthComponent) 98 if health_component.has_damage_dealt_listener(self.draw_health): 99 health_component.remove_damage_dealt_listener(self.draw_health) 100 self.instance = None
101 102
103 -class WeaponStorageWidget(HBox):
104 """Widget that shows a small overview for one instance weapons"""
105 - def init(self, instance):
106 self.instance = instance 107 self.update()
108
109 - def remove(self, caller=None):
110 self.instance = None
111
112 - def update(self):
113 self.removeAllChildren() 114 weapons_added = False 115 if hasattr(self.instance, 'get_weapon_storage'): 116 storage = self.instance.get_weapon_storage() 117 for weapon, amount in storage: 118 weapons_added = True 119 icon_image = get_res_icon_path(weapon, 24) 120 weapon_name = self.instance.session.db.get_res_name(weapon) 121 # You usually do not need to change anything here when translating 122 helptext = T('{weapon}: {amount}').format(weapon=weapon_name, amount=amount) 123 icon = Icon(image=icon_image, helptext=helptext) 124 self.addChild(icon) 125 if not weapons_added: 126 icon_image = "content/gui/icons/resources/none.png" 127 icon = Icon(image=icon_image, helptext=T("none")) 128 self.addChild(icon)
129