X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fwidgets%2Ftext.py;h=f105e3bc1cb972dd6dde63534b1fd2e833d05928;hb=ad524ce884e9ca69f29dbf8efca089ef1c410a97;hp=1ddab05915122bf9c22bdbf033f78da57ea0a13f;hpb=75d1f94ef500df4bea81d8988757d37221a310dd;p=naja.git diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 1ddab05..f105e3b 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -1,8 +1,9 @@ import pygame +import pygame.locals as pgl -from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE +from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE, KEYS from naja.resources import resources -from naja.resources.mutators import EIGHT_BIT, blender +from naja.resources.mutators import EIGHT_BIT, R180, blender from naja.utils import convert_colour from naja.widgets.base import Widget @@ -12,6 +13,7 @@ MARKUP_MAP = { 'SOUTH': ('glyphs/arrow_down.png', None), 'EAST': ('glyphs/arrow_right.png', None), 'WEST': ('glyphs/arrow_left.png', None), + 'RETURN': ('glyphs/return.png', None), 'HEALTH': ('glyphs/health.png', PALETTE.DARK_RED), 'WINTOKEN': ('glyphs/win.png', PALETTE.DARK_OLIVE), 'KEY': ('glyphs/key.png', None), @@ -21,6 +23,9 @@ MARKUP_MAP = { 'BLUE': ('glyphs/key.png', PALETTE.BLUE), 'CLOCKWISE': ('glyphs/clockwise.png', None), 'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None), + 'SHIFT_LEFT': ('glyphs/shift_left.png', None), + 'SHIFT_RIGHT': ('glyphs/shift_right.png', None), + 'COUNTDOWN': ('glyphs/countdown_4.png', PALETTE.DARK_VIOLET), 'HEALTH_NOCOLOUR': ('glyphs/health.png', None), 'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None), @@ -36,9 +41,13 @@ class Glyph(object): class TextWidget(Widget): - def __init__(self, pos, text, size=None, fontname=None, fontsize=None, - colour=None, unselectable_colour=None): - super(TextWidget, self).__init__(pos, size) + VIEW_PORT_DY = 50 + + def __init__(self, pos, text, fontname=None, fontsize=None, + colour=None, unselectable_colour=None, view_port=None, + centre=False, border=0, border_colour=PALETTE.GREY, + padding=2): + super(TextWidget, self).__init__(pos) self.text = text self.fontname = fontname or FONT @@ -47,6 +56,13 @@ class TextWidget(Widget): if unselectable_colour is not None: unselectable_colour = convert_colour(unselectable_colour) self.unselectable_colour = unselectable_colour + self.view_port = ( + pygame.Rect((0, 0), view_port) if view_port is not None else None) + self.centre = centre + self.centre_pos = pos + self.border = border + self.border_colour = convert_colour(border_colour) + self.padding = padding def render_line(self, text): colour = self.colour @@ -58,52 +74,109 @@ class TextWidget(Widget): text_surf, (text_rect.width * EIGHT_BIT_SCALE, text_rect.height * EIGHT_BIT_SCALE)) + def render_border(self, surface): + if not self.border or not self.border_colour: + return + rect = surface.get_rect() + rect = rect.inflate(- self.border / 2, - self.border / 2) + pygame.draw.rect(surface, self.border_colour, rect, self.border) + + def update_size(self): + if self.view_port is not None: + self.size = self.view_port.size + else: + self.size = self.surface.get_rect().size + if self.centre: + self.pos = (self.centre_pos[0] - self.size[0] // 2, + self.centre_pos[1]) + def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) self.surface = self.render_line(self.text) - self.size = self.surface.get_rect().size + self.render_border(self.surface) + self.update_size() + + def handle_event(self, ev): + if self.view_port is None: + return super(TextWidget, self).handle_event(ev) + if ev.type == pgl.KEYDOWN: + if ev.key in KEYS.DOWN: + self.view_port.move_ip(0, self.VIEW_PORT_DY) + if self.view_port.bottom > self.surface.get_rect().bottom: + self.view_port.bottom = self.surface.get_rect().bottom + return True + elif ev.key in KEYS.UP: + self.view_port.move_ip(0, -self.VIEW_PORT_DY) + if self.view_port.top < 0: + self.view_port.top = 0 + return True + return super(TextWidget, self).handle_event(ev) def draw(self, surface): - surface.blit(self.surface, self.pos) + 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.padding = kwargs.pop('padding', 4) - self.border = kwargs.pop('border', 2) self.bg_colour = convert_colour(kwargs.pop('bg_colour', PALETTE.LIGHT_VIOLET)) - self.border_colour = convert_colour(kwargs.pop('border_colour', - PALETTE.BLACK)) 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): + 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 + x = size[0] * EIGHT_BIT_SCALE 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, 0), image)) x += image.get_width() + return glyphs def _check_markup(self, word): suffix = '' - if word[-1] in '.,': + 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): @@ -111,10 +184,10 @@ 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] + width = self.font.size(words_line)[0] * EIGHT_BIT_SCALE if width < self.box_width: return True elif len(words) == 1: @@ -123,7 +196,12 @@ class TextBoxWidget(TextWidget): line_count = 0 for line in self.text.splitlines(): - current_words = [] + line = line.strip() + if not line: + line_count += 1 + yield (line, []) + continue + current_words, glyphs = [], [] remaining_words = line.split() while remaining_words: word = remaining_words.pop(0) @@ -133,42 +211,46 @@ 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) self.surface = pygame.surface.Surface((width, height), pygame.locals.SRCALPHA) self.surface.fill(self.bg_colour) - self.size = self.surface.get_rect().size + 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) - def draw(self, surface): - surface.blit(self.surface, self.rect) + self.render_border(self.surface)