changeset 447:f04a2490c35f

The sub-toolbar rewrite, the finally not crashing version
author Neil Muller <drnlmuller@gmail.com>
date Sat, 21 Nov 2009 19:58:49 +0000
parents 0c523d384681
children 92e7a641b4a6
files gamelib/constants.py gamelib/gameboard.py gamelib/toolbar.py
diffstat 3 files changed, 126 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/constants.py	Sat Nov 21 19:33:12 2009 +0000
+++ b/gamelib/constants.py	Sat Nov 21 19:58:49 2009 +0000
@@ -44,13 +44,16 @@
 
 # Toolbar constants
 
-TOOL_SELL_CHICKEN = 1
-TOOL_SELL_EGG = 2
-TOOL_SELL_BUILDING = 3
-TOOL_REPAIR_BUILDING = 4
-TOOL_PLACE_ANIMALS = 5
-TOOL_LOGGING = 6
-TOOL_SELL_EQUIPMENT = 7
+TOOL_SELECT_CHICKENS = 1
+TOOL_SELL_CHICKEN = 2
+TOOL_SELL_EGG = 3
+TOOL_SELL_BUILDING = 4
+TOOL_REPAIR_BUILDING = 5
+TOOL_PLACE_ANIMALS = 6
+TOOL_LOGGING = 7
+TOOL_SELL_EQUIPMENT = 8
+TOOL_BUY_BUILDING = 9
+TOOL_BUY_EQUIPMENT = 10
 
 NIGHT_LENGTH = 150
 
--- a/gamelib/gameboard.py	Sat Nov 21 19:33:12 2009 +0000
+++ b/gamelib/gameboard.py	Sat Nov 21 19:58:49 2009 +0000
@@ -84,6 +84,7 @@
         self.day, self.night = True, False
         # For the level loading case
         if self.disp:
+            self.toolbar = None
             self.create_display()
             self.add_cash(level.starting_cash)
             self.add_wood(level.starting_wood)
@@ -103,12 +104,22 @@
         width, height = self.disp.rect.w, self.disp.rect.h
         tbl = gui.Table()
         tbl.tr()
-        self.toolbar = toolbar.ToolBar(self, width=constants.TOOLBAR_WIDTH)
+        self.toolbar = toolbar.DefaultToolBar(self, width=constants.TOOLBAR_WIDTH)
         tbl.td(self.toolbar, valign=-1)
         self.tvw = VidWidget(self, self.tv, width=width-constants.TOOLBAR_WIDTH, height=height)
         tbl.td(self.tvw)
         self.top_widget = tbl
 
+    def change_toolbar(self, new_toolbar):
+        """Replace the toolbar"""
+        td = self.toolbar.container
+        td.remove(self.toolbar)
+        td.add(new_toolbar, 0, 0)
+        self.toolbar = new_toolbar
+        self.toolbar.rect.size = self.toolbar.resize(height=td.rect.height)
+        td.resize()
+        td.repaint()
+
     def update(self):
         self.tvw.reupdate()
 
--- a/gamelib/toolbar.py	Sat Nov 21 19:33:12 2009 +0000
+++ b/gamelib/toolbar.py	Sat Nov 21 19:58:49 2009 +0000
@@ -51,10 +51,10 @@
         self.repaint()
     return update_counter
 
-class ToolBar(gui.Table):
+class BaseToolBar(gui.Table):
     def __init__(self, gameboard, **params):
         gui.Table.__init__(self, **params)
-        self.group = gui.Group(name='toolbar', value=None)
+        self.group = gui.Group(name='base_toolbar', value=None)
         self._next_tool_value = 0
         self.gameboard = gameboard
         self.cash_counter = mklabel(align=1)
@@ -63,10 +63,9 @@
         self.egg_counter = mklabel(align=1)
         self.day_counter = mklabel(align=1)
         self.killed_foxes = mklabel(align=1)
+        self.add_labels()
 
-        self.make_default_toolbar()
-
-    def make_default_toolbar(self):
+    def add_labels(self):
         self.tr()
         self.td(gui.Spacer(self.rect.w/2, 0))
         self.td(gui.Spacer(self.rect.w/2, 0))
@@ -78,51 +77,6 @@
         self.add_counter(icons.KILLED_FOX, self.killed_foxes)
         self.add_spacer(5)
 
