Merge branch 'master' of ctpug.org.za:koperkapel
[koperkapel.git] / koperkapel / scenes / menu.py
1 """ Main menu scene. """
2
3 from pgzero.constants import keys
4 from pgzero.loaders import images
5 from ..actors.navigator import ActorNavigator
6 from ..actors.orientatedsurf import OrientatedSurfActor, SelectableSurfActor
7 from ..util import safepath
8 from .base import Scene, ChangeSceneEvent, WorldEvent, QuitEvent
9
10
11 class MenuScene(Scene):
12     """ Main menu scene. """
13
14     def __init__(self):
15         super().__init__()
16         self.actors.add_layer("buttons", level=10)
17         self._init_bg()
18         self._init_buttons()
19
20     def _init_bg(self):
21         self.actors.default.clear()
22         self.actors.default.add(OrientatedSurfActor(
23             images.load(safepath("splash/background"))))
24
25     def _init_buttons(self):
26         self._nav = ActorNavigator()
27         menu = [
28             self._build_button("new_game", action=self.change_to_new_game),
29             self._build_button("resume_game", action=self.change_to_resume),
30             self._build_button("credits", action=self.change_to_credits),
31             self._build_button("quit", action=self.quit),
32         ]
33         for i, item in enumerate(menu):
34             item.pos = (675, 275 + (i * 100))
35             self.actors.add(item)
36         self._nav.connect(
37             menu, [keys.DOWN, keys.RIGHT], [keys.UP, keys.LEFT],
38             wrap=True)
39         self._nav.current.selected = True
40
41     def _build_button(self, name, action):
42         actor = SelectableSurfActor(
43             images.load(safepath("splash/%s" % (name,))),
44             images.load(safepath("splash/%s_selected" % (name,))))
45         actor.action = action
46         return actor
47
48     def change_to_resume(self):
49         from .level import GameLevelScene
50         return [ChangeSceneEvent(GameLevelScene())]
51
52     def change_to_new_game(self):
53         from .level import GameLevelScene
54         return [
55             WorldEvent("reset"),
56             ChangeSceneEvent(GameLevelScene())
57         ]
58
59     def change_to_credits(self):
60         from .credits import CreditsScene
61         return [ChangeSceneEvent(CreditsScene())]
62
63     def quit(self):
64         return [QuitEvent()]
65
66     def on_key_down(self, key, mod, unicode):
67         if key == keys.RETURN:
68             current = self._nav.current
69             if current is not None:
70                 return current.action()
71         else:
72             prev, current = self._nav.follow(key)
73             if prev is not None:
74                 prev.selected = False
75             if current is not None:
76                 current.selected = True