view mamba/widgets/text.py @ 245:0975a995113b

Factor out entry widget a bit
author Neil Muller <drnlmuller@gmail.com>
date Thu, 15 Sep 2011 00:20:02 +0200
parents 77ea895e4d37
children a44eabb108a5
line wrap: on
line source

import pygame
from pygame.constants import SRCALPHA, KEYDOWN

from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR
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)
        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)