-        self.add_tool_button("Move Hen", constants.TOOL_PLACE_ANIMALS,
-                None, cursors.cursors['select'])
-        self.add_spacer(5)
-
-        self.add_heading("Sell ...")
-        self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
-                self.gameboard.level.sell_price_chicken, cursors.cursors['sell'])
-        self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
-                self.gameboard.level.sell_price_egg, cursors.cursors['sell'])
-        self.add_tool_button("Building", constants.TOOL_SELL_BUILDING,
-                None, cursors.cursors['sell'])
-        self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
-                None, cursors.cursors['sell'])
-        self.add_spacer(5)
-
-        self.add_heading("Buy ...")
-
-        for building_cls in buildings.BUILDINGS:
-            self.add_tool_button(building_cls.NAME.title(), building_cls,
-                    None, cursors.cursors.get('build', None))
-
-        for equipment_cls in equipment.EQUIPMENT:
-            self.add_tool_button(equipment_cls.NAME.title(),
-                    equipment_cls,
-                    equipment_cls.BUY_PRICE,
-                    cursors.cursors.get('buy', None))
-
-        self.add_spacer(5)
-        self.add_tool_button("Repair", constants.TOOL_REPAIR_BUILDING, None, cursors.cursors['repair'])
-
-        self.add_spacer(5)
-        self.add_tool("Price Reference", self.show_prices)
-
-        #self.add_spacer(5)
-        #self.add_tool("Save Game", self.save_game)
-        #self.add_tool("Load Game", self.load_game)
-
-        self.fin_tool = self.add_tool("Finished Day", self.day_done)
-
-    def day_done(self):
-        if self.gameboard.day:
-            pygame.event.post(engine.START_NIGHT)
-        else:
-            pygame.event.post(engine.FAST_FORWARD)
-
     def start_night(self):
         self.clear_tool()
         self._set_all_disabled(True)
@@ -276,3 +230,103 @@
         width = constants.TOOLBAR_WIDTH
         return width, height
 
+class DefaultToolBar(BaseToolBar):
+    def __init__(self, gameboard, **params):
+        BaseToolBar.__init__(self, gameboard, **params)
+        self.group = gui.Group(name='default_toolbar', value=None)
+        self.make_toolbar()
+
+    def make_toolbar(self):
+        self.add_tool_button("Select chicken", constants.TOOL_SELECT_CHICKENS,
+                None, cursors.cursors['select'])
+
+        self.add_spacer(5)
+
+        self.add_heading("Sell ...")
+        self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
+                self.gameboard.level.sell_price_chicken, cursors.cursors['sell'])
+        self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
+                self.gameboard.level.sell_price_egg, cursors.cursors['sell'])
+        self.add_tool_button("Building", constants.TOOL_SELL_BUILDING,
+                None, cursors.cursors['sell'])
+        self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
+                None, cursors.cursors['sell'])
+        self.add_spacer(5)
+
+        self.add_heading(" ")
+
+        self.add_tool('Buy building', self.add_building_toolbar)
+
+        self.add_heading("For selection, ...")
+
+        self.add_tool('Buy equipment', self.add_equipment_toolbar)
+
+        self.add_tool_button("Move selected hen", constants.TOOL_PLACE_ANIMALS,
+                None, cursors.cursors['select'])
+
+        self.add_heading(" ")
+        self.add_tool_button("Repair", constants.TOOL_REPAIR_BUILDING, None, cursors.cursors['repair'])
+
+        self.add_heading("Help")
+        self.add_tool("Price Reference", self.show_prices)
+
+        self.add_spacer(5)
+        self.add_tool("Save Game", self.save_game)
+        self.add_tool("Load Game", self.load_game)
+
+        self.add_heading(" ")
+        self.add_spacer(10)
+        self.fin_tool = self.add_tool("Finished Day", self.day_done)
+
+    def add_building_toolbar(self):
+        self.gameboard.change_toolbar(BuildingToolBar(self.gameboard,
+                width=self.style.width))
+
+    def add_equipment_toolbar(self):
+        self.gameboard.change_toolbar(EquipmentToolBar(self.gameboard,
+                width=self.style.width))
+
+    def day_done(self):
+        if self.gameboard.day:
+            pygame.event.post(engine.START_NIGHT)
+        else:
+            pygame.event.post(engine.FAST_FORWARD)
+
+class BuildingToolBar(BaseToolBar):
+    def __init__(self, gameboard, **params):
+        BaseToolBar.__init__(self, gameboard, **params)
+        self.group = gui.Group(name='building_toolbar', value=None)
+        self.make_toolbar()
+
+    def make_toolbar(self):
+        self.gameboard.set_cursor(cursors.cursors['arrow'], None)
+        for building_cls in buildings.BUILDINGS:
+            self.add_tool_button(building_cls.NAME.title(), building_cls,
+                    None, cursors.cursors.get('build', None))
+        self.add_spacer(15)
+        self.add_tool('Done', self.add_default_toolbar)
+
+    def add_default_toolbar(self):
+        self.gameboard.change_toolbar(DefaultToolBar(self.gameboard,
+                width=self.style.width))
+
+class EquipmentToolBar(BaseToolBar):
+    def __init__(self, gameboard, **params):
+        BaseToolBar.__init__(self, gameboard, **params)
+        self.group = gui.Group(name='building_toolbar', value=None)
+        self.make_toolbar()
+
+    def make_toolbar(self):
+        self.gameboard.set_cursor(cursors.cursors['arrow'], None)
+        for equipment_cls in equipment.EQUIPMENT:
+            self.add_tool_button(equipment_cls.NAME.title(),
+                    equipment_cls,
+                    equipment_cls.BUY_PRICE,
+                    cursors.cursors.get('buy', None))
+        self.add_spacer(15)
+        self.add_tool('Done', self.add_default_toolbar)
+
+    def add_default_toolbar(self):
+        self.gameboard.change_toolbar(DefaultToolBar(self.gameboard,
+                width=self.style.width))
+