comparison pyntnclick/widgets/text.py @ 559:b7d8b89de71a pyntnclick

Rip out keyboard handling
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 14:42:24 +0200
parents f9f04cb35697
children 1aac5a3b17e1
comparison
equal deleted inserted replaced
558:f9f04cb35697 559:b7d8b89de71a
1 import pygame 1 import pygame
2 from pygame.constants import (SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN, K_UP, 2 from pygame.constants import SRCALPHA
3 K_DOWN, K_SPACE, K_KP_ENTER)
4 3
5 from pyntnclick.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS 4 from pyntnclick.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS
6 from pyntnclick.widgets.base import Widget, Button 5 from pyntnclick.widgets.base import Widget, Button
7 from pyntnclick.data import filepath 6 from pyntnclick.data import filepath
8 from pyntnclick.constants import DEFAULT_FONT 7 from pyntnclick.constants import DEFAULT_FONT
73 72
74 def draw(self, surface): 73 def draw(self, surface):
75 if self._focussed != self.focussed: 74 if self._focussed != self.focussed:
76 self.prepare() 75 self.prepare()
77 super(TextButton, self).draw(surface) 76 super(TextButton, self).draw(surface)
78
79 def event(self, ev):
80 if ev.type == KEYDOWN and ev.key == K_SPACE:
81 return self.forced_click()
82 return super(TextButton, self).event(ev)
83
84
85 class EntryTextWidget(TextWidget):
86 def __init__(self, rect, text, **kwargs):
87 self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR)
88 self.prompt = kwargs.pop('prompt', 'Entry:')
89 self.value = text
90 text = '%s %s' % (self.prompt, text)
91 self.base_color = COLOR
92 self.update_func = kwargs.pop('update', None)
93 super(EntryTextWidget, self).__init__(rect, text, **kwargs)
94 if not isinstance(self.focus_color, pygame.Color):
95 self.focus_color = pygame.Color(self.focus_color)
96 self.focussable = True
97 self.base_color = self.color
98 self.add_callback(KEYDOWN, self.update)
99
100 def update(self, ev, widget):
101 old_value = self.value
102 if ev.key in DELETE_KEYS:
103 if self.value:
104 self.value = self.value[:-1]
105 elif ev.key in (K_ESCAPE, K_RETURN, K_KP_ENTER, K_UP, K_DOWN):
106 return False # ignore these
107 else:
108 self.value += ev.unicode
109 if old_value != self.value:
110 self.text = '%s %s' % (self.prompt, self.value)
111 self.prepare()
112 return True
113
114 def prepare(self):
115 self.color = self.focus_color if self.focussed else self.base_color
116 super(EntryTextWidget, self).prepare()
117 self._focussed = self.focussed
118
119 def draw(self, surface):
120 if self._focussed != self.focussed:
121 self.prepare()
122 super(EntryTextWidget, self).draw(surface)
123 if self.focussed:
124 text_rect = self.text_rect
125 # Warning, 00:30 AM code on the last Saturday
126 cur_start = text_rect.move(
127 (self.rect.left + 2, self.rect.top + 3)).topright
128 cur_end = text_rect.move(
129 (self.rect.left + 2, self.rect.top - 3)).bottomright
130 pygame.draw.line(surface, self.focus_color,
131 cur_start, cur_end, 2)