widgets start arriving
authorNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 10:40:24 +0000 (12:40 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 10:40:24 +0000 (12:40 +0200)
tabakrolletjie/scenes/menu.py
tabakrolletjie/widgets.py [new file with mode: 0644]

index 0492ae9e38c42083ef506d0f42d8b6e5dbcdafd8..e87005491f9fd79867b1fbc30b5c9242f298a08c 100644 (file)
@@ -6,6 +6,7 @@ from .base import BaseScene
 from ..events import QuitEvent, SceneChangeEvent
 from ..loader import loader
 from ..constants import FONTS
+from ..widgets import TextButton
 
 
 class MenuScene(BaseScene):
@@ -16,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))
-        font_menu = loader.load_font(FONTS['sans'], size=24)
-        self._menu = [
-            font_menu.render("Load Level", True, (255, 255, 255)),
-            font_menu.render("Load Saved Game", 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)),
+        }
 
     def render(self, surface, gamestate):
         surface.fill((0, 128, 0))
@@ -29,10 +30,23 @@ class MenuScene(BaseScene):
         surface.blit(self._title, pos, None)
 
         height = 150
-        for item in self._menu:
-            pos = ((surface.get_width() - item.get_width()) / 2, height)
-            surface.blit(item, pos, None)
-            height += 50
+        for label in sorted(self._menu):
+            item = self._menu[label]
+            if not item.position:
+                item.position = ((surface.get_width() - item.get_width()) / 2,
+                                 height)
+            item.render(surface)
+            height += item.get_height() + 30
+
+    def _get_pressed(self, ev):
+        for label, button in self._menu.items():
+            if button.pressed(ev):
+                return label
+        return None
+
+    def _do_day(self):
+        from .day import DayScene
+        SceneChangeEvent.post(scene=DayScene())
 
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
@@ -42,8 +56,16 @@ class MenuScene(BaseScene):
                 from .night import NightScene
                 SceneChangeEvent.post(scene=NightScene())
             elif ev.key == pgl.K_d:
-                from .day import DayScene
-                SceneChangeEvent.post(scene=DayScene())
+                self._do_day()
             elif ev.key == pgl.K_l:
                 print "Loading Station Alpha ..."
                 gamestate.load_station("station-alpha.json")
+        elif ev.type == pgl.MOUSEBUTTONDOWN:
+            pressed = self._get_pressed(ev)
+            if pressed == 'load level':
+                print "Loading Station Alpha ..."
+                gamestate.load_station("station-alpha.json")
+            elif pressed == 'start game':
+                self._do_day()
+            elif pressed:
+                print 'Pressed', pressed
diff --git a/tabakrolletjie/widgets.py b/tabakrolletjie/widgets.py
new file mode 100644 (file)
index 0000000..ff854de
--- /dev/null
@@ -0,0 +1,59 @@
+# Simple widgets
+# This just unifies some logic.
+# There is no implied container / window system (yet)
+
+import pygame.locals as pgl
+
+from .loader import loader
+from .constants import FONTS
+
+
+class Button(object):
+
+    def __init__(self, size, pos=None, padding=10):
+        self._size = size
+        self._padding = padding
+        self.position = pos
+
+    @property
+    def position(self):
+        return self._pos
+
+    @position.setter
+    def position(self, pos):
+        self._pos = pos
+        if pos is not None:
+            self._min_x = pos[0] - self._padding
+            self._max_x = pos[0] + self._size[0] + self._padding
+            self._min_y = pos[1] - self._padding
+            self._max_y = pos[1] + self._size[1] + self._padding
+
+    def get_width(self):
+        return self._size[0]
+
+    def get_height(self):
+        return self._size[1]
+
+    def render(self, surface):
+        pass
+
+    def pressed(self, ev):
+        if self._pos is None:
+            # Unplaced buttons can't be pressed
+            return False
+        if ev.type == pgl.MOUSEBUTTONDOWN and ev.button == 1:
+            if self._min_x < ev.pos[0] < self._max_x:
+                if self._min_y < ev.pos[1] < self._max_y:
+                    return True
+        return False
+
+
+class TextButton(Button):
+
+    def __init__(self, text, colour, 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)
+
+    def render(self, surface):
+        surface.blit(self._text, self._pos, None)