changeset 91:ebd8f46cc553

Text Button
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 11 Sep 2011 19:05:20 +0200
parents 5b8eb97dc675
children 1e9da4893d51
files mamba/habitats/mainmenu.py mamba/widgets/text.py
diffstat 2 files changed, 40 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/habitats/mainmenu.py	Sun Sep 11 19:05:11 2011 +0200
+++ b/mamba/habitats/mainmenu.py	Sun Sep 11 19:05:20 2011 +0200
@@ -1,20 +1,21 @@
 """Main menu."""
 
+from pygame.constants import K_RETURN
 from pygame.locals import MOUSEMOTION, KEYDOWN, QUIT
 import pygame.event
 
 from mamba.constants import ESCAPE_KEYS
 from mamba.engine import Habitat
-from mamba.widgets.text import TextWidget
+from mamba.widgets.text import TextButton
 
 
 class MainMenu(Habitat):
     def __init__(self):
         super(MainMenu, self).__init__()
-        hello = TextWidget((100, 100), "Hello", color='white')
+        hello = TextButton((100, 100), "Hello", color='white')
         self.container.add(hello)
-        hello.add_callback(MOUSEMOTION, self.debug_event)
-        quit = TextWidget((100, 200), "Quit", color='red')
+        quit = TextButton((100, 200), "Quit", color='white')
+        quit.add_callback(KEYDOWN, self.quit_keydown_event)
         self.container.add(quit)
         self.container.add_callback(KEYDOWN, self.keydown_event)
 
@@ -23,5 +24,7 @@
             pygame.event.post(pygame.event.Event(QUIT))
             return True
 
-    def debug_event(self, ev, widget):
-        print ev
+    def quit_keydown_event(self, ev, widget):
+        if ev.key == K_RETURN:
+            pygame.event.post(pygame.event.Event(QUIT))
+            return True
--- a/mamba/widgets/text.py	Sun Sep 11 19:05:11 2011 +0200
+++ b/mamba/widgets/text.py	Sun Sep 11 19:05:20 2011 +0200
@@ -28,3 +28,34 @@
 
     def draw(self, surface):
         surface.blit(self.surface, self.rect)
+
+
+class TextButton(TextWidget):
+    def __init__(self, *args, **kwargs):
+        self.focus_color = kwargs.pop('focus_color', 'red')
+        self.padding = kwargs.pop('padding', 10)
+        self.border = kwargs.pop('border', 3)
+        super(TextButton, self).__init__(*args, **kwargs)
+        if not isinstance(self.focus_color, pygame.Color):
+            self.focus_color = pygame.Color(self.focus_color)
+        self.focussable = True
+
+    def prepare(self):
+        super(TextButton, self).prepare()
+        text = self.surface
+        text_rect = text.get_rect()
+        self._focussed = self.focussed
+        color = self.focus_color if self.focussed else self.color
+
+        self.rect.width = text_rect.width + self.padding * 2
+        self.rect.height = text_rect.height + self.padding * 2
+        self.surface = pygame.Surface(self.rect.size)
+        self.surface.blit(text, text.get_rect().move(self.padding,
+                                                     self.padding))
+        pygame.draw.rect(self.surface, color, self.surface.get_rect(),
+                         self.border)
+
+    def draw(self, surface):
+        if self._focussed != self.focussed:
+            self.prepare()
+        super(TextButton, self).draw(surface)