Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/api/MultiLabel.py
blob: 3dee9dea4534504b788323ece64582cc6acd0ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-

import pygame
from api.Label import CLabel

font_path = 'assets/fonts/'

class CMultiLabel(CLabel):
    """ accepts a string separate with \n, creates a multi-line
        label to display text 
        same properties as label except textLines
        is a list of strings. There is no text
        property.
        Set the size manually. Vertical size should be at 
        least 30 pixels per line (with the default font)
    """
    
    def __init__(self, fontName = "DejaVuSans.ttf", transparent=False):
        CLabel.__init__(self, fontName, transparent)
        self.vSize = 35
        
    def set_vertical(self, aVer=35):
        self.vSize = aVer

    def _update_image(self):
        self.image = pygame.Surface(self.size)
        self.image.fill(self.bgColor)
        if self.transparent:
            self.image.set_colorkey(self.bgColor)
        textLines = self.text.split('\n')
        numLines = len(textLines)
        #self.vSize = self.image.get_height() / numLines
        total_text_height = self.vSize * numLines
        fix = (self.image.get_height() - total_text_height) / 2
        for lineNum in range(numLines):
            currentLine = textLines[lineNum]
            if self.transparent:
                fontSurface = self.font.render(currentLine, True, self.fgColor)
            else:
                fontSurface = self.font.render(currentLine, True, self.fgColor, self.bgColor)
            #center the text
            xPos = (self.image.get_width() - fontSurface.get_width())/2
            yPos = lineNum * self.vSize + fix
            self.image.blit(fontSurface, (xPos, yPos))
        self.update()