Mercurial > nagslang
changeset 395:450081926426
Better MultiLineWidget
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 07 Sep 2013 01:03:55 +0200 |
parents | 76f053cf2322 |
children | b3df20c76afd |
files | nagslang/widgets/text.py |
diffstat | 1 files changed, 24 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/nagslang/widgets/text.py Sat Sep 07 01:10:53 2013 +0200 +++ b/nagslang/widgets/text.py Sat Sep 07 01:03:55 2013 +0200 @@ -31,26 +31,6 @@ surface.blit(self.surface, self.rect) -class MultiLineWidget(TextWidget): - - def prepare(self): - self.font = resources.get_font(self.fontname, self.fontsize) - surfaces = [] - height = 0 - width = 0 - for line in self.text.split('\n'): - surface = self.font.render(line, True, self.colour) - width = max(width, surface.get_rect().width) - height += surface.get_rect().height - surfaces.append(surface) - self.surface = pygame.surface.Surface((width, height)) - self.surface.fill(pygame.Color('white')) - y = 0 - for surface in surfaces: - self.surface.blit(surface, (0, y)) - y += surface.get_rect().height - - class LabelWidget(TextWidget): def __init__(self, *args, **kwargs): self.padding = kwargs.pop('padding', 5) @@ -74,3 +54,27 @@ pygame.draw.rect(surface, self.border_colour, surface.get_rect(), self.border) self.surface = surface + + +class MultiLineWidget(LabelWidget): + + def prepare(self): + self.font = resources.get_font(self.fontname, self.fontsize) + surfaces = [] + height = 0 + width = 0 + for line in self.text.split('\n'): + surface = self.font.render(line, True, self.colour) + width = max(width, surface.get_rect().width) + height += surface.get_rect().height + surfaces.append(surface) + self.surface = pygame.surface.Surface((width, height), + pygame.locals.SRCALPHA) + self.surface.fill(self.bg_colour) + y = 0 + for surface in surfaces: + self.surface.blit(surface, (0, y)) + y += surface.get_rect().height + self.text_rect = self.surface.get_rect() + if not self.size: + self.rect.size = self.text_rect.size