changeset 813:3a875256f795 pyntnclick

better visible handling
author Neil Muller <neil@dip.sun.ac.za>
date Sun, 27 Jan 2013 17:33:04 +0200
parents 2dd400a7c16d
children 0430f7a8a86c
files pyntnclick/menuscreen.py pyntnclick/tools/rect_drawer.py pyntnclick/widgets/base.py pyntnclick/widgets/imagebutton.py pyntnclick/widgets/text.py
diffstat 5 files changed, 50 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/menuscreen.py	Sun Jan 27 16:50:04 2013 +0200
+++ b/pyntnclick/menuscreen.py	Sun Jan 27 17:33:04 2013 +0200
@@ -31,7 +31,7 @@
         self.set_button_state(self._save_game_button, running)
 
     def set_button_state(self, button, enabled):
-        button.visible = enabled
+        button.set_visible(enabled)
         if enabled:
             button.enable()
         else:
--- a/pyntnclick/tools/rect_drawer.py	Sun Jan 27 16:50:04 2013 +0200
+++ b/pyntnclick/tools/rect_drawer.py	Sun Jan 27 17:33:04 2013 +0200
@@ -57,13 +57,14 @@
         self._palette.cur_selection = self
 
     def draw(self, surface):
-        self.do_prepare()
-        surface.fill(pygame.color.Color(0, 0, 0), self.rect)
-        if self.selected:
-            surface.fill(self.sel_colour, self._button_rect)
-        else:
-            surface.fill(self.unsel_colour, self._button_rect)
-        surface.fill(self._colour, self._colour_rect)
+        if self.visible:
+            self.do_prepare()
+            surface.fill(pygame.color.Color(0, 0, 0), self.rect)
+            if self.selected:
+                surface.fill(self.sel_colour, self._button_rect)
+            else:
+                surface.fill(self.unsel_colour, self._button_rect)
+            surface.fill(self._colour, self._colour_rect)
 
 
 class AppPalette(Container):
@@ -259,9 +260,9 @@
     def toggle_images(self, ev, widget):
         self.draw_images = not self.draw_images
         for image in self.images:
-            image.visible = self.draw_images
+            image.set_visible(self.draw_images)
         if self.current_image:
-            self.current_image.visible = self.draw_images
+            self.current_image.set_visible(self.draw_images)
         self.invalidate()
 
     def toggle_trans_images(self, ev, widget):
@@ -297,10 +298,13 @@
         self.clear_display = True
 
     def draw(self, surface):
+        if not self.visible:
+            return
         self.do_prepare()
         if self.clear_display:
             surface.fill(pygame.color.Color(0, 0, 0),
-                    pygame.Rect(0, 0, constants.screen[0], constants.screen[1]))
+                    pygame.Rect(0, 0, constants.screen[0],
+                        constants.screen[1]))
             self.clear_display = False
 
         if self.zoom_display:
--- a/pyntnclick/widgets/base.py	Sun Jan 27 16:50:04 2013 +0200
+++ b/pyntnclick/widgets/base.py	Sun Jan 27 17:33:04 2013 +0200
@@ -21,6 +21,7 @@
         self.modal = False
         self.parent = None
         self.disabled = False
+        self.visible = True
         self.callbacks = collections.defaultdict(list)
         # To track which widget the mouse is over
         self.mouseover_widget = self
@@ -34,7 +35,7 @@
 
     def event(self, ev):
         "Don't override this without damn good reason"
-        if self.disabled:
+        if self.disabled or not self.visible:
             return True
 
         type_ = ev.type
@@ -75,6 +76,12 @@
             self.prepare()
             self.is_prepared = True
 
+    def set_visible(self, visible):
+        if self.visible != visible:
+            self.visible = visible
+            self.prepare()
+            self.is_prepared = True
+
     def global_to_local(self, pos):
         x, y = pos
         return (x - self.rect.left, y - self.rect.top)
@@ -145,9 +152,10 @@
             self.remove(widget)
 
     def draw(self, surface):
-        self.do_prepare()
-        for child in self.children:
-            child.draw(surface)
+        if self.visible:
+            self.do_prepare()
+            for child in self.children:
+                child.draw(surface)
 
 
 class ModalStackContainer(Container):
@@ -181,12 +189,13 @@
         return self.top is widget
 
     def draw(self, surface):
-        self.do_prepare()
-        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)
+        if self.visible:
+            self.do_prepare()
+            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 Box(Container):
@@ -194,18 +203,19 @@
     padding = 4
 
     def draw(self, surface):
-        self.do_prepare()
-        # TODO: Why isn't this done in prepare?
-        expandrect = self.rect.move((-self.padding, -self.padding))
-        expandrect.width = self.rect.width + 2 * self.padding
-        expandrect.height = self.rect.height + 2 * self.padding
-        border = pygame.Surface(expandrect.size, SRCALPHA)
-        border.fill(pygame.Color('black'))
-        surface.blit(border, expandrect)
-        background = pygame.Surface(self.rect.size, SRCALPHA)
-        background.fill(pygame.Color('gray'))
-        surface.blit(background, self.rect)
-        super(Box, self).draw(surface)
+        if self.visible:
+            self.do_prepare()
+            # TODO: Why isn't this done in prepare?
+            expandrect = self.rect.move((-self.padding, -self.padding))
+            expandrect.width = self.rect.width + 2 * self.padding
+            expandrect.height = self.rect.height + 2 * self.padding
+            border = pygame.Surface(expandrect.size, SRCALPHA)
+            border.fill(pygame.Color('black'))
+            surface.blit(border, expandrect)
+            background = pygame.Surface(self.rect.size, SRCALPHA)
+            background.fill(pygame.Color('gray'))
+            surface.blit(background, self.rect)
+            super(Box, self).draw(surface)
 
 
 def convert_color(color):
--- a/pyntnclick/widgets/imagebutton.py	Sun Jan 27 16:50:04 2013 +0200
+++ b/pyntnclick/widgets/imagebutton.py	Sun Jan 27 17:33:04 2013 +0200
@@ -9,9 +9,7 @@
         if not size:
             self.rect.size = image.get_rect().size
         self.image = image
-        self.visible = True
 
     def draw(self, surface):
-        self.disabled = not self.visible
         if self.visible:
             surface.blit(self.image, self.rect)
--- a/pyntnclick/widgets/text.py	Sun Jan 27 16:50:04 2013 +0200
+++ b/pyntnclick/widgets/text.py	Sun Jan 27 17:33:04 2013 +0200
@@ -15,7 +15,6 @@
         self.fontname = fontname or constants.font
         self.fontsize = fontsize or constants.font_size
         self.color = color or constants.text_color
-        self.visible = True
 
     def prepare(self):
         self.font = self.resource.get_font(self.fontname, self.fontsize)
@@ -59,8 +58,9 @@
         self.surface = new_surface
 
     def draw(self, surface):
-        self.do_prepare()
-        surface.blit(self.surface, self.rect)
+        if self.visible:
+            self.do_prepare()
+            surface.blit(self.surface, self.rect)
 
 
 class TextButton(Button, TextWidget):