diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-25 10:38:18 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-25 10:38:18 +0100 |
commit | 327154fd428e0798eea544d9f073f8e1293aa158 (patch) | |
tree | 0bf5ea14a33cf54ab6bbddd22bce5e72f9398cb0 /src | |
parent | 987fc16a8ae124548e2074d6f46806387c03e161 (diff) |
most test working
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Button.h | 3 | ||||
-rw-r--r-- | src/crepe/api/EventManager.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/UiObject.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 86 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 4 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 125 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 17 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/test/EventTest.cpp | 66 | ||||
-rw-r--r-- | src/test/inputTest.cpp | 231 |
11 files changed, 266 insertions, 275 deletions
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 9b1341f..709854a 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,3 +1,4 @@ +#pragma once #include <functional> #include "Component.h" @@ -6,7 +7,7 @@ namespace crepe { class Button : public UiObject{ public: - Button(); + Button(game_object_id_t id) : UiObject(id){}; bool interactable = true; bool is_toggle = false; bool is_pressed = false; diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index a5f4556..3eae90a 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -1,5 +1,5 @@ #pragma once - +#include <iostream> #include "EventManager.h" namespace crepe { diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index 1d7be69..82140a0 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -4,12 +4,14 @@ #include "api/EventHandler.h" namespace crepe { + class UiObject : public Component{ public: - UiObject(); + UiObject(game_object_id_t id) : Component(id){}; int width = 0; int height = 0; public: virtual int get_instances_max() const { return 1; } }; + } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1ef222c..f87cc61 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -76,75 +76,6 @@ SDLContext::~SDLContext() { SDL_Quit(); } -void SDLContext::handle_events(bool &running) { - EventManager& event_manager = EventManager::get_instance(); - SDL_Event event; - - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - running = false; - event_manager.trigger_event(ShutDownEvent{}); - break; - case SDL_KEYDOWN: - event_manager.trigger_event<KeyPressEvent>(KeyPressEvent{ - .repeat = (!event.key.repeat == 0), - .key = this->sdl_to_keycode(event.key.keysym.scancode) - }); - break; - - case SDL_KEYUP: - event_manager.trigger_event<KeyReleaseEvent>(KeyReleaseEvent{ - .key = this->sdl_to_keycode(event.key.keysym.scancode), - }); - break; - - case SDL_MOUSEBUTTONDOWN: - { - int x, y; - SDL_GetMouseState(&x, &y); - event_manager.trigger_event<MousePressEvent>(MousePressEvent{ - .mouse_x = x, - .mouse_y = y, - .button = this->sdl_to_mousebutton(event.button.button) - }); - } - break; - - case SDL_MOUSEBUTTONUP: - { - int x, y; - SDL_GetMouseState(&x, &y); - event_manager.trigger_event<MouseReleaseEvent>(MouseReleaseEvent{ - .mouse_x = x, - .mouse_y = y, - .button = this->sdl_to_mousebutton(event.button.button) - }); - } - break; - - case SDL_MOUSEMOTION: - event_manager.trigger_event<MouseMoveEvent>(MouseMoveEvent{ - .mouse_x = event.motion.x, - .mouse_y = event.motion.y, - .rel_x = event.motion.xrel, - .rel_y = event.motion.yrel - }); - break; - - case SDL_MOUSEWHEEL: - event_manager.trigger_event<MouseScrollEvent>(MouseScrollEvent{ - .scroll_x = event.wheel.x, - .scroll_y = event.wheel.y, - .direction = (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1) - }); - break; - } - } -} - - - Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] { std::array<Keycode, SDL_NUM_SCANCODES> table{}; @@ -259,8 +190,8 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { return LOOKUP_TABLE[sdl_key]; } MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { - static const std::array<MouseButton, 8> MOUSE_BUTTON_LOOKUP_TABLE = [] { - std::array<MouseButton, 8> table{}; + static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] { + std::array<MouseButton, 5> table{}; table.fill(MouseButton::NONE); table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; @@ -409,7 +340,7 @@ std::vector<SDLContext::EventData> SDLContext::get_events(){ event_list.push_back(EventData{ .event_type = SDLContext::Event::MOUSEDOWN, .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {x,y}, + .mouse_position = {event.button.x,event.button.y}, }); } break; @@ -420,32 +351,27 @@ std::vector<SDLContext::EventData> SDLContext::get_events(){ event_list.push_back(EventData{ .event_type = SDLContext::Event::MOUSEUP, .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {x,y}, + .mouse_position = {event.button.x,event.button.y}, }); } break; case SDL_MOUSEMOTION: { - int x,y; - SDL_GetMouseState(&x, &y); event_list.push_back(EventData{ .event_type = SDLContext::Event::MOUSEMOVE, - .mouse_position = {x,y}, + .mouse_position = {event.button.x,event.button.y}, }); } break; case SDL_MOUSEWHEEL: { - int x, y; - SDL_GetMouseState(&x, &y); - event_list.push_back(EventData{ .event_type = SDLContext::Event::MOUSEWHEEL, .mouse_position = {event.motion.x,event.motion.y}, .wheel_delta = event.wheel.y, - .rel_mouse_move {event.motion.yrel,event.motion.xrel}, + .rel_mouse_move = {event.motion.yrel,event.motion.xrel}, }); } break; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 5b6b093..1b881d1 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -6,6 +6,7 @@ #include <SDL2/SDL_video.h> #include <cmath> #include <functional> +#include <utility> #include <memory> #include <string> @@ -43,7 +44,7 @@ public: MOUSEWHEEL, KEYUP, KEYDOWN, - SHUTDOWN + SHUTDOWN, }; struct EventData { @@ -73,7 +74,6 @@ private: * \brief Handles SDL events such as window close and input. * \param running Reference to a boolean flag that controls the main loop. */ - void handle_events(bool & running); std::vector<SDLContext::EventData> get_events(); Keycode get_key(); diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index d658b25..95f6e33 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(crepe PUBLIC CollisionSystem.cpp RenderSystem.cpp AnimatorSystem.cpp + InputSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,4 +16,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES CollisionSystem.h RenderSystem.h AnimatorSystem.h + InputSystem.h ) diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index c61a80f..4560411 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,7 +1,7 @@ #include "ComponentManager.h" -#include "../api/Button.h" + #include "../api/EventManager.h" -#include "../api/Transform.h" + #include "../facade/SDLContext.h" #include "../api/Event.h" @@ -14,32 +14,67 @@ using namespace crepe; void InputSystem::update() { EventManager& event_mgr = EventManager::get_instance(); std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events(); - + for (SDLContext::EventData event : event_list) { switch (event.event_type) { case SDLContext::Event::KEYDOWN: { - event_mgr.queue_event(KeyPressEvent{ + event_mgr.queue_event<KeyPressEvent>(KeyPressEvent{ + .repeat = event.key_repeat, .key = event.key, - .repeat = event.key_repeat, }); break; } case SDLContext::Event::KEYUP: { - event_mgr.queue_event(KeyReleaseEvent{ + event_mgr.queue_event<KeyReleaseEvent>(KeyReleaseEvent{ .key = event.key, }); break; } case SDLContext::Event::MOUSEDOWN: { - event_mgr.queue_event(MousePressEvent{ + event_mgr.queue_event<MousePressEvent>(MousePressEvent{ .mouse_x = event.mouse_position.first, .mouse_y = event.mouse_position.second, .button = event.mouse_button, }); + last_mouse_down_position = {event.mouse_position.first,event.mouse_position.second}; + last_mouse_button = event.mouse_button; break; } + case SDLContext::Event::MOUSEUP: { + MouseReleaseEvent mouse_release_event = MouseReleaseEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .button = event.mouse_button, + }; + event_mgr.queue_event<MouseReleaseEvent>(mouse_release_event); + + // Calculate deltas for click detection + int delta_x = event.mouse_position.first - last_mouse_down_position.first; + int delta_y = event.mouse_position.second - last_mouse_down_position.second; + + // Ensure last_mouse_button is properly set and matches the current mouse button + if (last_mouse_button == event.mouse_button && + std::abs(delta_x) <= click_tolerance && + std::abs(delta_y) <= click_tolerance) { + std::cout << "Click registered at (" << event.mouse_position.first + << ", " << event.mouse_position.second << ") with button " + << static_cast<int>(event.mouse_button) << std::endl; + + event_mgr.queue_event<MouseClickEvent>(MouseClickEvent{ + .mouse_x = event.mouse_position.first, + .mouse_y = event.mouse_position.second, + .button = event.mouse_button, + }); + + this->handle_click(mouse_release_event); + } else { + std::cout << "Mouse release did not register as a click." << std::endl; + } + + break; + } case SDLContext::Event::MOUSEMOVE: { - event_mgr.queue_event(MouseMoveEvent{ + event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{ .mouse_x = event.mouse_position.first, .mouse_y = event.mouse_position.second, .rel_x = event.rel_mouse_move.first, @@ -47,27 +82,9 @@ void InputSystem::update() { }); break; } - case SDLContext::Event::MOUSEUP: { - event_mgr.queue_event(MouseReleaseEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .button = event.mouse_button, - }); - 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) { - event_mgr.queue_event(MouseClickEvent{ - .mouse_x = event.mouse_position.first, - .mouse_y = event.mouse_position.second, - .button = event.mouse_button, - }); - } - break; - } + case SDLContext::Event::MOUSEWHEEL: { - event_mgr.queue_event(MouseScrollEvent{ + event_mgr.queue_event<MouseScrollEvent>(MouseScrollEvent{ .scroll_x = event.mouse_position.first, .scroll_y = event.mouse_position.second, .direction = event.wheel_delta, @@ -75,7 +92,7 @@ void InputSystem::update() { break; } case SDLContext::Event::SHUTDOWN: { - event_mgr.queue_event(ShutDownEvent{}); + event_mgr.queue_event<ShutDownEvent>(ShutDownEvent{}); break; } default: @@ -84,24 +101,44 @@ void InputSystem::update() { } } -bool InputSystem::handle_click(const MouseClickEvent &event) { +void InputSystem::handle_click(const MouseReleaseEvent event) { ComponentManager &mgr = this->component_manager; - std::vector<std::reference_wrapper<Button>> buttons = - mgr.get_components_by_type<Button>(); - - std::vector<std::reference_wrapper<Transform>> transforms = - mgr.get_components_by_type<Transform>(); + // Get the buttons and transforms + std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>(); + std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>(); + for (Button &button : buttons) { - for(Transform& transform : transforms){ - if(button.game_object_id != transform.game_object_id){continue;} - if (!button.interactable) {break;} - if (event.mouse_x >= transform.position.x && event.mouse_x <= transform.position.x + button.width && - event.mouse_y >= transform.position.y && event.mouse_y <= transform.position.y + button.height) { - button.on_click(); - } - } + Transform* transform = find_transform_for_button(button, transforms); + if (!transform) continue; + + if (button.interactable && is_mouse_inside_button(event, button, *transform)) { + handle_button_press(button, event); + } + } +} + +Transform* InputSystem::find_transform_for_button(Button &button, std::vector<std::reference_wrapper<Transform>> &transforms) { + for (Transform &transform : transforms) { + if (button.game_object_id == transform.game_object_id) { + return &transform; + } } - return false; + return nullptr; +} + +bool InputSystem::is_mouse_inside_button(const MouseReleaseEvent &event, const Button &button, const Transform &transform) { + return event.mouse_x >= transform.position.x && event.mouse_x <= transform.position.x + button.width && + event.mouse_y >= transform.position.y && event.mouse_y <= transform.position.y + button.height; } +void InputSystem::handle_button_press(Button &button, const MouseReleaseEvent &event) { + if (button.is_toggle) { + if (!button.is_pressed) { + button.on_click(); + } + button.is_pressed = !button.is_pressed; + } else { + button.on_click(); + } +} diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 231aa45..db6b374 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,7 +1,13 @@ #pragma once +#include <utility> + #include "System.h" + #include "../api/Event.h" +#include "../api/Button.h" +#include "../api/Transform.h" + namespace crepe { class InputSystem : public System { @@ -14,10 +20,13 @@ private: std::pair<int, int> last_mouse_down_position{-1, -1}; MouseButton last_mouse_button = MouseButton::NONE; const int click_tolerance = 5; - bool handle_click(const MouseClickEvent &event); - bool handle_move(const MouseMoveEvent &event); - bool handle_key_press(const KeyPressEvent &event); - bool handle_key_release(const KeyReleaseEvent &event); + void handle_click(const MouseReleaseEvent); + + Transform* find_transform_for_button(Button &button, std::vector<std::reference_wrapper<Transform>> &transforms); + + bool is_mouse_inside_button(const MouseReleaseEvent &event, const Button &button, const Transform &transform); + + void handle_button_press(Button &button, const MouseReleaseEvent &event); }; } // namespace crepe diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d310f6a..ac4d73a 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,4 +12,5 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp + inputTest.cpp ) diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index b0e6c9c..4af0830 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -154,37 +154,45 @@ 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; - EventHandler<MouseClickEvent> mouse_handler1 = [&](const MouseClickEvent & e) { - triggered1 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); - EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); - return false; // Allows propagation - }; - EventHandler<MouseClickEvent> mouse_handler2 = [&](const MouseClickEvent & e) { - triggered2 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); - EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); - return false; // Allows propagation - }; - event_manager.subscribe<MouseClickEvent>(mouse_handler1); - event_manager.subscribe<MouseClickEvent>(mouse_handler2, test_channel); - - event_manager.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); - event_manager.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}, - 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 6b820ee..2fc5ad9 100644 --- a/src/test/inputTest.cpp +++ b/src/test/inputTest.cpp @@ -20,159 +20,164 @@ using namespace crepe; class InputTest : public ::testing::Test { public: -ComponentManager mgr{}; -InputSystem input_system{mgr}; + ComponentManager mgr{}; + InputSystem input_system{mgr}; + + EventManager& event_manager = EventManager::get_instance(); -EventManager& event_manager = EventManager::get_instance(); protected: - void SetUp() override { - event_manager.clear(); - } - - void TearDown() override { - event_manager.clear(); - } -void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { - SDL_Event event; + void SetUp() override { + event_manager.clear(); + } - // Simulate Mouse Button Down event - SDL_zero(event); - event.type = SDL_MOUSEBUTTONDOWN; - event.button.x = mouse_x; - event.button.y = mouse_y; - event.button.button = mouse_button; - SDL_PushEvent(&event); - SDL_zero(event); - event.type = SDL_MOUSEBUTTONUP; - event.button.x = mouse_x; - event.button.y = mouse_y; - event.button.button = mouse_button; - SDL_PushEvent(&event); -} + void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { + SDL_Event event; + // Simulate Mouse Button Down event + SDL_zero(event); + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = mouse_x; + event.button.y = mouse_y; + event.button.button = mouse_button; + SDL_PushEvent(&event); + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = mouse_x; + event.button.y = mouse_y; + event.button.button = mouse_button; + SDL_PushEvent(&event); + } }; TEST_F(InputTest, MouseDown) { - bool mouse_triggered = false; - EventHandler<MousePressEvent> on_mouse_click = [&](const MousePressEvent& event) { - // Handle the mouse click event here - mouse_triggered = true; + bool mouse_triggered = false; + EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent& event) { + mouse_triggered = true; EXPECT_EQ(event.mouse_x, 10); EXPECT_EQ(event.mouse_y, 10); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - return false; + return false; }; - event_manager.subscribe<MousePressEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); + event_manager.subscribe<MousePressEvent>(on_mouse_down); + + SDL_Event event; + SDL_zero(event); event.type = SDL_MOUSEBUTTONDOWN; event.button.x = 10; event.button.y = 10; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); - EXPECT_TRUE(mouse_triggered); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(mouse_triggered); } TEST_F(InputTest, MouseUp) { - EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) { + bool function_triggered = false; + EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent& e) { // Handle the mouse click event here - EXPECT_EQ(event.mouse_x, 10); - EXPECT_EQ(event.mouse_y, 10); - EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - return false; + function_triggered = true; + EXPECT_EQ(e.mouse_x, 10); + EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; }; - event_manager.subscribe<MouseReleaseEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); - event.type = SDL_MOUSEBUTTONUP; - event.button.x = 10; - event.button.y = 10; - event.button.button = SDL_BUTTON_LEFT; - SDL_PushEvent(&event); -} -TEST_F(InputTest, MouseUp) { - EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) { - // Handle the mouse click event here - EXPECT_EQ(event.mouse_x, 10); - EXPECT_EQ(event.mouse_y, 10); - EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - return false; - }; - event_manager.subscribe<MouseReleaseEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); + event_manager.subscribe<MouseReleaseEvent>(on_mouse_release); + + SDL_Event event; + SDL_zero(event); event.type = SDL_MOUSEBUTTONUP; event.button.x = 10; event.button.y = 10; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); } + TEST_F(InputTest, KeyDown) { - EventHandler<KeyPressEvent> on_mouse_click = [&](const KeyPressEvent& event) { - // Handle the mouse click event here - EXPECT_EQ(event.key, Keycode::B); - EXPECT_EQ(event.repeat, true); - return false; + bool function_triggered = false; + + // Define event handler for KeyPressEvent + 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 + return false; }; - event_manager.subscribe<KeyPressEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); - event.type = SDL_KEYUP; - event.button.button = SDL_BUTTON_LEFT; - event.key.repeat = 1; - SDL_PushEvent(&event); + + event_manager.subscribe<KeyPressEvent>(on_key_press); + + // Simulate SDL_KEYDOWN event + SDL_Event test_event; + SDL_zero(test_event); + test_event.type = SDL_KEYDOWN; // Key down event + test_event.key.keysym.scancode = SDL_SCANCODE_B; // Set scancode for 'B' + test_event.key.repeat = 1; // Set repeat flag + SDL_PushEvent(&test_event); + + input_system.update(); // Process the event + event_manager.dispatch_events(); // Dispatch events to handlers + + EXPECT_TRUE(function_triggered); // Check if the handler was triggered } + TEST_F(InputTest, KeyUp) { - EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) { - // Handle the mouse click event here + bool function_triggered = false; + EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent& event) { + function_triggered = true; EXPECT_EQ(event.key, Keycode::B); - return false; + return false; }; - event_manager.subscribe<KeyReleaseEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); + event_manager.subscribe<KeyReleaseEvent>(on_key_release); + + SDL_Event event; + SDL_zero(event); event.type = SDL_KEYUP; - event.button.button = SDL_BUTTON_LEFT; + event.key.keysym.scancode = SDL_SCANCODE_B; SDL_PushEvent(&event); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); } -TEST_F(InputTest, KeyClick) { - GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - Button test_button; - bool button_clicked = false; - test_button.active = true; - test_button.width = 100; - test_button.height = 100; - std::function<void()> on_click = [&]() { - button_clicked = true; - }; - test_button.on_click = on_click; - test_button.is_pressed = false; - test_button.is_toggle = false; - obj.add_component<Button>(); - EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) { - EXPECT_EQ(event.key, Keycode::B); - return false; + +TEST_F(InputTest, MouseClick) { + bool on_click_triggered = false; + 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); + EXPECT_EQ(event.mouse_y, 10); + return false; }; - event_manager.subscribe<KeyReleaseEvent>(on_mouse_click); - SDL_Event event; - SDL_zero(event); - event.type = SDL_KEYUP; - event.button.button = SDL_BUTTON_LEFT; - SDL_PushEvent(&event); + event_manager.subscribe<MouseClickEvent>(on_mouse_click); + + this->simulate_mouse_click(10,10,SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(on_click_triggered); } + TEST_F(InputTest, testButton) { - GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - Button test_button; + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + //auto test_button = std::make_unique<Button>(); + // Button test_button = new Button(obj.id); + + auto button = obj.add_component<Button>(); bool button_clicked = false; - test_button.active = true; - test_button.width = 100; - test_button.height = 100; - std::function<void()> on_click = [&]() { + button.active = true; + button.interactable = true; + button.width = 100; + button.height = 100; + std::function<void()> on_click = [&]() { button_clicked = true; }; - test_button.on_click = on_click; - test_button.is_pressed = false; - test_button.is_toggle = false; - obj.add_component<Button>(); - this->simulate_mouse_click(10,10,MouseButton::LEFT_MOUSE); + button.on_click = on_click; + button.is_pressed = false; + button.is_toggle = false; + this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + //EXPECT_TRUE(button_clicked); } |