comparison gamelib/state.py @ 187:fbfd8e748ac0

Remove screen reference from State.
author Jeremy Thurgood <firxen@gmail.com>
date Wed, 25 Aug 2010 16:45:06 +0200
parents c6ea3b11514c
children 278774b31d3c
comparison
equal deleted inserted replaced
186:f15c4f7c24e6 187:fbfd8e748ac0
32 if self.message: 32 if self.message:
33 scene_widget.show_message(self.message) 33 scene_widget.show_message(self.message)
34 if self.detail_view: 34 if self.detail_view:
35 scene_widget.show_detail(self.detail_view) 35 scene_widget.show_detail(self.detail_view)
36 36
37 def initial_state(screen): 37 def initial_state():
38 """Load the initial state.""" 38 """Load the initial state."""
39 state = State(screen) 39 state = State()
40 state.load_scenes("cryo") 40 state.load_scenes("cryo")
41 state.load_scenes("bridge") 41 state.load_scenes("bridge")
42 state.load_scenes("mess") 42 state.load_scenes("mess")
43 state.load_scenes("engine") 43 state.load_scenes("engine")
44 state.load_scenes("machine") 44 state.load_scenes("machine")
56 56
57 * items 57 * items
58 * scenes 58 * scenes
59 """ 59 """
60 60
61 def __init__(self, screen): 61 def __init__(self):
62 # map of scene name -> Scene object 62 # map of scene name -> Scene object
63 self.scenes = {} 63 self.scenes = {}
64 # map of detail view name -> DetailView object 64 # map of detail view name -> DetailView object
65 self.detail_views = {} 65 self.detail_views = {}
66 # map of item name -> Item object 66 # map of item name -> Item object
77 self.previous_scene = None 77 self.previous_scene = None
78 # scene transion helpers 78 # scene transion helpers
79 self.do_check = None 79 self.do_check = None
80 self.old_pos = None 80 self.old_pos = None
81 81
82 self.screen = screen
83
84 def add_scene(self, scene): 82 def add_scene(self, scene):
85 self.scenes[scene.name] = scene 83 self.scenes[scene.name] = scene
86 84
87 def add_detail_view(self, detail_view): 85 def add_detail_view(self, detail_view):
88 self.detail_views[detail_view.name] = detail_view 86 self.detail_views[detail_view.name] = detail_view
134 return False 132 return False
135 return True 133 return True
136 134
137 def set_tool(self, item): 135 def set_tool(self, item):
138 self.tool = item 136 self.tool = item
139 if item is None: 137
140 self.screen.set_cursor(HAND) 138 def draw(self, surface, screen):
141 else:
142 self.screen.set_cursor(item.CURSOR)
143
144 def draw(self, surface):
145 if self.do_check and self.previous_scene and self.do_check == constants.LEAVE: 139 if self.do_check and self.previous_scene and self.do_check == constants.LEAVE:
146 # We still need to handle leave events, so still display the scene 140 # We still need to handle leave events, so still display the scene
147 self.previous_scene.draw(surface) 141 self.previous_scene.draw(surface, screen)
148 else: 142 else:
149 self.current_scene.draw(surface) 143 self.current_scene.draw(surface, screen)
150 144
151 def draw_detail(self, surface): 145 def draw_detail(self, surface, screen):
152 self.current_detail.draw(surface) 146 self.current_detail.draw(surface, screen)
153 147
154 def interact(self, pos): 148 def interact(self, pos):
155 return self.current_scene.interact(self.tool, pos) 149 return self.current_scene.interact(self.tool, pos)
156 150
157 def interact_detail(self, pos): 151 def interact_detail(self, pos):
159 153
160 def animate(self): 154 def animate(self):
161 if not self.do_check: 155 if not self.do_check:
162 return self.current_scene.animate() 156 return self.current_scene.animate()
163 157
164 def check_enter_leave(self): 158 def check_enter_leave(self, screen):
165 if not self.do_check: 159 if not self.do_check:
166 return None 160 return None
167 if self.do_check == constants.LEAVE: 161 if self.do_check == constants.LEAVE:
168 self.do_check = constants.ENTER 162 self.do_check = constants.ENTER
169 if self.previous_scene: 163 if self.previous_scene:
171 return None 165 return None
172 elif self.do_check == constants.ENTER: 166 elif self.do_check == constants.ENTER:
173 self.do_check = None 167 self.do_check = None
174 # Fix descriptions, etc. 168 # Fix descriptions, etc.
175 if self.old_pos: 169 if self.old_pos:
176 self.current_scene.mouse_move(self.tool, self.old_pos) 170 self.current_scene.mouse_move(self.tool, self.old_pos, screen)
177 return self.current_scene.enter() 171 return self.current_scene.enter()
178 raise RuntimeError('invalid do_check value %s' % self.do_check) 172 raise RuntimeError('invalid do_check value %s' % self.do_check)
179 173
180 def mouse_move(self, pos): 174 def mouse_move(self, pos, screen):
181 self.current_scene.mouse_move(self.tool, pos) 175 self.current_scene.mouse_move(self.tool, pos, screen)
182 # So we can do sensible things on enter and leave 176 # So we can do sensible things on enter and leave
183 self.old_pos = pos 177 self.old_pos = pos
184 178
185 def set_do_enter_leave(self): 179 def set_do_enter_leave(self):
186 """Flag that we need to run the enter loop""" 180 """Flag that we need to run the enter loop"""
187 self.do_check = constants.LEAVE 181 self.do_check = constants.LEAVE
188 182
189 def mouse_move_detail(self, pos): 183 def mouse_move_detail(self, pos, screen):
190 self.current_detail.mouse_move(self.tool, pos) 184 self.current_detail.mouse_move(self.tool, pos, screen)
191 185
192 186
193 class StatefulGizmo(object): 187 class StatefulGizmo(object):
194 188
195 # initial data (optional, defaults to none) 189 # initial data (optional, defaults to none)
256 label.border_color = (0, 0, 0) 250 label.border_color = (0, 0, 0)
257 label.bg_color = (127, 127, 127) 251 label.bg_color = (127, 127, 127)
258 label.fg_color = (0, 0, 0) 252 label.fg_color = (0, 0, 0)
259 return label 253 return label
260 254
261 def draw_description(self, surface): 255 def draw_description(self, surface, screen):
262 if self._current_description is not None: 256 if self._current_description is not None:
263 sub = self.state.screen.get_root().surface.subsurface( 257 sub = screen.get_root().surface.subsurface(
264 Rect(5, 5, *self._current_description.size)) 258 Rect(5, 5, *self._current_description.size))
265 self._current_description.draw_all(sub) 259 self._current_description.draw_all(sub)
266 260
267 def draw_background(self, surface): 261 def draw_background(self, surface):
268 if self._background is not None: 262 if self._background is not None:
272 266
273 def draw_things(self, surface): 267 def draw_things(self, surface):
274 for thing in self.things.itervalues(): 268 for thing in self.things.itervalues():
275 thing.draw(surface) 269 thing.draw(surface)
276 270
277 def draw(self, surface): 271 def draw(self, surface, screen):
278 self.draw_background(surface) 272 self.draw_background(surface)
279 self.draw_things(surface) 273 self.draw_things(surface)
280 self.draw_description(surface) 274 self.draw_description(surface, screen)
281 275
282 def interact(self, item, pos): 276 def interact(self, item, pos):
283 """Interact with a particular position. 277 """Interact with a particular position.
284 278
285 Item may be an item in the list of items or None for the hand. 279 Item may be an item in the list of items or None for the hand.
311 305
312 def leave(self): 306 def leave(self):
313 self._current_description = None 307 self._current_description = None
314 return None 308 return None
315 309
316 def mouse_move(self, item, pos): 310 def mouse_move(self, item, pos, screen):
317 """Call to check whether the cursor has entered / exited a thing. 311 """Call to check whether the cursor has entered / exited a thing.
318 312
319 Item may be an item in the list of items or None for the hand. 313 Item may be an item in the list of items or None for the hand.
320 """ 314 """
321 if self._current_thing is not None: 315 if self._current_thing is not None:
322 if self._current_thing.contains(pos): 316 if self._current_thing.contains(pos):
323 self.state.screen.cursor_highlight(True) 317 screen.cursor_highlight(True)
324 return 318 return
325 else: 319 else:
326 self._current_thing.leave() 320 self._current_thing.leave()
327 self._current_thing = None 321 self._current_thing = None
328 self._current_description = None 322 self._current_description = None
331 thing.enter(item) 325 thing.enter(item)
332 self._current_thing = thing 326 self._current_thing = thing
333 self._current_description = self._make_description( 327 self._current_description = self._make_description(
334 thing.get_description()) 328 thing.get_description())
335 break 329 break
336 self.state.screen.cursor_highlight(self._current_thing is not None) 330 screen.cursor_highlight(self._current_thing is not None)
337 331
338 def get_detail_size(self): 332 def get_detail_size(self):
339 return self._background.get_size() 333 return self._background.get_size()
340 334
341 335