comparison gamelib/gamegui.py @ 66:43b34b013462

Add development and activity screens
author Neil Muller <drnlmuller@gmail.com>
date Tue, 08 May 2012 22:04:11 +0200
parents 5c4c67673112
children 06c8539df478
comparison
equal deleted inserted replaced
65:5c4c67673112 66:43b34b013462
6 from pygame import image 6 from pygame import image
7 7
8 from gamelib.data import filepath 8 from gamelib.data import filepath
9 from gamelib.gui_base import Window, TextLabel, font_small, font_medium 9 from gamelib.gui_base import Window, TextLabel, font_small, font_medium
10 from gamelib.gui import BigButton 10 from gamelib.gui import BigButton
11 from gamelib.engine import PopWindow 11 from gamelib.engine import PopWindow, AddWindow
12 from gamelib.constants import WIDTH 12 from gamelib.constants import WIDTH
13 from gamelib.gamestate import Game 13 from gamelib.gamestate import Game
14 14
15 15
16 class ExitGameButton(BigButton): 16 class ExitGameButton(BigButton):
37 def __init__(self, parent): 37 def __init__(self, parent):
38 super(ResetButton, self).__init__((10, 10), 'Reset points') 38 super(ResetButton, self).__init__((10, 10), 'Reset points')
39 self.parent = parent 39 self.parent = parent
40 40
41 def on_click(self): 41 def on_click(self):
42 self.parent.reset() 42 self.parent.do_reset()
43
44
45 class SwitchWinButton(BigButton):
46
47 def __init__(self, pos, text, new_window):
48 super(SwitchWinButton, self).__init__(pos, text)
49 self.new_window = new_window
50
51 def on_click(self):
52 PopWindow.post()
53 # Refresh state
54 self.new_window.update()
55 AddWindow.post(self.new_window)
43 56
44 57
45 class ScienceWidget(BigButton): 58 class ScienceWidget(BigButton):
46 59
47 WIDTH = 200 60 WIDTH = 200
82 'Available Human Resources : 0', font_medium, (255, 255, 0)) 95 'Available Human Resources : 0', font_medium, (255, 255, 0))
83 96
84 def set_value(self, value): 97 def set_value(self, value):
85 self.text = 'Available Human Resource : %d' % value 98 self.text = 'Available Human Resource : %d' % value
86 self._draw_text() 99 self._draw_text()
100
101
102 class ActivityWindow(Window):
103
104 def __init__(self, screen, lab, develop):
105 super(ActivityWindow, self).__init__(screen)
106 self.lab = lab
107 self.develop = develop
108 self.game = lab.game
109 exitbut = ExitGameButton()
110 self.add_child(exitbut)
111 end_turn = EndTurnButton(self)
112 self.add_child(end_turn)
113 reset = ResetButton(self)
114 self.add_child(reset)
115
116 self.points = PointsLabel()
117 self.add_child(self.points)
118
119 labbut = SwitchWinButton((150, 10), 'Research', lab)
120 self.add_child(labbut)
121
122 devbut = SwitchWinButton((300, 10), 'Development', develop)
123 self.add_child(devbut)
124
125 self.update_points()
126 self._make_widgets()
127
128 def _make_widgets(self):
129 pass
130
131 def end_turn(self):
132 # Drop back to the research screen
133 PopWindow.post()
134 self.lab.update()
135 AddWindow.post(self.lab)
136 self.lab.end_turn()
137
138 def update_widgets(self):
139 pass
140
141 def update(self):
142 self.update_points()
143
144 def update_points(self):
145 self.points.set_value(self.game.get_available_points())
146
147 def do_reset(self):
148 self.lab.reset()
149 self.develop.reset()
150 self.reset()
151
152 def reset(self):
153 self.update_points()
154
155
156 class DevelopmentWindow(Window):
157 """Window for handling schematics research"""
158
159 def __init__(self, screen, lab):
160 super(DevelopmentWindow, self).__init__(screen)
161 self.lab = lab
162 self.game = lab.game
163 exitbut = ExitGameButton()
164 self.add_child(exitbut)
165 end_turn = EndTurnButton(self)
166 self.add_child(end_turn)
167 reset = ResetButton(self)
168 self.add_child(reset)
169
170 self.points = PointsLabel()
171 self.add_child(self.points)
172
173 labbut = SwitchWinButton((150, 10), 'Research', lab)
174 self.add_child(labbut)
175 self.activity = None
176
177 self.update_points()
178 self._sciences = []
179 self._make_science_widgets()
180
181 def set_activity_window(self, activity):
182 # Oh, what tangled webs we weave
183 if not self.activity:
184 self.activity = activity
185 actbut = SwitchWinButton((300, 10), 'Activities', activity)
186 self.add_child(actbut)
187
188 def _make_science_widgets(self):
189 x = 0
190 y = 150
191
192 for science in self.game.lab.science:
193 if science.SCIENCE_TYPE == 'schematic':
194 widget = ScienceWidget(science, (x, y), self)
195 self.add_child(widget)
196 self._sciences.append(widget)
197 x += 200
198 if x >= WIDTH:
199 x = 0
200 y += 100
201
202 def end_turn(self):
203 # Drop back to the research screen
204 PopWindow.post()
205 self.lab.update()
206 AddWindow.post(self.lab)
207 self.lab.end_turn()
208
209 def update_widgets(self):
210 for widget in self._sciences:
211 self.remove_child(widget)
212 self._sciences = []
213 self._make_science_widgets()
214 self.update_points()
215
216 def update(self):
217 self.update_points()
218
219 def update_points(self):
220 self.points.set_value(self.game.get_available_points())
221
222 def do_reset(self):
223 self.reset()
224 self.lab.reset()
225 self.activity.reset()
226
227 def reset(self):
228 for widget in self._sciences:
229 widget.reset()
87 230
88 231
89 class LabWindow(Window): 232 class LabWindow(Window):
90 """Window for the research lab""" 233 """Window for the research lab"""
91 234
100 self.add_child(reset) 243 self.add_child(reset)
101 244
102 self.points = PointsLabel() 245 self.points = PointsLabel()
103 self.add_child(self.points) 246 self.add_child(self.points)
104 247
248 self.develop = DevelopmentWindow(screen, self)
249 self.activity = ActivityWindow(screen, self, self.develop)
250 self.develop.set_activity_window(self.activity)
251
252 devbut = SwitchWinButton((150, 10), 'Development', self.develop)
253 self.add_child(devbut)
254 actbut = SwitchWinButton((300, 10), 'Activities', self.activity)
255 self.add_child(actbut)
256
105 self._sciences = [] 257 self._sciences = []
106 # Setup for the first turn 258 # Setup for the first turn
107 self.game.start_turn() 259 self.game.start_turn()
108 self.update_points() 260 self.update_points()
109 self._make_science_widgets() 261 self._make_science_widgets()
111 def _make_science_widgets(self): 263 def _make_science_widgets(self):
112 x = 0 264 x = 0
113 y = 150 265 y = 150
114 266
115 for science in self.game.lab.science: 267 for science in self.game.lab.science:
116 widget = ScienceWidget(science, (x, y), self) 268 if science.SCIENCE_TYPE == 'research':
117 self.add_child(widget) 269 widget = ScienceWidget(science, (x, y), self)
118 self._sciences.append(widget) 270 self.add_child(widget)
119 x += 200 271 self._sciences.append(widget)
120 if x >= WIDTH: 272 x += 200
121 x = 0 273 if x >= WIDTH:
122 y += 100 274 x = 0
123 275 y += 100
124 def end_turn(self): 276
125 self.game.end_turn() 277 def update_widgets(self):
126 # FIXME: Horrible hackery 278 # FIXME: Horrible hackery
127 for widget in self._sciences: 279 for widget in self._sciences:
128 self.remove_child(widget) 280 self.remove_child(widget)
129 self._sciences = [] 281 self._sciences = []
130 self._make_science_widgets() 282 self._make_science_widgets()
283
284 def end_turn(self):
285 self.game.end_turn()
131 self.game.start_turn() 286 self.game.start_turn()
287 self.update_points()
288 self.update_widgets()
289 self.develop.update_widgets()
290 self.activity.update_widgets()
291
292 def update(self):
132 self.update_points() 293 self.update_points()
133 294
134 def update_points(self): 295 def update_points(self):
135 self.points.set_value(self.game.get_available_points()) 296 self.points.set_value(self.game.get_available_points())
297
298 def do_reset(self):
299 self.reset()
300 self.develop.reset()
301 self.activity.reset()
136 302
137 def reset(self): 303 def reset(self):
138 for widget in self._sciences: 304 for widget in self._sciences:
139 widget.reset() 305 widget.reset()