comparison gamelib/gamegui.py @ 77:9ebce976a683

prettified
author Rizmari Versfeld <rizziepit@gmail.com>
date Wed, 09 May 2012 00:27:08 +0200
parents 06c8539df478
children a40a76012bd7
comparison
equal deleted inserted replaced
76:ae2c053ad80e 77:9ebce976a683
5 5
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, ImageDrawable
11 from gamelib.engine import PopWindow, AddWindow 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
84 self.parent.game.cur_allocation.remove(self.science) 84 self.parent.game.cur_allocation.remove(self.science)
85 self.points -= 1 85 self.points -= 1
86 self.text = '%s: %d' % (self.science.NAME, self.science.points) 86 self.text = '%s: %d' % (self.science.NAME, self.science.points)
87 self._draw_text() 87 self._draw_text()
88 self.parent.update_labels() 88 self.parent.update_labels()
89
90
91 class MissionWidget(BigButton):
92
93 WIDTH = 200
94
95 BG_IMAGE_NORMAL = image.load(filepath('images/science_normal.png'))
96 BG_IMAGE_DOWN = image.load(filepath('images/science_down.png'))
97 BG_IMAGE_SELECTED = image.load(filepath('images/mission_selected.png'))
98
99 def __init__(self, mission, pos, parent):
100 self.mission = mission
101 self.parent = parent
102 super(MissionWidget, self).__init__(pos, '%s' % mission.NAME,
103 font_small)
104 selected = ImageDrawable((0, 0, self.WIDTH, self.HEIGHT),
105 self.BG_IMAGE_SELECTED)
106 self.add_state('SELECTED', selected)
107 self.mode = 'NORMAL'
108 self.equipment = []
109
110 def on_mouse_cancel(self, pos):
111 self.set_state(self.mode)
112
113 def on_mouse_up(self, pos):
114 self.set_state(self.mode)
115 self.on_click()
116
117 def on_click(self):
118 if self.mode == 'SELECTED':
119 # unselect mission
120 self.mode = 'NORMAL'
121 self.set_state('NORMAL')
122 else:
123 # select mission and equipment
124 self.mode = 'SELECTED'
125 self.set_state('SELECTED')
126
127 def selected(self):
128 return self.mode == 'SELECTED'
129
130 def get_equipment(self):
131 return self.equipment
132
133 def reset(self):
134 self.equipment = []
135 self.mode = 'NORMAL'
136 self.set_state('NORMAL')
89 137
90 138
91 class ValueLabel(TextLabel): 139 class ValueLabel(TextLabel):
92 140
93 def __init__(self, pos, description): 141 def __init__(self, pos, description):
125 173
126 devbut = SwitchWinButton((300, 10), 'Development', develop) 174 devbut = SwitchWinButton((300, 10), 'Development', develop)
127 self.add_child(devbut) 175 self.add_child(devbut)
128 176
129 self.update_labels() 177 self.update_labels()
178 self._missions = []
130 self._make_widgets() 179 self._make_widgets()
131 180
132 def _make_widgets(self): 181 def _make_widgets(self):
133 pass 182 x = 0
183 y = 150
184 for mission in self.game.get_available_missions():
185 widget = MissionWidget(mission, (x, y), self)
186 self._missions.append(widget)
187 self.add_child(widget)
188 x += 200
189 if x >= WIDTH:
190 x = 0
191 y += 100
134 192
135 def end_turn(self): 193 def end_turn(self):
136 # Drop back to the research screen 194 # Drop back to the research screen
137 PopWindow.post() 195 PopWindow.post()
138 self.lab.update() 196 self.lab.update()
139 AddWindow.post(self.lab) 197 AddWindow.post(self.lab)
140 self.lab.end_turn() 198 self.lab.end_turn()
141 199
142 def update_widgets(self): 200 def update_widgets(self):
143 pass 201 for widget in self._missions:
202 self.remove_child(widget)
203 self._missions = []
204 self._make_widgets()
144 205
145 def update(self): 206 def update(self):
146 self.update_labels() 207 self.update_labels()
147 208
148 def update_labels(self): 209 def update_labels(self):
152 def do_reset(self): 213 def do_reset(self):
153 self.lab.reset() 214 self.lab.reset()
154 self.develop.reset() 215 self.develop.reset()
155 self.reset() 216 self.reset()
156 217
157 def reset(self): 218 def get_mission_list(self):
219 selected_missions = []
220 for widget in self._missions:
221 if widget.selected():
222 equipment = widget.get_equipment()
223 mission = widget.mission
224 selected_missions.append((mission, equipment))
225 return selected_missions
226
227 def reset(self):
228 for widget in self._missions:
229 widget.reset()
158 self.update_labels() 230 self.update_labels()
159 231
160 232
161 class DevelopmentWindow(Window): 233 class DevelopmentWindow(Window):
162 """Window for handling schematics research""" 234 """Window for handling schematics research"""
290 self.remove_child(widget) 362 self.remove_child(widget)
291 self._sciences = [] 363 self._sciences = []
292 self._make_science_widgets() 364 self._make_science_widgets()
293 365
294 def end_turn(self): 366 def end_turn(self):
367 self.game.cur_missions = self.activity.get_mission_list()
295 self.game.end_turn() 368 self.game.end_turn()
296 self.game.start_turn() 369 self.game.start_turn()
297 self.update_labels() 370 self.update_labels()
298 self.update_widgets() 371 self.update_widgets()
299 self.develop.update_widgets() 372 self.develop.update_widgets()