Callbacks for widget events
authorStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 20:44:25 +0000 (22:44 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 20:45:44 +0000 (22:45 +0200)
naja/scenes/menu.py
naja/widgets/base.py

index 46b2b3b13eab19a19e18ea17d8a9f8ea4c1b2998..317e50f4886949601f2512c2bc78080a40c916f5 100644 (file)
@@ -13,29 +13,22 @@ from naja.events import SceneChangeEvent
 
 
 class MenuScene(Scene):
-
     def __init__(self, state):
         super(MenuScene, self).__init__(state)
-        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
+        run_game = TextWidget((100, 100), 'Game', fontsize=32, colour='white')
+        run_game.add_callback('click',
+                              lambda event: SceneChangeEvent.post(GameScene))
+        self.add(run_game)
+        credits = TextWidget((100, 200), 'Credits', fontsize=32, colour='white')
+        credits.add_callback('click',
+                             lambda event: SceneChangeEvent.post(CreditsScene))
+        self.add(credits)
+        quit = TextWidget((100, 300), 'Quit', fontsize=32, colour='white')
+        quit.add_callback('click', lambda event: QuitGameEvent.post())
+        self.add(quit)
 
     def handle_scene_event(self, ev):
         if ev.type == pgl.KEYDOWN:
             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
                 QuitGameEvent.post()
                 return
-            if ev.key == pgl.K_DOWN:
-                self.pos = (self.pos + 1) % 3
-            elif ev.key == pgl.K_UP:
-                self.pos = (self.pos - 1) % 3
-            elif ev.key == pgl.K_RETURN:
-                if self.pos == 2:
-                    QuitGameEvent.post()
-                    return
-                elif self.pos == 0:
-                    scene = GameScene
-                elif self.pos == 1:
-                    scene = CreditsScene
-                SceneChangeEvent.post(scene)
index 133cc532c2ae8fb0886117255decbba65f1e374c..81f11b649df9734371017f34891fa3ec6b360505 100644 (file)
@@ -1,4 +1,8 @@
+from collections import defaultdict
+
 import pygame
+from pygame import locals as pgl
+
 from naja.events import InvalidateTheWorld
 
 
@@ -7,6 +11,7 @@ class Widget(object):
         self.pos = pos
         self.size = size or (0, 0)
         self._prepared = False
+        self.callbacks = defaultdict(list)
 
     @property
     def rect(self):
@@ -33,8 +38,17 @@ class Widget(object):
             # True for for this event
             self._prepared = False
             return False
+        if ev.type == pgl.MOUSEBUTTONDOWN:
+            self.callback('click')
         return False
 
+    def add_callback(self, event, callback):
+        self.callbacks[event].append(callback)
+
+    def callback(self, event):
+        for callback in self.callbacks[event]:
+            callback(event)
+
 
 class Container(object):
     def __init__(self, *widgets):
@@ -50,7 +64,16 @@ class Container(object):
             widget.render(surface)
 
     def handle_event(self, ev):
-        for widget in self.widgets:
-            if widget.handle_event(ev):
-                return True
+        if hasattr(ev, 'pos'):
+            for widget in self.widgets:
+                if isinstance(widget, Container):
+                    if widget.handle_event(ev):
+                        return True
+                elif widget.rect.collidepoint(ev.pos):
+                    if widget.handle_event(ev):
+                        return True
+        else:
+            for widget in self.widgets:
+                if widget.handle_event(ev):
+                    return True
         return False