1 | from nagslang.constants import FONT, FONT_SIZE
|
---|
2 | from nagslang.widgets.base import Widget
|
---|
3 | import pygame
|
---|
4 |
|
---|
5 | from nagslang.utils import convert_colour
|
---|
6 | from nagslang.resources import resources
|
---|
7 |
|
---|
8 |
|
---|
9 | class 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):
|
---|
22 | self.font = resources.get_font(self.fontname, self.fontsize)
|
---|
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)
|
---|
32 |
|
---|
33 |
|
---|
34 | class 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, 192)))
|
---|
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
|
---|
57 |
|
---|
58 |
|
---|
59 | class MultiLineWidget(LabelWidget):
|
---|
60 |
|
---|
61 | def prepare(self):
|
---|
62 | self.font = resources.get_font(self.fontname, self.fontsize)
|
---|
63 | surfaces = []
|
---|
64 | height = 0
|
---|
65 | width = 0
|
---|
66 | for line in self.text.split('\n'):
|
---|
67 | surface = self.font.render(line, True, self.colour)
|
---|
68 | width = max(width, surface.get_rect().width)
|
---|
69 | height += surface.get_rect().height
|
---|
70 | surfaces.append(surface)
|
---|
71 | width += 2 * self.padding
|
---|
72 | height += 2 * self.padding
|
---|
73 | self.surface = pygame.surface.Surface((width, height),
|
---|
74 | pygame.locals.SRCALPHA)
|
---|
75 | self.surface.fill(self.bg_colour)
|
---|
76 | y = 0
|
---|
77 | for surface in surfaces:
|
---|
78 | self.surface.blit(surface, (self.padding, y + self.padding))
|
---|
79 | y += surface.get_rect().height
|
---|
80 | self.text_rect = self.surface.get_rect()
|
---|
81 | if not self.size:
|
---|
82 | self.rect.size = self.text_rect.size
|
---|