comparison gamelib/gameboard.py @ 208:f82d17f99882

Fix bugs in clicking around inside buildings while moving chickens.
author Simon Cross <hodgestar@gmail.com>
date Fri, 04 Sep 2009 21:12:16 +0000
parents 653da96db572
children b999abd5993b
comparison
equal deleted inserted replaced
207:3e1c02d51d68 208:f82d17f99882
438 self.disp.open(tbl) 438 self.disp.open(tbl)
439 return tbl 439 return tbl
440 440
441 def open_building_dialog(self, building, sell_callback=None): 441 def open_building_dialog(self, building, sell_callback=None):
442 """Create dialog for manipulating the contents of a building.""" 442 """Create dialog for manipulating the contents of a building."""
443 def select_occupant(place, button, sell_callback): 443
444 """Select occupant in place.""" 444 place_button_map = {}
445
446 def update_button(animal, empty=False):
447 """Update a button image (either to the animal, or to empty)."""
448 if animal:
449 button = place_button_map.get(id(animal.abode))
450 if button:
451 if empty:
452 button.value = icons.EMPTY_NEST_ICON
453 else:
454 button.value = icons.animal_icon(animal)
455
456 def nest_clicked(place, button):
457 """Handle a nest being clicked."""
445 # sell_callback should return true if we need to remove the 458 # sell_callback should return true if we need to remove the
446 # occupant 459 # occupant
447 self.select_animal_to_place(place.occupant) 460 if place.occupant:
448 if not sell_callback: 461 # there is an occupant, select or sell it
449 pygame.mouse.set_cursor(*cursors.cursors['chicken']) 462 if not sell_callback:
463 old_animal = self.animal_to_place
464 self.select_animal_to_place(place.occupant)
465 # deselect old animal (on button)
466 update_button(old_animal)
467 # select new animal (on button)
468 update_button(self.animal_to_place)
469 else:
470 # Attempt to sell the occupant
471 if sell_callback(place.occupant):
472 # empty the nest (on button)
473 update_button(place.occupant, empty=True)
450 else: 474 else:
451 # Attempt to sell the occupant 475 # there is no occupant, attempt to fill the space
452 self.select_animal_to_place(None) 476 if self.animal_to_place is not None:
453 if sell_callback(place.occupant): 477 # empty old nest (on button)
454 button.value = icons.EMPTY_NEST_ICON 478 update_button(self.animal_to_place, empty=True)
455 button.disconnect(gui.CLICK, select_occupant) 479 self.relocate_animal(self.animal_to_place, place=place)
456 button.connect(gui.CLICK, set_occupant, place, button, 480 # populate the new nest (on button)
457 sell_callback) 481 update_button(self.animal_to_place)
458
459 def set_occupant(place, button, sell_callback):
460 """Set occupant of a given place."""
461 if self.animal_to_place is not None:
462 button.value = icons.animal_icon(self.animal_to_place)
463 button.disconnect(gui.CLICK, set_occupant)
464 button.connect(gui.CLICK, select_occupant, place, button,
465 sell_callback)
466
467 old_abode = self.animal_to_place.abode
468 if id(old_abode) in place_button_map:
469 old_button = place_button_map[id(old_abode)]
470 old_button.value = icons.EMPTY_NEST_ICON
471 old_button.disconnect(gui.CLICK, select_occupant)
472 old_button.connect(gui.CLICK, set_occupant, place, button,
473 sell_callback)
474
475 self.relocate_animal(self.animal_to_place, place=place)
476
477 place_button_map = {}
478 482
479 tbl = gui.Table() 483 tbl = gui.Table()
480 columns = building.max_floor_width() 484 columns = building.max_floor_width()
481 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }} 485 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }}
482 for floor in building.floors(): 486 for floor in building.floors():
486 for row in floor.rows(): 490 for row in floor.rows():
487 tbl.tr() 491 tbl.tr()
488 for place in row: 492 for place in row:
489 if place.occupant is None: 493 if place.occupant is None:
490 button = gui.Button(icons.EMPTY_NEST_ICON) 494 button = gui.Button(icons.EMPTY_NEST_ICON)
491 button.connect(gui.CLICK, set_occupant, place, button,
492 sell_callback)
493 else: 495 else:
494 button = gui.Button(icons.animal_icon(place.occupant)) 496 button = gui.Button(icons.animal_icon(place.occupant))
495 button.connect(gui.CLICK, select_occupant, place, button,
496 sell_callback)
497 place_button_map[id(place)] = button 497 place_button_map[id(place)] = button
498 button.connect(gui.CLICK, nest_clicked, place, button)
498 tbl.td(button, **kwargs) 499 tbl.td(button, **kwargs)
499 500
500 building.selected(True) 501 building.selected(True)
501 def close_callback(): 502 def close_callback():
502 building.selected(False) 503 building.selected(False)