From: Simon Cross Date: Sun, 28 Feb 2016 21:38:33 +0000 (+0200) Subject: Add menu navigation. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=4398178d5515ab2f2056ceb80276f5c4db1809a6;p=koperkapel.git Add menu navigation. --- diff --git a/koperkapel/scenes/menu.py b/koperkapel/scenes/menu.py index e5746e8..39ab12c 100644 --- a/koperkapel/scenes/menu.py +++ b/koperkapel/scenes/menu.py @@ -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()