From 0c9d708d4d44b2de0bb8c8f59e7e6fb098eca272 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 3 Dec 2024 12:35:00 +0100 Subject: changed test for changes --- src/crepe/system/InputSystem.cpp | 14 ++++--- src/crepe/system/InputSystem.h | 6 +-- src/test/InputTest.cpp | 82 ++++++++++++++++++++++++++-------------- 3 files changed, 65 insertions(+), 37 deletions(-) diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 5b220eb..1cd3974 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -141,11 +141,15 @@ void InputSystem::handle_click(const MouseButton& mouse_button, const int& world } } -bool InputSystem::is_mouse_inside_button(const int& mouse_x, const int& mouse_y, const Button & button, const Transform & transform) { - return mouse_x >= transform.position.x - && mouse_x <= transform.position.x + button.width - && mouse_y >= transform.position.y - && mouse_y <= transform.position.y + button.height; +bool InputSystem::is_mouse_inside_button( + const int& mouse_x, const int& mouse_y, + const Button & button, const Transform & transform) { + int half_width = button.width / 2; + int half_height = button.height / 2; + return mouse_x >= transform.position.x - half_width + && mouse_x <= transform.position.x + half_width + && mouse_y >= transform.position.y - half_height + && mouse_y <= transform.position.y + half_height; } void InputSystem::handle_button_press(Button & button) { diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index e937d01..4244d91 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -64,13 +64,13 @@ private: /** * \brief Checks if the mouse position is inside the bounds of the button. - * \param mouse_x The X coordinate of the mouse. - * \param mouse_y The Y coordinate of the mouse. + * \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 button The button to check. * \param transform The transform component of the button. * \return True if the mouse is inside the button, false otherwise. */ - bool is_mouse_inside_button(const int& mouse_x, const int& mouse_y, const Button& button, const Transform& transform); + bool is_mouse_inside_button(const int& world_mouse_x, const int& world_mouse_y, const Button& button, const Transform& 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 9725836..790dbff 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -22,14 +22,13 @@ using namespace crepe; class InputTest : public ::testing::Test { public: ComponentManager mgr{}; - InputSystem input_system{mgr}; // Initializes the InputSystem with the ComponentManager + InputSystem input_system{mgr}; EventManager & event_manager = EventManager::get_instance(); - + //GameObject camera; protected: void SetUp() override { event_manager.clear(); - // GameObject camera = mgr.new_object(Color{0,0,0,0},ivec2{0,0},vec2{500,500},0,vec2{0,0}); } void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { @@ -51,15 +50,18 @@ protected: event.button.button = mouse_button; SDL_PushEvent(&event); } - GameObject camera; }; TEST_F(InputTest, MouseDown) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool mouse_triggered = false; EventHandler on_mouse_down = [&](const MousePressEvent & event) { mouse_triggered = true; - EXPECT_EQ(event.mouse_x, 10); - EXPECT_EQ(event.mouse_y, 10); + //middle of the screen = 0,0 + EXPECT_EQ(event.mouse_x, 0); + EXPECT_EQ(event.mouse_y, 0); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); return false; }; @@ -68,8 +70,9 @@ TEST_F(InputTest, MouseDown) { SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEBUTTONDOWN; - event.button.x = 10; - event.button.y = 10; + // middle of the screen of a 500*500 camera = 250*250 + event.button.x = 250; + event.button.y = 250; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); @@ -79,11 +82,14 @@ TEST_F(InputTest, MouseDown) { } TEST_F(InputTest, MouseUp) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool function_triggered = false; EventHandler on_mouse_release = [&](const MouseReleaseEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_x, 10); - EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.mouse_x, 0); + EXPECT_EQ(e.mouse_y, 0); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; @@ -92,8 +98,8 @@ TEST_F(InputTest, MouseUp) { SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEBUTTONUP; - event.button.x = 10; - event.button.y = 10; + event.button.x = 250; + event.button.y = 250; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); @@ -103,11 +109,14 @@ TEST_F(InputTest, MouseUp) { } TEST_F(InputTest, MouseMove) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool function_triggered = false; EventHandler on_mouse_move = [&](const MouseMoveEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_x, 10); - EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.mouse_x, 0); + EXPECT_EQ(e.mouse_y, 0); EXPECT_EQ(e.rel_x, 10); EXPECT_EQ(e.rel_y, 10); return false; @@ -117,8 +126,8 @@ TEST_F(InputTest, MouseMove) { SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEMOTION; - event.motion.x = 10; - event.motion.y = 10; + event.motion.x = 250; + event.motion.y = 250; event.motion.xrel = 10; event.motion.yrel = 10; SDL_PushEvent(&event); @@ -129,6 +138,9 @@ TEST_F(InputTest, MouseMove) { } TEST_F(InputTest, KeyDown) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool function_triggered = false; // Define event handler for KeyPressEvent @@ -156,6 +168,9 @@ TEST_F(InputTest, KeyDown) { } TEST_F(InputTest, KeyUp) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool function_triggered = false; EventHandler on_key_release = [&](const KeyReleaseEvent & event) { function_triggered = true; @@ -176,49 +191,58 @@ TEST_F(InputTest, KeyUp) { } TEST_F(InputTest, MouseClick) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; bool on_click_triggered = false; EventHandler on_mouse_click = [&](const MouseClickEvent & event) { on_click_triggered = true; EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - EXPECT_EQ(event.mouse_x, 10); - EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.mouse_x, 0); + EXPECT_EQ(event.mouse_y, 0); return false; }; event_manager.subscribe(on_mouse_click); - this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT); + this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT); input_system.update(); event_manager.dispatch_events(); EXPECT_TRUE(on_click_triggered); } TEST_F(InputTest, testButtonClick) { - GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto& camera = obj.add_component(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); + camera.active = true; + GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_clicked = false; std::function on_click = [&]() { button_clicked = true; }; - auto & button = obj.add_component