Mercurial > mamba
changeset 228:d83fd4ebddd3
Add entrybox widget
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 14 Sep 2011 23:07:01 +0200 |
parents | c068190c0b1d |
children | b0ef7eecc67a |
files | mamba/constants.py mamba/widgets/entrybox.py |
diffstat | 2 files changed, 78 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/constants.py Wed Sep 14 23:06:14 2011 +0200 +++ b/mamba/constants.py Wed Sep 14 23:07:01 2011 +0200 @@ -1,4 +1,4 @@ -from pygame.locals import K_ESCAPE, K_q +from pygame.locals import K_ESCAPE, K_q, K_BACKSPACE, K_DELETE # Display constants SCREEN = (800, 600) @@ -17,6 +17,7 @@ # Keyboard constants ESCAPE_KEYS = (K_ESCAPE, K_q) +DELETE_KEYS = (K_BACKSPACE, K_DELETE) # For easy access later DEFAULT_FONT = 'DejaVuSans.ttf'
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mamba/widgets/entrybox.py Wed Sep 14 23:07:01 2011 +0200 @@ -0,0 +1,76 @@ +import pygame +from pygame.constants import SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN + +from mamba.widgets.base import Container +from mamba.constants import DELETE_KEYS +from mamba.widgets.text import TextWidget, TextButton + + +class EntryBox(Container): + + def __init__(self, rect, text, init_value, accept_callback=None, + color='white', entry_color='yellow'): + super(EntryBox, self).__init__(rect) + self.text = text + self.accept_callback = accept_callback + self.color = color + self.entry_color = entry_color + 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.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) + self.add(ok_button) + cancel_button = ok_button = TextButton( + (self.entry_text.rect.right + 60, self.entry_text.rect.bottom), + 'Cancel') + cancel_button.add_callback('clicked', self.close, False) + self.add(cancel_button) + + def draw(self, surface): + background = pygame.Surface(self.rect.size, SRCALPHA) + background.fill(pygame.Color('gray')) + surface.blit(background, self.rect) + super(EntryBox, self).draw(surface) + + def close(self, ev, widget, ok): + if self.accept_callback and ok: + if self.accept_callback(self.value): + self.parent.remove(self) + # Don't remove if the accept callback failed + return + self.parent.remove(self) + + def edit(self, ev, widget): + if ev.key == K_ESCAPE: + self.close(ev, widget, False) + elif ev.key == K_RETURN: + self.close(ev, widget, True) + 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() + else: + self.value += ev.unicode + self.entry_text.text = 'Entry: %s' % self.value + self.entry_text.prepare() + print self.value + return True + + def grab_focus(self): + self.entry_text.grab_focus()