From 210ff1309aa545aa3574d991acc7734a1f268b8e Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 18 Nov 2024 16:12:12 +0100 Subject: added keycode conversion --- src/crepe/system/InputSystem.cpp | 0 src/crepe/system/InputSystem.h | 14 ++++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/crepe/system/InputSystem.cpp create mode 100644 src/crepe/system/InputSystem.h (limited to 'src/crepe/system') diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h new file mode 100644 index 0000000..642035f --- /dev/null +++ b/src/crepe/system/InputSystem.h @@ -0,0 +1,14 @@ +#pragma once + +#include "System.h" + +namespace crepe { + +class InputSystem : public System { +public: + using System::System; + void update() override; + void +}; + +} // namespace crepe -- 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/system') 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