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': ('glyphs/arrow_up.png', None),
12 'SOUTH': ('glyphs/arrow_down.png', None),
13 'EAST': ('glyphs/arrow_right.png', None),
14 'WEST': ('glyphs/arrow_left.png', None),
15 'HEALTH': ('glyphs/health.png', PALETTE.DARK_RED),
16 'WINTOKEN': ('glyphs/win.png', PALETTE.DARK_OLIVE),
17 'KEY': ('glyphs/key.png', None),
18 'MSB': ('glyphs/msb.png', None),
19 'RED': ('glyphs/key.png', PALETTE.ORANGE),
20 'GREEN': ('glyphs/key.png', PALETTE.GREEN),
21 'BLUE': ('glyphs/key.png', PALETTE.BLUE),
22 'CLOCKWISE': ('glyphs/clockwise.png', None),
23 'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None),
25 'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
26 'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
31 def __init__(self, markup_text, glyph_keys, suffix=''):
32 self.markup_text = markup_text
33 self.glyph_keys = glyph_keys
34 self.text = ' ' * len(self.glyph_keys) + suffix
37 class TextWidget(Widget):
39 def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
41 super(TextWidget, self).__init__(pos, size)
44 self.fontname = fontname or FONT
45 self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
46 self.colour = convert_colour(colour or PALETTE.BLACK)
48 def render_line(self, text):
49 text_surf = self.font.render(text, True, self.colour)
50 text_rect = text_surf.get_rect()
51 return pygame.transform.scale(
52 text_surf, (text_rect.width * EIGHT_BIT_SCALE,
53 text_rect.height * EIGHT_BIT_SCALE))
56 self.font = resources.get_font(self.fontname, self.fontsize)
57 self.surface = self.render_line(self.text)
58 self.size = self.surface.get_rect().size
60 def draw(self, surface):
61 surface.blit(self.surface, self.pos)
64 class TextBoxWidget(TextWidget):
65 def __init__(self, *args, **kwargs):
66 self.padding = kwargs.pop('padding', 4)
67 self.border = kwargs.pop('border', 2)
68 self.bg_colour = convert_colour(kwargs.pop('bg_colour',
69 PALETTE.LIGHT_VIOLET))
70 self.border_colour = convert_colour(kwargs.pop('border_colour',
72 self.box_width = kwargs.pop('box_width', 0)
74 super(TextBoxWidget, self).__init__(*args, **kwargs)
76 def lines(self, image_map):
77 if self.box_width != 0:
78 return self._wrapped_lines(image_map)
80 return self.text.splitlines()
82 def _prepare_glyph(self, image_map, glyph, current_words, lines):
83 size = self.font.size(' '.join(current_words[:-1] + ['']))
84 x = size[0] * EIGHT_BIT_SCALE + self.padding
85 y = size[1] * lines * EIGHT_BIT_SCALE + self.padding
86 for glyph_key in glyph.glyph_keys:
87 image_name, colour = MARKUP_MAP[glyph_key]
90 image = resources.get_image(
91 image_name, transforms=(EIGHT_BIT, blender(colour)))
92 image_map[(x, y)] = image
93 x += image.get_width()
95 def _check_markup(self, word):
101 if word[0] == '{' and word[-1] == '}':
102 subwords = word[1:-1].split(',')
103 if all(subword in MARKUP_MAP for subword in subwords):
104 return Glyph(word + suffix, subwords, suffix)
108 def _wrapped_lines(self, image_map):
109 def words_fit(words):
110 words_line = ' '.join(words)
111 width = self.font.size(words_line)[0]
112 if width < self.box_width:
114 elif len(words) == 1:
115 Exception("Word %r too long for box." % (words[0],))
119 for line in self.text.splitlines():
121 remaining_words = line.split()
122 while remaining_words:
123 word = remaining_words.pop(0)
124 glyph = self._check_markup(word)
125 if glyph is not None:
127 current_words.append(word)
128 if words_fit(current_words):
129 if glyph is not None:
131 image_map, glyph, current_words, line_count)
134 yield ' '.join(current_words[:-1])
136 if glyph is not None:
137 word = glyph.markup_text
138 remaining_words.insert(0, word)
139 if current_words and words_fit(current_words):
140 yield ' '.join(current_words)
143 self.font = resources.get_font(self.fontname, self.fontsize)
146 width, height = self.padding * 2, self.padding * 2
147 for line in self.lines(image_map):
148 line_surface = self.render_line(line)
149 line_rect = line_surface.get_rect()
150 rendered_lines.append(line_surface)
151 width = max(width, line_rect.width + self.padding * 2)
152 height += line_rect.height
154 self.surface = pygame.surface.Surface((width, height),
155 pygame.locals.SRCALPHA)
156 self.surface.fill(self.bg_colour)
157 self.size = self.surface.get_rect().size
159 x, y = self.padding, self.padding
160 for line_surface in rendered_lines:
161 self.surface.blit(line_surface, (x, y))
162 y += line_surface.get_rect().height
163 for pos, img in image_map.items():
164 self.surface.blit(img, pos)
166 def draw(self, surface):
167 surface.blit(self.surface, self.rect)