changeset 245:0975a995113b

Factor out entry widget a bit
author Neil Muller <drnlmuller@gmail.com>
date Thu, 15 Sep 2011 00:20:02 +0200
parents c629dabe2038
children e54ba3f8f180
files mamba/widgets/entrybox.py mamba/widgets/text.py
diffstat 2 files changed, 38 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/widgets/entrybox.py	Thu Sep 15 00:19:58 2011 +0200
+++ b/mamba/widgets/entrybox.py	Thu Sep 15 00:20:02 2011 +0200
@@ -1,9 +1,9 @@
 import pygame
-from pygame.constants import SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN
+from pygame.constants import SRCALPHA, K_ESCAPE, K_RETURN, K_UP, K_DOWN
 
 from mamba.widgets.base import Container
 from mamba.constants import DELETE_KEYS
-from mamba.widgets.text import TextWidget, TextButton
+from mamba.widgets.text import TextWidget, TextButton, EntryTextWidget
 
 
 class EntryBox(Container):
@@ -18,19 +18,16 @@
         self.value = init_value
         self.prepare()
         self.modal = True
-        self.focussable = True
 
     def prepare(self):
         message = TextWidget((self.rect.left + 50, self.rect.top + 2),
                 self.text, color=self.color)
         self.rect.width = message.rect.width + 100
         self.add(message)
-        self.entry_text = TextWidget((self.rect.left + 5,
-            self.rect.top + message.rect.height + 5), 'Entry: %s' % self.value,
-            color=self.entry_color)
+        self.entry_text = EntryTextWidget((self.rect.left + 5,
+            self.rect.top + message.rect.height + 5), self.value,
+            update=self.edit, focus_color=self.entry_color)
         self.add(self.entry_text)
-        self.entry_text.add_callback(KEYDOWN, self.edit)
-        self.entry_text.focussable = True
         ok_button = TextButton((self.rect.left + 50,
             self.entry_text.rect.bottom), 'Accept')
         ok_button.add_callback('clicked', self.close, True)
@@ -64,12 +61,12 @@
         elif ev.key in DELETE_KEYS:
             if self.value:
                 self.value = self.value[:-1]
-                self.entry_text.text = 'Entry: %s' % self.value
-                self.entry_text.prepare()
+                self.entry_text.update(self.value)
+        elif ev.key in (K_UP, K_DOWN):
+            return False  # pass this up to parent
         else:
             self.value += ev.unicode
-            self.entry_text.text = 'Entry: %s' % self.value
-            self.entry_text.prepare()
+            self.entry_text.update(self.value)
         return True
 
     def grab_focus(self):
--- a/mamba/widgets/text.py	Thu Sep 15 00:19:58 2011 +0200
+++ b/mamba/widgets/text.py	Thu Sep 15 00:20:02 2011 +0200
@@ -1,5 +1,5 @@
 import pygame
-from pygame.constants import SRCALPHA
+from pygame.constants import SRCALPHA, KEYDOWN
 
 from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR
 from mamba.widgets.base import Widget, Button
@@ -64,3 +64,31 @@
         if self._focussed != self.focussed:
             self.prepare()
         super(TextButton, self).draw(surface)
+
+
+class EntryTextWidget(TextWidget):
+    def __init__(self, rect, text, **kwargs):
+        self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR)
+        text = 'Entry: %s' % text
+        self.base_color = COLOR
+        self.update_func = kwargs.pop('update', None)
+        super(EntryTextWidget, self).__init__(rect, text, **kwargs)
+        if not isinstance(self.focus_color, pygame.Color):
+            self.focus_color = pygame.Color(self.focus_color)
+        self.focussable = True
+        self.base_color = self.color
+        self.add_callback(KEYDOWN, self.update_func)
+
+    def update(self, new_value):
+        self.text = 'Entry: %s' % new_value
+        self.prepare()
+
+    def prepare(self):
+        self.color = self.focus_color if self.focussed else self.base_color
+        super(EntryTextWidget, self).prepare()
+        self._focussed = self.focussed
+
+    def draw(self, surface):
+        if self._focussed != self.focussed:
+            self.prepare()
+        super(EntryTextWidget, self).draw(surface)