sep-pm-platformer/project/ui_elements/TextLabel.py

77 lines
2.6 KiB
Python
Raw Normal View History

2023-03-25 15:41:32 +01:00
import pygame
2023-03-25 18:11:41 +01:00
from pygame import Surface, Rect
2023-03-25 15:41:32 +01:00
from pygame.font import Font
from physics.TickData import TickData
2023-03-26 11:11:10 +02:00
from sprite.BoundingBox import BoundingBox
2023-03-25 15:41:32 +01:00
from sprite.PositionScale import PositionScale
2023-03-26 09:51:11 +02:00
from ui_elements.UiElement import UiElement
2023-03-25 15:41:32 +01:00
2023-03-26 09:51:11 +02:00
class TextLabel(UiElement):
def tick(self, tick_data: TickData):
2023-03-26 09:51:11 +02:00
pass
def __init__(self, text: str, x_position: float, y_position: float, font_size: int, alignment: str = "left"):
super().__init__()
2023-03-25 15:41:32 +01:00
self.text = text
2023-03-26 09:51:11 +02:00
2023-03-25 15:41:32 +01:00
self.x_position = x_position
self.y_position = y_position
self.current_width = 0
self.current_height = 0
2023-03-26 09:51:11 +02:00
self.alignment = alignment
2023-03-25 15:41:32 +01:00
self.font_size = font_size
2023-03-25 18:11:41 +01:00
self.font = Font('data/font/MilkyNice.ttf', self.font_size)
2023-03-26 09:51:11 +02:00
def render_sprite_image(self) -> pygame.Surface:
return self.font.render(str(self.text), True, pygame.Color('white'))
2023-03-25 15:41:32 +01:00
2023-03-25 16:18:44 +01:00
def draw(self, screen: Surface, screen_transform: PositionScale):
2023-03-25 18:11:41 +01:00
rendered_font = self.font.render(str(self.text), True, pygame.Color('white'))
2023-03-25 15:41:32 +01:00
self.current_width = rendered_font.get_width()
self.current_height = rendered_font.get_height()
if self.alignment == "right":
2023-03-26 09:51:11 +02:00
self.position_scale.position = (self.x_position - self.current_width, self.y_position)
2023-03-25 18:11:41 +01:00
elif self.alignment == "left":
2023-03-26 09:51:11 +02:00
self.position_scale.position = (self.x_position, self.y_position)
2023-03-25 15:41:32 +01:00
elif self.alignment == "center":
2023-03-26 09:51:11 +02:00
self.position_scale.position = (self.x_position - self.current_width / 2, self.y_position)
2023-03-25 18:11:41 +01:00
2023-03-26 09:51:11 +02:00
super().draw(screen, screen_transform)
2023-03-25 15:41:32 +01:00
2023-03-26 09:51:11 +02:00
def set_text(self, new_text: str):
self.text = new_text
2023-03-26 11:11:10 +02:00
def collides_point(self, position):
x, y = self.get_bounding_box().get_position()
# check for the collision on the x-axis
if position[1] < y:
return False
if position[1] > y + self.current_height:
return False
# the y-axis check is dependent on the alignment
if position[0] < x:
return False
if position[0] > x + self.current_width:
return False
# if the point is not outside the text, it is inside the text
return True
def get_bounding_box(self) -> BoundingBox:
bounding_box_x = self.x_position
if self.alignment == "right":
bounding_box_x = self.x_position - self.current_width
elif self.alignment == "center":
bounding_box_x = self.x_position - self.current_width / 2
return BoundingBox(bounding_box_x, self.y_position, self.current_width, self.current_height)