Mercurial > mamba
view mamba/widgets/text.py @ 324:ffec90bda89d
Make sure we clear list before refilling it
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Fri, 16 Sep 2011 20:30:12 +0200 |
parents | feca52afc109 |
children | 737e19e79857 |
line wrap: on
line source
import pygame from pygame.constants import (SRCALPHA, KEYDOWN, K_ESCAPE, K_RETURN, K_UP, K_DOWN) 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 class TextWidget(Widget): fontcache = {} def __init__(self, rect, text, fontsize=FONT_SIZE, color=COLOR): super(TextWidget, self).__init__(rect) self.text = text self.fontsize = fontsize self.color = color self.prepare() def prepare(self): self.fontname = DEFAULT_FONT font = (self.fontname, self.fontsize) if font not in TextWidget.fontcache: fontfn = filepath('fonts/' + self.fontname) TextWidget.fontcache[font] = pygame.font.Font(fontfn, self.fontsize) self.font = TextWidget.fontcache[font] if not isinstance(self.color, pygame.Color): self.color = pygame.Color(self.color) self.surface = self.font.render(self.text, True, self.color) self.rect.width, self.rect.height = self.surface.get_rect().size def draw(self, surface): surface.blit(self.surface, self.rect) class TextButton(Button, TextWidget): def __init__(self, *args, **kwargs): self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR) 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, SRCALPHA) self.surface.fill(0) 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) class EntryTextWidget(TextWidget): 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) 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) 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 super(EntryTextWidget, self).prepare() self._focussed = self.focussed def draw(self, surface): if self._focussed != self.focussed: self.prepare() super(EntryTextWidget, self).draw(surface)