diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-20 11:12:51 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-20 11:12:51 +0100 |
commit | 5c65e3e5662e261311a2a544d00a9eabbf632198 (patch) | |
tree | 9e9a71f6d2ab2e4f9e9ccfe101ad5256f9dde33c | |
parent | 44741315c6d2df9bd6591c19ea189163e5c70add (diff) | |
parent | 31ffbbbab541760b0574c6e6819a99575c1ce8b1 (diff) |
Merge branch 'wouter/relative_ui' of github.com:lonkaars/crepe
-rw-r--r-- | src/crepe/api/UIObject.h | 2 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 66 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 11 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 64 |
4 files changed, 102 insertions, 41 deletions
diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index f7f4fba..f1318ab 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -20,6 +20,8 @@ public: vec2 dimensions; //! Position offset relative to this GameObjects Transform vec2 offset; + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; }; } // namespace crepe diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index d209282..858a645 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -12,7 +12,7 @@ void InputSystem::fixed_update() { ComponentManager & mgr = this->mediator.component_manager; SDLContext & context = this->mediator.sdl_context; std::vector<EventData> event_list = context.get_events(); - RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); + const RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); OptionalRef<Camera> curr_cam_ref; // Find the active camera @@ -24,9 +24,8 @@ void InputSystem::fixed_update() { if (!curr_cam_ref) return; Camera & current_cam = curr_cam_ref; - RefVector<Transform> transform_vec - = mgr.get_components_by_id<Transform>(current_cam.game_object_id); - Transform & cam_transform = transform_vec.front().get(); + const Transform & cam_transform + = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front(); vec2 camera_origin = cam_transform.position + current_cam.data.postion_offset - (current_cam.viewport_size / 2); @@ -50,7 +49,7 @@ void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camer EventManager & event_mgr = this->mediator.event_manager; vec2 adjusted_mouse; adjusted_mouse.x = event.data.mouse_data.mouse_position.x + camera_origin.x; - adjusted_mouse.x = event.data.mouse_data.mouse_position.y + camera_origin.y; + adjusted_mouse.y = event.data.mouse_data.mouse_position.y + camera_origin.y; // Check if the mouse is within the viewport if ((adjusted_mouse.x < camera_origin.x || adjusted_mouse.x > camera_origin.x + current_cam.viewport_size.x @@ -83,7 +82,8 @@ void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camer .mouse_pos = adjusted_mouse, .button = event.data.mouse_data.mouse_button, }); - this->handle_click(event.data.mouse_data.mouse_button, adjusted_mouse); + this->handle_click(event.data.mouse_data.mouse_button, adjusted_mouse, + current_cam); } break; } @@ -93,7 +93,7 @@ void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camer .mouse_pos = adjusted_mouse, .mouse_delta = event.data.mouse_data.rel_mouse_move, }); - this->handle_move(event, adjusted_mouse); + this->handle_move(event, adjusted_mouse, current_cam); break; case EventType::MOUSE_WHEEL: @@ -151,19 +151,24 @@ void InputSystem::handle_non_mouse_event(const EventData & event) { } } -void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos) { +void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos, + const Camera & current_cam) { ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; - RefVector<Button> buttons = mgr.get_components_by_type<Button>(); + const RefVector<Button> buttons = mgr.get_components_by_type<Button>(); for (Button & button : buttons) { if (!button.active) continue; - Metadata & metadata - = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); - Transform & transform + + const Transform & transform = mgr.get_components_by_id<Transform>(button.game_object_id).front(); + const Transform & cam_transform + = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front(); + const Metadata & metadata + = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); bool was_hovering = button.hover; - if (this->is_mouse_inside_button(mouse_pos, button, transform)) { + + if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) { button.hover = true; if (!was_hovering) { event_mgr.trigger_event<ButtonEnterEvent>(metadata); @@ -177,33 +182,36 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p } } -void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos) { +void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos, + const Camera & current_cam) { ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; - RefVector<Button> buttons = mgr.get_components_by_type<Button>(); - + const RefVector<Button> buttons = mgr.get_components_by_type<Button>(); + const Transform & cam_transform + = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front(); for (Button & button : buttons) { if (!button.active) continue; - Metadata & metadata + const Metadata & metadata = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); - Transform & transform + const Transform & transform = mgr.get_components_by_id<Transform>(button.game_object_id).front(); - - if (this->is_mouse_inside_button(mouse_pos, button, transform)) { + if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) { event_mgr.trigger_event<ButtonPressEvent>(metadata); } } } bool InputSystem::is_mouse_inside_button(const vec2 & mouse_pos, const Button & button, - const Transform & transform) { - int actual_x = transform.position.x + button.offset.x; - int actual_y = transform.position.y + button.offset.y; - - int half_width = button.dimensions.x / 2; - int half_height = button.dimensions.y / 2; + const Transform & transform, + const Transform & cam_transform) { + vec2 actual_pos = transform.position + button.offset; + if (!button.world_space) { + actual_pos += cam_transform.position; + } + vec2 half_dimensions = button.dimensions / 2; - // Check if the mouse is within the button's boundaries - return mouse_pos.x >= actual_x - half_width && mouse_pos.x <= actual_x + half_width - && mouse_pos.y >= actual_y - half_height && mouse_pos.y <= actual_y + half_height; + return mouse_pos.x >= actual_pos.x - half_dimensions.x + && mouse_pos.x <= actual_pos.x + half_dimensions.x + && mouse_pos.y >= actual_pos.y - half_dimensions.y + && mouse_pos.y <= actual_pos.y + half_dimensions.y; } diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 0f1bfa1..2cb80e5 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -94,20 +94,24 @@ private: * \param mouse_button The mouse button involved in the click. * \param world_mouse_x The X coordinate of the mouse in world space. * \param world_mouse_y The Y coordinate of the mouse in world space. + * \param current_cam The current active camera. * * This method processes the mouse click event and triggers the corresponding button action. */ - void handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos); + void handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos, + const Camera & current_cam); /** * \brief Handles the mouse movement event. * \param event_data The event data containing information about the mouse movement. * \param world_mouse_x The X coordinate of the mouse in world space. * \param world_mouse_y The Y coordinate of the mouse in world space. + * \param current_cam The current active camera. * * This method processes the mouse movement event and updates the button hover state. */ - void handle_move(const EventData & event_data, const vec2 & mouse_pos); + void handle_move(const EventData & event_data, const vec2 & mouse_pos, + const Camera & current_cam); /** * \brief Checks if the mouse position is inside the bounds of the button. @@ -115,10 +119,11 @@ private: * \param world_mouse_y The Y coordinate of the mouse in world space. * \param button The button to check. * \param transform The transform component of the button. + * \param cam_transform the transform of the current active camera * \return True if the mouse is inside the button, false otherwise. */ bool is_mouse_inside_button(const vec2 & mouse_pos, const Button & button, - const Transform & transform); + const Transform & transform, const Transform & cam_transform); /** * \brief Handles the button press event, calling the on_click callback if necessary. diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index f5ebaf0..86afad9 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -36,10 +36,12 @@ public: RenderSystem render{mediator}; EventManager event_manager{mediator}; //GameObject camera; + int offset_x = 100; + int offset_y = 200; protected: void SetUp() override { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + GameObject obj = mgr.new_object("camera", "camera", vec2{offset_x, offset_y}, 0, 1); auto & camera = obj.add_component<Camera>(ivec2{500, 500}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); @@ -75,8 +77,8 @@ TEST_F(InputTest, MouseDown) { EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) { mouse_triggered = true; //middle of the screen = 0,0 - EXPECT_EQ(event.mouse_pos.x, 0); - EXPECT_EQ(event.mouse_pos.y, 0); + EXPECT_EQ(event.mouse_pos.x, offset_x); + EXPECT_EQ(event.mouse_pos.y, offset_y); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); return false; }; @@ -100,8 +102,8 @@ TEST_F(InputTest, MouseUp) { bool function_triggered = false; EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_pos.x, 0); - EXPECT_EQ(e.mouse_pos.y, 0); + EXPECT_EQ(e.mouse_pos.x, offset_x); + EXPECT_EQ(e.mouse_pos.y, offset_y); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; @@ -124,8 +126,8 @@ TEST_F(InputTest, MouseMove) { bool function_triggered = false; EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_pos.x, 0); - EXPECT_EQ(e.mouse_pos.y, 0); + EXPECT_EQ(e.mouse_pos.x, offset_x); + EXPECT_EQ(e.mouse_pos.y, offset_y); EXPECT_EQ(e.mouse_delta.x, 10); EXPECT_EQ(e.mouse_delta.y, 10); return false; @@ -198,8 +200,8 @@ TEST_F(InputTest, MouseClick) { EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) { on_click_triggered = true; EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - EXPECT_EQ(event.mouse_pos.x, 0); - EXPECT_EQ(event.mouse_pos.y, 0); + EXPECT_EQ(event.mouse_pos.x, offset_x); + EXPECT_EQ(event.mouse_pos.y, offset_y); return false; }; event_manager.subscribe<MouseClickEvent>(on_mouse_click); @@ -232,7 +234,51 @@ TEST_F(InputTest, testButtonClick) { event_manager.dispatch_events(); EXPECT_TRUE(button_clicked); } +TEST_F(InputTest, buttonPositionCamera) { + GameObject button_obj = mgr.new_object("body", "person", vec2{50, 50}, 0, 1); + bool button_clicked = false; + event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { + button_clicked = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2{10, 10}, vec2{0, 0}); + + button.world_space = false; + bool hover = false; + button.active = true; + this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(button_clicked); +} +TEST_F(InputTest, buttonPositionWorld) { + GameObject button_obj = mgr.new_object("body", "person", vec2{50, 50}, 0, 1); + bool button_clicked = false; + event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { + button_clicked = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2{10, 10}, vec2{0, 0}); + button.world_space = true; + bool hover = false; + button.active = true; + this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); +} TEST_F(InputTest, testButtonHover) { GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_hover = false; |