Scenes get a Container for widgets
authorStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 19:00:58 +0000 (21:00 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 19:00:58 +0000 (21:00 +0200)
naja/scenes/credits.py
naja/scenes/game.py
naja/scenes/menu.py
naja/scenes/scene.py
naja/widgets/base.py

index a1c7d8f4ff3aefeb3b6fa4106b49a34e41e1e5e2..747dd01c6bda99da6833fb0f459a45f5be6542fe 100644 (file)
@@ -15,14 +15,11 @@ class CreditsScene(Scene):
 
     def __init__(self, state):
         super(CreditsScene, self).__init__(state)
-        self.widgets.append(TextWidget((60, 10), 'Credits', fontsize=32,
-                            colour='white'))
-        self.widgets.append(TextWidget((60, 30),
-                                       'Your mom\n'
-                                       'A stranger', fontsize=32,
-                            colour='white'))
+        self.add(TextWidget((60, 10), 'Credits', fontsize=32, colour='white'))
+        self.add(TextWidget((60, 30), 'Your mom\nA stranger', fontsize=32,
+                 colour='white'))
 
-    def handle_event(self, ev):
+    def handle_scene_event(self, ev):
         if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
             from naja.scenes.menu import MenuScene
             SceneChangeEvent.post(MenuScene)
index 9d8b29046a5c8b7ee81f79b5702a5b994973519c..32285cd4dedbb02f0538478b1cbda34e06d4cc0a 100644 (file)
@@ -19,12 +19,12 @@ class GameScene(Scene):
 
     def __init__(self, state):
         super(GameScene, self).__init__(state)
-        self.widgets.append(PlayerBitsWidget((0, 0)))
-        self.widgets.append(BoardWidget((0, 60)))
-        self.widgets.append(GameBitsWidget((0, 540)))
-        self.widgets.append(InfoAreaWidget((480, 0)))
+        self.add(PlayerBitsWidget((0, 0)))
+        self.add(BoardWidget((0, 60)))
+        self.add(GameBitsWidget((0, 540)))
+        self.add(InfoAreaWidget((480, 0)))
 
-    def handle_event(self, ev):
+    def handle_scene_event(self, ev):
         from naja.scenes.menu import MenuScene
         if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
             SceneChangeEvent.post(MenuScene)
index a73c2ecb498ff94d5d76a2e9bd7d06a08ff3fa82..46b2b3b13eab19a19e18ea17d8a9f8ea4c1b2998 100644 (file)
@@ -16,15 +16,12 @@ class MenuScene(Scene):
 
     def __init__(self, state):
         super(MenuScene, self).__init__(state)
-        self.widgets.append(TextWidget((10, 10), 'Game', fontsize=32,
-                            colour='white'))
-        self.widgets.append(TextWidget((10, 40), 'Credits', fontsize=32,
-                            colour='white'))
-        self.widgets.append(TextWidget((10, 70), 'Quit', fontsize=32,
-                            colour='white'))
+        self.add(TextWidget((10, 10), 'Game', fontsize=32, colour='white'))
+        self.add(TextWidget((10, 40), 'Credits', fontsize=32, colour='white'))
+        self.add(TextWidget((10, 70), 'Quit', fontsize=32, colour='white'))
         self.pos = 0
 
-    def handle_event(self, ev):
+    def handle_scene_event(self, ev):
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
                 QuitGameEvent.post()
index ce0b68488eb1a0dc6369fa37c3dd4902cdb8d3e8..4841ab5a628cb3a318ab0c947d1d537f9f0ee03b 100644 (file)
@@ -2,6 +2,8 @@
 Base Scene class.
 """
 
+from naja.widgets.base import Container
+
 
 class Scene(object):
     """
@@ -11,7 +13,10 @@ class Scene(object):
     """
     def __init__(self, state):
         self.state = state
-        self.widgets = []
+        self.container = Container()
+
+    def add(self, widget):
+        self.container.add(widget)
 
     def enter(self):
         pass
@@ -19,12 +24,17 @@ class Scene(object):
     def exit(self):
         pass
 
-    def render(self, surface):
-        self.render_widgets(surface)
+    def render_scene(self, surface):
+        pass
 
-    def render_widgets(self, surface):
-        for widget in self.widgets:
-            widget.render(surface)
+    def render(self, surface):
+        self.render_scene(surface)
+        self.container.render(surface)
 
     def handle_event(self, ev):
+        if self.container.handle_event(ev):
+            return
+        self.handle_scene_event(ev)
+
+    def handle_scene_event(ev):
         pass
index 32ff46fa21811e3b99b61d84e6fbac8325cc2433..6b86a870f5adadbb518ed4fc4caae837ccce7650 100644 (file)
@@ -22,3 +22,26 @@ class Widget(object):
 
     def prepare(self):
         raise NotImplemented()
+
+    def handle_event(self, ev):
+        return False
+
+
+class Container(object):
+    def __init__(self, *widgets):
+        self.widgets = []
+        for widget in widgets:
+            self.add(widget)
+
+    def add(self, widget):
+        self.widgets.append(widget)
+
+    def render(self, surface):
+        for widget in self.widgets:
+            widget.render(surface)
+
+    def handle_event(self, ev):
+        for widget in self.widgets:
+            if widget.handle_event(ev):
+                return True
+        return False