+ if self.view_port is None:
+ rect = self.rect
+ area = None
+ else:
+ rect = self.pos
+ area = self.view_port
+ surface.blit(self.surface, rect, area)
+ if self.view_port is not None:
+ self.draw_arrows(surface)
+
+ def draw_arrows(self, surface):
+ if self.view_port.top > 0:
+ up = resources.get_image('bits', 'arrow_on.png',
+ transforms=(EIGHT_BIT,))
+ icon_size = up.get_rect().height
+ pos = (self.pos[0] + self.view_port.width - icon_size, self.pos[1])
+ surface.blit(up, pos)
+ if self.view_port.bottom < self.surface.get_rect().bottom:
+ down = resources.get_image('bits', 'arrow_on.png',
+ transforms=(R180, EIGHT_BIT))
+ icon_size = down.get_rect().height
+ pos = (self.pos[0] + self.view_port.width - icon_size,
+ self.pos[1] + self.view_port.height - icon_size)
+ surface.blit(down, pos)
+
+
+class TextBoxWidget(TextWidget):
+ def __init__(self, *args, **kwargs):
+ self.bg_colour = convert_colour(kwargs.pop('bg_colour',
+ PALETTE.LIGHT_VIOLET))
+ self.box_width = kwargs.pop('box_width', 0)
+ self.full_width = kwargs.pop('full_width', True)
+ kwargs.setdefault('padding', 4)
+
+ super(TextBoxWidget, self).__init__(*args, **kwargs)
+
+ def lines(self, image_map):
+ if self.box_width != 0:
+ return self._wrapped_lines(image_map)
+ 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 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):
+ return Glyph(word + suffix, subwords, suffix)
+
+ return None
+
+ def _wrapped_lines(self, image_map):
+ def words_fit(words):
+ words_line = ' '.join(words)
+ width = self.font.size(words_line)[0] * EIGHT_BIT_SCALE
+ if width < self.box_width:
+ return True
+ elif len(words) == 1:
+ Exception("Word %r too long for box." % (words[0],))
+ return False
+
+ line_count = 0
+ for line in self.text.splitlines():
+ line = line.strip()
+ if not line:
+ line_count += 1
+ yield line
+ continue
+ current_words = []
+ 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 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):
+ line_count += 1
+ yield ' '.join(current_words)
+
+ 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):
+ 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
+
+ if self.full_width:
+ width = max(width, self.box_width)
+
+ self.surface = pygame.surface.Surface((width, height),
+ pygame.locals.SRCALPHA)
+ self.surface.fill(self.bg_colour)
+ self.update_size()
+
+ x, y = self.padding, self.padding
+ for line_surface in rendered_lines:
+ if self.centre:
+ x = (width - line_surface.get_rect().width) / 2
+ 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)