source: nagslang/widgets/text.py@ 196:40f618978c00

Last change on this file since 196:40f618978c00 was 196:40f618978c00, checked in by Stefano Rivera <stefano@…>, 8 years ago

Overlays that don't suck as much

File size: 2.0 KB
RevLine 
[196]1import pygame
2
[29]3from nagslang.constants import FONT, FONT_SIZE
4from nagslang.widgets.base import Widget
5from nagslang.utils import convert_colour
[33]6from nagslang.resources import resources
[29]7
8
9class TextWidget(Widget):
10 def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
11 colour=None):
12 super(TextWidget, self).__init__(pos, size)
13
14 self.text = text
15 self.fontname = fontname or FONT
16 self.fontsize = fontsize or FONT_SIZE
17 self.colour = convert_colour(colour or (0, 0, 0))
18
19 self.prepare()
20
21 def prepare(self):
[33]22 self.font = resources.get_font(self.fontname, self.fontsize)
[29]23 self.surface = self.font.render(self.text, True, self.colour)
24 self.text_rect = self.surface.get_rect()
25 if not self.size:
26 self.rect.size = self.text_rect.size
27
28 def draw(self, surface):
29 if self.visible:
30 self.do_prepare()
31 surface.blit(self.surface, self.rect)
[196]32
33
34class LabelWidget(TextWidget):
35 def __init__(self, *args, **kwargs):
36 self.padding = kwargs.pop('padding', 5)
37 self.border = kwargs.pop('border', 2)
38 self.bg_colour = convert_colour(kwargs.pop('bg_colour',
39 (255, 255, 255, 150)))
40 self.border_colour = convert_colour(kwargs.pop('border_colour',
41 (0, 0, 0)))
42 super(LabelWidget, self).__init__(*args, **kwargs)
43
44 def prepare(self):
45 super(LabelWidget, self).prepare()
46 if not self.size:
47 self.rect.width += 2 * self.padding
48 self.rect.height += 2 * self.padding
49 surface = pygame.Surface(self.rect.size)
50 surface = surface.convert_alpha()
51 surface.fill(self.bg_colour)
52 surface.blit(self.surface, self.surface.get_rect().move(
53 (self.padding, self.padding)))
54 pygame.draw.rect(surface, self.border_colour, surface.get_rect(),
55 self.border)
56 self.surface = surface
Note: See TracBrowser for help on using the repository browser.