X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fwidgets%2Ftext.py;h=6ca239d6fbdc08d9e467a718fcf86d955d4cf252;hb=74d3560d2f0eea96a596c137c644e70bc095fd85;hp=239e10a621b2a74bf9af8a1ad2320582a9308a09;hpb=0b4f46ac5262aed61cf3aeb994a1b55fa7823c7c;p=naja.git diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 239e10a..6ca239d 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -1,6 +1,9 @@ +import pygame + from naja.constants import FONT, FONT_SIZE from naja.widgets.base import Widget from naja.resources import resources +from naja.utils import convert_colour class TextWidget(Widget): @@ -10,14 +13,84 @@ class TextWidget(Widget): self.text = text self.fontname = fontname or FONT - self.fontsize = fontsize or FONT_SIZE - self.colour = colour or (0, 0, 0) + self.fontsize = (fontsize or FONT_SIZE) // 4 + self.colour = convert_colour(colour or (0, 0, 0)) + + def prepare(self): + self.font = resources.get_font(self.fontname, self.fontsize) + text = self.font.render(self.text, True, self.colour) + text_rect = text.get_rect() + self.surface = pygame.transform.scale(text, (text_rect.width * 4, + text_rect.height * 4)) + self.size = self.surface.get_rect().size + + def draw(self, surface): + surface.blit(self.surface, self.pos) + - def _prepare(self): +class TextBoxWidget(TextWidget): + def __init__(self, *args, **kwargs): + self.padding = kwargs.pop('padding', 5) + self.border = kwargs.pop('border', 2) + self.bg_colour = convert_colour(kwargs.pop('bg_colour', + (255, 255, 255, 192))) + self.border_colour = convert_colour(kwargs.pop('border_colour', + (0, 0, 0))) + self.box_width = kwargs.pop('box_width', 0) + + super(TextBoxWidget, self).__init__(*args, **kwargs) + + def lines(self): + if self.box_width != 0: + return self._wrapped_lines() + else: + return self.text.splitlines() + + def _wrapped_lines(self): + def words_fit(words): + words_line = ' '.join(words) + width = self.font.size(words_line)[0] + if width < self.box_width: + return True + elif len(words) == 1: + Exception("Word %r too long for box." % (words[0],)) + return False + + for line in self.text.splitlines(): + current_words = [] + for word in line.split(): + current_words.append(word) + if words_fit(current_words): + continue + else: + yield ' '.join(current_words[:-1]) + current_words = current_words[-1:] + if current_words and words_fit(current_words): + yield ' '.join(current_words) + + def prepare(self): self.font = resources.get_font(self.fontname, self.fontsize) - self.surface = self.font.render(self.text, True, self.colour) + rendered_lines = [] + width, height = self.padding, self.padding + for line in self.lines(): + line_surface = self.font.render(line, True, self.colour) + line_rect = line_surface.get_rect() + line_surface = pygame.transform.scale( + line_surface, (line_rect.width * 2, line_rect.height * 2)) + line_rect = line_surface.get_rect() + rendered_lines.append(line_surface) + width = max(width, line_rect.width) + height += line_rect.height + + self.surface = pygame.surface.Surface((width, height), + pygame.locals.SRCALPHA) + self.surface.fill(self.bg_colour) self.size = self.surface.get_rect().size + x, y = self.padding, self.padding + for line_surface in rendered_lines: + self.surface.blit(line_surface, (x, y)) + y += line_surface.get_rect().height + def draw(self, surface): - self.prepare() surface.blit(self.surface, self.rect)