added exit button to load level screen
[tabakrolletjie.git] / tabakrolletjie / scenes / load_level.py
1 """ Menu scene. """
2
3 import os
4
5 import pygame.locals as pgl
6
7 from .base import BaseScene
8 from ..constants import SCREEN_SIZE
9 from ..events import SceneChangeEvent
10 from ..widgets import TextButton, ImageButton
11 from ..loader import loader
12
13
14 class LoadLevelScene(BaseScene):
15
16     def _list_stations(self):
17         station_path = loader.full_path("stations")
18         files = [f for f in os.listdir(station_path) if f.endswith(".json")]
19         files.sort()
20         return [loader.load_station(f) for f in files]
21
22     def enter(self, gamestate):
23         """Construct list of stations"""
24         width = SCREEN_SIZE[0] / 2
25         self._title = TextButton(
26             'Select a planet:', (255, 255, 255), pos=(width, 75),
27             size=32, font='bold', centre=True)
28         height = 150
29         self._buttons = []
30         for station in self._list_stations():
31             title = station["config"]["name"]
32             pos = (width, height)
33             button = TextButton(title, (255, 255, 255), pos=pos, centre=True)
34             button.station = station
35             height += button.get_height() + 20
36             self._buttons.append(button)
37
38         self._tools = self.create_tools(gamestate)
39
40     def create_tools(self, gamestate):
41         tools = []
42         tools.append(ImageButton(
43             '32', 'exit.png', name='exit',
44             pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
45         return tools
46
47     def render(self, surface, gamestate):
48         surface.fill((0, 128, 128))
49         self._title.render(surface)
50         for button in self._buttons:
51             button.render(surface)
52
53         for tool in self._tools:
54             tool.render(surface)
55
56     def _get_pressed(self, ev):
57         for button in self._buttons:
58             if button.pressed(ev):
59                 return button
60         return None
61
62     def _do_load(self, station, gamestate):
63         from .day import DayScene
64         print "Loading station", station["config"]["name"]
65         gamestate.set_station(station)
66         SceneChangeEvent.post(scene=DayScene())
67
68     def event(self, ev, gamestate):
69         if ev.type == pgl.KEYDOWN:
70             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
71                 from .menu import MenuScene
72                 SceneChangeEvent.post(scene=MenuScene())
73         elif ev.type == pgl.MOUSEBUTTONDOWN:
74             pressed = self._get_pressed(ev)
75             if pressed:
76                 self._do_load(pressed.station, gamestate)
77             else:
78                 # Check tools
79                 for tool in self._tools:
80                     if tool.pressed(ev):
81                         if tool.name == 'exit':
82                             from .menu import MenuScene
83                             SceneChangeEvent.post(scene=MenuScene())