extended help
[tabakrolletjie.git] / tabakrolletjie / scenes / menu.py
1 """ Menu scene. """
2
3 import pygame.locals as pgl
4 import pygame.surface
5
6 from .base import BaseScene
7 from ..events import QuitEvent, SceneChangeEvent
8 from ..loader import loader
9 from ..utils import save_file_exists, get_save_data
10 from ..widgets import ImageButton
11 from ..transforms import Overlay
12
13 class SplashButton(ImageButton):
14     def __init__(self, name, pos):
15         super(SplashButton, self).__init__("splash", "%s.png" % name, name=name,
16             pos=pos, disabled_colour=(50, 50, 50))
17         w, h = self._img.get_size()
18         img = pygame.surface.Surface((w+8, h+4), pgl.SWSURFACE).convert_alpha()
19         img.fill((0, 0, 0, 0))
20         img.blit(self._img, (8, 4), None)
21         img.fill((0, 0, 0, 255), None, pgl.BLEND_RGBA_MULT)
22         img.blit(self._img, (0, 0), None)
23         self._img = img
24
25
26 class MenuScene(BaseScene):
27
28     BRIGHTNESS = Overlay(colour=(255, 255, 255, 50))
29
30     def enter(self, gamestate):
31         if gamestate.station is None:
32             print "Loading Station Alpha ..."
33             gamestate.set_station(loader.load_station("station-alpha.json"))
34
35         self._cont_but = SplashButton("continue_retry", (500, 210))
36         if not save_file_exists():
37             self._cont_but.disable()
38         self._menu = [
39             SplashButton("new_game", (600, 150)),
40             self._cont_but,
41             SplashButton("help", (700, 270)),
42             SplashButton("quit", (800, 330)),
43         ]
44         
45         # Background
46         self._soil = loader.load_image(
47             "textures", "soil.png", transform=self.BRIGHTNESS).convert_alpha()
48         splash = loader.load_image("splash", "splash.png").convert_alpha()
49         self._soil.blit(splash, (0, 0), None)
50
51     def render(self, surface, gamestate):
52         surface.blit(self._soil, (0, 0), None)
53
54         for item in self._menu:
55             item.render(surface)
56
57     def _get_pressed(self, ev):
58         for button in self._menu:
59             if button.pressed(ev):
60                 return button.name
61         return None
62
63     def _do_load_save(self, gamestate):
64         gamestate.load_from_json(get_save_data())
65         from .day import DayScene
66         SceneChangeEvent.post(scene=DayScene())
67
68     def _start_new_game(self):
69         from .load_level import LoadLevelScene
70         SceneChangeEvent.post(scene=LoadLevelScene())
71
72     def _do_help(self):
73         from .help import HelpScene
74         SceneChangeEvent.post(scene=HelpScene())
75
76     def event(self, ev, gamestate):
77         if ev.type == pgl.KEYDOWN:
78             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
79                 QuitEvent.post()
80             elif ev.key == pgl.K_n:
81                 from .night import NightScene
82                 SceneChangeEvent.post(scene=NightScene())
83             elif ev.key == pgl.K_d:
84                 self._do_day()
85             elif ev.key == pgl.K_l:
86                 self._start_new_game()
87             elif ev.key == pgl.K_h:
88                 self._do_help()
89         elif ev.type == pgl.MOUSEBUTTONDOWN:
90             pressed = self._get_pressed(ev)
91             if pressed == 'new_game':
92                 self._start_new_game()
93             elif pressed == 'continue_retry':
94                 self._do_load_save(gamestate)
95             elif pressed == 'help':
96                 self._do_help()
97             elif pressed == 'quit':
98                 QuitEvent.post()
99             elif pressed:
100                 print 'Pressed', pressed