Mercurial > mamba
diff mamba/widgets/text.py @ 292:feca52afc109
Move editing code around
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 15 Sep 2011 23:55:48 +0200 |
parents | a44eabb108a5 |
children | 737e19e79857 |
line wrap: on
line diff
--- a/mamba/widgets/text.py Thu Sep 15 23:25:02 2011 +0200 +++ b/mamba/widgets/text.py Thu Sep 15 23:55:48 2011 +0200 @@ -1,7 +1,8 @@ import pygame -from pygame.constants import SRCALPHA, KEYDOWN +from pygame.constants import (SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN, K_UP, + K_DOWN) -from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR +from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS from mamba.widgets.base import Widget, Button from mamba.data import filepath from mamba.constants import DEFAULT_FONT @@ -70,6 +71,7 @@ def __init__(self, rect, text, **kwargs): self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR) self.prompt = kwargs.pop('prompt', 'Entry:') + self.value = text text = '%s %s' % (self.prompt, text) self.base_color = COLOR self.update_func = kwargs.pop('update', None) @@ -78,11 +80,21 @@ self.focus_color = pygame.Color(self.focus_color) self.focussable = True self.base_color = self.color - self.add_callback(KEYDOWN, self.update_func) + self.add_callback(KEYDOWN, self.update) - def update(self, new_value): - self.text = '%s %s' % (self.prompt, new_value) - self.prepare() + def update(self, ev, widget): + old_value = self.value + if ev.key in DELETE_KEYS: + if self.value: + self.value = self.value[:-1] + elif ev.key in (K_ESCAPE, K_RETURN, K_UP, K_DOWN): + return False # ignore these + else: + self.value += ev.unicode + if old_value != self.value: + self.text = '%s %s' % (self.prompt, self.value) + self.prepare() + return True def prepare(self): self.color = self.focus_color if self.focussed else self.base_color