Merge of doom
[koperkapel.git] / koperkapel / scenes / menu.py
1 """ Main menu scene. """
2
3 from pgzero.constants import keys
4 from ..actors.buttons import TextButton
5 from ..actors.navigator import ActorNavigator
6 from .base import Scene, ChangeSceneEvent, WorldEvent, QuitEvent
7
8
9 class MenuScene(Scene):
10     """ Main menu scene. """
11
12     def __init__(self):
13         super().__init__()
14         self._title = self.actors.add(TextButton("Main Menu"))
15         self._title.pos = (300, 100)
16         self._nav = ActorNavigator()
17         self._menu = [
18             TextButton("Play", action=self.change_to_level),
19             TextButton("View Last Generated Level",
20                        action=self.change_to_viewer),
21             TextButton("Manage Roaches", action=self.change_to_roaches),
22             TextButton("Credits", action=self.change_to_credits),
23             TextButton("Quit", action=self.quit),
24         ]
25         for i, item in enumerate(self._menu):
26             item.pos = (300, 200 + (i * 100))
27             self.actors.add(item)
28         self._nav.connect(
29             self._menu, [keys.DOWN, keys.RIGHT], [keys.UP, keys.LEFT],
30             wrap=True)
31         self._nav.current.select()
32
33     def change_to_level(self):
34         from .level import LevelScene
35         return [ChangeSceneEvent(LevelScene())]
36
37     def change_to_viewer(self):
38         from .viewlevel import ViewLevelScene
39         return [
40             WorldEvent("set", {"level.name": "map"}),
41             ChangeSceneEvent(ViewLevelScene())
42         ]
43
44     def change_to_roaches(self):
45         from .roaches import RoachesScene
46         return [ChangeSceneEvent(RoachesScene())]
47
48     def change_to_credits(self):
49         from .credits import CreditsScene
50         return [ChangeSceneEvent(CreditsScene())]
51
52     def quit(self):
53         return [QuitEvent()]
54
55     def on_key_down(self, key, mod, unicode):
56         if key == keys.RETURN:
57             current = self._nav.current
58             if current is not None:
59                 return current.action()
60         else:
61             prev, current = self._nav.follow(key)
62             if prev is not None:
63                 prev.deselect()
64             if current is not None:
65                 current.select()