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/crepe | |
parent | 987fc16a8ae124548e2074d6f46806387c03e161 (diff) |
most test working
Diffstat (limited to 'src/crepe')
-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 |
8 files changed, 110 insertions, 133 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 |