source: nagslang/widgets/text.py@ 29:58505d3482b6

Last change on this file since 29:58505d3482b6 was 29:58505d3482b6, checked in by Stefano Rivera <stefano@…>, 8 years ago

Text on the menu screen

File size: 1.2 KB
Line 
1import pygame
2
3from nagslang.constants import FONT, FONT_SIZE
4from nagslang.data import filepath
5from nagslang.widgets.base import Widget
6from nagslang.utils import convert_colour
7
8
9class TextWidget(Widget):
10 fonts = {}
11
12 def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
13 colour=None):
14 super(TextWidget, self).__init__(pos, size)
15
16 self.text = text
17 self.fontname = fontname or FONT
18 self.fontsize = fontsize or FONT_SIZE
19 self.colour = convert_colour(colour or (0, 0, 0))
20
21 self.prepare()
22
23 def prepare(self):
24 font = (self.fontname, self.fontsize)
25 if font not in TextWidget.fonts:
26 fn = filepath('fonts/' + self.fontname)
27 TextWidget.fonts[font] = pygame.font.Font(fn, self.fontsize)
28 self.font = TextWidget.fonts[font]
29 self.surface = self.font.render(self.text, True, self.colour)
30 self.text_rect = self.surface.get_rect()
31 if not self.size:
32 self.rect.size = self.text_rect.size
33
34 def draw(self, surface):
35 if self.visible:
36 self.do_prepare()
37 surface.blit(self.surface, self.rect)
Note: See TracBrowser for help on using the repository browser.