added exit button to load level screen
[tabakrolletjie.git] / tabakrolletjie / scenes / load_level.py
index 7b3478918209e3042a3eeb8fb3ed5b4cdd7cca7f..015a5f69bb6b8b378d2c52db1e0c104521f2c5b6 100644 (file)
@@ -5,8 +5,9 @@ import os
 import pygame.locals as pgl
 
 from .base import BaseScene
+from ..constants import SCREEN_SIZE
 from ..events import SceneChangeEvent
-from ..widgets import TextButton
+from ..widgets import TextButton, ImageButton
 from ..loader import loader
 
 
@@ -14,52 +15,69 @@ class LoadLevelScene(BaseScene):
 
     def _list_stations(self):
         station_path = loader.full_path("stations")
-        files = os.listdir(station_path)
-        result = []
-        for f in files:
-            name, ext = os.path.splitext(f)
-            if ext == '.json':
-                result.append((name, f))
-        result.sort()
-        return result
+        files = [f for f in os.listdir(station_path) if f.endswith(".json")]
+        files.sort()
+        return [loader.load_station(f) for f in files]
 
     def enter(self, gamestate):
         """Construct list of stations"""
-        height = 50
+        width = SCREEN_SIZE[0] / 2
+        self._title = TextButton(
+            'Select a planet:', (255, 255, 255), pos=(width, 75),
+            size=32, font='bold', centre=True)
+        height = 150
         self._buttons = []
-        width = 30
-        for station, filename in self._list_stations():
+        for station in self._list_stations():
+            title = station["config"]["name"]
             pos = (width, height)
-            button = TextButton(station, (255, 255, 255), filename, pos)
-            if width < 400:
-                width += 350
-            else:
-                width = 30
-                height += button.get_height() + 20
+            button = TextButton(title, (255, 255, 255), pos=pos, centre=True)
+            button.station = station
+            height += button.get_height() + 20
             self._buttons.append(button)
 
+        self._tools = self.create_tools(gamestate)
+
+    def create_tools(self, gamestate):
+        tools = []
+        tools.append(ImageButton(
+            '32', 'exit.png', name='exit',
+            pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
+        return tools
+
     def render(self, surface, gamestate):
         surface.fill((0, 128, 128))
+        self._title.render(surface)
         for button in self._buttons:
             button.render(surface)
 
+        for tool in self._tools:
+            tool.render(surface)
+
     def _get_pressed(self, ev):
         for button in self._buttons:
             if button.pressed(ev):
-                return button.name
+                return button
         return None
 
-    def _do_load(self, name, gamestate):
+    def _do_load(self, station, gamestate):
         from .day import DayScene
-        print "Loading station", name
-        gamestate.load_station(name)
+        print "Loading station", station["config"]["name"]
+        gamestate.set_station(station)
         SceneChangeEvent.post(scene=DayScene())
 
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN:
-            if ev.key == pgl.K_l:
-                self._do_load("station_alpha.json", gamestate)
+            if ev.key in (pgl.K_q, pgl.K_ESCAPE):
+                from .menu import MenuScene
+                SceneChangeEvent.post(scene=MenuScene())
         elif ev.type == pgl.MOUSEBUTTONDOWN:
             pressed = self._get_pressed(ev)
             if pressed:
-                self._do_load(pressed, gamestate)
+                self._do_load(pressed.station, gamestate)
+            else:
+                # Check tools
+                for tool in self._tools:
+                    if tool.pressed(ev):
+                        if tool.name == 'exit':
+                            from .menu import MenuScene
+                            SceneChangeEvent.post(scene=MenuScene())