worktree-test/elements/Textbox.py

56 lines
1.6 KiB
Python

import pygame
class Textbox():
def __init__(self, size, x, y, font_size):
self.background = pygame.Color("grey")
self.surface = pygame.Surface( size )
self.surface.fill( self.background )
self.color = pygame.Color("White")
self.fontsize = 20
pygame.font.init()
self.font = pygame.font.Font(pygame.font.get_default_font(), font_size)
text = self.font.render("", True, self.color)
self.surface.blit( text, (0,0) )
self.x = x
self.y = y
def setText(self, text):
self.lines = text.split('\n')
self.nr_of_lines = len(self.lines)
def setTextColor(self, color):
try:
self.color = pygame.Color(color)
except:
self.color = pygame.Color("black")
def setBackgroundColor(self, color):
try:
self.background = pygame.Color(color)
except:
self.background = pygame.Color("grey")
def get(self):
return self.surface
def getX(self):
return self.x
def getY(self):
return self.y
def update(self, value):
self.surface.fill( self.background )
for lineNr, line in enumerate(self.lines):
text = self.font.render(line.format(value), True, self.color)
spacing = text.get_height()//10
x = self.surface.get_width()//2 - text.get_width()//2
height_total = self.nr_of_lines * (text.get_height() + spacing)
y = self.surface.get_height()//2 - height_total//2 + (lineNr * (text.get_height()+spacing))
self.surface.blit( text, (x,y) )