# HG changeset patch # User Stefano Rivera # Date 1315946564 -7200 # Node ID 31881bf8dddad6c2de1f367ac8c2e71c661d82a3 # Parent 77ea895e4d37fbca8e03e15df3c4bcda2726da8e Lockable text button diff -r 77ea895e4d37 -r 31881bf8ddda mamba/widgets/imagebutton.py --- 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)