Fix bug in text box widget and multiline strings with markup
[naja.git] / naja / scenes / menu.py
1 """
2 Main menu scene.
3 """
4
5 import pygame.locals as pgl
6
7 from naja.constants import KEYS, PALETTE
8 from naja.events import SceneChangeEvent, QuitGameEvent
9 from naja.scenes.scene import Scene
10 from naja.scenes.credits import CreditsScene
11 from naja.scenes.game import GameScene
12 from naja.scenes.load_save import LoadGameScene, SaveGameScene
13 from naja.scenes.new_game import NewGameScene
14 from naja.widgets.selector import SelectorWidget
15 from naja.widgets.text import TextWidget
16
17
18 class MenuScene(Scene):
19     def __init__(self, state):
20         super(MenuScene, self).__init__(state)
21         selector = SelectorWidget()
22         self.add(selector)
23         y_offset = 0
24
25         y_offset += 50
26         resume = TextWidget(
27             (100, y_offset), 'Resume Game', colour=PALETTE.WHITE,
28             unselectable_colour=PALETTE.GREY)
29         resume.add_callback('click', self.scene_callback(GameScene))
30         resume.set_selectable_callback(lambda: state is not None)
31         selector.add(resume)
32
33         y_offset += 50
34         new = TextWidget((100, y_offset), 'New Game', colour=PALETTE.WHITE)
35         new.add_callback('click', self.scene_callback(NewGameScene))
36         selector.add(new)
37
38         y_offset += 50
39         load = TextWidget((100, y_offset), 'Load', colour=PALETTE.WHITE)
40         load.add_callback('click', self.scene_callback(LoadGameScene))
41         selector.add(load)
42
43         y_offset += 50
44         save = TextWidget(
45             (100, y_offset), 'Save', colour=PALETTE.WHITE,
46             unselectable_colour=PALETTE.GREY)
47         save.add_callback('click', self.scene_callback(SaveGameScene))
48         save.set_selectable_callback(lambda: state is not None)
49         selector.add(save)
50
51         y_offset += 50
52         credits = TextWidget((100, y_offset), 'Credits', colour=PALETTE.WHITE)
53         credits.add_callback('click', self.scene_callback(CreditsScene))
54         selector.add(credits)
55
56         y_offset += 50
57         quit = TextWidget((100, y_offset), 'Quit', colour=PALETTE.WHITE)
58         quit.add_callback('click', lambda event: QuitGameEvent.post())
59         selector.add(quit)
60
61     def scene_callback(self, scene_class):
62         return lambda event: SceneChangeEvent.post(scene_class)
63
64     def handle_scene_event(self, ev):
65         if ev.type == pgl.KEYDOWN:
66             if ev.key in KEYS.QUIT:
67                 QuitGameEvent.post()
68                 return