diff options
-rw-r--r-- | src/crepe/api/Button.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 5 | ||||
-rw-r--r-- | src/crepe/api/UiObject.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/UiObject.h | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 25 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 6 | ||||
-rw-r--r-- | src/test/EventTest.cpp | 74 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 26 |
9 files changed, 79 insertions, 75 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 077a5e7..c0ff5a8 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,7 +2,12 @@ namespace crepe { -Button::Button(game_object_id_t id, int width, int height, bool is_toggle, std::function<void()> on_click) - : UiObject(id, width, height), is_toggle(is_toggle), is_pressed(false), hover(false), on_click(on_click) {} +Button::Button(game_object_id_t id, int width, int height, bool is_toggle, + std::function<void()> on_click) + : UiObject(id, width, height), + is_toggle(is_toggle), + is_pressed(false), + hover(false), + on_click(on_click) {} } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index df6f1e0..2fa94ae 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,7 +1,7 @@ #pragma once -#include <functional> #include "UiObject.h" +#include <functional> namespace crepe { @@ -24,7 +24,8 @@ public: * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false. * \param on_click callback function that will be invoked when the button is clicked. */ - Button(game_object_id_t id, int width, int height, bool is_toggle = false, std::function<void()> on_click = nullptr); + Button(game_object_id_t id, int width, int height, bool is_toggle = false, + std::function<void()> on_click = nullptr); /** * \brief Indicates if the button is a toggle button (can be pressed and released). diff --git a/src/crepe/api/UiObject.cpp b/src/crepe/api/UiObject.cpp index 987fc06..7859a90 100644 --- a/src/crepe/api/UiObject.cpp +++ b/src/crepe/api/UiObject.cpp @@ -2,4 +2,7 @@ using namespace crepe; -UiObject::UiObject(game_object_id_t id,int width,int height) : Component(id),width(width),height(height){}; +UiObject::UiObject(game_object_id_t id, int width, int height) + : Component(id), + width(width), + height(height){}; diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index 6b0323e..c056877 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -14,7 +14,7 @@ public: * \brief Constructs a UiObject with the specified game object ID. * \param id The unique ID of the game object associated with this UI object. */ - UiObject(game_object_id_t id,int width,int height); + UiObject(game_object_id_t id, int width, int height); //! The width of the UI object. int width = 0; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 9f18728..886dda8 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,11 +11,11 @@ #include <string> #include <utility> +#include "api/Camera.h" #include "api/Event.h" #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "api/Camera.h" #include "types.h" diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index bb67e5f..070f804 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,6 +1,6 @@ +#include "ComponentManager.h" #include "api/Button.h" #include "api/EventManager.h" -#include "ComponentManager.h" #include "InputSystem.h" @@ -41,7 +41,7 @@ void InputSystem::update() { int delta_x = event.mouse_position.first - last_mouse_down_position.first; int delta_y = event.mouse_position.second - last_mouse_down_position.second; - + if (last_mouse_button == event.mouse_button && std::abs(delta_x) <= click_tolerance && std::abs(delta_y) <= click_tolerance) { @@ -53,8 +53,7 @@ void InputSystem::update() { handle_click(event); } - } - break; + } break; case SDLContext::EventType::MOUSEMOVE: event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{ .mouse_x = event.mouse_position.first, @@ -113,20 +112,18 @@ void InputSystem::handle_click(const SDLContext::EventData & event_data) { } } +OptionalRef<Transform> +InputSystem::find_transform_for_button(Button & button, RefVector<Transform> & transforms) { -OptionalRef<Transform> InputSystem::find_transform_for_button( - Button & button, RefVector<Transform>& transforms) { - - for (auto& transform : transforms) { - if (button.game_object_id == transform.get().game_object_id) { - return OptionalRef<Transform>(transform); - } - } + for (auto & transform : transforms) { + if (button.game_object_id == transform.get().game_object_id) { + return OptionalRef<Transform>(transform); + } + } - return OptionalRef<Transform>(); + return OptionalRef<Transform>(); } - bool InputSystem::is_mouse_inside_button(const SDLContext::EventData & event_data, const Button & button, const Transform & transform) { return event_data.mouse_position.first >= transform.position.x diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 13c3fc1..b648144 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,8 +1,8 @@ #pragma once #include "facade/SDLContext.h" -#include "util/OptionalRef.h" #include "types.h" +#include "util/OptionalRef.h" #include "System.h" @@ -61,8 +61,8 @@ private: * \param transforms A list of transforms to search through. * \return A pointer to the transform of the button, or nullptr if not found. */ - OptionalRef<Transform> - find_transform_for_button(Button & button, RefVector<Transform>& transforms); + OptionalRef<Transform> find_transform_for_button(Button & button, + RefVector<Transform> & transforms); /** * \brief Checks if the mouse position is inside the bounds of the button. diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 4af0830..c75bcc1 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -154,44 +154,44 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { } TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { - EventManager & event_manager = EventManager::get_instance(); - bool triggered1 = false; - bool triggered2 = false; - int test_channel = 1; - - // Adjusted to use KeyPressEvent with repeat as the first variable - EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) { - triggered1 = true; - EXPECT_EQ(e.repeat, false); // Expecting repeat to be false - EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code - return false; // Allows propagation - }; - - EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) { - triggered2 = true; - EXPECT_EQ(e.repeat, false); // Expecting repeat to be false - EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code - return false; // Allows propagation - }; - - // Subscribe handlers to KeyPressEvent - event_manager.subscribe<KeyPressEvent>(key_handler1); - event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel); - - // Queue a KeyPressEvent instead of KeyDownEvent - event_manager.queue_event<KeyPressEvent>( - KeyPressEvent{.repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first - - event_manager.queue_event<KeyPressEvent>( - KeyPressEvent{.repeat = false, .key = Keycode::A}, // Adjust event for second subscription - test_channel); - - event_manager.dispatch_events(); - - EXPECT_TRUE(triggered1); - EXPECT_TRUE(triggered2); -} + EventManager & event_manager = EventManager::get_instance(); + bool triggered1 = false; + bool triggered2 = false; + int test_channel = 1; + // Adjusted to use KeyPressEvent with repeat as the first variable + EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) { + triggered1 = true; + EXPECT_EQ(e.repeat, false); // Expecting repeat to be false + EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code + return false; // Allows propagation + }; + + EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) { + triggered2 = true; + EXPECT_EQ(e.repeat, false); // Expecting repeat to be false + EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code + return false; // Allows propagation + }; + + // Subscribe handlers to KeyPressEvent + event_manager.subscribe<KeyPressEvent>(key_handler1); + event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel); + + // Queue a KeyPressEvent instead of KeyDownEvent + event_manager.queue_event<KeyPressEvent>(KeyPressEvent{ + .repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first + + event_manager.queue_event<KeyPressEvent>( + KeyPressEvent{.repeat = false, + .key = Keycode::A}, // Adjust event for second subscription + test_channel); + + event_manager.dispatch_events(); + + EXPECT_TRUE(triggered1); + EXPECT_TRUE(triggered2); +} TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { EventManager & event_manager = EventManager::get_instance(); diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 0e3e097..d6ebb6f 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -20,14 +20,12 @@ 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}; // Initializes the InputSystem with the ComponentManager - EventManager& event_manager = EventManager::get_instance(); + EventManager & event_manager = EventManager::get_instance(); protected: - void SetUp() override { - event_manager.clear(); - } + void SetUp() override { event_manager.clear(); } void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { SDL_Event event; @@ -52,7 +50,7 @@ protected: TEST_F(InputTest, MouseDown) { bool mouse_triggered = false; - EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent& event) { + EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) { mouse_triggered = true; EXPECT_EQ(event.mouse_x, 10); EXPECT_EQ(event.mouse_y, 10); @@ -76,7 +74,7 @@ TEST_F(InputTest, MouseDown) { TEST_F(InputTest, MouseUp) { bool function_triggered = false; - EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent& e) { + EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { function_triggered = true; EXPECT_EQ(e.mouse_x, 10); EXPECT_EQ(e.mouse_y, 10); @@ -100,7 +98,7 @@ TEST_F(InputTest, MouseUp) { TEST_F(InputTest, MouseMove) { bool function_triggered = false; - EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent& e) { + EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) { function_triggered = true; EXPECT_EQ(e.mouse_x, 10); EXPECT_EQ(e.mouse_y, 10); @@ -128,7 +126,7 @@ TEST_F(InputTest, KeyDown) { bool function_triggered = false; // Define event handler for KeyPressEvent - EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent& event) { + EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent & event) { function_triggered = true; EXPECT_EQ(event.key, Keycode::B); // Validate the key is 'B' EXPECT_EQ(event.repeat, true); // Validate repeat flag @@ -153,7 +151,7 @@ TEST_F(InputTest, KeyDown) { TEST_F(InputTest, KeyUp) { bool function_triggered = false; - EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent& event) { + EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) { function_triggered = true; EXPECT_EQ(event.key, Keycode::B); return false; @@ -173,7 +171,7 @@ TEST_F(InputTest, KeyUp) { TEST_F(InputTest, MouseClick) { bool on_click_triggered = false; - EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent& event) { + EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) { on_click_triggered = true; EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); EXPECT_EQ(event.mouse_x, 10); @@ -190,8 +188,8 @@ TEST_F(InputTest, MouseClick) { TEST_F(InputTest, testButtonClick) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - - auto& button = obj.add_component<Button>(100,100); + + auto & button = obj.add_component<Button>(100, 100); bool button_clicked = false; bool hover = false; button.active = true; @@ -212,7 +210,7 @@ TEST_F(InputTest, testButtonClick) { TEST_F(InputTest, testButtonHover) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - auto& button = obj.add_component<Button>(100,100); + auto & button = obj.add_component<Button>(100, 100); bool button_clicked = false; button.active = true; button.width = 100; |