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'),
12 'SOUTH': (1, 'glyphs/arrow_down.png'),
13 'EAST': (1, 'glyphs/arrow_right.png'),
14 'WEST': (1, 'glyphs/arrow_left.png'),
15 'HEALTH': (1, 'glyphs/health.png'),
16 'WIN': (1, 'glyphs/win.png'),
17 'KEY': (1, 'glyphs/key.png'),
18 'MSB': (1, 'glyphs/msb.png'),
22 class TextWidget(Widget):
24 def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
26 super(TextWidget, self).__init__(pos, size)
29 self.fontname = fontname or FONT
30 self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
31 self.colour = convert_colour(colour or PALETTE.BLACK)
33 def render_line(self, text):
34 text_surf = self.font.render(text, True, self.colour)
35 text_rect = text_surf.get_rect()
36 return pygame.transform.scale(
37 text_surf, (text_rect.width * EIGHT_BIT_SCALE,
38 text_rect.height * EIGHT_BIT_SCALE))
41 self.font = resources.get_font(self.fontname, self.fontsize)
42 self.surface = self.render_line(self.text)
43 self.size = self.surface.get_rect().size
45 def draw(self, surface):
46 surface.blit(self.surface, self.pos)
49 class TextBoxWidget(TextWidget):
50 def __init__(self, *args, **kwargs):
51 self.padding = kwargs.pop('padding', 5)
52 self.border = kwargs.pop('border', 2)
53 self.bg_colour = convert_colour(kwargs.pop('bg_colour',
54 PALETTE.LIGHT_VIOLET))
55 self.border_colour = convert_colour(kwargs.pop('border_colour',
57 self.box_width = kwargs.pop('box_width', 0)
59 super(TextBoxWidget, self).__init__(*args, **kwargs)
61 def lines(self, image_map):
62 if self.box_width != 0:
63 return self._wrapped_lines(image_map)
65 return self.text.splitlines()
67 def _wrapped_lines(self, image_map):
69 words_line = ' '.join(words)
70 width = self.font.size(words_line)[0]
71 if width < self.box_width:
74 Exception("Word %r too long for box." % (words[0],))
78 for line in self.text.splitlines():
80 remaining_words = line.split()
81 while remaining_words:
82 word = remaining_words[0]
87 markup_data = MARKUP_MAP.get(word, None)
88 if markup_data is not None:
89 word = ' ' * markup_data[0]
91 current_words.append(word)
92 if words_fit(current_words):
93 if markup_data is not None:
94 size = self.font.size(
95 ' '.join(current_words[:-1] + ['']))
96 pos = (size[0] * EIGHT_BIT_SCALE,
97 size[1] * line_count * EIGHT_BIT_SCALE)
98 pos = (pos[0] + self.padding, pos[1] + self.padding)
99 image_map[pos] = resources.get_image(
101 transforms=(EIGHT_BIT, blender(self.colour)))
102 remaining_words.pop(0)
105 yield ' '.join(current_words[:-1])
107 if current_words and words_fit(current_words):
108 yield ' '.join(current_words)
111 self.font = resources.get_font(self.fontname, self.fontsize)
114 width, height = self.padding * 2, self.padding * 2
115 for line in self.lines(image_map):
116 line_surface = self.render_line(line)
117 line_rect = line_surface.get_rect()
118 rendered_lines.append(line_surface)
119 width = max(width, line_rect.width + self.padding * 2)
120 height += line_rect.height
122 self.surface = pygame.surface.Surface((width, height),
123 pygame.locals.SRCALPHA)
124 self.surface.fill(self.bg_colour)
125 self.size = self.surface.get_rect().size
127 x, y = self.padding, self.padding
128 for line_surface in rendered_lines:
129 self.surface.blit(line_surface, (x, y))
130 y += line_surface.get_rect().height
131 for pos, img in image_map.items():
132 self.surface.blit(img, pos)
134 def draw(self, surface):
135 surface.blit(self.surface, self.rect)