changeset 157:31881bf8ddda

Lockable text button
author Stefano Rivera <stefano@rivera.za.net>
date Tue, 13 Sep 2011 22:42:44 +0200
parents 77ea895e4d37
children 6a866d4710e3
files mamba/widgets/imagebutton.py
diffstat 1 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/widgets/imagebutton.py	Tue Sep 13 22:42:21 2011 +0200
+++ b/mamba/widgets/imagebutton.py	Tue Sep 13 22:42:44 2011 +0200
@@ -2,6 +2,7 @@
 from pygame.locals import SRCALPHA
 
 from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR
+from mamba.data import load_image
 from mamba.widgets.base import Button
 from mamba.widgets.text import TextWidget
 
@@ -40,3 +41,48 @@
         if self._focussed != self.focussed:
             self.prepare()
         super(ImageButtonWidget, self).draw(surface)
+
+
+class LockableTextButton(Button):
+
+    def __init__(self, rect, text, locked=False, fontsize=FONT_SIZE,
+                 color=COLOR):
+        super(LockableTextButton, self).__init__(rect)
+        self.text = text
+        self.locked = locked
+        self.fontsize = fontsize
+        self.color = color
+        self.focus_color = pygame.Color(FOCUS_COLOR)
+        self.background = 0
+        self.focussable = True
+        self.border = 3
+        self.rect.width = 50
+        self.rect.height = 50
+        self.prepare()
+
+    def prepare(self):
+        self.surface = pygame.Surface(self.rect.size, SRCALPHA)
+        self.surface.fill(0)
+
+        if not isinstance(self.color, pygame.Color):
+            self.color = pygame.Color(self.color)
+
+        self._text = TextWidget((0, 0), self.text, self.fontsize, self.color)
+        self._text.prepare()
+
+        color = self.focus_color if self.focussed else self.color
+        pygame.draw.rect(self.surface, color, self.surface.get_rect(),
+                         self.border)
+        text_rect = pygame.Rect((0, 0), self.rect.size).inflate(
+                self._text.rect.width - self.rect.width,
+                self._text.rect.height - self.rect.height)
+        self.surface.blit(self._text.surface, text_rect)
+        if self.locked:
+            lock = load_image('menus/lock.png')
+            self.surface.blit(lock, lock.get_rect())
+        self._state = (self.locked, self.focussed)
+
+    def draw(self, surface):
+        if self._state != (self.locked, self.focussed):
+            self.prepare()
+        surface.blit(self.surface, self.rect)