comparison pyntnclick/widgets/text.py @ 638:d1ec9e739e23 pyntnclick

Make TextWidget work (probably)
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 01:27:53 +0200
parents 1aac5a3b17e1
children fdc63049b08c
comparison
equal deleted inserted replaced
637:c27087877c84 638:d1ec9e739e23
1 import pygame 1 import pygame
2 from pygame.constants import SRCALPHA 2 from pygame.constants import SRCALPHA
3 3
4 from pyntnclick.constants import COLOR, FONT_SIZE, FOCUS_COLOR, DELETE_KEYS
5 from pyntnclick.widgets.base import Widget, Button 4 from pyntnclick.widgets.base import Widget, Button
6 from pyntnclick.data import filepath 5
7 from pyntnclick.constants import DEFAULT_FONT 6 # XXX: Needs a way to get at resource:
7 from pyntnclick.resources import Resources
8 get_font = Resources("Resources").get_font
8 9
9 10
10 class TextWidget(Widget): 11 class TextWidget(Widget):
11 fontcache = {} 12 fontcache = {}
12 13
13 def __init__(self, rect, text, fontsize=FONT_SIZE, color=COLOR): 14 def __init__(self, rect, text, fontname=None, fontsize=None, color=None):
14 super(TextWidget, self).__init__(rect) 15 super(TextWidget, self).__init__(rect)
15 self.text = text 16 self.text = text
16 self.fontsize = fontsize 17 if fontname is None:
17 self.color = color 18 self.fontname = 'Vera.ttf' # FIXME: Hardcoded...
19 else:
20 self.fontname = fontname
21 if fontsize is None:
22 self.fontsize = 24 # FIXME: Hardcoded...
23 else:
24 self.fontsize = fontsize
25 if color is None:
26 self.color = 'red' # FIXME: Hardcoded...
27 else:
28 self.color = color
18 self.prepare() 29 self.prepare()
19 30
20 def prepare(self): 31 def prepare(self):
21 self.fontname = DEFAULT_FONT 32 self.font = get_font(self.fontname, self.fontsize)
22 font = (self.fontname, self.fontsize)
23 if font not in TextWidget.fontcache:
24 fontfn = filepath('fonts/' + self.fontname)
25 TextWidget.fontcache[font] = pygame.font.Font(fontfn,
26 self.fontsize)
27 self.font = TextWidget.fontcache[font]
28 if not isinstance(self.color, pygame.Color): 33 if not isinstance(self.color, pygame.Color):
29 self.color = pygame.Color(self.color) 34 self.color = pygame.Color(self.color)
30 self.surface = self.font.render(self.text, True, self.color) 35 self.surface = self.font.render(self.text, True, self.color)
31 self.text_rect = self.surface.get_rect() 36 self.text_rect = self.surface.get_rect()
32 width, height = self.surface.get_rect().size 37 width, height = self.surface.get_rect().size