SelectorWidget
authorStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 20:45:31 +0000 (22:45 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 20:45:44 +0000 (22:45 +0200)
naja/events.py
naja/scenes/menu.py
naja/widgets/base.py
naja/widgets/selector.py [new file with mode: 0644]

index 0e102edf9229d0de4fbdc327514e0b08046144c8..e34f74043b31209fc690eb95f357860d3fbfecf6 100644 (file)
@@ -45,3 +45,11 @@ class InvalidateTheWorld(NajaEvent):
     @classmethod
     def post(cls):
         super(InvalidateTheWorld, cls).post()
+
+
+class SelectEvent(NajaEvent):
+    TYPE = "SELECT"
+
+    @classmethod
+    def post(cls):
+        super(SelectEvent, cls).post()
index 317e50f4886949601f2512c2bc78080a40c916f5..6b4152df59401eabd0d34cd31e255a81d4643dfc 100644 (file)
@@ -6,6 +6,7 @@ import pygame.locals as pgl
 
 from naja.scenes.scene import Scene
 from naja.widgets.text import TextWidget
+from naja.widgets.selector import SelectorWidget
 from naja.events import QuitGameEvent
 from naja.scenes.credits import CreditsScene
 from naja.scenes.game import GameScene
@@ -15,17 +16,19 @@ from naja.events import SceneChangeEvent
 class MenuScene(Scene):
     def __init__(self, state):
         super(MenuScene, self).__init__(state)
+        selector = SelectorWidget()
+        self.add(selector)
         run_game = TextWidget((100, 100), 'Game', fontsize=32, colour='white')
         run_game.add_callback('click',
                               lambda event: SceneChangeEvent.post(GameScene))
-        self.add(run_game)
+        selector.add(run_game)
         credits = TextWidget((100, 200), 'Credits', fontsize=32, colour='white')
         credits.add_callback('click',
                              lambda event: SceneChangeEvent.post(CreditsScene))
-        self.add(credits)
+        selector.add(credits)
         quit = TextWidget((100, 300), 'Quit', fontsize=32, colour='white')
         quit.add_callback('click', lambda event: QuitGameEvent.post())
-        self.add(quit)
+        selector.add(quit)
 
     def handle_scene_event(self, ev):
         if ev.type == pgl.KEYDOWN:
index 81f11b649df9734371017f34891fa3ec6b360505..c82026179c9856b4812f6937d433881a8321fddc 100644 (file)
@@ -3,7 +3,7 @@ from collections import defaultdict
 import pygame
 from pygame import locals as pgl
 
-from naja.events import InvalidateTheWorld
+from naja.events import InvalidateTheWorld, SelectEvent
 
 
 class Widget(object):
@@ -38,7 +38,7 @@ class Widget(object):
             # True for for this event
             self._prepared = False
             return False
-        if ev.type == pgl.MOUSEBUTTONDOWN:
+        if SelectEvent.matches(ev) or ev.type == pgl.MOUSEBUTTONDOWN:
             self.callback('click')
         return False
 
diff --git a/naja/widgets/selector.py b/naja/widgets/selector.py
new file mode 100644 (file)
index 0000000..c0c91b9
--- /dev/null
@@ -0,0 +1,35 @@
+import pygame.locals as pgl
+
+from naja.widgets.base import Container
+from naja.resources import resources
+from naja.resources.mutators import R270
+
+
+class SelectorWidget(Container):
+    def __init__(self, *args, **kwargs):
+        super(SelectorWidget, self).__init__(*args, **kwargs)
+        self.position = 0
+        self.selector = resources.get_image('bits', 'arrow_on.png',
+                                            transforms=(R270,))
+
+    def render(self, surface):
+        super(SelectorWidget, self).render(surface)
+        pos = self.selector.get_rect()
+        selected = self.widgets[self.position]
+        pos = pos.move(selected.pos)
+        pos = pos.move(-pos.width * 1.5, (selected.size[1] - pos.height) / 2)
+        surface.blit(self.selector, pos)
+
+    def handle_event(self, ev):
+        if ev.type == pgl.KEYDOWN:
+            if ev.key in (pgl.K_DOWN, pgl.K_UP):
+                if ev.key == pgl.K_DOWN:
+                    self.position += 1
+                else:
+                    self.position -= 1
+                self.position %= len(self.widgets)
+                return True
+            elif ev.key == pgl.K_RETURN:
+                return self.widgets[self.position].callback('click')
+
+        return super(SelectorWidget, self).handle_event(ev)