Source

Fat x Fast / FatxFast / gui / textwidget.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"


from FatxFast.gui.widget import Widget
from FatxFast.resource import resource_manager, get_color
import pygame

class TextWidget(Widget):
    
    def __init__(self, **kwargs):
        super(TextWidget, self).__init__(**kwargs)
        self.fontname = kwargs.get("font", "WendyOne-Regular.ttf")
        self.font = resource_manager.load_font(
            self.fontname, kwargs.get("size", 28))
        self.fontcolor = kwargs.get("color")
        self.font_bgcolor = get_color(kwargs.get("font_bgcolor"))
        self._height = kwargs.get("height")
        self._width = kwargs.get("width")
        self.align = kwargs.get("align")
        self.wrap = kwargs.get("wrap")
        self.image = None
        self.mask_color = None
        self._linebuffer = []
        self._text = ""
        self.text = kwargs.get("text", "")

    @property
    def fontcolor(self):
        return self._fontcolor

    @fontcolor.setter
    def fontcolor(self, value):
        self._fontcolor = get_color(value)

    def get_center(self, maxwidth, linesurface):
        return int((maxwidth-linesurface[0].get_width())/2)
    
    def get_linesurfaces(self, *params):
        return [
            [self.font.render(line.strip(), 1, *params),
                [0, index*self.font.get_height()]]
            for index, line in enumerate(self._linebuffer)]
            
    @property
    def height(self):
        if self._height is None:
            height = self.font.get_height()*len(self._linebuffer)
        else:
            height = self._height
        return height

    def _merge_linesurfaces(self, linesurfaces, flags):
        maxwidth = self.width
        surface = pygame.Surface((maxwidth, self.height), flags)
        for linesurf in linesurfaces:
            if self.align == 'center':
                linesurf[1][0] = self.get_center(maxwidth, linesurf)
            surface.blit(linesurf[0], dest=linesurf[1])
        return surface

    def render(self):
        flags = 0
        args = [self.fontcolor]
        if self.get_focus() and self.mask_color:
            args[0] = self.mask_color
        
        if self.font_bgcolor is None:
            flags = pygame.SRCALPHA
        else:
            args.append(self.font_bgcolor)

        linesurfaces = self.get_linesurfaces(*args)
        self.image = self._merge_linesurfaces(linesurfaces, flags)
        self.rect.size = self.image.get_size()

    @property
    def text(self):
        return self._text

    @text.setter
    def text(self, value):
        if value != self.text:
            old_amount = len(self._linebuffer)
            self._text = value.encode("utf-8")
            self._linebuffer = value.splitlines()
            if self._text:
                self.render()
            if (old_amount != len(self._linebuffer) and self.auto_y_pos and
                    self.auto_size):
                self.container.fix_children_y_pos()

    @property
    def width(self):
        if self._width is None:
            return max([self.font.size(line)[0] for line in self._linebuffer])
        else:
            return self._width