Mercurial > mamba
comparison mamba/widgets/text.py @ 91:ebd8f46cc553
Text Button
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 11 Sep 2011 19:05:20 +0200 |
parents | ac6688820528 |
children | d5aa5f805f00 |
comparison
equal
deleted
inserted
replaced
90:5b8eb97dc675 | 91:ebd8f46cc553 |
---|---|
26 self.surface = self.font.render(self.text, True, self.color) | 26 self.surface = self.font.render(self.text, True, self.color) |
27 self.rect.width, self.rect.height = self.surface.get_rect().size | 27 self.rect.width, self.rect.height = self.surface.get_rect().size |
28 | 28 |
29 def draw(self, surface): | 29 def draw(self, surface): |
30 surface.blit(self.surface, self.rect) | 30 surface.blit(self.surface, self.rect) |
31 | |
32 | |
33 class TextButton(TextWidget): | |
34 def __init__(self, *args, **kwargs): | |
35 self.focus_color = kwargs.pop('focus_color', 'red') | |
36 self.padding = kwargs.pop('padding', 10) | |
37 self.border = kwargs.pop('border', 3) | |
38 super(TextButton, self).__init__(*args, **kwargs) | |
39 if not isinstance(self.focus_color, pygame.Color): | |
40 self.focus_color = pygame.Color(self.focus_color) | |
41 self.focussable = True | |
42 | |
43 def prepare(self): | |
44 super(TextButton, self).prepare() | |
45 text = self.surface | |
46 text_rect = text.get_rect() | |
47 self._focussed = self.focussed | |
48 color = self.focus_color if self.focussed else self.color | |
49 | |
50 self.rect.width = text_rect.width + self.padding * 2 | |
51 self.rect.height = text_rect.height + self.padding * 2 | |
52 self.surface = pygame.Surface(self.rect.size) | |
53 self.surface.blit(text, text.get_rect().move(self.padding, | |
54 self.padding)) | |
55 pygame.draw.rect(self.surface, color, self.surface.get_rect(), | |
56 self.border) | |
57 | |
58 def draw(self, surface): | |
59 if self._focussed != self.focussed: | |
60 self.prepare() | |
61 super(TextButton, self).draw(surface) |