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

Source Code for Module horizons.gui.tabs.shiptabs

  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  import weakref 
 23   
 24  from horizons.command.uioptions import EquipWeaponFromInventory, UnequipWeaponToInventory 
 25  from horizons.component.selectablecomponent import SelectableComponent 
 26  from horizons.component.storagecomponent import StorageComponent 
 27  from horizons.constants import BUILDINGS, WEAPONS 
 28  from horizons.entities import Entities 
 29  from horizons.gui.widgets.routeconfig import RouteConfig 
 30  from horizons.i18n import gettext as T, gettext_lazy as LazyT 
 31  from horizons.util.python.callback import Callback 
 32   
 33  from .overviewtab import OverviewTab 
 34  from .tradetab import TradeTab 
 35   
 36   
37 -class ShipOverviewTab(OverviewTab):
38 widget = 'overview_ship.xml' 39 icon_path = 'icons/tabwidget/ship/ship_inv' 40 helptext = LazyT("Ship overview") 41
42 - def init_widget(self):
43 super().init_widget() 44 self.ship_inv = self.instance.get_component(StorageComponent).inventory 45 self.widget.child_finder('inventory').init(self.instance.session.db, self.ship_inv) 46 47 # FIXME having to access the WindowManager this way is pretty ugly 48 self._windows = self.instance.session.ingame_gui.windows 49 self.route_menu = RouteConfig(self._windows, self.instance)
50
51 - def _configure_route(self):
52 self._windows.toggle(self.route_menu)
53
54 - def _refresh_found_settlement_button(self, events):
55 island_without_player_settlement_found = False 56 helptext = T("The ship needs to be close to an island to found a settlement.") 57 for island in self.instance.session.world.get_islands_in_radius(self.instance.position, self.instance.radius): 58 if not any(settlement.owner.is_local_player for settlement in island.settlements): 59 island_without_player_settlement_found = True 60 else: 61 helptext = T("You already have a settlement on this island.") 62 63 if island_without_player_settlement_found: 64 events['found_settlement'] = Callback(self.instance.session.ingame_gui._build, 65 BUILDINGS.WAREHOUSE, 66 weakref.ref(self.instance)) 67 self.widget.child_finder('found_settlement_bg').set_active() 68 self.widget.child_finder('found_settlement').set_active() 69 self.widget.child_finder('found_settlement').helptext = T("Build settlement") 70 else: 71 events['found_settlement'] = None 72 self.widget.child_finder('found_settlement_bg').set_inactive() 73 self.widget.child_finder('found_settlement').set_inactive() 74 self.widget.child_finder('found_settlement').helptext = helptext 75 76 cb = Callback(self.instance.session.ingame_gui.resource_overview.set_construction_mode, 77 self.instance, 78 Entities.buildings[BUILDINGS.WAREHOUSE].costs) 79 events['found_settlement/mouseEntered'] = cb 80 81 cb1 = Callback(self.instance.session.ingame_gui.resource_overview.close_construction_mode) 82 cb2 = Callback(self.widget.child_finder('found_settlement').hide_tooltip) 83 #TODO the tooltip should actually hide on its own. Ticket #1096 84 cb = Callback.ChainedCallbacks(cb1, cb2) 85 events['found_settlement/mouseExited'] = cb
86
87 - def _refresh_trade_button(self, events):
88 warehouses = self.instance.get_tradeable_warehouses() 89 90 if warehouses: 91 if warehouses[0].owner is self.instance.owner: 92 helptext = T('Load/Unload') 93 else: 94 helptext = T('Buy/Sell') 95 events['trade'] = Callback(self.instance.get_component(SelectableComponent).show_menu, TradeTab) 96 self.widget.findChild(name='trade_bg').set_active() 97 self.widget.findChild(name='trade').set_active() 98 self.widget.findChild(name='trade').helptext = helptext 99 else: 100 events['trade'] = None 101 self.widget.findChild(name='trade_bg').set_inactive() 102 self.widget.findChild(name='trade').set_inactive() 103 self.widget.findChild(name='trade').helptext = T('Too far from the nearest tradeable warehouse')
104
105 - def _refresh_combat(self): # no combat
106 def click_on_cannons(button): 107 button.button.capture(Callback( 108 self.instance.session.ingame_gui.open_popup, 109 T("Cannot equip trade ship with weapons"), 110 T("It is not possible to equip a trade ship with weapons.") 111 ))
112 self.widget.findChild(name='inventory').apply_to_buttons(click_on_cannons, lambda b: b.res_id == WEAPONS.CANNON) 113
114 - def _refresh_traderoute_config_button(self, events):
115 #verify if there are possible destinations for a traderoute 116 warehouses = self.instance.session.world.settlements 117 118 possible_warehouses = [warehouse for warehouse in warehouses if self.instance.session.world.diplomacy.can_trade(self.instance.session.world.player, warehouse.owner)] 119 120 if len(possible_warehouses) > 1: 121 events['configure_route'] = Callback(self._configure_route) 122 self.widget.findChild(name='configure_route').set_active() 123 self.widget.findChild(name='configure_route').helptext = T("Configure trade route") 124 else: 125 events['configure_route'] = None 126 self.widget.findChild(name='configure_route').set_inactive() 127 self.widget.findChild(name='configure_route').helptext = T("No available destinations for a trade route")
128
129 - def refresh(self):
130 events = { 131 # show rename when you click on name 132 'name': Callback(self.instance.session.ingame_gui.show_change_name_dialog, self.instance), 133 } 134 135 self._refresh_found_settlement_button(events) 136 self._refresh_trade_button(events) 137 self._refresh_traderoute_config_button(events) 138 self.widget.mapEvents(events) 139 140 self.widget.child_finder('inventory').update() 141 self._refresh_combat() 142 super().refresh()
143
144 - def hide(self):
145 self.route_menu.hide() 146 super().hide()
147 148
149 -class FightingShipOverviewTab(ShipOverviewTab):
150 widget = 'overview_war_ship.xml' 151 # TODO why is this here: 152 icon_path = 'icons/tabwidget/ship/ship_inv' 153 154 has_stance = True 155
156 - def init_widget(self):
157 super().init_widget() 158 # Create weapon inventory, needed only in gui for inventory widget. 159 self.weapon_inventory = self.instance.get_weapon_storage() 160 self.widget.findChild(name='weapon_inventory').init(self.instance.session.db, self.weapon_inventory)
161
162 - def _refresh_combat(self):
163 def apply_equip(button): 164 button.button.helptext = T("Equip weapon") 165 button.button.capture(Callback(self.equip_weapon, button.res_id))
166 167 def apply_unequip(button): 168 button.button.helptext = T("Unequip weapon") 169 button.button.capture(Callback(self.unequip_weapon, button.res_id))
170 171 self.widget.findChild(name='weapon_inventory').apply_to_buttons(apply_unequip, lambda b: b.res_id == WEAPONS.CANNON) 172 self.widget.findChild(name='inventory').apply_to_buttons(apply_equip, lambda b: b.res_id == WEAPONS.CANNON) 173
174 - def equip_weapon(self, weapon_id):
175 if EquipWeaponFromInventory(self.instance, weapon_id, 1).execute(self.instance.session) == 0: 176 self.weapon_inventory.alter(weapon_id, 1) 177 self.widget.child_finder('weapon_inventory').update() 178 self.refresh()
179
180 - def unequip_weapon(self, weapon_id):
181 if UnequipWeaponToInventory(self.instance, weapon_id, 1).execute(self.instance.session) == 0: 182 self.weapon_inventory.alter(weapon_id, -1) 183 self.widget.child_finder('weapon_inventory').update() 184 self.refresh()
185
186 - def on_instance_removed(self):
187 self.weapon_inventory = None 188 super().on_instance_removed()
189 190
191 -class TradeShipOverviewTab(ShipOverviewTab):
192 widget = 'overview_trade_ship.xml' 193 icon_path = 'icons/tabwidget/ship/ship_inv' 194 helptext = LazyT("Ship overview") 195
197 if self.ship_inv.get_sum_of_stored_resources() == 0: 198 self.widget.findChild(name='discard_res_bg').set_inactive() 199 self.widget.findChild(name='discard_res').set_inactive() 200 else: 201 self.widget.findChild(name='discard_res_bg').set_active() 202 self.widget.findChild(name='discard_res').set_active()
203
204 - def _discard_resources(self):
205 self.ship_inv.reset_all() 206 self.widget.child_finder('inventory').update()
207
208 - def refresh(self):
209 super().refresh() 210 events = { 211 212 'discard_res/mouseClicked': Callback(self._discard_resources) 213 } 214 self.widget.mapEvents(events) 215 self._refresh_discard_resources() 216 super().refresh()
217 218
219 -class TraderShipOverviewTab(OverviewTab):
220 widget = 'overview_tradership.xml' 221 icon_path = 'icons/tabwidget/ship/ship_inv' 222 helptext = LazyT("Ship overview")
223 224
225 -class EnemyShipOverviewTab(OverviewTab):
226 widget = 'overview_enemyunit.xml' 227 icon_path = 'icons/tabwidget/ship/ship_inv' 228 helptext = LazyT("Ship overview") 229
230 - def init_widget(self):
231 super().init_widget() 232 self.widget.findChild(name="headline").text = self.instance.owner.name
233