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

Source Code for Module horizons.gui.tabs.barrackstabs

  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 operator import itemgetter 
 23   
 24  from fife.extensions.pychan.widgets import Container, Icon, Label 
 25   
 26  from horizons.command.production import AddProduction 
 27  from horizons.constants import PRODUCTIONLINES, RES, UNITS 
 28  from horizons.gui.util import create_resource_icon 
 29  from horizons.gui.widgets.imagebutton import CancelButton, OkButton 
 30  from horizons.i18n import gettext as T, gettext_lazy as LazyT 
 31  from horizons.util.python.callback import Callback 
 32   
 33  from .boatbuildertabs import ProducerOverviewTabBase, UnitbuilderTabBase 
 34   
 35   
36 -class BarracksTab(UnitbuilderTabBase):
37 widget = 'barracks.xml' 38 helptext = LazyT("Barracks overview") 39 40 UNIT_THUMBNAIL = "content/gui/icons/thumbnails/{type_id}.png" 41 UNIT_PREVIEW_IMAGE = "content/gui/images/objects/groundunit/116/{type_id}.png"
42 43 # this tab additionally requests functions for: 44 # * decide: show [start view] = nothing but info text, look up the xml, or [building status view] 45 # * get: currently built groundunit: name / image / upgrades 46 # * resources still needed: 47 # (a) which ones? three most important (image, name) 48 # (b) how many? sort by amount, display (amount, overall amount needed of them, image) 49 # * pause production (keep order and "running" running costs [...] but collect no new resources) 50 # * abort building process: delete task, remove all resources, display [start view] again 51 52
53 -class BarracksSelectTab(ProducerOverviewTabBase):
54 widget = 'barracks_showcase.xml' 55
56 - def init_widget(self):
57 super().init_widget() 58 self.widget.findChild(name='headline').text = self.helptext 59 60 showcases = self.widget.findChild(name='showcases') 61 for i, (groundunit, prodline) in enumerate(self.groundunits): 62 showcase = self.build_groundunit_info(i, groundunit, prodline) 63 showcases.addChild(showcase)
64
65 - def build_groundunit_info(self, index, groundunit, prodline):
66 size = (260, 90) 67 widget = Container(name='showcase_{}'.format(index), position=(0, 20 + index * 90), 68 min_size=size, max_size=size, size=size) 69 bg_icon = Icon(image='content/gui/images/background/square_80.png', name='bg_{}'.format(index)) 70 widget.addChild(bg_icon) 71 72 image = 'content/gui/images/objects/groundunit/76/{unit_id}.png'.format(unit_id=groundunit) 73 helptext = self.instance.session.db.get_unit_tooltip(groundunit) 74 unit_icon = Icon(image=image, name='icon_{}'.format(index), position=(2, 2), 75 helptext=helptext) 76 widget.addChild(unit_icon) 77 78 # if not buildable, this returns string with reason why to be displayed as helptext 79 #groundunit_unbuildable = self.is_groundunit_unbuildable(groundunit) 80 groundunit_unbuildable = False 81 if not groundunit_unbuildable: 82 button = OkButton(position=(60, 50), name='ok_{}'.format(index), helptext=T('Build this groundunit!')) 83 button.capture(Callback(self.start_production, prodline)) 84 else: 85 button = CancelButton(position=(60, 50), name='ok_{}'.format(index), 86 helptext=groundunit_unbuildable) 87 88 widget.addChild(button) 89 90 # Get production line info 91 production = self.producer.create_production_line(prodline) 92 # consumed == negative, reverse to sort in *ascending* order: 93 costs = sorted(production.consumed_res.items(), key=itemgetter(1)) 94 for i, (res, amount) in enumerate(costs): 95 xoffset = 103 + (i % 2) * 55 96 yoffset = 20 + (i // 2) * 20 97 icon = create_resource_icon(res, self.instance.session.db) 98 icon.max_size = icon.min_size = icon.size = (16, 16) 99 icon.position = (xoffset, yoffset) 100 label = Label(name='cost_{}_{}'.format(index, i)) 101 if res == RES.GOLD: 102 label.text = str(-amount) 103 else: 104 label.text = '{amount:02}t'.format(amount=-amount) 105 label.position = (22 + xoffset, yoffset) 106 widget.addChild(icon) 107 widget.addChild(label) 108 return widget
109
110 - def start_production(self, prod_line_id):
111 AddProduction(self.producer, prod_line_id).execute(self.instance.session) 112 # show overview tab 113 self.instance.session.ingame_gui.get_cur_menu().show_tab(0)
114 115
116 -class BarracksSwordmanTab(BarracksSelectTab):
117 icon_path = 'icons/tabwidget/barracks/swordman' 118 helptext = LazyT("Swordman") 119 120 groundunits = [ 121 (UNITS.SWORDSMAN, PRODUCTIONLINES.SWORDSMAN), 122 ]
123 124 # these tabs additionally request functions for: 125 # * goto: show [confirm view] tab (not accessible via tab button in the end) 126 # need to provide information about the selected groundunit (which of the 4 buttons clicked) 127 # * check: mark those groundunit's buttons as unbuildable (close graphics) which do not meet the specified requirements. 128 # the tooltips contain this info as well. 129 130
131 -class BarracksConfirmTab(ProducerOverviewTabBase):
132 widget = 'barracks_confirm.xml' 133 helptext = LazyT("Confirm order") 134
135 - def init_widget(self):
136 super().init_widget() 137 events = {'create_unit': self.start_production} 138 self.widget.mapEvents(events)
139
140 - def start_production(self):
141 AddProduction(self.producer, 15).execute(self.instance.session)
142 143 # this "tab" additionally requests functions for: 144 # * get: currently ordered groundunit: name / image / type (fisher/trade/war) 145 # * => get: currently ordered groundunit: description text / costs / available upgrades 146 # (fisher/trade/war, builder level) 147 # * if resource icons not hardcoded: resource icons, sort them by amount 148 # UPGRADES: * checkboxes * check for groundunit builder level (+ research) * add. costs (get, add, display) 149 # * def start_production(self): <<< actually start to produce the selected groundunit unit with the selected upgrades 150 # (use inventory or go collect resources, switch focus to overview tab). 151 # IMPORTANT: lock this button until unit is actually produced (no queue!) 152