comparison gamelib/state.py @ 324:3476e8f3b100

Fixed mouse and cursor handling.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 28 Aug 2010 13:30:44 +0200
parents fd849354be58
children bd649aa51472
comparison
equal deleted inserted replaced
323:0630a37cb371 324:3476e8f3b100
89 # scene we came from, for enter and leave processing 89 # scene we came from, for enter and leave processing
90 self.previous_scene = None 90 self.previous_scene = None
91 # scene transion helpers 91 # scene transion helpers
92 self.do_check = None 92 self.do_check = None
93 self.old_pos = None 93 self.old_pos = None
94 # current thing
95 self.current_thing = None
96 self.highlight_override = False
94 97
95 def add_scene(self, scene): 98 def add_scene(self, scene):
96 self.scenes[scene.name] = scene 99 self.scenes[scene.name] = scene
97 100
98 def add_detail_view(self, detail_view): 101 def add_detail_view(self, detail_view):
110 self.add_detail_view(scene_cls(self)) 113 self.add_detail_view(scene_cls(self))
111 114
112 def set_current_scene(self, name): 115 def set_current_scene(self, name):
113 old_scene = self.current_scene 116 old_scene = self.current_scene
114 self.current_scene = self.scenes[name] 117 self.current_scene = self.scenes[name]
118 self.current_thing = None
115 if old_scene and old_scene != self.current_scene: 119 if old_scene and old_scene != self.current_scene:
116 self.previous_scene = old_scene 120 self.previous_scene = old_scene
117 self.set_do_enter_leave() 121 self.set_do_enter_leave()
118 122
119 def set_current_detail(self, name): 123 def set_current_detail(self, name):
124 self.current_thing = None
120 if name is None: 125 if name is None:
121 self.current_detail = None 126 self.current_detail = None
122 else: 127 else:
123 self.current_detail = self.detail_views[name] 128 self.current_detail = self.detail_views[name]
124 self.current_scene._current_thing = None
125 return self.current_detail.get_detail_size() 129 return self.current_detail.get_detail_size()
126 130
127 def add_inventory_item(self, name): 131 def add_inventory_item(self, name):
128 self.inventory.append(self.items[name]) 132 self.inventory.append(self.items[name])
129 133
185 return None 189 return None
186 elif self.do_check == constants.ENTER: 190 elif self.do_check == constants.ENTER:
187 self.do_check = None 191 self.do_check = None
188 # Fix descriptions, etc. 192 # Fix descriptions, etc.
189 if self.old_pos: 193 if self.old_pos:
190 self.current_scene.mouse_move(self.tool, self.old_pos, screen) 194 self.current_scene.update_current_thing(self.old_pos)
191 return self.current_scene.enter() 195 return self.current_scene.enter()
192 raise RuntimeError('invalid do_check value %s' % self.do_check) 196 raise RuntimeError('invalid do_check value %s' % self.do_check)
193 197
194 def mouse_move(self, pos, screen): 198 def mouse_move(self, pos):
195 self.current_scene.mouse_move(self.tool, pos, screen) 199 self.current_scene.mouse_move(pos)
196 # So we can do sensible things on enter and leave 200 # So we can do sensible things on enter and leave
197 self.old_pos = pos 201 self.old_pos = pos
198 202
199 def set_do_enter_leave(self): 203 def set_do_enter_leave(self):
200 """Flag that we need to run the enter loop""" 204 """Flag that we need to run the enter loop"""
201 self.do_check = constants.LEAVE 205 self.do_check = constants.LEAVE
202 206
203 def mouse_move_detail(self, pos, screen): 207 def mouse_move_detail(self, pos):
204 self.current_detail.mouse_move(self.tool, pos, screen) 208 self.current_detail.mouse_move(pos)
205 209
206 210
207 class StatefulGizmo(object): 211 class StatefulGizmo(object):
208 212
209 # initial data (optional, defaults to none) 213 # initial data (optional, defaults to none)
249 self.things = {} 253 self.things = {}
250 if self.BACKGROUND is not None: 254 if self.BACKGROUND is not None:
251 self._background = get_image(self.FOLDER, self.BACKGROUND) 255 self._background = get_image(self.FOLDER, self.BACKGROUND)
252 else: 256 else:
253 self._background = None 257 self._background = None
254 self._current_thing = None
255 258
256 def add_item(self, item): 259 def add_item(self, item):
257 self.state.add_item(item) 260 self.state.add_item(item)
258 261
259 def add_thing(self, thing): 262 def add_thing(self, thing):
263 def remove_thing(self, thing): 266 def remove_thing(self, thing):
264 del self.things[thing.name] 267 del self.things[thing.name]
265 self.leave() 268 self.leave()
266 269
267 def _get_description(self): 270 def _get_description(self):
268 text = self._current_thing and self._current_thing.get_description() 271 text = (self.state.current_thing and
272 self.state.current_thing.get_description())
269 if text is None: 273 if text is None:
270 return None 274 return None
271 label = BoomLabel(text) 275 label = BoomLabel(text)
272 label.set_margin(5) 276 label.set_margin(5)
273 label.border_width = 1 277 label.border_width = 1
303 307
304 Item may be an item in the list of items or None for the hand. 308 Item may be an item in the list of items or None for the hand.
305 309
306 Returns a Result object to provide feedback to the player. 310 Returns a Result object to provide feedback to the player.
307 """ 311 """
308 if self._current_thing is not None: 312 if self.state.current_thing is not None:
309 return self._current_thing.interact(item) 313 return self.state.current_thing.interact(item)
310 314
311 def animate(self): 315 def animate(self):
312 """Animate all the things in the scene. 316 """Animate all the things in the scene.
313 317
314 Return true if any of them need to queue a redraw""" 318 Return true if any of them need to queue a redraw"""
322 return None 326 return None
323 327
324 def leave(self): 328 def leave(self):
325 return None 329 return None
326 330
327 def mouse_move(self, item, pos, screen): 331 def update_current_thing(self, pos):
332 if self.state.current_thing is not None:
333 if not self.state.current_thing.contains(pos):
334 self.state.current_thing.leave()
335 self.state.current_thing = None
336 for thing in self.things.itervalues():
337 if thing.contains(pos):
338 thing.enter(self.state.tool)
339 self.state.current_thing = thing
340 break
341
342 def mouse_move(self, pos):
328 """Call to check whether the cursor has entered / exited a thing. 343 """Call to check whether the cursor has entered / exited a thing.
329 344
330 Item may be an item in the list of items or None for the hand. 345 Item may be an item in the list of items or None for the hand.
331 """ 346 """
332 if self._current_thing is not None: 347 self.update_current_thing(pos)
333 if self._current_thing.contains(pos):
334 screen.cursor_highlight(self._current_thing.is_interactive())
335 return
336 else:
337 self._current_thing.leave()
338 self._current_thing = None
339 for thing in self.things.itervalues():
340 if thing.contains(pos):
341 thing.enter(item)
342 self._current_thing = thing
343 break
344 screen.cursor_highlight(self._current_thing is not None)
345 348
346 def get_detail_size(self): 349 def get_detail_size(self):
347 return self._background.get_size() 350 return self._background.get_size()
348 351
349 352