Add menu navigation.
authorSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 21:38:33 +0000 (23:38 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 21:38:33 +0000 (23:38 +0200)
koperkapel/scenes/menu.py

index e5746e80463a82d87b1e7075115ce45c0c6621bc..39ab12c1ff1e1615edc25dc191ce007ff84f97c7 100644 (file)
@@ -2,7 +2,10 @@
 
 from pgzero.constants import keys
 from ..actors.buttons import TextButton
-from .base import Scene, ChangeSceneEvent
+from ..actors.navigator import ActorNavigator
+from .base import Scene, ChangeSceneEvent, QuitEvent
+
+
 
 
 class MenuScene(Scene):
@@ -12,15 +15,39 @@ class MenuScene(Scene):
         super().__init__()
         self._title = self.actors.add(TextButton("Main Menu"))
         self._title.pos = (300, 200)
-        self._level = self.actors.add(TextButton("Level"))
-        self._level.pos = (300, 300)
-        self._credits = self.actors.add(TextButton("Credits"))
-        self._credits.pos = (300, 400)
+        self._nav = ActorNavigator()
+        self._menu = [
+            TextButton("Play", action=self.change_to_level),
+            TextButton("Credits", action=self.change_to_credits),
+            TextButton("Quit", action=self.quit),
+        ]
+        for i, item in enumerate(self._menu):
+            item.pos = (300, 300 + (i * 100))
+            self.actors.add(item)
+        self._nav.connect(
+            self._menu, [keys.DOWN, keys.RIGHT], [keys.UP, keys.LEFT],
+            wrap=True)
+        self._nav.current.select()
+
+    def change_to_level(self):
+        from .level import LevelScene
+        return [ChangeSceneEvent(LevelScene('level1'))]
+
+    def change_to_credits(self):
+        from .credits import CreditsScene
+        return [ChangeSceneEvent(CreditsScene())]
+
+    def quit(self):
+        return [QuitEvent()]
 
     def on_key_down(self, key, mod, unicode):
-        if key == keys.C:
-            from .credits import CreditsScene
-            return [ChangeSceneEvent(CreditsScene())]
-        if key == keys.L:
-            from .level import LevelScene
-            return [ChangeSceneEvent(LevelScene('level1'))]
+        if key == keys.RETURN:
+            current = self._nav.current
+            if current is not None:
+                return current.action()
+        else:
+            prev, current = self._nav.follow(key)
+            if prev is not None:
+                prev.deselect()
+            if current is not None:
+                current.select()