comparison gamelib/state.py @ 319:fd849354be58

Mouse handling refactorings.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 28 Aug 2010 12:43:12 +0200
parents 63e702d93e0e
children 3476e8f3b100
comparison
equal deleted inserted replaced
318:9b96bc740209 319:fd849354be58
119 def set_current_detail(self, name): 119 def set_current_detail(self, name):
120 if name is None: 120 if name is None:
121 self.current_detail = None 121 self.current_detail = None
122 else: 122 else:
123 self.current_detail = self.detail_views[name] 123 self.current_detail = self.detail_views[name]
124 self.current_scene._current_description = None
125 self.current_scene._current_thing = None 124 self.current_scene._current_thing = None
126 return self.current_detail.get_detail_size() 125 return self.current_detail.get_detail_size()
127 126
128 def add_inventory_item(self, name): 127 def add_inventory_item(self, name):
129 self.inventory.append(self.items[name]) 128 self.inventory.append(self.items[name])
251 if self.BACKGROUND is not None: 250 if self.BACKGROUND is not None:
252 self._background = get_image(self.FOLDER, self.BACKGROUND) 251 self._background = get_image(self.FOLDER, self.BACKGROUND)
253 else: 252 else:
254 self._background = None 253 self._background = None
255 self._current_thing = None 254 self._current_thing = None
256 self._current_description = None
257 255
258 def add_item(self, item): 256 def add_item(self, item):
259 self.state.add_item(item) 257 self.state.add_item(item)
260 258
261 def add_thing(self, thing): 259 def add_thing(self, thing):
264 262
265 def remove_thing(self, thing): 263 def remove_thing(self, thing):
266 del self.things[thing.name] 264 del self.things[thing.name]
267 self.leave() 265 self.leave()
268 266
269 def _make_description(self, text): 267 def _get_description(self):
268 text = self._current_thing and self._current_thing.get_description()
270 if text is None: 269 if text is None:
271 return None 270 return None
272 label = BoomLabel(text) 271 label = BoomLabel(text)
273 label.set_margin(5) 272 label.set_margin(5)
274 label.border_width = 1 273 label.border_width = 1
276 label.bg_color = Color(127, 127, 127, 255) 275 label.bg_color = Color(127, 127, 127, 255)
277 label.fg_color = (0, 0, 0) 276 label.fg_color = (0, 0, 0)
278 return label 277 return label
279 278
280 def draw_description(self, surface, screen): 279 def draw_description(self, surface, screen):
281 if self._current_description is not None: 280 description = self._get_description()
281 if description is not None:
282 sub = screen.get_root().surface.subsurface( 282 sub = screen.get_root().surface.subsurface(
283 Rect(5, 5, *self._current_description.size)) 283 Rect(5, 5, *description.size))
284 self._current_description.draw_all(sub) 284 description.draw_all(sub)
285 285
286 def draw_background(self, surface): 286 def draw_background(self, surface):
287 if self._background is not None: 287 if self._background is not None:
288 surface.blit(self._background, self.OFFSET, None) 288 surface.blit(self._background, self.OFFSET, None)
289 else: 289 else:
303 303
304 Item may be an item in the list of items or None for the hand. 304 Item may be an item in the list of items or None for the hand.
305 305
306 Returns a Result object to provide feedback to the player. 306 Returns a Result object to provide feedback to the player.
307 """ 307 """
308 for thing in self.things.itervalues(): 308 if self._current_thing is not None:
309 if thing.contains(pos): 309 return self._current_thing.interact(item)
310 result = thing.interact(item)
311 if result:
312 if self._current_thing:
313 # Also update descriptions if needed
314 self._current_description = self._make_description(
315 self._current_thing.get_description())
316 return result
317 310
318 def animate(self): 311 def animate(self):
319 """Animate all the things in the scene. 312 """Animate all the things in the scene.
320 313
321 Return true if any of them need to queue a redraw""" 314 Return true if any of them need to queue a redraw"""
327 320
328 def enter(self): 321 def enter(self):
329 return None 322 return None
330 323
331 def leave(self): 324 def leave(self):
332 self._current_description = None
333 return None 325 return None
334 326
335 def mouse_move(self, item, pos, screen): 327 def mouse_move(self, item, pos, screen):
336 """Call to check whether the cursor has entered / exited a thing. 328 """Call to check whether the cursor has entered / exited a thing.
337 329
342 screen.cursor_highlight(self._current_thing.is_interactive()) 334 screen.cursor_highlight(self._current_thing.is_interactive())
343 return 335 return
344 else: 336 else:
345 self._current_thing.leave() 337 self._current_thing.leave()
346 self._current_thing = None 338 self._current_thing = None
347 self._current_description = None
348 for thing in self.things.itervalues(): 339 for thing in self.things.itervalues():
349 if thing.contains(pos): 340 if thing.contains(pos):
350 thing.enter(item) 341 thing.enter(item)
351 self._current_thing = thing 342 self._current_thing = thing
352 self._current_description = self._make_description(
353 thing.get_description())
354 break 343 break
355 screen.cursor_highlight(self._current_thing is not None) 344 screen.cursor_highlight(self._current_thing is not None)
356 345
357 def get_detail_size(self): 346 def get_detail_size(self):
358 return self._background.get_size() 347 return self._background.get_size()