MARKUP_MAP = {
- 'NORTH': (1, 'glyphs/arrow_up.png'),
- 'SOUTH': (1, 'glyphs/arrow_down.png'),
- 'EAST': (1, 'glyphs/arrow_right.png'),
- 'WEST': (1, 'glyphs/arrow_left.png'),
+ '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),
+ 'RED': ('glyphs/key.png', PALETTE.ORANGE),
+ 'GREEN': ('glyphs/key.png', PALETTE.GREEN),
+ 'BLUE': ('glyphs/key.png', PALETTE.BLUE),
+ 'CLOCKWISE': ('glyphs/clockwise.png', None),
+ 'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None),
+
+ 'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
+ 'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
}
+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):
+ colour=None, unselectable_colour=None):
super(TextWidget, self).__init__(pos, size)
self.text = text
self.fontname = fontname or FONT
self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
self.colour = convert_colour(colour or PALETTE.BLACK)
+ if unselectable_colour is not None:
+ unselectable_colour = convert_colour(unselectable_colour)
+ self.unselectable_colour = unselectable_colour
def render_line(self, text):
- text_surf = self.font.render(text, True, self.colour)
+ colour = self.colour
+ if not self.is_selectable() and self.unselectable_colour is not None:
+ colour = self.unselectable_colour
+ text_surf = self.font.render(text, True, colour)
text_rect = text_surf.get_rect()
return pygame.transform.scale(
text_surf, (text_rect.width * EIGHT_BIT_SCALE,
self.size = self.surface.get_rect().size
def draw(self, surface):
- surface.blit(self.surface, self.pos)
+ surface.blit(self.surface, self.rect)
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',
PALETTE.LIGHT_VIOLET))
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)
+
+ return None
+
def _wrapped_lines(self, image_map):
def words_fit(words):
words_line = ' '.join(words)
line_count = 0
for line in self.text.splitlines():
current_words = []
- for word in line.split():
- 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
+ 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):
- 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)
- image_map[pos] = resources.get_image(
- markup_data[1],
- transforms=(EIGHT_BIT, blender(self.colour)))
- 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):
+ line_count += 1
yield ' '.join(current_words)
def prepare(self):
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)