From 327154fd428e0798eea544d9f073f8e1293aa158 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 25 Nov 2024 10:38:18 +0100 Subject: most test working --- src/crepe/api/Button.h | 3 +- src/crepe/api/EventManager.hpp | 2 +- src/crepe/api/UiObject.h | 4 +- src/crepe/facade/SDLContext.cpp | 86 +-------------- src/crepe/facade/SDLContext.h | 4 +- src/crepe/system/CMakeLists.txt | 2 + src/crepe/system/InputSystem.cpp | 125 +++++++++++++-------- src/crepe/system/InputSystem.h | 17 ++- src/test/CMakeLists.txt | 1 + src/test/EventTest.cpp | 66 ++++++----- src/test/inputTest.cpp | 231 ++++++++++++++++++++------------------- 11 files changed, 266 insertions(+), 275 deletions(-) (limited to 'src') 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 #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 #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{ - .repeat = (!event.key.repeat == 0), - .key = this->sdl_to_keycode(event.key.keysym.scancode) - }); - break; - - case SDL_KEYUP: - event_manager.trigger_event(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{ - .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{ - .mouse_x = x, - .mouse_y = y, - .button = this->sdl_to_mousebutton(event.button.button) - }); - } - break; - - case SDL_MOUSEMOTION: - event_manager.trigger_event(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{ - .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 LOOKUP_TABLE = [] { std::array 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 MOUSE_BUTTON_LOOKUP_TABLE = [] { - std::array table{}; + static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { + std::array table{}; table.fill(MouseButton::NONE); table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; @@ -409,7 +340,7 @@ std::vector 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::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 #include #include +#include #include #include @@ -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 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 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{ + .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{ .key = event.key, }); break; } case SDLContext::Event::MOUSEDOWN: { - event_mgr.queue_event(MousePressEvent{ + event_mgr.queue_event(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(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(event.mouse_button) << std::endl; + + event_mgr.queue_event(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{ .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{ .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{}); 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> buttons = - mgr.get_components_by_type