changeset 440:93ba8c966cb9

Disableable text buttons
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 17 Sep 2011 20:02:49 +0200
parents 3017612e613e
children 72f210945576
files mamba/widgets/base.py mamba/widgets/text.py
diffstat 2 files changed, 25 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/widgets/base.py	Sat Sep 17 19:54:07 2011 +0200
+++ b/mamba/widgets/base.py	Sat Sep 17 20:02:49 2011 +0200
@@ -19,6 +19,7 @@
         self.focussed = False
         self.modal = False
         self.parent = None
+        self.disabled = False
         self.callbacks = collections.defaultdict(list)
 
     def add_callback(self, eventtype, callback, *args):
@@ -26,6 +27,9 @@
 
     def event(self, ev):
         "Don't override this without damn good reason"
+        if self.disabled:
+            return True
+
         type_ = ev.type
         if type_ == USEREVENT:
             for k in self.callbacks.iterkeys():
@@ -79,6 +83,21 @@
             return True
         return False
 
+    def disable(self):
+        if not self.disabled:
+            self.disabled = True
+            self._focussable_when_enabled = self.focussable
+            self.focussable = False
+            if hasattr(self, 'prepare'):
+                self.prepare()
+
+    def enable(self):
+        if self.disabled:
+            self.disabled = False
+            self.focussable = self._focussable_when_enabled
+            if hasattr(self, 'prepare'):
+                self.prepare()
+
 
 class Button(Widget):
 
--- a/mamba/widgets/text.py	Sat Sep 17 19:54:07 2011 +0200
+++ b/mamba/widgets/text.py	Sat Sep 17 20:02:49 2011 +0200
@@ -50,7 +50,12 @@
         text = self.surface
         text_rect = text.get_rect()
         self._focussed = self.focussed
-        color = self.focus_color if self.focussed else self.color
+        if self.disabled:
+            color = pygame.Color('#666666')
+        elif self.focussed:
+            color = self.focus_color
+        else:
+            color = self.color
 
         self.rect.width = text_rect.width + self.padding * 2
         self.rect.height = text_rect.height + self.padding * 2