create correct bounding boxes for text label

main
Mike 2023-03-28 12:30:12 +02:00
parent 37e786eb30
commit 09a2489703
3 changed files with 29 additions and 13 deletions

View File

@ -9,10 +9,9 @@ from physics.SpriteManager import SpriteManager, DrawLayers
from physics.TickData import TickData
from physics.sprites.PlayerSprite import PlayerSprite
from sprite.PositionScale import PositionScale
from sprite.SpritesheetManager import SpritesheetManager
from sprite.Sprite import Sprite
from sprite.SpritesheetManager import SpritesheetManager
from sprite.StaticSprite import StaticSprite
from ui_elements import CoordinateTransform
from ui_elements.ClickEvent import ClickEvent
from ui_elements.KeyManager import KeyManager
from ui_elements.LevelMenu import LevelMenu
@ -60,6 +59,9 @@ if what_to_run == 'menu':
for level_text_label in level_menu.level_text_labels:
sprite_manager.add_ui_element(DrawLayers.UI, level_text_label)
# for testing
last_click_pos = (0, 0)
while True:
clock.tick(frame_rate)
@ -70,7 +72,8 @@ if what_to_run == 'menu':
for event in click_events:
for layer in sprite_manager.layers:
for sprite in sprite_manager.layers[layer]:
if sprite.get_bounding_box().contains_point(event.screen_position):
last_click_pos = event.world_position
if sprite.get_bounding_box().contains_point(last_click_pos):
sprite.click(event)
for event in pygame_events:
@ -84,6 +87,19 @@ if what_to_run == 'menu':
# level_menu.draw(screen, screen_transform)
sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform))
sprite_manager.draw(screen, screen_transform)
# print bounding boxes of menu labels for testing
# for text_label in level_menu.level_text_labels:
# bound_box = text_label.get_bounding_box()
# # transformed_pos = transform_world_to_screen((bound_box.x, bound_box.y), screen_transform)
# # rect = pygame.Rect(transformed_pos[0], transformed_pos[1], bound_box.width, bound_box.height)
# rect = pygame.Rect(bound_box.x, bound_box.y, bound_box.width, bound_box.height)
# # if text_label.text == 'level-01':
# pygame.draw.rect(screen, (255, 0, 0), rect, 1)
#
# rect = pygame.Rect(last_click_pos[0], last_click_pos[1], 5, 5)
# pygame.draw.rect(screen, (0, 255, 0), rect, 1)
pygame.display.update()
elif what_to_run == 'level':

View File

@ -28,8 +28,10 @@ class LevelMenu:
for index, level in enumerate(self.levels):
text_label = TextLabel(level.name, current_text_label_x, current_text_label_y, 60)
text_label.position_scale.scale = (0.5, 0.5)
# text_label = TextLabel(self.extract_level(level.name), current_text_label_x, current_text_label_y, 35)
text_label.add_click_listener(lambda click_event: self.handle_level_text_label_click(click_event, level))
text_label.add_click_listener(
lambda click_event, selected_level=level:
self.handle_level_text_label_click(click_event, selected_level)
)
self.level_text_labels.append(text_label)
if (index + 1) % self.column_count == 0:

View File

@ -48,11 +48,9 @@ class TextLabel(UiElement):
self.text = new_text
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
self.bounding_box = BoundingBox(bounding_box_x, self.y_position, self.current_width, self.current_height)
return self.bounding_box
return BoundingBox(
self.position_scale.position[0],
self.position_scale.position[1],
self.current_width * self.position_scale.scale[0],
self.current_height * self.position_scale.scale[1]
)