Add name to buttons to simplify logic
authorNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 12:07:29 +0000 (14:07 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 12:07:29 +0000 (14:07 +0200)
tabakrolletjie/scenes/day.py
tabakrolletjie/scenes/menu.py
tabakrolletjie/widgets.py

index 184c301c8de9eec6cf5e2631ce221dfe40188c13..7702fc78c07da51057845a487cbbaeffc8a4d4ab 100644 (file)
@@ -22,7 +22,8 @@ class DayScene(BaseScene):
         self._lights = LightManager(self._space, gamestate)
         # Toolbar
         self._tools = [
-            ImageButton('32', 'seed.png', pos=(50, SCREEN_SIZE[1] - 40)),
+            ImageButton('32', 'seed.png', name='seed',
+                        pos=(50, SCREEN_SIZE[1] - 40)),
         ]
 
     @debug_timer("day.render")
@@ -44,7 +45,7 @@ class DayScene(BaseScene):
                 # Check tools
                 for tool in self._tools:
                     if tool.pressed(ev):
-                        print 'tool'
+                        print 'tool', tool.name
                         return
                 # Not tool, so check lights
                 self._lights.toggle_nearest(ev.pos, surfpos=True)
index e87005491f9fd79867b1fbc30b5c9242f298a08c..1d14abcd70d80f71fc445a2f280c5f5fbce8cf7d 100644 (file)
@@ -17,11 +17,11 @@ class MenuScene(BaseScene):
         font_title = loader.load_font(FONTS['bold'], size=32)
         self._title = font_title.render('A Game with a title', True,
                                         (255, 255, 255))
-        self._menu = {
-            'load level': TextButton("Load Level", (255, 255, 255)),
-            'saved game': TextButton("Load Saved Game", (255, 255, 255)),
-            'start game': TextButton("Start Game (Day)", (255, 255, 255)),
-        }
+        self._menu = [
+            TextButton("Load Level", (255, 255, 255), name='load level'),
+            TextButton("Start Game (Day)", (255, 255, 255), name='start game'),
+            TextButton("Load Saved Game", (255, 255, 255), name='load game'),
+        ]
 
     def render(self, surface, gamestate):
         surface.fill((0, 128, 0))
@@ -30,8 +30,7 @@ class MenuScene(BaseScene):
         surface.blit(self._title, pos, None)
 
         height = 150
-        for label in sorted(self._menu):
-            item = self._menu[label]
+        for item in self._menu:
             if not item.position:
                 item.position = ((surface.get_width() - item.get_width()) / 2,
                                  height)
@@ -39,9 +38,9 @@ class MenuScene(BaseScene):
             height += item.get_height() + 30
 
     def _get_pressed(self, ev):
-        for label, button in self._menu.items():
+        for button in self._menu:
             if button.pressed(ev):
-                return label
+                return button.name
         return None
 
     def _do_day(self):
index 3f37b52919c15cc28d1fd37e4c5327732c8fab2b..f34fa35fccc0427477107fae9677b4be91d1cea0 100644 (file)
@@ -10,10 +10,11 @@ from .constants import FONTS
 
 class Button(object):
 
-    def __init__(self, size, pos=None, padding=10):
+    def __init__(self, size, name=None, pos=None, padding=10):
         self._size = size
         self._padding = padding
         self.position = pos
+        self.name = name
 
     @property
     def position(self):
@@ -50,10 +51,11 @@ class Button(object):
 
 class TextButton(Button):
 
-    def __init__(self, text, colour, pos=None, padding=10):
+    def __init__(self, text, colour, name=None, pos=None, padding=10):
         font = loader.load_font(FONTS['sans'], size=24)
         self._text = font.render(text, True, colour)
-        super(TextButton, self).__init__(self._text.get_size(), pos, padding)
+        super(TextButton, self).__init__(self._text.get_size(), name,
+                                         pos, padding)
 
     def render(self, surface):
         surface.blit(self._text, self._pos, None)
@@ -63,9 +65,11 @@ class ImageButton(Button):
 
     def __init__(self, *imgparts, **kwargs):
         self._img = loader.load_image(*imgparts)
+        name = kwargs.get('name')
         pos = kwargs.get('pos')
         padding = kwargs.get('padding', 0)
-        super(ImageButton, self).__init__(self._img.get_size(), pos, padding)
+        super(ImageButton, self).__init__(self._img.get_size(), name,
+                                          pos, padding)
 
     def render(self, surface):
         surface.blit(self._img, self._pos, None)