From a50a8a391693f50590b7de9ed9c89c9a576af05c Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sat, 17 May 2014 23:28:39 +0200 Subject: [PATCH] Two by two, boxes of blue. --- naja/widgets/info_area.py | 16 ++++++---------- naja/widgets/text.py | 37 +++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/naja/widgets/info_area.py b/naja/widgets/info_area.py index 36b3755..e348048 100644 --- a/naja/widgets/info_area.py +++ b/naja/widgets/info_area.py @@ -54,13 +54,6 @@ class InfoAreaWidget(Widget): y_offset = 0 pos = lambda: (INFO_LEFT_PADDING, y_offset) - # Top title - title = TextWidget( - pos(), TITLES[self.state.gameboard.player_mode], - colour=PALETTE.WHITE) - title.render(self.surface) - y_offset += title.surface.get_rect().height - 4 - # Bits bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0' for bit in reversed(range(8))) @@ -68,9 +61,12 @@ class InfoAreaWidget(Widget): bits_text = '%s %s' % ( bits_text, bit_glyphs(self.card.bitwise_operand)) card_bits = TextBoxWidget( - pos(), bits_text, box_width=box_width, - colour=PALETTE.LIGHT_TURQUOISE, bg_colour=PALETTE.BLACK) - card_bits.render(self.surface) + (0, 0), bits_text, padding=4, centre=True, + colour=PALETTE.WHITE, border=2, + bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE, + box_width=box_width) + card_bits.prepare() + self.surface.blit(card_bits.surface, pos()) y_offset += card_bits.surface.get_rect().height + 4 # Actions diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 5dac4e6..b976f2a 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -149,24 +149,26 @@ 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 + y = 0 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, y), image)) x += image.get_width() + return glyphs def _check_markup(self, word): suffix = '' @@ -183,7 +185,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 @@ -198,9 +200,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) @@ -210,30 +212,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) @@ -250,7 +253,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) -- 2.34.1