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

Source Code for Module horizons.gui.widgets.productionoverview

  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 math 
 23  from operator import itemgetter 
 24   
 25  from fife.extensions.pychan import widgets 
 26   
 27  from horizons.component.namedcomponent import NamedComponent 
 28  from horizons.constants import GAME_SPEED 
 29  from horizons.gui.util import create_resource_icon, load_uh_widget 
 30  from horizons.gui.widgets.imagebutton import OkButton 
 31  from horizons.gui.widgets.statswidget import StatsWidget 
 32  from horizons.gui.windows import Window 
 33  from horizons.i18n import gettext as T 
 34  from horizons.scheduler import Scheduler 
 35  from horizons.util.python.callback import Callback 
36 37 38 -class MultiPageStatsWidget(StatsWidget):
39 """ 40 A widget that creates a large table with statistics that can be browsed through page by 41 page. 42 """ 43 widget_file_name = None # type: str 44
45 - def _init_gui(self):
46 self._gui = load_uh_widget(self.widget_file_name, center_widget=self.center_widget) 47 if not self.center_widget: 48 self._gui.position_technique = 'center+20:center+25' 49 self._page_left = self._gui.findChild(name='page_left') 50 self._page_right = self._gui.findChild(name='page_right') 51 self.refresh()
52
53 - def _clear_entries(self):
54 self._page_left.removeAllChildren() 55 self._page_right.removeAllChildren()
56
57 58 -class ProductionOverview(MultiPageStatsWidget, Window):
59 """ 60 Widget that shows every produced resource in this game. 61 62 Implementation based on https://github.com/unknown-horizons/unknown-horizons/issues/749 . 63 """ 64 LINES_PER_PAGE = 12 65 66 widget_file_name = 'island_production.xml' 67
68 - def __init__(self, windows, settlement):
69 StatsWidget.__init__(self, settlement.session, center_widget=True) 70 Window.__init__(self, windows) 71 72 self.current_page = 0 73 self.settlement = settlement 74 self.db = self.settlement.session.db 75 Scheduler().add_new_object(Callback(self._refresh_tick), self, run_in=GAME_SPEED.TICKS_PER_SECOND, loops=-1)
76
77 - def _init_gui(self):
78 super()._init_gui() 79 self._gui.findChild(name=OkButton.DEFAULT_NAME).capture(self._windows.close) 80 self._gui.findChild(name='forwardButton').capture(self.go_to_next_page) 81 self._gui.findChild(name='backwardButton').capture(self.go_to_previous_page)
82 83 @property
84 - def displayed_resources(self):
85 """ 86 Returns all resources of the settlement that should be shown. 87 """ 88 data = sorted(self.settlement.produced_res.items(), key=itemgetter(1), reverse=True) 89 return [(resource_id, amount) for (resource_id, amount) in data 90 if self.db.get_res_inventory_display(resource_id)]
91 92 @property
93 - def max_pages(self):
94 """ 95 Returns number of pages the resources need. 96 """ 97 # int() and float() wrapping needed? 98 return int(math.ceil(len(self.displayed_resources) / float(self.LINES_PER_PAGE)))
99
100 - def go_to_next_page(self):
101 """ 102 Scrolls forward two pages. `self.current_page` will always be the index of the 103 left page of the book. 104 """ 105 self.current_page = min(self.max_pages - 1, self.current_page + 2) 106 self.current_page -= self.current_page % 2 107 self.refresh()
108
109 - def go_to_previous_page(self):
110 """ 111 Scrolls backward two pages. `self.current_page` will always be the index of the 112 left page of the book. 113 """ 114 self.current_page = max(0, self.current_page - 2) 115 self.refresh()
116
117 - def refresh(self):
118 super().refresh() 119 120 name = self.settlement.get_component(NamedComponent).name 121 text = T('Production overview of {settlement}').format(settlement=name) 122 self._gui.findChild(name='headline').text = text 123 124 forward_button = self._gui.findChild(name='forwardButton') 125 backward_button = self._gui.findChild(name='backwardButton') 126 127 if self.current_page == 0: 128 backward_button.set_inactive() 129 else: 130 backward_button.set_active() 131 132 max_left_page_idx = self.max_pages - 1 133 max_left_page_idx -= max_left_page_idx % 2 134 if self.current_page == max_left_page_idx: 135 forward_button.set_inactive() 136 else: 137 forward_button.set_active() 138 139 data = self.displayed_resources 140 data = data[self.current_page * self.LINES_PER_PAGE:(self.current_page + 2) * self.LINES_PER_PAGE] 141 142 for idx, (resource_id, amount) in enumerate(data, start=1): 143 if idx > self.LINES_PER_PAGE: 144 container = self._page_right 145 else: 146 container = self._page_left 147 148 self._add_line_to_gui(container, resource_id, amount) 149 150 self._page_left.adaptLayout() 151 self._page_right.adaptLayout()
152
153 - def _add_line_to_gui(self, container, resource_id, amount):
154 res_name = self.db.get_res_name(resource_id) 155 156 icon = create_resource_icon(resource_id, self.db) 157 icon.name = 'icon_{}'.format(resource_id) 158 icon.max_size = icon.min_size = icon.size = (20, 20) 159 160 label = widgets.Label(name='resource_{}'.format(resource_id)) 161 label.text = res_name 162 label.min_size = (100, 20) 163 164 amount_label = widgets.Label(name='produced_sum_{}'.format(resource_id)) 165 amount_label.text = str(amount) 166 167 hbox = widgets.HBox() 168 hbox.addChild(icon) 169 hbox.addChild(label) 170 hbox.addChild(amount_label) 171 container.addChild(hbox)
172