comparison gamelib/toolbar.py @ 433:8643893635e7

Seperate toolbar and gameboard
author Neil Muller <drnlmuller@gmail.com>
date Sat, 21 Nov 2009 16:59:49 +0000
parents
children f2a55e5e24db
comparison
equal deleted inserted replaced
432:d630465d7a84 433:8643893635e7
1 import pygame
2 from pgu import gui
3
4 import icons
5 import constants
6 import buildings
7 import equipment
8 import cursors
9 import engine
10
11 class OpaqueLabel(gui.Label):
12 def __init__(self, value, **params):
13 gui.Label.__init__(self, value, **params)
14 if 'width' in params:
15 self._width = params['width']
16 if 'height' in params:
17 self._height = params['height']
18 self._set_size()
19
20 def _set_size(self):
21 width, height = self.font.size(self.value)
22 width = getattr(self, '_width', width)
23 height = getattr(self, '_height', height)
24 self.style.width, self.style.height = width, height
25
26 def paint(self, s):
27 s.fill(self.style.background)
28 if self.style.align > 0:
29 r = s.get_rect()
30 w, _ = self.font.size(self.value)
31 s = s.subsurface(r.move((r.w-w, 0)).clip(r))
32 gui.Label.paint(self, s)
33
34 def update_value(self, value):
35 self.value = value
36 self._set_size()
37 self.repaint()
38
39 def mklabel(text="", **params):
40 params.setdefault('color', constants.FG_COLOR)
41 params.setdefault('width', constants.TOOLBAR_WIDTH/2)
42 return OpaqueLabel(text, **params)
43
44 def mkcountupdate(counter):
45 def update_counter(self, value):
46 getattr(self, counter).update_value("%s " % value)
47 self.repaint()
48 return update_counter
49
50 class ToolBar(gui.Table):
51 def __init__(self, gameboard, level, **params):
52 gui.Table.__init__(self, **params)
53 self.group = gui.Group(name='toolbar', value=None)
54 self._next_tool_value = 0
55 self.gameboard = gameboard
56 self.cash_counter = mklabel(align=1)
57 self.wood_counter = mklabel(align=1)
58 self.chicken_counter = mklabel(align=1)
59 self.egg_counter = mklabel(align=1)
60 self.day_counter = mklabel(align=1)
61 self.killed_foxes = mklabel(align=1)
62
63 self.tr()
64 self.td(gui.Spacer(self.rect.w/2, 0))
65 self.td(gui.Spacer(self.rect.w/2, 0))
66 self.add_counter(mklabel("Day:"), self.day_counter)
67 self.add_counter(mklabel("Groats:"), self.cash_counter)
68 self.add_counter(mklabel("Planks:"), self.wood_counter)
69 self.add_counter(mklabel("Eggs:"), self.egg_counter)
70 self.add_counter(icons.CHKN_ICON, self.chicken_counter)
71 self.add_counter(icons.KILLED_FOX, self.killed_foxes)
72 self.add_spacer(5)
73
74 self.add_tool_button("Move Hen", constants.TOOL_PLACE_ANIMALS,
75 None, cursors.cursors['select'])
76 self.add_spacer(5)
77
78 self.add_heading("Sell ...")
79 self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
80 level.sell_price_chicken, cursors.cursors['sell'])
81 self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
82 level.sell_price_egg, cursors.cursors['sell'])
83 self.add_tool_button("Building", constants.TOOL_SELL_BUILDING,
84 None, cursors.cursors['sell'])
85 self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
86 None, cursors.cursors['sell'])
87 self.add_spacer(5)
88
89 self.add_heading("Buy ...")
90
91 for building_cls in buildings.BUILDINGS:
92 self.add_tool_button(building_cls.NAME.title(), building_cls,
93 None, cursors.cursors.get('build', None))
94
95 for equipment_cls in equipment.EQUIPMENT:
96 self.add_tool_button(equipment_cls.NAME.title(),
97 equipment_cls,
98 equipment_cls.BUY_PRICE,
99 cursors.cursors.get('buy', None))
100
101 self.add_spacer(5)
102 self.add_tool_button("Repair", constants.TOOL_REPAIR_BUILDING, None, cursors.cursors['repair'])
103
104 self.add_spacer(5)
105 self.add_tool("Price Reference", self.show_prices)
106
107 self.fin_tool = self.add_tool("Finished Day", self.day_done)
108
109 self.anim_clear_tool = False # Flag to clear the tool on an anim loop
110 # pgu's tool widget fiddling happens after the tool action, so calling
111 # clear_tool in the tool's action doesn't work, so we punt it to
112 # the anim loop
113
114 def day_done(self):
115 if self.gameboard.day:
116 pygame.event.post(engine.START_NIGHT)
117 else:
118 self.anim_clear_tool = True
119 pygame.event.post(engine.FAST_FORWARD)
120
121 def update_fin_tool(self, day):
122 if day:
123 self.fin_tool.widget = gui.basic.Label('Finished Day')
124 self.fin_tool.resize()
125 else:
126 self.fin_tool.widget = gui.basic.Label('Fast Forward')
127 self.fin_tool.resize()
128
129 def show_prices(self):
130 """Popup dialog of prices"""
131 def make_box(text):
132 style = {
133 'border' : 1
134 }
135 word = gui.Label(text, style=style)
136 return word
137
138 def fix_widths(doc):
139 """Loop through all the widgets in the doc, and set the
140 width of the labels to max + 10"""
141 # We need to do this because of possible font issues
142 max_width = 0
143 for thing in doc.widgets:
144 if hasattr(thing, 'style'):
145 # A label
146 if thing.style.width > max_width:
147 max_width = thing.style.width
148 for thing in doc.widgets:
149 if hasattr(thing, 'style'):
150 thing.style.width = max_width + 10
151
152 tbl = gui.Table()
153 tbl.tr()
154 doc = gui.Document(width=510)
155 space = doc.style.font.size(" ")
156 for header in ['Item', 'Buy Price', 'Sell Price', 'Repair Price']:
157 doc.add(make_box(header))
158 doc.br(space[1])
159 for building in buildings.BUILDINGS:
160 doc.add(make_box(building.NAME))
161 doc.add(make_box('%d' % building.BUY_PRICE))
162 doc.add(make_box('%d' % building.SELL_PRICE))
163 if building.BREAKABLE:
164 doc.add(make_box('%d' % building.REPAIR_PRICE))
165 else:
166 doc.add(make_box('N/A'))
167 doc.br(space[1])
168 for equip in equipment.EQUIPMENT:
169 doc.add(make_box(equip.NAME))
170 doc.add(make_box('%d' % equip.BUY_PRICE))
171 doc.add(make_box('%d' % equip.SELL_PRICE))
172 doc.add(make_box('N/A'))
173 doc.br(space[1])
174
175 fix_widths(doc)
176 for word in "Damaged equipment or buildings will be sold for" \
177 " less than the sell price.".split():
178 doc.add(gui.Label(word))
179 doc.space(space)
180 close_button = gui.Button("Close")
181 tbl.td(doc)
182 tbl.tr()
183 tbl.td(close_button, align=1)
184 dialog = gui.Dialog(gui.Label('Price Reference'), tbl)
185 close_button.connect(gui.CLICK, dialog.close)
186 dialog.open()
187 self.anim_clear_tool = True
188
189 update_cash_counter = mkcountupdate('cash_counter')
190 update_wood_counter = mkcountupdate('wood_counter')
191 update_fox_counter = mkcountupdate('killed_foxes')
192 update_chicken_counter = mkcountupdate('chicken_counter')
193 update_egg_counter = mkcountupdate('egg_counter')
194 update_day_counter = mkcountupdate('day_counter')
195
196 def add_spacer(self, height):
197 self.tr()
198 self.td(gui.Spacer(0, height), colspan=2)
199
200 def add_heading(self, text):
201 self.tr()
202 self.td(mklabel(text), colspan=2)
203
204 def add_tool_button(self, text, tool, price=None, cursor=None):
205 if price is not None:
206 text = "%s (%s)" % (text, price)
207 self.add_tool(text, lambda: self.gameboard.set_selected_tool(tool,
208 cursor))
209
210 def add_tool(self, text, func):
211 label = gui.basic.Label(text)
212 value = self._next_tool_value
213 self._next_tool_value += 1
214 tool = gui.Tool(self.group, label, value, width=self.rect.w, style={"padding_left": 0})
215 tool.connect(gui.CLICK, func)
216 self.tr()
217 self.td(tool, align=-1, colspan=2)
218 return tool
219
220 def clear_tool(self):
221 self.group.value = None
222 for item in self.group.widgets:
223 item.pcls = ""
224 self.anim_clear_tool = False
225
226 def add_counter(self, icon, label):
227 self.tr()
228 self.td(icon, width=self.rect.w/2)
229 self.td(label, width=self.rect.w/2)
230
231 def resize(self, width=None, height=None):
232 width, height = gui.Table.resize(self, width, height)
233 width = constants.TOOLBAR_WIDTH
234 return width, height
235