view mamba/widgets/text.py @ 18:a0604a61762e

More widget API
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 11 Sep 2011 13:49:03 +0200
parents 771db7d6e862
children d0e8940df703
line wrap: on
line source

import pygame

from mamba.widgets import Widget
from mamba.data import filepath

class Text(Widget):
    fontcache = {}
    def __init__(self, rect, text, fontsize=16, color='black'):
        super(Widget, self).__init(rect)
        self.text = text
        self.fontsize = fontsize
        self.color = color
        self.prepare()

    def prepare(self):
        self.fontname = 'DejaVuSans.ttf'
        font = (self.fontname, self.fontsize)
        if font not in Text.fontcache:
            fontfn = filepath('fonts/' + self.fontname)
            Text.fontcache[font] = pygame.font.Font(fontfn, self.fontsize)
        self.font = Text.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 = self.surface.get_rect()

    def do_draw(self, surface):
        surface.blit(self.surface, self.pos)