Changeset 208:f82d17f99882


Ignore:
Timestamp:
Sep 4, 2009, 9:12:16 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@209
Message:

Fix bugs in clicking around inside buildings while moving chickens.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/gameboard.py

    r203 r208  
    441441    def open_building_dialog(self, building, sell_callback=None):
    442442        """Create dialog for manipulating the contents of a building."""
    443         def select_occupant(place, button, sell_callback):
    444             """Select occupant in place."""
     443
     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."""
    445458            # sell_callback should return true if we need to remove the
    446459            # occupant
    447             self.select_animal_to_place(place.occupant)
    448             if not sell_callback:
    449                 pygame.mouse.set_cursor(*cursors.cursors['chicken'])
     460            if place.occupant:
     461                # there is an occupant, select or sell it
     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)
    450474            else:
    451                 # Attempt to sell the occupant
    452                 self.select_animal_to_place(None)
    453                 if sell_callback(place.occupant):
    454                     button.value = icons.EMPTY_NEST_ICON
    455                     button.disconnect(gui.CLICK, select_occupant)
    456                     button.connect(gui.CLICK, set_occupant, place, button,
    457                             sell_callback)
    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 = {}
     475                # there is no occupant, attempt to fill the space
     476                if self.animal_to_place is not None:
     477                    # empty old nest (on button)
     478                    update_button(self.animal_to_place, empty=True)
     479                    self.relocate_animal(self.animal_to_place, place=place)
     480                    # populate the new nest (on button)
     481                    update_button(self.animal_to_place)
    478482
    479483        tbl = gui.Table()
     
    489493                    if place.occupant is None:
    490494                        button = gui.Button(icons.EMPTY_NEST_ICON)
    491                         button.connect(gui.CLICK, set_occupant, place, button,
    492                                 sell_callback)
    493495                    else:
    494496                        button = gui.Button(icons.animal_icon(place.occupant))
    495                         button.connect(gui.CLICK, select_occupant, place, button,
    496                                 sell_callback)
    497497                    place_button_map[id(place)] = button
     498                    button.connect(gui.CLICK, nest_clicked, place, button)
    498499                    tbl.td(button, **kwargs)
    499500
Note: See TracChangeset for help on using the changeset viewer.