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

Source Code for Module horizons.gui.widgets.playersships

  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 HBox, Label 
 23   
 24  from horizons.component.healthcomponent import HealthComponent 
 25  from horizons.component.namedcomponent import NamedComponent 
 26  from horizons.component.selectablecomponent import SelectableComponent 
 27  from horizons.gui.widgets.statswidget import StatsWidget 
 28  from horizons.i18n import gettext as T 
 29  from horizons.util.python.callback import Callback 
 30  from horizons.world.units.fightingship import FightingShip 
 31   
 32   
33 -class PlayersShips(StatsWidget):
34 """Widget that shows a list of the player's ships.""" 35 36 widget_file_name = 'ships_list.xml' 37
38 - def __init__(self, session):
39 super().__init__(session)
40
41 - def refresh(self):
42 super().refresh() 43 player = self.session.world.player 44 self._clear_entries() 45 self._gui.findChild(name='headline').text = T("Ships of {player}").format(player=self.session.world.player.name) 46 47 sequence_number = 0 48 events = {} 49 for ship in sorted(self.session.world.ships, key=lambda ship: (ship.get_component(NamedComponent).name, ship.worldid)): 50 if ship.owner is player and ship.has_component(SelectableComponent): 51 sequence_number += 1 52 name_label, rename_icon, status_label, status_position = \ 53 self._add_line_to_gui(ship, sequence_number) 54 events['{}/mouseClicked'.format(name_label.name)] = Callback(self._go_to_ship, ship) 55 cb = Callback(self.session.ingame_gui.show_change_name_dialog, ship) 56 events['{}/mouseClicked'.format(rename_icon.name)] = cb 57 events['{}/mouseClicked'.format(status_label.name)] = Callback(self._go_to_point, status_position) 58 self._gui.mapEvents(events) 59 self._content_vbox.adaptLayout()
60
61 - def _go_to_ship(self, ship):
63
64 - def _go_to_point(self, point):
65 self.session.view.center(point.x, point.y)
66
67 - def _add_line_to_gui(self, ship, sequence_number):
68 sequence_number_label = Label(name='sequence_number_{:d}'.format(ship.worldid)) 69 sequence_number_label.text = str(sequence_number) 70 sequence_number_label.min_size = sequence_number_label.max_size = (15, 20) 71 72 ship_name = Label(name='ship_name_{:d}'.format(ship.worldid)) 73 ship_name.text = ship.get_component(NamedComponent).name 74 ship_name.min_size = ship_name.max_size = (100, 20) 75 76 from horizons.engine.pychan_util import RenameImageButton 77 rename_icon = RenameImageButton(name='rename_{:d}'.format(ship.worldid)) 78 rename_icon.path = "images/background/rename_feather_20" 79 rename_icon.helptext = T("Click to change the name of this ship") 80 rename_icon.max_size = (20, 20) # (width, height) 81 82 ship_type = Label(name='ship_type_{:d}'.format(ship.worldid)) 83 ship_type.text = ship.classname 84 ship_type.min_size = ship_type.max_size = (60, 20) 85 86 weapons = Label(name='weapons_{:d}'.format(ship.worldid)) 87 if isinstance(ship, FightingShip): 88 weapon_list = [] 89 for weapon_id, amount in sorted(ship.get_weapon_storage().itercontents()): 90 weapon_list.append('{:d} {}'.format(amount, self.session.db.get_res_name(weapon_id))) 91 if weapon_list: 92 weapons.text = ', '.join(weapon_list) 93 else: 94 #i18n There are no weapons equipped at the moment. 95 weapons.text = T('None') 96 else: 97 weapons.text = T('N/A') 98 weapons.min_size = weapons.max_size = (60, 20) 99 100 health = Label(name='health_{:d}'.format(ship.worldid)) 101 health_component = ship.get_component(HealthComponent) 102 health.text = '{:.1f}/{:.1f}'.format(health_component.health, health_component.max_health) 103 health.min_size = health.max_size = (65, 20) 104 105 status = Label(name='status_{:d}'.format(ship.worldid)) 106 status.text, status_position = ship.get_status() 107 status.min_size = status.max_size = (320, 20) 108 109 hbox = HBox() 110 hbox.addChild(sequence_number_label) 111 hbox.addChild(ship_name) 112 hbox.addChild(rename_icon) 113 hbox.addChild(ship_type) 114 hbox.addChild(weapons) 115 hbox.addChild(health) 116 hbox.addChild(status) 117 self._content_vbox.addChild(hbox) 118 return (ship_name, rename_icon, status, status_position)
119