X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fwidgets%2Ftext.py;h=899dacec7c095cea5a79540d2f71b75cd2f41232;hb=343dbbaa8126ed72ffec8cb4a3ff45050fd5d06a;hp=6ca239d6fbdc08d9e467a718fcf86d955d4cf252;hpb=d8daea1f8a67c1a547440b3934454da9792236ef;p=naja.git diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 6ca239d..899dace 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -1,27 +1,55 @@ import pygame -from naja.constants import FONT, FONT_SIZE -from naja.widgets.base import Widget +from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE from naja.resources import resources +from naja.resources.mutators import EIGHT_BIT, blender from naja.utils import convert_colour +from naja.widgets.base import Widget + + +MARKUP_MAP = { + 'NORTH': ('glyphs/arrow_up.png', None), + 'SOUTH': ('glyphs/arrow_down.png', None), + 'EAST': ('glyphs/arrow_right.png', None), + 'WEST': ('glyphs/arrow_left.png', None), + 'HEALTH': ('glyphs/health.png', PALETTE.DARK_RED), + 'WINTOKEN': ('glyphs/win.png', PALETTE.DARK_OLIVE), + 'KEY': ('glyphs/key.png', None), + 'MSB': ('glyphs/msb.png', None), + 'REDKEY': ('glyphs/key.png', PALETTE.ORANGE), + 'GREENKEY': ('glyphs/key.png', PALETTE.GREEN), + 'BLUEKEY': ('glyphs/key.png', PALETTE.BLUE), +} + + +class Glyph(object): + def __init__(self, markup_text, glyph_keys, suffix=''): + self.markup_text = markup_text + self.glyph_keys = glyph_keys + self.text = ' ' * len(self.glyph_keys) + suffix class TextWidget(Widget): + def __init__(self, pos, text, size=None, fontname=None, fontsize=None, colour=None): super(TextWidget, self).__init__(pos, size) self.text = text self.fontname = fontname or FONT - self.fontsize = (fontsize or FONT_SIZE) // 4 - self.colour = convert_colour(colour or (0, 0, 0)) + self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE + self.colour = convert_colour(colour or PALETTE.BLACK) + + def render_line(self, text): + text_surf = self.font.render(text, True, self.colour) + text_rect = text_surf.get_rect() + return pygame.transform.scale( + text_surf, (text_rect.width * EIGHT_BIT_SCALE, + text_rect.height * EIGHT_BIT_SCALE)) def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) - text = self.font.render(self.text, True, self.colour) - text_rect = text.get_rect() - self.surface = pygame.transform.scale(text, (text_rect.width * 4, - text_rect.height * 4)) + self.surface = self.render_line(self.text) self.size = self.surface.get_rect().size def draw(self, surface): @@ -30,23 +58,51 @@ class TextWidget(Widget): class TextBoxWidget(TextWidget): def __init__(self, *args, **kwargs): - self.padding = kwargs.pop('padding', 5) + self.padding = kwargs.pop('padding', 4) self.border = kwargs.pop('border', 2) self.bg_colour = convert_colour(kwargs.pop('bg_colour', - (255, 255, 255, 192))) + PALETTE.LIGHT_VIOLET)) self.border_colour = convert_colour(kwargs.pop('border_colour', - (0, 0, 0))) + PALETTE.BLACK)) self.box_width = kwargs.pop('box_width', 0) super(TextBoxWidget, self).__init__(*args, **kwargs) - def lines(self): + def lines(self, image_map): if self.box_width != 0: - return self._wrapped_lines() + return self._wrapped_lines(image_map) else: return self.text.splitlines() - def _wrapped_lines(self): + def _prepare_glyph(self, image_map, glyph, current_words, lines): + 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 + 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 + x += image.get_width() + + def _check_markup(self, word): + suffix = '' + if word[-1] in '.,': + suffix = word[-1] + word = word[:-1] + + if word[0] == '{' and word[-1] == '}': + subwords = word[1:-1].split(',') + if all(subword in MARKUP_MAP for subword in subwords): + return Glyph(word + suffix, subwords, suffix) + elif word in MARKUP_MAP: + return Glyph(word + suffix, [word], suffix) + + return None + + def _wrapped_lines(self, image_map): def words_fit(words): words_line = ' '.join(words) width = self.font.size(words_line)[0] @@ -56,30 +112,40 @@ class TextBoxWidget(TextWidget): Exception("Word %r too long for box." % (words[0],)) return False + line_count = 0 for line in self.text.splitlines(): current_words = [] - for word in line.split(): + remaining_words = line.split() + while remaining_words: + word = remaining_words.pop(0) + glyph = self._check_markup(word) + if glyph is not None: + word = glyph.text current_words.append(word) if words_fit(current_words): - continue + if glyph is not None: + self._prepare_glyph( + image_map, glyph, current_words, line_count) else: + line_count += 1 yield ' '.join(current_words[:-1]) - current_words = current_words[-1:] + current_words = [] + if glyph is not None: + word = glyph.markup_text + remaining_words.insert(0, word) if current_words and words_fit(current_words): yield ' '.join(current_words) def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) + image_map = {} rendered_lines = [] - width, height = self.padding, self.padding - for line in self.lines(): - line_surface = self.font.render(line, True, self.colour) - line_rect = line_surface.get_rect() - line_surface = pygame.transform.scale( - line_surface, (line_rect.width * 2, line_rect.height * 2)) + width, height = self.padding * 2, self.padding * 2 + for line in self.lines(image_map): + line_surface = self.render_line(line) line_rect = line_surface.get_rect() rendered_lines.append(line_surface) - width = max(width, line_rect.width) + width = max(width, line_rect.width + self.padding * 2) height += line_rect.height self.surface = pygame.surface.Surface((width, height), @@ -91,6 +157,8 @@ class TextBoxWidget(TextWidget): for line_surface in rendered_lines: 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) def draw(self, surface): surface.blit(self.surface, self.rect)