TextLabel click listener

main
Stephan Halder 2023-03-26 11:11:10 +02:00
parent ec550683f8
commit 975948f068
1 changed files with 29 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import pygame
from pygame import Surface, Rect from pygame import Surface, Rect
from pygame.font import Font from pygame.font import Font
from sprite.BoundingBox import BoundingBox
from sprite.PositionScale import PositionScale from sprite.PositionScale import PositionScale
from ui_elements.UiElement import UiElement from ui_elements.UiElement import UiElement
@ -44,3 +45,31 @@ class TextLabel(UiElement):
def set_text(self, new_text: str): def set_text(self, new_text: str):
self.text = new_text self.text = new_text
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)