diff options
Diffstat (limited to 'src/crepe/system')
-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 |
3 files changed, 96 insertions, 48 deletions
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 |