X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fwidgets%2Ftext.py;h=f105e3bc1cb972dd6dde63534b1fd2e833d05928;hb=ad524ce884e9ca69f29dbf8efca089ef1c410a97;hp=6501fd730e6dd3912f581282181ee0250f516a0d;hpb=c0cfbfaca703063c855486361b431209ac0463e1;p=naja.git diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 6501fd7..f105e3b 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -13,6 +13,7 @@ MARKUP_MAP = { 'SOUTH': ('glyphs/arrow_down.png', None), 'EAST': ('glyphs/arrow_right.png', None), 'WEST': ('glyphs/arrow_left.png', None), + 'RETURN': ('glyphs/return.png', None), 'HEALTH': ('glyphs/health.png', PALETTE.DARK_RED), 'WINTOKEN': ('glyphs/win.png', PALETTE.DARK_OLIVE), 'KEY': ('glyphs/key.png', None), @@ -24,6 +25,7 @@ MARKUP_MAP = { 'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None), 'SHIFT_LEFT': ('glyphs/shift_left.png', None), 'SHIFT_RIGHT': ('glyphs/shift_right.png', None), + 'COUNTDOWN': ('glyphs/countdown_4.png', PALETTE.DARK_VIOLET), 'HEALTH_NOCOLOUR': ('glyphs/health.png', None), 'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None), @@ -59,7 +61,7 @@ class TextWidget(Widget): self.centre = centre self.centre_pos = pos self.border = border - self.border_colour = border_colour + self.border_colour = convert_colour(border_colour) self.padding = padding def render_line(self, text): @@ -147,31 +149,34 @@ class TextBoxWidget(TextWidget): super(TextBoxWidget, self).__init__(*args, **kwargs) - def lines(self, image_map): + def lines(self): if self.box_width != 0: - return self._wrapped_lines(image_map) + return self._wrapped_lines() else: - return self.text.splitlines() + return ((line, []) for line in self.text.splitlines()) - def _prepare_glyph(self, image_map, glyph, current_words, lines): + def _prepare_glyph(self, glyph, current_words): + glyphs = [] size = self.font.size(' '.join(current_words[:-1] + [''])) - x = size[0] * EIGHT_BIT_SCALE + self.padding - y = size[1] * lines * EIGHT_BIT_SCALE + self.padding + x = size[0] * EIGHT_BIT_SCALE for glyph_key in glyph.glyph_keys: image_name, colour = MARKUP_MAP[glyph_key] if colour is None: colour = self.colour image = resources.get_image( image_name, transforms=(EIGHT_BIT, blender(colour))) - image_map[(x, y)] = image + glyphs.append(((x, 0), image)) x += image.get_width() + return glyphs def _check_markup(self, word): suffix = '' - if word[-1] in '.,': + if word[-1] in '.,:': suffix = word[-1] word = word[:-1] + if not word: + return None if word[0] == '{' and word[-1] == '}': subwords = word[1:-1].split(',') if all(subword in MARKUP_MAP for subword in subwords): @@ -179,7 +184,7 @@ class TextBoxWidget(TextWidget): return None - def _wrapped_lines(self, image_map): + def _wrapped_lines(self): def words_fit(words): words_line = ' '.join(words) width = self.font.size(words_line)[0] * EIGHT_BIT_SCALE @@ -194,9 +199,9 @@ class TextBoxWidget(TextWidget): line = line.strip() if not line: line_count += 1 - yield line + yield (line, []) continue - current_words = [] + current_words, glyphs = [], [] remaining_words = line.split() while remaining_words: word = remaining_words.pop(0) @@ -206,30 +211,31 @@ class TextBoxWidget(TextWidget): current_words.append(word) if words_fit(current_words): if glyph is not None: - self._prepare_glyph( - image_map, glyph, current_words, line_count) + glyphs.extend(self._prepare_glyph( + glyph, current_words)) else: line_count += 1 - yield ' '.join(current_words[:-1]) - current_words = [] + yield (' '.join(current_words[:-1]), glyphs) + current_words, glyphs = [], [] if glyph is not None: word = glyph.markup_text remaining_words.insert(0, word) if current_words and words_fit(current_words): line_count += 1 - yield ' '.join(current_words) + yield (' '.join(current_words), glyphs) def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) - image_map = {} rendered_lines = [] width, height = self.padding * 2, self.padding * 2 - for line in self.lines(image_map): + for line, glyphs in self.lines(): line_surface = self.render_line(line) line_rect = line_surface.get_rect() rendered_lines.append(line_surface) width = max(width, line_rect.width + self.padding * 2) height += line_rect.height + for pos, img in glyphs: + line_surface.blit(img, pos) if self.full_width: width = max(width, self.box_width) @@ -246,7 +252,5 @@ class TextBoxWidget(TextWidget): x += self.padding self.surface.blit(line_surface, (x, y)) y += line_surface.get_rect().height - for pos, img in image_map.items(): - self.surface.blit(img, pos) self.render_border(self.surface)