working TextLabel
parent
e3744241ea
commit
c5ce98b255
Binary file not shown.
|
@ -1,12 +1,12 @@
|
|||
import pygame
|
||||
from pygame import Surface
|
||||
from pygame import Surface, Rect
|
||||
from pygame.font import Font
|
||||
|
||||
from sprite.PositionScale import PositionScale
|
||||
|
||||
|
||||
class TextLabel:
|
||||
def __init__(self, text: str, x_position: float, y_position: float, font_size: float, font: Font,
|
||||
def __init__(self, text: str, x_position: float, y_position: float, font_size: int,
|
||||
alignment: str = "left"):
|
||||
self.text = text
|
||||
self.x_position = x_position
|
||||
|
@ -15,20 +15,33 @@ class TextLabel:
|
|||
self.current_width = 0
|
||||
self.current_height = 0
|
||||
self.font_size = font_size
|
||||
self.font = font
|
||||
self.font = Font('data/font/MilkyNice.ttf', self.font_size)
|
||||
self.position_scale = PositionScale()
|
||||
|
||||
def draw(self, screen: Surface, screen_transform: PositionScale):
|
||||
rendered_font = self.font.render(str(self.text), True, (255, 255, 255))
|
||||
rendered_font = self.font.render(str(self.text), True, pygame.Color('white'))
|
||||
|
||||
self.current_width = rendered_font.get_width()
|
||||
self.current_height = rendered_font.get_height()
|
||||
|
||||
if self.alignment == "right":
|
||||
screen.blit(rendered_font, (self.x_position - self.current_width / 2, self.y_position))
|
||||
elif self.alignment == "right":
|
||||
screen.blit(rendered_font, (self.x_position - self.current_width, self.y_position))
|
||||
elif self.alignment == "left":
|
||||
screen.blit(rendered_font, (self.x_position, self.y_position))
|
||||
elif self.alignment == "center":
|
||||
screen.blit(rendered_font, (self.x_position - self.current_width, self.y_position))
|
||||
screen.blit(rendered_font, (self.x_position - self.current_width / 2, self.y_position))
|
||||
|
||||
def set_text(self, new_text: str):
|
||||
self.text = new_text
|
||||
target_scale = (screen_transform.scale[0] * self.position_scale.scale[0],
|
||||
screen_transform.scale[1] * self.position_scale.scale[1])
|
||||
|
||||
target_position = ((self.position_scale.position[0] + screen_transform.position[0]) * target_scale[0],
|
||||
(self.position_scale.position[1] + screen_transform.position[1]) * target_scale[1])
|
||||
|
||||
target_size = (int(target_scale[0] * self.x_position),
|
||||
int(target_scale[1] * self.y_position))
|
||||
|
||||
target_image = pygame.transform.scale(rendered_font, target_size)
|
||||
|
||||
|
||||
def set_text(self, new_text: str):
|
||||
self.text = new_text
|
||||
|
|
Loading…
Reference in New Issue