3 from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE
4 from naja.resources import resources
5 from naja.resources.mutators import EIGHT_BIT, blender
6 from naja.utils import convert_colour
7 from naja.widgets.base import Widget
11 'NORTH': (1, 'glyphs/arrow_up.png', None),
12 'SOUTH': (1, 'glyphs/arrow_down.png', None),
13 'EAST': (1, 'glyphs/arrow_right.png', None),
14 'WEST': (1, 'glyphs/arrow_left.png', None),
15 'HEALTH': (1, 'glyphs/health.png', PALETTE.DARK_RED),
16 'WINTOKEN': (1, 'glyphs/win.png', PALETTE.DARK_OLIVE),
17 'KEY': (1, 'glyphs/key.png', None),
18 'MSB': (1, 'glyphs/msb.png', None),
19 'REDKEY': (1, 'glyphs/key.png', PALETTE.ORANGE),
20 'GREENKEY': (1, 'glyphs/key.png', PALETTE.GREEN),
21 'BLUEKEY': (1, 'glyphs/key.png', PALETTE.BLUE),
25 class TextWidget(Widget):
27 def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
29 super(TextWidget, self).__init__(pos, size)
32 self.fontname = fontname or FONT
33 self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
34 self.colour = convert_colour(colour or PALETTE.BLACK)
36 def render_line(self, text):
37 text_surf = self.font.render(text, True, self.colour)
38 text_rect = text_surf.get_rect()
39 return pygame.transform.scale(
40 text_surf, (text_rect.width * EIGHT_BIT_SCALE,
41 text_rect.height * EIGHT_BIT_SCALE))
44 self.font = resources.get_font(self.fontname, self.fontsize)
45 self.surface = self.render_line(self.text)
46 self.size = self.surface.get_rect().size
48 def draw(self, surface):
49 surface.blit(self.surface, self.pos)
52 class TextBoxWidget(TextWidget):
53 def __init__(self, *args, **kwargs):
54 self.padding = kwargs.pop('padding', 5)
55 self.border = kwargs.pop('border', 2)
56 self.bg_colour = convert_colour(kwargs.pop('bg_colour',
57 PALETTE.LIGHT_VIOLET))
58 self.border_colour = convert_colour(kwargs.pop('border_colour',
60 self.box_width = kwargs.pop('box_width', 0)
62 super(TextBoxWidget, self).__init__(*args, **kwargs)
64 def lines(self, image_map):
65 if self.box_width != 0:
66 return self._wrapped_lines(image_map)
68 return self.text.splitlines()
70 def _wrapped_lines(self, image_map):
72 words_line = ' '.join(words)
73 width = self.font.size(words_line)[0]
74 if width < self.box_width:
77 Exception("Word %r too long for box." % (words[0],))
81 for line in self.text.splitlines():
83 remaining_words = line.split()
84 while remaining_words:
85 word = remaining_words[0]
90 markup_data = MARKUP_MAP.get(word, None)
91 if markup_data is not None:
92 word = ' ' * markup_data[0]
94 current_words.append(word)
95 if words_fit(current_words):
96 if markup_data is not None:
97 size = self.font.size(
98 ' '.join(current_words[:-1] + ['']))
99 pos = (size[0] * EIGHT_BIT_SCALE,
100 size[1] * line_count * EIGHT_BIT_SCALE)
101 pos = (pos[0] + self.padding, pos[1] + self.padding)
103 if markup_data[2] is not None:
104 colour = markup_data[2]
105 image_map[pos] = resources.get_image(
107 transforms=(EIGHT_BIT, blender(colour)))
108 remaining_words.pop(0)
111 yield ' '.join(current_words[:-1])
113 if current_words and words_fit(current_words):
114 yield ' '.join(current_words)
117 self.font = resources.get_font(self.fontname, self.fontsize)
120 width, height = self.padding * 2, self.padding * 2
121 for line in self.lines(image_map):
122 line_surface = self.render_line(line)
123 line_rect = line_surface.get_rect()
124 rendered_lines.append(line_surface)
125 width = max(width, line_rect.width + self.padding * 2)
126 height += line_rect.height
128 self.surface = pygame.surface.Surface((width, height),
129 pygame.locals.SRCALPHA)
130 self.surface.fill(self.bg_colour)
131 self.size = self.surface.get_rect().size
133 x, y = self.padding, self.padding
134 for line_surface in rendered_lines:
135 self.surface.blit(line_surface, (x, y))
136 y += line_surface.get_rect().height
137 for pos, img in image_map.items():
138 self.surface.blit(img, pos)
140 def draw(self, surface):
141 surface.blit(self.surface, self.rect)