Ignore:
Timestamp:
Sep 6, 2013, 11:03:55 PM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
0a80fe5099fc7d3a5f6610596e524cc78cf4e0e1
Message:

Better MultiLineWidget?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/widgets/text.py

    r237 r395  
    3232
    3333
    34 class MultiLineWidget(TextWidget):
    35 
    36     def prepare(self):
    37         self.font = resources.get_font(self.fontname, self.fontsize)
    38         surfaces = []
    39         height = 0
    40         width = 0
    41         for line in self.text.split('\n'):
    42             surface = self.font.render(line, True, self.colour)
    43             width = max(width, surface.get_rect().width)
    44             height += surface.get_rect().height
    45             surfaces.append(surface)
    46         self.surface = pygame.surface.Surface((width, height))
    47         self.surface.fill(pygame.Color('white'))
    48         y = 0
    49         for surface in surfaces:
    50             self.surface.blit(surface, (0, y))
    51             y += surface.get_rect().height
    52 
    53 
    5434class LabelWidget(TextWidget):
    5535    def __init__(self, *args, **kwargs):
     
    7555                         self.border)
    7656        self.surface = surface
     57
     58
     59class MultiLineWidget(LabelWidget):
     60
     61    def prepare(self):
     62        self.font = resources.get_font(self.fontname, self.fontsize)
     63        surfaces = []
     64        height = 0
     65        width = 0
     66        for line in self.text.split('\n'):
     67            surface = self.font.render(line, True, self.colour)
     68            width = max(width, surface.get_rect().width)
     69            height += surface.get_rect().height
     70            surfaces.append(surface)
     71        self.surface = pygame.surface.Surface((width, height),
     72                                              pygame.locals.SRCALPHA)
     73        self.surface.fill(self.bg_colour)
     74        y = 0
     75        for surface in surfaces:
     76            self.surface.blit(surface, (0, y))
     77            y += surface.get_rect().height
     78        self.text_rect = self.surface.get_rect()
     79        if not self.size:
     80            self.rect.size = self.text_rect.size
Note: See TracChangeset for help on using the changeset viewer.