Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/api/MultiLabel.py
blob: 956adf35508e968ee782d39d5fe0cd9e16b4decc (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-

from Sprite import CSprite
import pygame

class CMultiLabel(CSprite):
    """ 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, transparent=False):
        CSprite.__init__(self)
        self.text = 'This\nis\nsample\ntext'
        #self.font = pygame.font.Font("freesansbold.ttf", 20)

        self.font = pygame.font.Font('assets/fonts/DejaVuSans.ttf', 20)
        self.fgColor = ((0x00, 0x00, 0x00))
        self.bgColor = ((0xFF, 0xFF, 0xFF))
        self.transparent = transparent
        self.center = (0, 0)
        self.size = (300, 150)
        self.createImage()
    
    def set_center(self, aCenter):
        self.center = aCenter
        self.createImage()
        
    def set_size(self, aSize):
        self.size = aSize
        self.createImage()

    def set_text(self, aText):
        self.text = aText
        self.createImage()

    def createImage(self):
        self.image = pygame.Surface(self.size)
        self.image.fill(self.bgColor)
        if self.transparent:
            self.image.set_colorkey(self.bgColor)
        
        self.textLines = self.text.split('\n')
        numLines = len(self.textLines)
        vSize = self.image.get_height() / numLines
        
        for lineNum in range(numLines):
            currentLine = self.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 * vSize
            self.image.blit(fontSurface, (xPos, yPos))
        
        self.rect = self.image.get_rect()
        self.rect.center = self.center
        self.setImage(self.image)

    def update(self):
        self.rect = self.image.get_rect()
        self.rect.center = self.center