From b3e10ee7a1a522f2145596ea97fb15bf7dc0eaa4 Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Fri, 16 May 2014 11:21:24 +0200 Subject: [PATCH] Multiple glyphs in a word. --- naja/actions.py | 2 +- naja/widgets/text.py | 89 ++++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/naja/actions.py b/naja/actions.py index 6c6f123..88f539f 100644 --- a/naja/actions.py +++ b/naja/actions.py @@ -90,7 +90,7 @@ class LoseHealthOrMSBAndSetBits(LocationAction): class AcquireWinToken(LocationAction): - TEXT = "Gain WINTOKEN, then clear all key bits." + TEXT = "Gain WINTOKEN, then clear {REDKEY,GREENKEY,BLUEKEY}." def perform_action(self, board, location): board.acquire_win_token() diff --git a/naja/widgets/text.py b/naja/widgets/text.py index ab8b53d..ca653e8 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -8,20 +8,27 @@ from naja.widgets.base import Widget MARKUP_MAP = { - 'NORTH': (1, 'glyphs/arrow_up.png', None), - 'SOUTH': (1, 'glyphs/arrow_down.png', None), - 'EAST': (1, 'glyphs/arrow_right.png', None), - 'WEST': (1, 'glyphs/arrow_left.png', None), - 'HEALTH': (1, 'glyphs/health.png', PALETTE.DARK_RED), - 'WINTOKEN': (1, 'glyphs/win.png', PALETTE.DARK_OLIVE), - 'KEY': (1, 'glyphs/key.png', None), - 'MSB': (1, 'glyphs/msb.png', None), - 'REDKEY': (1, 'glyphs/key.png', PALETTE.ORANGE), - 'GREENKEY': (1, 'glyphs/key.png', PALETTE.GREEN), - 'BLUEKEY': (1, 'glyphs/key.png', PALETTE.BLUE), + '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, @@ -67,6 +74,34 @@ class TextBoxWidget(TextWidget): else: return self.text.splitlines() + 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) @@ -82,34 +117,22 @@ class TextBoxWidget(TextWidget): current_words = [] remaining_words = line.split() while remaining_words: - word = remaining_words[0] - suffix = '' - if word[-1] in '.,': - suffix = word[-1] - word = word[:-1] - markup_data = MARKUP_MAP.get(word, None) - if markup_data is not None: - word = ' ' * markup_data[0] - word += suffix + 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): - if markup_data is not None: - size = self.font.size( - ' '.join(current_words[:-1] + [''])) - pos = (size[0] * EIGHT_BIT_SCALE, - size[1] * line_count * EIGHT_BIT_SCALE) - pos = (pos[0] + self.padding, pos[1] + self.padding) - colour = self.colour - if markup_data[2] is not None: - colour = markup_data[2] - image_map[pos] = resources.get_image( - markup_data[1], - transforms=(EIGHT_BIT, blender(colour))) - remaining_words.pop(0) + 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 = [] + 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) -- 2.34.1