changeset 678:36d7f7e9650e pyntnclick

Modal magic. (And yes, it is quite magical.)
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 12 Feb 2012 21:47:06 +0200
parents a8c42709a689
children fa168b5e2624
files pyntnclick/constants.py pyntnclick/gamescreen.py pyntnclick/widgets/base.py
diffstat 3 files changed, 36 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/constants.py	Sun Feb 12 19:47:56 2012 +0200
+++ b/pyntnclick/constants.py	Sun Feb 12 21:47:06 2012 +0200
@@ -38,6 +38,8 @@
     button_bg_color = (0x66, 0x66, 0x66, 0xFF)
     button_disabled_color = (0x66, 0x66, 0x66, 0xFF)
 
+    modal_obscure_color = (0, 0, 0, 0xB0)
+
     # User event IDs:
     enter = 1
     leave = 2
--- a/pyntnclick/gamescreen.py	Sun Feb 12 19:47:56 2012 +0200
+++ b/pyntnclick/gamescreen.py	Sun Feb 12 21:47:06 2012 +0200
@@ -10,8 +10,8 @@
 from pyntnclick.cursor import CursorScreen
 from pyntnclick.engine import Screen
 from pyntnclick.state import handle_result
-from pyntnclick.widgets.base import Container
-from pyntnclick.widgets.text import TextButton
+from pyntnclick.widgets.base import Container, ModalStackContainer
+from pyntnclick.widgets.text import TextButton, LabelWidget
 from pyntnclick.widgets.imagebutton import ImageButtonWidget
 
 # XXX: Need a way to get at the constants.
@@ -202,6 +202,7 @@
 
     def show_message(self, message, style=None):
         # Display the message as a modal dialog
+        # self.screen.modal_magic.add(LabelWidget((50, 50), self.gd, message))
         print message
         # XXX: MessageDialog(self.screen, message, 60, style=style).present()
         # queue a redraw to show updated state
@@ -359,16 +360,20 @@
 
     def start_game(self):
         self._clear_all()
+        self.modal_magic = self.container.add(
+            ModalStackContainer(self.container.rect.copy(), self.gd))
+        self.inner_container = self.modal_magic.add(
+            Container(self.container.rect.copy(), self.gd))
         toolbar_height = self.gd.constants.button_size
         rect = Rect(0, 0, self.surface_size[0],
                     self.surface_size[1] - toolbar_height)
         self.game = self.create_initial_state()
         self.state_widget = StateWidget(rect, self.gd, self)
-        self.container.add(self.state_widget)
+        self.inner_container.add(self.state_widget)
 
         self.toolbar = ToolBar((0, rect.height), self.gd, self)
         self.inventory = self.toolbar.inventory
-        self.container.add(self.toolbar)
+        self.inner_container.add(self.toolbar)
 
         self.gd.running = True
 
--- a/pyntnclick/widgets/base.py	Sun Feb 12 19:47:56 2012 +0200
+++ b/pyntnclick/widgets/base.py	Sun Feb 12 21:47:06 2012 +0200
@@ -122,6 +122,31 @@
             child.draw(surface)
 
 
+class ModalStackContainer(Container):
+
+    def __init__(self, rect, gd, obscure_color=None):
+        super(ModalStackContainer, self).__init__(rect, gd)
+        if obscure_color is None:
+            obscure_color = gd.constants.modal_obscure_color
+        self.obscure_color = convert_color(obscure_color)
+
+    def event(self, ev):
+        """Only the topmost child gets events.
+        """
+        if self.children[-1].event(ev):
+            return True
+
+        if super(Container, self).event(ev):
+            return True
+
+    def draw(self, surface):
+        obscure = pygame.Surface(self.rect.size, SRCALPHA)
+        obscure.fill(self.obscure_color)
+        for child in self.children:
+            surface.blit(obscure, self.rect)
+            child.draw(surface)
+
+
 class GridContainer(Container):
     """Hacky container that only supports grids, won't work with Container
     children, or modal children.