diff --git a/project/main.py b/project/main.py index 9abf09e..1dfb498 100644 --- a/project/main.py +++ b/project/main.py @@ -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': diff --git a/project/ui_elements/LevelMenu.py b/project/ui_elements/LevelMenu.py index ca8dd4d..92cdb9c 100644 --- a/project/ui_elements/LevelMenu.py +++ b/project/ui_elements/LevelMenu.py @@ -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: diff --git a/project/ui_elements/TextLabel.py b/project/ui_elements/TextLabel.py index bf047b5..f985f52 100644 --- a/project/ui_elements/TextLabel.py +++ b/project/ui_elements/TextLabel.py @@ -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] + )