changeset 119:d5f7cccfdb6c

Hook up "detail view" scenes.
author Jeremy Thurgood <firxen@gmail.com>
date Tue, 24 Aug 2010 17:04:56 +0200
parents e548f4a13741
children 48d24a48d0ce
files gamelib/gamescreen.py gamelib/scenes/cryo.py gamelib/state.py
diffstat 3 files changed, 84 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/gamescreen.py	Tue Aug 24 15:39:02 2010 +0200
+++ b/gamelib/gamescreen.py	Tue Aug 24 17:04:56 2010 +0200
@@ -70,20 +70,25 @@
     def __init__(self, state):
         Widget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1]))
         self.state = state
+        self.detail = DetailWindow(state)
 
     def draw(self, surface):
         self.state.draw(surface)
 
     def mouse_down(self, event):
-        result = self.state.interact(event.pos)
-        if result:
-            if result.sound:
-                result.sound.play()
-            if result.message:
-                # Display the message as a modal dialog
-                MessageDialog(result.message, 60).present()
-                # queue a redraw to show updated state
-                self.invalidate()
+        if self.subwidgets:
+            self.remove(self.detail)
+            self.state.set_current_detail(None)
+        else:
+            result = self.state.interact(event.pos)
+            if result:
+                if result.sound:
+                    result.sound.play()
+                if result.message:
+                    # Display the message as a modal dialog
+                    MessageDialog(result.message, 60).present()
+                    # queue a redraw to show updated state
+                    self.invalidate()
 
     def animate(self):
         if self.state.animate():
@@ -94,28 +99,33 @@
         if not self.subwidgets:
             self.state.mouse_move(event.pos)
 
+    def show_detail(self, detail):
+        w, h = self.state.set_current_detail(detail)
+        self.detail.set_rect(Rect(0, 0, w, h))
+        self.add_centered(self.detail)
+
 
 class DetailWindow(Widget):
     def __init__(self, state):
-        Widget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1]))
+        Widget.__init__(self)
         self.state = state
-        self.draw_area = Rect(0, 0, 300, 300)
-        self.draw_area.center = self.center
 
     def draw(self, surface):
-        surface.fill(Color('green'), self.draw_area)
+        self.state.draw_detail(surface)
 
     def mouse_down(self, event):
-        if self.draw_area.collidepoint(event.pos):
-            # TODO: Interact with detail view
-            pass
-        else:
-            self.parent.remove(self)
+        result = self.state.interact_detail(self.global_to_local(event.pos))
+        if result:
+            if result.sound:
+                result.sound.play()
+            if result.message:
+                # Display the message as a modal dialog
+                MessageDialog(result.message, 60).present()
+                # queue a redraw to show updated state
+                self.invalidate()
 
     def mouse_move(self, event):
-        if self.draw_area.collidepoint(event.pos):
-            # TODO: mouse_move stuff
-            pass
+        self.state.mouse_move_detail(event.pos)
 
 
 class ToolBar(Row):
@@ -148,7 +158,7 @@
 
         self.inventory = InventoryView(self.state, self.handbutton)
 
-        self.testbutton = Button('Test', action=self.show_detail)
+        self.testbutton = Button('Test', lambda: self.state_widget.show_detail('cryo_detail'))
 
         self.toolbar = ToolBar([
                 self.menubutton,
@@ -159,13 +169,8 @@
         self.toolbar.bottomleft = self.bottomleft
         self.add(self.toolbar)
 
-        self.detail = DetailWindow(self.state)
-
         self.running = True
 
-    def show_detail(self):
-        self.state_widget.add_centered(self.detail)
-
     # Albow uses magic method names (command + '_cmd'). Yay.
     # Albow's search order means they need to be defined here, not in
     # PopMenu, which is annoying.
--- a/gamelib/scenes/cryo.py	Tue Aug 24 15:39:02 2010 +0200
+++ b/gamelib/scenes/cryo.py	Tue Aug 24 17:04:56 2010 +0200
@@ -155,4 +155,37 @@
     INITIAL = "info"
 
 
+class CryoTriangle(Thing):
+    "Triangle in the cryo room."
+
+    NAME = "cryo.triangle"
+
+    INTERACTS = {
+        "triangular": InteractImage(50, 50, "door_open.png"),
+        }
+
+    INITIAL = "triangular"
+
+    def interact_without(self):
+        return Result("You interacted.")
+
+    def is_interactive(self):
+        return True
+
+
+class CryoUnitWithCorpse(Scene):
+
+    FOLDER = "cryo"
+    BACKGROUND = "cryo_room.png"
+    NAME = "cryo_detail"
+
+    SIZE = (300, 300)
+
+    def __init__(self, state):
+        super(CryoUnitWithCorpse, self).__init__(state)
+        self.add_thing(CryoTriangle())
+
+
+
 SCENES = [Cryo]
+DETAIL_VIEWS = [CryoUnitWithCorpse]
--- a/gamelib/state.py	Tue Aug 24 15:39:02 2010 +0200
+++ b/gamelib/state.py	Tue Aug 24 17:04:56 2010 +0200
@@ -71,10 +71,20 @@
         mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname])
         for scene_cls in mod.SCENES:
             self.add_scene(scene_cls(self))
+        if hasattr(mod, 'DETAIL_VIEWS'):
+            for scene_cls in mod.DETAIL_VIEWS:
+                self.add_detail_view(scene_cls(self))
 
     def set_current_scene(self, name):
         self.current_scene = self.scenes[name]
 
+    def set_current_detail(self, name):
+        if name is None:
+            self.current_detail = None
+        else:
+            self.current_detail = self.detail_views[name]
+            return self.current_detail.SIZE
+
     def add_inventory_item(self, name):
         self.inventory.append(self.items[name])
 
@@ -90,15 +100,24 @@
     def draw(self, surface):
         self.current_scene.draw(surface)
 
+    def draw_detail(self, surface):
+        self.current_detail.draw(surface)
+
     def interact(self, pos):
         return self.current_scene.interact(self.tool, pos)
 
+    def interact_detail(self, pos):
+        return self.current_detail.interact(self.tool, pos)
+
     def animate(self):
         return self.current_scene.animate()
 
     def mouse_move(self, pos):
         self.current_scene.mouse_move(self.tool, pos)
 
+    def mouse_move_detail(self, pos):
+        self.current_detail.mouse_move(self.tool, pos)
+
 
 class StatefulGizmo(object):