From 92e83ded2b6afb26082d7661c28af1d9d4e950a2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 18 Nov 2024 16:55:21 +0100 Subject: added mouse and key event triggering --- src/crepe/api/Event.h | 16 ++++++++++++++++ src/crepe/api/LoopManager.h | 13 +++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index d5ddf0a..ef6a791 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -87,6 +87,10 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; + // Relative movement in x + int rel_x; + // Relative movement in y + int rel_y; }; /** @@ -113,3 +117,15 @@ public: class ShutDownEvent : public Event { public: }; + +class MouseScrollEvent : public Event { +public: + //! X-coordinate of the mouse position at the time of the event. + int scroll_x = 0; + + //! Y-coordinate of the mouse position at the time of the event. + int scroll_y = 0; + + int direction = 0; +}; + diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f6904be..b18c9d1 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -10,6 +10,12 @@ namespace crepe { class LoopManager { public: void start(); + /** + * \brief Set game running variable + * + * \param running running (false = game shutdown, true = game running) + */ + void set_running(bool running); LoopManager(); private: @@ -53,12 +59,7 @@ private: * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ void fixed_update(); - /** - * \brief Set game running variable - * - * \param running running (false = game shutdown, true = game running) - */ - void set_running(bool running); + /** * \brief Function for executing render-related systems. * -- cgit v1.2.3 From 6c2fc3716c9c6c68e982b243af5f7ed04fb35e86 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 19 Nov 2024 15:16:16 +0100 Subject: button handling --- src/crepe/api/Button.cpp | 0 src/crepe/api/Button.h | 17 +++++++++++++ src/crepe/api/UiObject.h | 14 +++++++++++ src/crepe/facade/SDLContext.cpp | 6 ++--- src/crepe/system/InputSystem.cpp | 52 ++++++++++++++++++++++++++++++++++++++++ src/crepe/system/InputSystem.h | 8 +++++-- 6 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/crepe/api/Button.cpp create mode 100644 src/crepe/api/Button.h create mode 100644 src/crepe/api/UiObject.h (limited to 'src/crepe/api') diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h new file mode 100644 index 0000000..5035729 --- /dev/null +++ b/src/crepe/api/Button.h @@ -0,0 +1,17 @@ +#include + +#include "Component.h" +#include "api/EventHandler.h" +#include "api/UiObject.h" +namespace crepe { +class Button : public UiObject{ +public: + ~Button(){}; + bool interactable = true; + bool is_toggle = false; + bool is_pressed = false; + std::function on_click; +public: +virtual int get_instances_max() const { return 1; } +}; +} diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h new file mode 100644 index 0000000..f57f7ef --- /dev/null +++ b/src/crepe/api/UiObject.h @@ -0,0 +1,14 @@ +#include + +#include "Component.h" +#include "api/EventHandler.h" +namespace crepe { +class UiObject : public Component{ +public: + ~UiObject(){}; + 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 d0977cb..43ef3f1 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -94,7 +94,6 @@ void SDLContext::handle_events(bool &running) { break; case SDL_KEYDOWN: - std::cout << "keyDown: " << event.key.keysym.sym << std::endl; event_manager.trigger_event(KeyPressEvent{ .repeat = event.key.repeat, .key = this->sdl_to_keycode(event.key.keysym.scancode) @@ -157,7 +156,6 @@ void SDLContext::handle_events(bool &running) { Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { static const std::array LOOKUP_TABLE = [] { std::array table{}; - // Default to NONE for unmapped keys table.fill(Keycode::NONE); table[SDL_SCANCODE_SPACE] = Keycode::SPACE; @@ -271,7 +269,7 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { std::array table{}; - table.fill(MouseButton::NONE); // Default to NONE for unmapped buttons + table.fill(MouseButton::NONE); table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE; @@ -287,7 +285,7 @@ MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { return MouseButton::NONE; } - return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button]; // Return mapped button + return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button]; } void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index e69de29..b7a86f4 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -0,0 +1,52 @@ +#include "ComponentManager.h" +#include "../api/Button.h" +#include "../api/EventManager.h" +#include "../api/Transform.h" +#include "../api/Event.h" + +#include "system/InputSystem.h" + +using namespace crepe; + +InputSystem::InputSystem(ComponentManager &component_manager) + : System(component_manager) { + auto &event_manager = EventManager::get_instance(); + + event_manager.subscribe([this](const MouseClickEvent &event) { + return this->handle_click(event); + }); + + event_manager.subscribe([this](const MouseMoveEvent &event) { + return this->handle_move(event); + }); +} + +void InputSystem::update() { +} + +bool InputSystem::handle_click(const MouseClickEvent &event) { + ComponentManager &mgr = this->component_manager; + + std::vector> buttons = + mgr.get_components_by_type