From 880a7b1fb233a67303ad5a4155dc459f79734762 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 10:50:40 +0100 Subject: big cleanup events + events are now structs --- src/crepe/api/Event.h | 68 +++++++++++++++++++++++++++++++++++ src/crepe/api/EventHandler.cpp | 2 ++ src/crepe/api/EventHandler.h | 38 ++++++++++++++++++++ src/crepe/api/EventManager.cpp | 69 ++++++++++++++++++++++++++++++++++++ src/crepe/api/EventManager.h | 76 ++++++++++++++++++++++++++++++++++++++++ src/crepe/api/IKeyListener.cpp | 20 +++++++++++ src/crepe/api/IKeyListener.h | 20 +++++++++++ src/crepe/api/IMouseListener.cpp | 35 ++++++++++++++++++ src/crepe/api/IMouseListener.h | 24 +++++++++++++ src/makefile | 12 +++++-- 10 files changed, 362 insertions(+), 2 deletions(-) create mode 100644 src/crepe/api/Event.h create mode 100644 src/crepe/api/EventHandler.cpp create mode 100644 src/crepe/api/EventHandler.h create mode 100644 src/crepe/api/EventManager.cpp create mode 100644 src/crepe/api/EventManager.h create mode 100644 src/crepe/api/IKeyListener.cpp create mode 100644 src/crepe/api/IKeyListener.h create mode 100644 src/crepe/api/IMouseListener.cpp create mode 100644 src/crepe/api/IMouseListener.h diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h new file mode 100644 index 0000000..8d9b241 --- /dev/null +++ b/src/crepe/api/Event.h @@ -0,0 +1,68 @@ +#pragma once +#include +//#include "keyCodes.h" +#include +#include +#include +#include +#include +using Keycode = uint16_t; +enum class MouseButton { + NONE = 0, + LEFT_MOUSE = 1, + RIGHT_MOUSE = 2, + MIDDLE_MOUSE = 3, + X1_MOUSE = 4, + X2_MOUSE = 5, + SCROLL_UP = 6, + SCROLL_DOWN = 7, +}; +class Event { +public: +}; + +class KeyPressEvent : public Event { +public: + int repeat = 0; + Keycode key = 0; +}; + +class MouseClickEvent : public Event { +public: + int mouse_x = 0; + int mouse_y = 0; + MouseButton button; +}; +class KeyReleaseEvent : public Event { +public: + Keycode key = 0; +}; + +class MousePressEvent : public Event { +public: + int mouse_x = 0; + int mouse_y = 0; + MouseButton button; +}; +class MouseReleaseEvent : public Event { +public: + int mouse_x = 0; + int mouse_y = 0; + MouseButton button; +}; +class MouseMoveEvent : public Event { +public: + int mouse_x = 0; + int mouse_y = 0; +}; +class CollisionEvent : public Event{ +public: + //Collision collisionData; +}; +class TextSubmitEvent : public Event{ +public: + std::string text; +}; +class ShutDownEvent : public Event { +public: +}; diff --git a/src/crepe/api/EventHandler.cpp b/src/crepe/api/EventHandler.cpp new file mode 100644 index 0000000..93a116a --- /dev/null +++ b/src/crepe/api/EventHandler.cpp @@ -0,0 +1,2 @@ +#include "EventHandler.h" +bool IEventHandlerWrapper::exec(const Event & e) { return call(e); } diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h new file mode 100644 index 0000000..eea1c79 --- /dev/null +++ b/src/crepe/api/EventHandler.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include +#include "Event.h" + +template +using EventHandler = std::function; + +class IEventHandlerWrapper { +public: + virtual ~IEventHandlerWrapper() = default; + + bool exec(const Event & e); + + virtual std::string get_type() const = 0; + +private: + virtual bool call(const Event & e) = 0; +}; + +template +class EventHandlerWrapper : public IEventHandlerWrapper { +public: + explicit EventHandlerWrapper(const EventHandler & handler) + : m_handler(handler), m_handler_type(m_handler.target_type().name()) { + } + +private: + bool call(const Event & e) override { + return m_handler(static_cast(e)); + } + + std::string get_type() const override { return m_handler_type; } + + EventHandler m_handler; + const std::string m_handler_type; +}; diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp new file mode 100644 index 0000000..07ef1f7 --- /dev/null +++ b/src/crepe/api/EventManager.cpp @@ -0,0 +1,69 @@ +#include "EventManager.h" + +void EventManager::unsubscribe(std::type_index eventType, const std::string & handlerName, + int eventId) { + // if (eventId) { + // std::unordered_map< + // std::type_index, std::unordered_map< + // int, std::vector>>>::iterator subscriber_list = subscribers_by_event_id.find(eventType); + // if (subscriber_list != subscribers_by_event_id.end()) { + // std::unordered_map< + // int, std::vector>> & + // handlers_map + // = subscriber_list->second; + // std::unordered_map< + // int, + // std::vector>>::iterator + // handlers + // = handlers_map.find(eventId); + // if (handlers != handlers_map.end()) { + // std::vector> & callbacks + // = handlers->second; + // for (std::vector< + // std::unique_ptr>::iterator it + // = callbacks.begin(); + // it != callbacks.end(); ++it) { + // if (it->get()->getType() == handlerName) { + // it = callbacks.erase(it); + // return; + // } + // } + // } + // } + // } else { + // std::unordered_map< + // std::type_index, std::vector>>::iterator + // handlers_it + // = subscribers.find(eventType); + // if (handlers_it != subscribers.end()) { + // std::vector> & handlers + // = handlers_it->second; + // for (std::vector>::iterator it + // = handlers.begin(); + // it != handlers.end(); ++it) { + // if (it->get()->getType() == handlerName) { + // it = handlers.erase(it); + // return; + // } + // } + // } + // } +} + +void EventManager::queue_event(std::unique_ptr && event_, int eventId) { + events_queue.emplace_back(std::move(event_), eventId); +} + +void EventManager::dispatch_events() { + // for (std::vector, int>>::iterator eventIt + // = eventsQueue.begin(); + // eventIt != eventsQueue.end();) { + // bool handled = false; + // if (!handled) { + // triggerEvent(*eventIt->first.get(), eventIt->second); + // eventIt = eventsQueue.erase(eventIt); + // } else { + // ++eventIt; + // } + // } +} diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h new file mode 100644 index 0000000..8c0685a --- /dev/null +++ b/src/crepe/api/EventManager.h @@ -0,0 +1,76 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include "Event.h" +#include "EventHandler.h" +//#include "keyCodes.h" + +class EventManager { +public: + EventManager(const EventManager &) = delete; + const EventManager & operator=(const EventManager &) = delete; + static EventManager & get_instance() { + static EventManager instance; + return instance; + } + template + void subscribe(EventHandler && callback, int channel = 0); + void unsubscribe(std::type_index eventType, const std::string & handlerName,int channel); + template + void trigger_event(const EventType & event, int channel); + void queue_event(std::unique_ptr && event, int channel); + void dispatch_events(); + +private: + EventManager() = default; + std::vector, int>> events_queue; + std::unordered_map>> subscribers; + std::unordered_map>>> subscribers_by_event_id; +}; + +template +void EventManager::subscribe(EventHandler && callback, int channel){ + std::type_index event_type = typeid(EventType); + auto handler = std::make_unique>(callback); + + if (channel) { + auto & handlers_map = subscribers_by_event_id[event_type]; + auto handlers = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + handlers->second.emplace_back(std::move(handler)); + } else { + handlers_map[channel].emplace_back(std::move(handler)); + } + } else { + subscribers[event_type].emplace_back(std::move(handler)); + } +} + +template +void EventManager::trigger_event(const EventType & event, int eventId) { + std::type_index event_type = std::type_index(typeid(EventType)); + + if (eventId > 0) { + auto handlers_it = subscribers_by_event_id[event_type].find(eventId); + if (handlers_it != subscribers_by_event_id[event_type].end()) { + auto & callbacks = handlers_it->second; + for (auto it = callbacks.begin(); it != callbacks.end();) { + if ((*it)->exec(event)) { + it = callbacks.erase(it); + } else { + ++it; + } + } + } + } else { + auto & handlers = subscribers[event_type]; + for (auto & handler : handlers) { + handler->exec(event); + } + } +} diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp new file mode 100644 index 0000000..b1ad5e5 --- /dev/null +++ b/src/crepe/api/IKeyListener.cpp @@ -0,0 +1,20 @@ +#include "IKeyListener.h" + +IKeyListener::~IKeyListener() { unsubscribe_events(); } + +void IKeyListener::subscribe_events(int listenerId) { + auto key_pressed_handler = [this](const KeyPressEvent & event) { + return this->on_key_pressed(event); + }; + auto key_released_handler = [this](const KeyReleaseEvent & event) { + return this->on_key_released(event); + }; + + EventManager::get_instance().subscribe(std::move(key_pressed_handler), listenerId); + EventManager::get_instance().subscribe(std::move(key_released_handler), listenerId); +} + +void IKeyListener::unsubscribe_events(int listenerId) { + EventManager::get_instance().unsubscribe(std::type_index(typeid(KeyPressEvent)), "on_key_pressed", listenerId); + EventManager::get_instance().unsubscribe(std::type_index(typeid(KeyReleaseEvent)), "on_key_released", listenerId); +} diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h new file mode 100644 index 0000000..4fd2238 --- /dev/null +++ b/src/crepe/api/IKeyListener.h @@ -0,0 +1,20 @@ +#pragma once +#include "Event.h" +#include "EventHandler.h" +#include "EventManager.h" +class IKeyListener { +public: + virtual ~IKeyListener(); + virtual bool on_key_pressed(const KeyPressEvent & event) = 0; + virtual bool on_key_released(const KeyReleaseEvent & event) = 0; + +protected: + void subscribe_events(int listenerId = 0); + void unsubscribe_events(int listenerId = 0); + void activate(int listenerId = 0) { subscribe_events(listenerId); } + void deactivate(int listenerId = 0) { unsubscribe_events(listenerId); } + +private: + EventHandler key_pressed_handler; + EventHandler key_released_handler; +}; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp new file mode 100644 index 0000000..043188c --- /dev/null +++ b/src/crepe/api/IMouseListener.cpp @@ -0,0 +1,35 @@ +#include "IMouseListener.h" + +IMouseListener::~IMouseListener() { + unsubscribe_events(); +} + +void IMouseListener::subscribe_events(int listenerId) { + // Wrap member functions in lambdas to ensure correct function signatures + mouse_click_handler = [this](const MouseClickEvent & event) { + return this->on_mouse_clicked(event); + }; + mouse_press_handler = [this](const MousePressEvent & event) { + return this->on_mouse_pressed(event); + }; + mouse_release_handler = [this](const MouseReleaseEvent & event) { + return this->on_mouse_released(event); + }; + mouse_move_handler = [this](const MouseMoveEvent & event) { + return this->on_mouse_moved(event); + }; + + // Use EventManager for subscribing + EventManager::get_instance().subscribe(std::move(mouse_click_handler), listenerId); + EventManager::get_instance().subscribe(std::move(mouse_press_handler), listenerId); + EventManager::get_instance().subscribe(std::move(mouse_release_handler), listenerId); + EventManager::get_instance().subscribe(std::move(mouse_move_handler), listenerId); +} + +void IMouseListener::unsubscribe_events(int listenerId) { + // Use EventManager for unsubscribing with correct event type names + EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseClickEvent)), "onMouseClicked", listenerId); + EventManager::get_instance().unsubscribe(std::type_index(typeid(MousePressEvent)), "onMousePressed", listenerId); + EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseReleaseEvent)), "onMouseReleased", listenerId); + EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseMoveEvent)), "onMouseMoved", listenerId); +} diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h new file mode 100644 index 0000000..9222905 --- /dev/null +++ b/src/crepe/api/IMouseListener.h @@ -0,0 +1,24 @@ +#pragma once +#include "Event.h" +#include "EventHandler.h" +#include "EventManager.h" + +class IMouseListener { +public: + virtual ~IMouseListener(); + + virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0; + virtual bool on_mouse_pressed(const MousePressEvent & event) = 0; + virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0; + virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; + +protected: + void subscribe_events(int listenerId = 0); + void unsubscribe_events(int listenerId = 0); + +private: + EventHandler mouse_click_handler; + EventHandler mouse_press_handler; + EventHandler mouse_release_handler; + EventHandler mouse_move_handler; +}; diff --git a/src/makefile b/src/makefile index b9c44c8..03a2c0e 100644 --- a/src/makefile +++ b/src/makefile @@ -94,8 +94,16 @@ LOEK += example/script.cpp LOEK += test/audio.cpp LOEK += test/dummy.cpp JARO += test/PhysicsTest.cpp - -FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +WOUTER += crepe/api/Event.h +WOUTER += crepe/api/EventHandler.cpp +WOUTER += crepe/api/EventHandler.h +WOUTER += crepe/api/EventManager.cpp +WOUTER += crepe/api/EventManager.h +WOUTER += crepe/api/IKeyListener.cpp +WOUTER += crepe/api/IKeyListener.h +WOUTER += crepe/api/IMouseListener.cpp +WOUTER += crepe/api/IMouseListener.h +FMT := $(WOUTER) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -- cgit v1.2.3 From 80c74b90a3e44e25a4fa9fdd25bf0aa9efbaf6fd Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 16:12:09 +0100 Subject: interfaces working but unsubscribe broken --- src/crepe/api/CMakeLists.txt | 9 +++ src/crepe/api/EventHandler.h | 1 + src/crepe/api/EventManager.cpp | 120 +++++++++++++++++++-------------------- src/crepe/api/EventManager.h | 39 ++++++++++++- src/crepe/api/IKeyListener.cpp | 26 ++++++--- src/crepe/api/IKeyListener.h | 1 + src/crepe/api/IMouseListener.cpp | 25 ++++---- src/crepe/api/IMouseListener.h | 1 + src/example/CMakeLists.txt | 2 +- src/example/events.cpp | 105 ++++++++++++++++++++++++++++++++++ 10 files changed, 247 insertions(+), 82 deletions(-) create mode 100644 src/example/events.cpp diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 87cbb09..4e8f8a0 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -18,6 +18,10 @@ target_sources(crepe PUBLIC Vector2.cpp Camera.cpp Animator.cpp + EventManager.cpp + EventHandler.cpp + IKeyListener.cpp + IMouseListener.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -42,4 +46,9 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SceneManager.hpp Camera.h Animator.h + EventManager.h + EventHandler.h + Event.h + IKeyListener.h + IMouseListener.h ) diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index eea1c79..0da6d7d 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -24,6 +24,7 @@ class EventHandlerWrapper : public IEventHandlerWrapper { public: explicit EventHandlerWrapper(const EventHandler & handler) : m_handler(handler), m_handler_type(m_handler.target_type().name()) { + std::cout << "subscribe name: " << m_handler_type << std::endl; } private: diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 07ef1f7..7a8be53 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,69 +1,69 @@ #include "EventManager.h" -void EventManager::unsubscribe(std::type_index eventType, const std::string & handlerName, - int eventId) { - // if (eventId) { - // std::unordered_map< - // std::type_index, std::unordered_map< - // int, std::vector>>>::iterator subscriber_list = subscribers_by_event_id.find(eventType); - // if (subscriber_list != subscribers_by_event_id.end()) { - // std::unordered_map< - // int, std::vector>> & - // handlers_map - // = subscriber_list->second; - // std::unordered_map< - // int, - // std::vector>>::iterator - // handlers - // = handlers_map.find(eventId); - // if (handlers != handlers_map.end()) { - // std::vector> & callbacks - // = handlers->second; - // for (std::vector< - // std::unique_ptr>::iterator it - // = callbacks.begin(); - // it != callbacks.end(); ++it) { - // if (it->get()->getType() == handlerName) { - // it = callbacks.erase(it); - // return; - // } - // } - // } - // } - // } else { - // std::unordered_map< - // std::type_index, std::vector>>::iterator - // handlers_it - // = subscribers.find(eventType); - // if (handlers_it != subscribers.end()) { - // std::vector> & handlers - // = handlers_it->second; - // for (std::vector>::iterator it - // = handlers.begin(); - // it != handlers.end(); ++it) { - // if (it->get()->getType() == handlerName) { - // it = handlers.erase(it); - // return; - // } - // } - // } - // } -} +// void EventManager::unsubscribe(std::type_index eventType, const std::string & handlerName, +// int eventId) { +// if (eventId) { +// std::unordered_map< +// std::type_index, std::unordered_map< +// int, std::vector>>>::iterator subscriber_list = subscribers_by_event_id.find(eventType); +// if (subscriber_list != subscribers_by_event_id.end()) { +// std::unordered_map< +// int, std::vector>> & +// handlers_map +// = subscriber_list->second; +// std::unordered_map< +// int, +// std::vector>>::iterator +// handlers +// = handlers_map.find(eventId); +// if (handlers != handlers_map.end()) { +// std::vector> & callbacks +// = handlers->second; +// for (std::vector< +// std::unique_ptr>::iterator it +// = callbacks.begin(); +// it != callbacks.end(); ++it) { +// if (it->get()->get_type() == handlerName) { +// it = callbacks.erase(it); +// return; +// } +// } +// } +// } +// } else { +// std::unordered_map< +// std::type_index, std::vector>>::iterator +// handlers_it +// = subscribers.find(eventType); +// if (handlers_it != subscribers.end()) { +// std::vector> & handlers +// = handlers_it->second; +// for (std::vector>::iterator it +// = handlers.begin(); +// it != handlers.end(); ++it) { +// if (it->get()->get_type() == handlerName) { +// it = handlers.erase(it); +// return; +// } +// } +// } +// } +// } void EventManager::queue_event(std::unique_ptr && event_, int eventId) { events_queue.emplace_back(std::move(event_), eventId); } void EventManager::dispatch_events() { - // for (std::vector, int>>::iterator eventIt - // = eventsQueue.begin(); - // eventIt != eventsQueue.end();) { - // bool handled = false; - // if (!handled) { - // triggerEvent(*eventIt->first.get(), eventIt->second); - // eventIt = eventsQueue.erase(eventIt); - // } else { - // ++eventIt; - // } - // } + for (std::vector, int>>::iterator event_it + = events_queue.begin(); + event_it != events_queue.end();) { + bool handled = false; + if (!handled) { + trigger_event(*event_it->first.get(), event_it->second); + event_it = events_queue.erase(event_it); + } else { + ++event_it; + } + } } diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 8c0685a..88a386a 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -20,7 +20,8 @@ public: } template void subscribe(EventHandler && callback, int channel = 0); - void unsubscribe(std::type_index eventType, const std::string & handlerName,int channel); + template + void unsubscribe(const EventHandler &, int eventId); template void trigger_event(const EventType & event, int channel); void queue_event(std::unique_ptr && event, int channel); @@ -74,3 +75,39 @@ void EventManager::trigger_event(const EventType & event, int eventId) { } } } +template +void EventManager::unsubscribe(const EventHandler & callback, int channel) { + std::type_index event_type(typeid(EventType)); + std::string handler_name(callback.target_type().name()); + std::cout << "unsubcribe name: " << handler_name << std::endl; + + if (channel) { + auto subscriber_list = subscribers_by_event_id.find(event_type); + if (subscriber_list != subscribers_by_event_id.end()) { + auto& handlers_map = subscriber_list->second; + auto handlers = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + auto& callbacks = handlers->second; + for (auto it = callbacks.begin(); it != callbacks.end(); ++it) { + if ((*it)->get_type() == handler_name) { + std::cout << "successfully erased an event" << std::endl; + it = callbacks.erase(it); + return; + } + } + } + } + } else { + auto handlers_it = subscribers.find(event_type); + if (handlers_it != subscribers.end()) { + auto& handlers = handlers_it->second; + for (auto it = handlers.begin(); it != handlers.end(); ++it) { + if ((*it)->get_type() == handler_name) { + std::cout << "successfully erased an event" << std::endl; + it = handlers.erase(it); + return; + } + } + } + } +} diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index b1ad5e5..f35dc20 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -1,6 +1,13 @@ #include "IKeyListener.h" - -IKeyListener::~IKeyListener() { unsubscribe_events(); } +#include +IKeyListener::IKeyListener(){ + + subscribe_events(0); +} +IKeyListener::~IKeyListener() { + std::cout << "destructor call" << std::endl; + unsubscribe_events(); + } void IKeyListener::subscribe_events(int listenerId) { auto key_pressed_handler = [this](const KeyPressEvent & event) { @@ -9,12 +16,17 @@ void IKeyListener::subscribe_events(int listenerId) { auto key_released_handler = [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }; - - EventManager::get_instance().subscribe(std::move(key_pressed_handler), listenerId); - EventManager::get_instance().subscribe(std::move(key_released_handler), listenerId); + std::cout << "IKeyListener subscribe: " << std::endl; + EventManager::get_instance().subscribe(key_pressed_handler, listenerId); + EventManager::get_instance().subscribe(key_released_handler, listenerId); + std::cout << std::endl; } void IKeyListener::unsubscribe_events(int listenerId) { - EventManager::get_instance().unsubscribe(std::type_index(typeid(KeyPressEvent)), "on_key_pressed", listenerId); - EventManager::get_instance().unsubscribe(std::type_index(typeid(KeyReleaseEvent)), "on_key_released", listenerId); + std::cout << "IKeyListener unsubscribe: " << std::endl; + std::cout << "key_pressed_handler name: " << key_pressed_handler.target_type().name() << std::endl; + std::cout << "key_released_handler name: " << key_released_handler.target_type().name() << std::endl; + EventManager::get_instance().unsubscribe(key_pressed_handler , listenerId); + EventManager::get_instance().unsubscribe(key_released_handler , listenerId); + std::cout << std::endl; } diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 4fd2238..fc8de16 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -4,6 +4,7 @@ #include "EventManager.h" class IKeyListener { public: + IKeyListener(); virtual ~IKeyListener(); virtual bool on_key_pressed(const KeyPressEvent & event) = 0; virtual bool on_key_released(const KeyReleaseEvent & event) = 0; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 043188c..515d801 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -1,25 +1,25 @@ #include "IMouseListener.h" - +IMouseListener::IMouseListener(){ + subscribe_events(); +} IMouseListener::~IMouseListener() { unsubscribe_events(); } void IMouseListener::subscribe_events(int listenerId) { - // Wrap member functions in lambdas to ensure correct function signatures - mouse_click_handler = [this](const MouseClickEvent & event) { + // Define handler lambdas and subscribe them + auto mouse_click_handler = [this](const MouseClickEvent& event) { return this->on_mouse_clicked(event); }; - mouse_press_handler = [this](const MousePressEvent & event) { + auto mouse_press_handler = [this](const MousePressEvent& event) { return this->on_mouse_pressed(event); }; - mouse_release_handler = [this](const MouseReleaseEvent & event) { + auto mouse_release_handler = [this](const MouseReleaseEvent& event) { return this->on_mouse_released(event); }; - mouse_move_handler = [this](const MouseMoveEvent & event) { + auto mouse_move_handler = [this](const MouseMoveEvent& event) { return this->on_mouse_moved(event); }; - - // Use EventManager for subscribing EventManager::get_instance().subscribe(std::move(mouse_click_handler), listenerId); EventManager::get_instance().subscribe(std::move(mouse_press_handler), listenerId); EventManager::get_instance().subscribe(std::move(mouse_release_handler), listenerId); @@ -27,9 +27,8 @@ void IMouseListener::subscribe_events(int listenerId) { } void IMouseListener::unsubscribe_events(int listenerId) { - // Use EventManager for unsubscribing with correct event type names - EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseClickEvent)), "onMouseClicked", listenerId); - EventManager::get_instance().unsubscribe(std::type_index(typeid(MousePressEvent)), "onMousePressed", listenerId); - EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseReleaseEvent)), "onMouseReleased", listenerId); - EventManager::get_instance().unsubscribe(std::type_index(typeid(MouseMoveEvent)), "onMouseMoved", listenerId); + EventManager::get_instance().unsubscribe(mouse_click_handler, listenerId); + EventManager::get_instance().unsubscribe(mouse_press_handler, listenerId); + EventManager::get_instance().unsubscribe(mouse_release_handler, listenerId); + EventManager::get_instance().unsubscribe(mouse_move_handler, listenerId); } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 9222905..1078257 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -5,6 +5,7 @@ class IMouseListener { public: + IMouseListener(); virtual ~IMouseListener(); virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0; diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 36f9d4d..da6ceba 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -28,4 +28,4 @@ add_example(proxy) add_example(db) add_example(ecs) add_example(scene_manager) - +add_example(events) diff --git a/src/example/events.cpp b/src/example/events.cpp new file mode 100644 index 0000000..53eb97f --- /dev/null +++ b/src/example/events.cpp @@ -0,0 +1,105 @@ +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "crepe/api/Event.h" +#include "crepe/api/EventManager.h" +#include "crepe/api/IKeyListener.h" +#include "crepe/api/IMouseListener.h" +using namespace crepe; +using namespace std; +class MyScript : public Script, public IKeyListener,public IMouseListener{ + void update() { + // Retrieve component from the same GameObject this script is on + Transform & test = get_component(); + dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y); + } + + bool on_key_pressed(const KeyPressEvent & event) override{ + std::cout << "KeyPressed function" << std::endl; + return false; + } + bool on_key_released(const KeyReleaseEvent & event) override{ + std::cout << "KeyRelease function" << std::endl; + return false; + } + bool on_mouse_clicked(const MouseClickEvent & event){ + std::cout << "MouseClick function" << std::endl; + return false; + } + bool on_mouse_pressed(const MousePressEvent & event){ + std::cout << "MousePress function" << std::endl; + return false; + } + bool on_mouse_released(const MouseReleaseEvent & event){ + std::cout << "MouseRelease function" << std::endl; + return false; + } + bool on_mouse_moved(const MouseMoveEvent & event){ + std::cout << "MouseMove function" << std::endl; + return false; + } + +}; +class TestKeyListener : public IKeyListener { +public: + bool on_key_pressed(const KeyPressEvent &event) override { + std::cout << "TestKeyListener: Key Pressed - Code: " << event.key << std::endl; + return true; // Return true if the listener should remain active + } + bool on_key_released(const KeyReleaseEvent &event) override { + std::cout << "TestKeyListener: Key Released - Code: " << event.key << std::endl; + return true; + } +}; +int main() { + { + // Instantiate TestKeyListener, which subscribes to key events + TestKeyListener testListener; + + // Create game object with Transform and BehaviorScript components + auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); + obj.add_component().set_script(); + + // Get ScriptSystem singleton instance (this would normally be done from the game loop) + ScriptSystem sys; + sys.update(); + + // Trigger some events + KeyPressEvent key_press; + key_press.key = 1; + key_press.repeat = 0; + + MouseClickEvent click_event; + click_event.button = MouseButton::LEFT_MOUSE; + click_event.mouse_x = 100; + click_event.mouse_y = 200; + std::cout << "testing custom listener:" << std::endl; + // Trigger the events while `testListener` is in scope + EventManager::get_instance().trigger_event(key_press, 0); + EventManager::get_instance().trigger_event(click_event, 0); + } // End of scope - triggers destruction of obj, MyScript, and `testListener`, unsubscribing from events + // After `testListener` is destroyed, triggering events again should have no output if unsubscription worked + KeyPressEvent key_press; + key_press.key = 1; + key_press.repeat = 0; + EventManager::get_instance().trigger_event(key_press, 0); + + MouseClickEvent click_event; + click_event.button = MouseButton::LEFT_MOUSE; + click_event.mouse_x = 100; + click_event.mouse_y = 200; + EventManager::get_instance().trigger_event(click_event, 0); + + return EXIT_SUCCESS; +} + + -- cgit v1.2.3 From 9ee280d8149f84f4240902c3363a6881bf425137 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 18:41:52 +0100 Subject: backup --- src/crepe/api/EventManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 88a386a..91120fd 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -78,7 +78,7 @@ void EventManager::trigger_event(const EventType & event, int eventId) { template void EventManager::unsubscribe(const EventHandler & callback, int channel) { std::type_index event_type(typeid(EventType)); - std::string handler_name(callback.target_type().name()); + const std::string handler_name = callback.target_type().name(); std::cout << "unsubcribe name: " << handler_name << std::endl; if (channel) { -- cgit v1.2.3 From 48fb3f48831b1db187942592343eb4a2dc6662fc Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 12 Nov 2024 12:45:19 +0100 Subject: dispatch and queue working --- lib/sdl2 | 2 +- lib/sdl_image | 2 +- lib/sdl_ttf | 2 +- src/crepe/api/Event.h | 1 + src/crepe/api/EventHandler.h | 1 - src/crepe/api/EventManager.cpp | 96 ++++++++++++++-------------------------- src/crepe/api/EventManager.h | 21 ++++++--- src/crepe/api/IKeyListener.cpp | 16 +++---- src/crepe/api/IMouseListener.cpp | 8 ++-- src/example/events.cpp | 51 +++++++++++---------- 10 files changed, 89 insertions(+), 111 deletions(-) diff --git a/lib/sdl2 b/lib/sdl2 index 9519b99..c98c4fb 160000 --- a/lib/sdl2 +++ b/lib/sdl2 @@ -1 +1 @@ -Subproject commit 9519b9916cd29a14587af0507292f2bd31dd5752 +Subproject commit c98c4fbff6d8f3016a3ce6685bf8f43433c3efcc diff --git a/lib/sdl_image b/lib/sdl_image index abcf63a..5656019 160000 --- a/lib/sdl_image +++ b/lib/sdl_image @@ -1 +1 @@ -Subproject commit abcf63aa71b4e3ac32120fa9870a6500ddcdcc89 +Subproject commit 56560190a6c5b5740e5afdce747e2a2bfbf16db1 diff --git a/lib/sdl_ttf b/lib/sdl_ttf index 4a318f8..a3d0895 160000 --- a/lib/sdl_ttf +++ b/lib/sdl_ttf @@ -1 +1 @@ -Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440 +Subproject commit a3d0895c1b60c41ff9e85d9203ddd7485c014dae diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 8d9b241..c0de4fc 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -19,6 +19,7 @@ enum class MouseButton { }; class Event { public: +bool handled = false; }; class KeyPressEvent : public Event { diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 0da6d7d..eea1c79 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -24,7 +24,6 @@ class EventHandlerWrapper : public IEventHandlerWrapper { public: explicit EventHandlerWrapper(const EventHandler & handler) : m_handler(handler), m_handler_type(m_handler.target_type().name()) { - std::cout << "subscribe name: " << m_handler_type << std::endl; } private: diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 7a8be53..869ec74 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,69 +1,39 @@ #include "EventManager.h" -// void EventManager::unsubscribe(std::type_index eventType, const std::string & handlerName, -// int eventId) { -// if (eventId) { -// std::unordered_map< -// std::type_index, std::unordered_map< -// int, std::vector>>>::iterator subscriber_list = subscribers_by_event_id.find(eventType); -// if (subscriber_list != subscribers_by_event_id.end()) { -// std::unordered_map< -// int, std::vector>> & -// handlers_map -// = subscriber_list->second; -// std::unordered_map< -// int, -// std::vector>>::iterator -// handlers -// = handlers_map.find(eventId); -// if (handlers != handlers_map.end()) { -// std::vector> & callbacks -// = handlers->second; -// for (std::vector< -// std::unique_ptr>::iterator it -// = callbacks.begin(); -// it != callbacks.end(); ++it) { -// if (it->get()->get_type() == handlerName) { -// it = callbacks.erase(it); -// return; -// } -// } -// } -// } -// } else { -// std::unordered_map< -// std::type_index, std::vector>>::iterator -// handlers_it -// = subscribers.find(eventType); -// if (handlers_it != subscribers.end()) { -// std::vector> & handlers -// = handlers_it->second; -// for (std::vector>::iterator it -// = handlers.begin(); -// it != handlers.end(); ++it) { -// if (it->get()->get_type() == handlerName) { -// it = handlers.erase(it); -// return; -// } -// } -// } -// } -// } -void EventManager::queue_event(std::unique_ptr && event_, int eventId) { - events_queue.emplace_back(std::move(event_), eventId); -} void EventManager::dispatch_events() { - for (std::vector, int>>::iterator event_it - = events_queue.begin(); - event_it != events_queue.end();) { - bool handled = false; - if (!handled) { - trigger_event(*event_it->first.get(), event_it->second); - event_it = events_queue.erase(event_it); - } else { - ++event_it; - } - } + for (auto event_it = events_queue.begin(); event_it != events_queue.end();) { + auto& event = std::get<0>(*event_it); + int channel = std::get<1>(*event_it); + std::type_index event_type = std::get<2>(*event_it); + if (channel) { + auto handlers_it = subscribers_by_event_id.find(event_type); + if (handlers_it != subscribers_by_event_id.end()) { + auto& handlers_map = handlers_it->second; + auto handlers = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + auto& callbacks = handlers->second; + for (auto& handler : callbacks) { + handler->exec(*event); + } + } + } + } else { + auto handlers_it = subscribers.find(event_type); + if (handlers_it != subscribers.end()) { + auto& handlers = handlers_it->second; + for (auto& handler : handlers) { + handler->exec(*event); + } + } + } + + if (event->handled) { + event_it = events_queue.erase(event_it); + } else { + ++event_it; + } + } } + diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 91120fd..be9a947 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -24,12 +24,13 @@ public: void unsubscribe(const EventHandler &, int eventId); template void trigger_event(const EventType & event, int channel); - void queue_event(std::unique_ptr && event, int channel); + template + void queue_event(EventType&& event, int channel); void dispatch_events(); private: EventManager() = default; - std::vector, int>> events_queue; + std::vector, int, std::type_index>> events_queue; std::unordered_map>> subscribers; std::unordered_map>>> subscribers_by_event_id; }; @@ -51,6 +52,19 @@ void EventManager::subscribe(EventHandler && callback, int channel){ subscribers[event_type].emplace_back(std::move(handler)); } } +template +void EventManager::queue_event(EventType&& event, int channel) { + std::type_index event_type = std::type_index(typeid(EventType)); + + std::unique_ptr event_ptr = std::make_unique(std::forward(event)); + + std::tuple, int, std::type_index> tuple( + std::move(event_ptr), + channel, + event_type + ); + events_queue.push_back(std::move(tuple)); +} template void EventManager::trigger_event(const EventType & event, int eventId) { @@ -79,7 +93,6 @@ template void EventManager::unsubscribe(const EventHandler & callback, int channel) { std::type_index event_type(typeid(EventType)); const std::string handler_name = callback.target_type().name(); - std::cout << "unsubcribe name: " << handler_name << std::endl; if (channel) { auto subscriber_list = subscribers_by_event_id.find(event_type); @@ -90,7 +103,6 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha auto& callbacks = handlers->second; for (auto it = callbacks.begin(); it != callbacks.end(); ++it) { if ((*it)->get_type() == handler_name) { - std::cout << "successfully erased an event" << std::endl; it = callbacks.erase(it); return; } @@ -103,7 +115,6 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha auto& handlers = handlers_it->second; for (auto it = handlers.begin(); it != handlers.end(); ++it) { if ((*it)->get_type() == handler_name) { - std::cout << "successfully erased an event" << std::endl; it = handlers.erase(it); return; } diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index f35dc20..663dbc7 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -5,27 +5,21 @@ IKeyListener::IKeyListener(){ subscribe_events(0); } IKeyListener::~IKeyListener() { - std::cout << "destructor call" << std::endl; unsubscribe_events(); - } +} void IKeyListener::subscribe_events(int listenerId) { - auto key_pressed_handler = [this](const KeyPressEvent & event) { + key_pressed_handler = [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }; - auto key_released_handler = [this](const KeyReleaseEvent & event) { + key_released_handler = [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }; - std::cout << "IKeyListener subscribe: " << std::endl; - EventManager::get_instance().subscribe(key_pressed_handler, listenerId); - EventManager::get_instance().subscribe(key_released_handler, listenerId); - std::cout << std::endl; + EventManager::get_instance().subscribe(std::move(key_pressed_handler), listenerId); + EventManager::get_instance().subscribe(std::move(key_released_handler), listenerId); } void IKeyListener::unsubscribe_events(int listenerId) { - std::cout << "IKeyListener unsubscribe: " << std::endl; - std::cout << "key_pressed_handler name: " << key_pressed_handler.target_type().name() << std::endl; - std::cout << "key_released_handler name: " << key_released_handler.target_type().name() << std::endl; EventManager::get_instance().unsubscribe(key_pressed_handler , listenerId); EventManager::get_instance().unsubscribe(key_released_handler , listenerId); std::cout << std::endl; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 515d801..bb59664 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -8,16 +8,16 @@ IMouseListener::~IMouseListener() { void IMouseListener::subscribe_events(int listenerId) { // Define handler lambdas and subscribe them - auto mouse_click_handler = [this](const MouseClickEvent& event) { + mouse_click_handler = [this](const MouseClickEvent& event) { return this->on_mouse_clicked(event); }; - auto mouse_press_handler = [this](const MousePressEvent& event) { + mouse_press_handler = [this](const MousePressEvent& event) { return this->on_mouse_pressed(event); }; - auto mouse_release_handler = [this](const MouseReleaseEvent& event) { + mouse_release_handler = [this](const MouseReleaseEvent& event) { return this->on_mouse_released(event); }; - auto mouse_move_handler = [this](const MouseMoveEvent& event) { + mouse_move_handler = [this](const MouseMoveEvent& event) { return this->on_mouse_moved(event); }; EventManager::get_instance().subscribe(std::move(mouse_click_handler), listenerId); diff --git a/src/example/events.cpp b/src/example/events.cpp index 53eb97f..1f757e1 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -31,19 +31,19 @@ class MyScript : public Script, public IKeyListener,public IMouseListener{ std::cout << "KeyRelease function" << std::endl; return false; } - bool on_mouse_clicked(const MouseClickEvent & event){ + bool on_mouse_clicked(const MouseClickEvent & event) override{ std::cout << "MouseClick function" << std::endl; return false; } - bool on_mouse_pressed(const MousePressEvent & event){ + bool on_mouse_pressed(const MousePressEvent & event) override { std::cout << "MousePress function" << std::endl; return false; } - bool on_mouse_released(const MouseReleaseEvent & event){ + bool on_mouse_released(const MouseReleaseEvent & event) override { std::cout << "MouseRelease function" << std::endl; return false; } - bool on_mouse_moved(const MouseMoveEvent & event){ + bool on_mouse_moved(const MouseMoveEvent & event) override { std::cout << "MouseMove function" << std::endl; return false; } @@ -61,6 +61,17 @@ public: } }; int main() { + // two events to trigger + KeyPressEvent key_press; + key_press.key = 1; + key_press.repeat = 0; + MouseClickEvent click_event; + click_event.button = MouseButton::LEFT_MOUSE; + click_event.mouse_x = 100; + click_event.mouse_y = 200; + // queue events to test queue + EventManager::get_instance().queue_event(std::move(key_press), 0); + EventManager::get_instance().queue_event(std::move(click_event), 0); { // Instantiate TestKeyListener, which subscribes to key events TestKeyListener testListener; @@ -73,32 +84,24 @@ int main() { ScriptSystem sys; sys.update(); - // Trigger some events - KeyPressEvent key_press; - key_press.key = 1; - key_press.repeat = 0; - - MouseClickEvent click_event; - click_event.button = MouseButton::LEFT_MOUSE; - click_event.mouse_x = 100; - click_event.mouse_y = 200; - std::cout << "testing custom listener:" << std::endl; // Trigger the events while `testListener` is in scope EventManager::get_instance().trigger_event(key_press, 0); EventManager::get_instance().trigger_event(click_event, 0); - } // End of scope - triggers destruction of obj, MyScript, and `testListener`, unsubscribing from events - // After `testListener` is destroyed, triggering events again should have no output if unsubscription worked - KeyPressEvent key_press; - key_press.key = 1; - key_press.repeat = 0; - EventManager::get_instance().trigger_event(key_press, 0); + } + // custom lambda event handler + EventHandler event_handler = [](const KeyPressEvent& e) { + std::cout << "lambda test" << std::endl; + return false; + }; + EventManager::get_instance().subscribe(std::move(event_handler),0); + // - MouseClickEvent click_event; - click_event.button = MouseButton::LEFT_MOUSE; - click_event.mouse_x = 100; - click_event.mouse_y = 200; + EventManager::get_instance().trigger_event(key_press, 0); + EventManager::get_instance().trigger_event(click_event, 0); + EventManager::get_instance().dispatch_events(); + EventManager::get_instance().unsubscribe(event_handler,0); return EXIT_SUCCESS; } -- cgit v1.2.3 From 80e4e1eb78898879eed11648808ca6b65eecb304 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 12 Nov 2024 15:08:37 +0100 Subject: removed auto from code --- src/crepe/api/EventHandler.h | 80 +++++++++++++++++++- src/crepe/api/EventManager.cpp | 21 ++++-- src/crepe/api/EventManager.h | 162 +++++++++++++++++++++++++++++++---------- src/crepe/api/IKeyListener.cpp | 20 ++++- src/crepe/api/IKeyListener.h | 8 +- src/makefile | 3 +- 6 files changed, 240 insertions(+), 54 deletions(-) diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index eea1c79..5b9ac0d 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -4,35 +4,109 @@ #include #include "Event.h" +/** + * \brief A type alias for an event handler function. + * + * The EventHandler is a std::function that takes an EventType reference and returns a boolean value + * indicating whether the event was successfully handled. + * + * \tparam EventType The type of event this handler will handle. + */ template using EventHandler = std::function; +/** + * \class IEventHandlerWrapper + * \brief An abstract base class for event handler wrappers. + * + * This class provides the interface for handling events. Derived classes must implement the + * `call()` method to process events and the `get_type()` method to return the handler's type. + */ class IEventHandlerWrapper { public: + /** + * \brief Virtual destructor for IEventHandlerWrapper. + */ virtual ~IEventHandlerWrapper() = default; + /** + * \brief Executes the handler with the given event. + * + * This method calls the `call()` method of the derived class, passing the event to the handler. + * + * \param e The event to be processed. + * \return A boolean value indicating whether the event is handled. + */ bool exec(const Event & e); + /** + * \brief Get the type of the event handler. + * + * This method returns the type of the event handler as a string. + * + * \return A string representing the handler's type. + */ virtual std::string get_type() const = 0; private: + /** + * \brief The method responsible for handling the event. + * + * This method is implemented by derived classes to process the event. + * + * \param e The event to be processed. + * \return A boolean value indicating whether the event is handled. + */ virtual bool call(const Event & e) = 0; }; +/** + * \class EventHandlerWrapper + * \brief A wrapper for event handler functions. + * + * This class wraps an event handler function of a specific event type. It implements the + * `call()` and `get_type()` methods to allow the handler to be executed and its type to be + * queried. + * + * \tparam EventType The type of event this handler will handle. + */ template class EventHandlerWrapper : public IEventHandlerWrapper { public: + /** + * \brief Constructs an EventHandlerWrapper with a given handler. + * + * The constructor takes an event handler function and stores it in the wrapper. + * + * \param handler The event handler function. + */ explicit EventHandlerWrapper(const EventHandler & handler) : m_handler(handler), m_handler_type(m_handler.target_type().name()) { } private: + /** + * \brief Calls the stored event handler with the event. + * + * This method casts the event to the appropriate type and calls the handler. + * + * \param e The event to be handled. + * \return A boolean value indicating whether the event is handled. + */ bool call(const Event & e) override { return m_handler(static_cast(e)); } + /** + * \brief Returns the type of the handler. + * + * This method returns a string representing the type of the event handler. + * + * \return The handler type as a string. + */ std::string get_type() const override { return m_handler_type; } - - EventHandler m_handler; - const std::string m_handler_type; + //! The event handler function. + EventHandler m_handler; + //! The type name of the handler function. + const std::string m_handler_type; }; diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 869ec74..3d8558b 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -7,6 +7,9 @@ void EventManager::dispatch_events() { auto& event = std::get<0>(*event_it); int channel = std::get<1>(*event_it); std::type_index event_type = std::get<2>(*event_it); + + bool event_handled = false; + if (channel) { auto handlers_it = subscribers_by_event_id.find(event_type); if (handlers_it != subscribers_by_event_id.end()) { @@ -15,25 +18,33 @@ void EventManager::dispatch_events() { if (handlers != handlers_map.end()) { auto& callbacks = handlers->second; for (auto& handler : callbacks) { - handler->exec(*event); + if (handler->exec(*event)) { + event_it = events_queue.erase(event_it); + event_handled = true; + break; + } } } } } else { + // Handle event for all channels auto handlers_it = subscribers.find(event_type); if (handlers_it != subscribers.end()) { auto& handlers = handlers_it->second; for (auto& handler : handlers) { - handler->exec(*event); + if (handler->exec(*event)) { + event_it = events_queue.erase(event_it); + event_handled = true; + break; + } } } } - if (event->handled) { - event_it = events_queue.erase(event_it); - } else { + if (!event_handled) { ++event_it; } } } + diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index be9a947..9b69b29 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -10,48 +10,127 @@ #include "EventHandler.h" //#include "keyCodes.h" +/** + * @class EventManager + * @brief The EventManager class is responsible for managing the subscription, triggering, + * and queueing of events. It handles events and dispatches them to appropriate subscribers. + */ class EventManager { public: - EventManager(const EventManager &) = delete; - const EventManager & operator=(const EventManager &) = delete; - static EventManager & get_instance() { - static EventManager instance; - return instance; - } - template - void subscribe(EventHandler && callback, int channel = 0); - template - void unsubscribe(const EventHandler &, int eventId); - template - void trigger_event(const EventType & event, int channel); - template - void queue_event(EventType&& event, int channel); - void dispatch_events(); + /** + * \brief Deleted copy constructor to prevent copying of the EventManager instance. + */ + EventManager(const EventManager &) = delete; + + /** + * @brief Deleted copy assignment operator to prevent assignment of the EventManager instance. + */ + const EventManager & operator=(const EventManager &) = delete; + + /** + * @brief Get the singleton instance of the EventManager. + * + * This method returns the unique instance of the EventManager, creating it on the first call. + * + * @return Reference to the EventManager instance. + */ + static EventManager & get_instance() { + static EventManager instance; + return instance; + } + + /** + * @brief Subscribe to an event. + * + * This method allows the registration of a callback for a specific event type and channel. + * + * @tparam EventType The type of the event to subscribe to. + * @param callback The callback function to invoke when the event is triggered. + * @param channel The channel number to subscribe to (default is 0). + */ + template + void subscribe(EventHandler && callback, int channel = 0); + + /** + * @brief Unsubscribe from an event. + * + * This method removes a previously registered callback from an event. + * + * @tparam EventType The type of the event to unsubscribe from. + * @param callback The callback function to remove from the subscription list. + * @param eventId The event ID to unsubscribe from. + */ + template + void unsubscribe(const EventHandler &, int eventId); + + /** + * @brief Trigger an event. + * + * This method invokes the appropriate callback(s) for the specified event. + * + * @tparam EventType The type of the event to trigger. + * @param event The event data to pass to the callback. + * @param channel The channel from which to trigger the event (default is 0). + */ + template + void trigger_event(const EventType & event, int channel); + + /** + * @brief Queue an event for later processing. + * + * This method adds an event to the event queue, which will be processed in the + * dispatch_events function. + * + * @tparam EventType The type of the event to queue. + * @param event The event to queue. + * @param channel The channel number for the event (default is 0). + */ + template + void queue_event(EventType&& event, int channel); + + /** + * @brief Dispatch all queued events. + * + * This method processes all events in the event queue and triggers the corresponding + * callbacks for each event. + */ + void dispatch_events(); private: - EventManager() = default; - std::vector, int, std::type_index>> events_queue; - std::unordered_map>> subscribers; - std::unordered_map>>> subscribers_by_event_id; + /** + * @brief Default constructor for the EventManager. + * + * This constructor is private to enforce the singleton pattern. + */ + EventManager() = default; + + //! The queue of events to be processed. + std::vector, int, std::type_index>> events_queue; + //! Registered event handlers. + std::unordered_map>> subscribers; + //! Event handlers indexed by event ID. + std::unordered_map>>> subscribers_by_event_id; }; template -void EventManager::subscribe(EventHandler && callback, int channel){ +void EventManager::subscribe(EventHandler && callback, int channel) { std::type_index event_type = typeid(EventType); - auto handler = std::make_unique>(callback); + std::unique_ptr> handler = std::make_unique>(callback); if (channel) { - auto & handlers_map = subscribers_by_event_id[event_type]; - auto handlers = handlers_map.find(channel); + std::unordered_map>>& handlers_map = subscribers_by_event_id[event_type]; + std::unordered_map>>::iterator handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { handlers->second.emplace_back(std::move(handler)); } else { handlers_map[channel].emplace_back(std::move(handler)); } } else { - subscribers[event_type].emplace_back(std::move(handler)); + std::vector>& handlers = subscribers[event_type]; + handlers.emplace_back(std::move(handler)); } } + template void EventManager::queue_event(EventType&& event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); @@ -71,10 +150,12 @@ void EventManager::trigger_event(const EventType & event, int eventId) { std::type_index event_type = std::type_index(typeid(EventType)); if (eventId > 0) { - auto handlers_it = subscribers_by_event_id[event_type].find(eventId); - if (handlers_it != subscribers_by_event_id[event_type].end()) { - auto & callbacks = handlers_it->second; - for (auto it = callbacks.begin(); it != callbacks.end();) { + std::unordered_map>>& handlers_map = subscribers_by_event_id[event_type]; + std::unordered_map>>::iterator handlers_it = handlers_map.find(eventId); + + if (handlers_it != handlers_map.end()) { + std::vector>& callbacks = handlers_it->second; + for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end();) { if ((*it)->exec(event)) { it = callbacks.erase(it); } else { @@ -83,25 +164,26 @@ void EventManager::trigger_event(const EventType & event, int eventId) { } } } else { - auto & handlers = subscribers[event_type]; - for (auto & handler : handlers) { - handler->exec(event); + std::vector>& handlers = subscribers[event_type]; + for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { + (*it)->exec(event); } } } + template void EventManager::unsubscribe(const EventHandler & callback, int channel) { std::type_index event_type(typeid(EventType)); - const std::string handler_name = callback.target_type().name(); + std::string handler_name = callback.target_type().name(); if (channel) { - auto subscriber_list = subscribers_by_event_id.find(event_type); + std::unordered_map>>>::iterator subscriber_list = subscribers_by_event_id.find(event_type); if (subscriber_list != subscribers_by_event_id.end()) { - auto& handlers_map = subscriber_list->second; - auto handlers = handlers_map.find(channel); + std::unordered_map>>& handlers_map = subscriber_list->second; + std::unordered_map>>::iterator handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { - auto& callbacks = handlers->second; - for (auto it = callbacks.begin(); it != callbacks.end(); ++it) { + std::vector>& callbacks = handlers->second; + for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end(); ++it) { if ((*it)->get_type() == handler_name) { it = callbacks.erase(it); return; @@ -110,10 +192,10 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha } } } else { - auto handlers_it = subscribers.find(event_type); + std::unordered_map>>::iterator handlers_it = subscribers.find(event_type); if (handlers_it != subscribers.end()) { - auto& handlers = handlers_it->second; - for (auto it = handlers.begin(); it != handlers.end(); ++it) { + std::vector>& handlers = handlers_it->second; + for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { if ((*it)->get_type() == handler_name) { it = handlers.erase(it); return; diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index 663dbc7..3a06f77 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -1,8 +1,12 @@ #include "IKeyListener.h" #include IKeyListener::IKeyListener(){ - - subscribe_events(0); + this->channel = channel; + subscribe_events(); +} +IKeyListener::IKeyListener(int channel){ + this->channel = channel; + subscribe_events(channel); } IKeyListener::~IKeyListener() { unsubscribe_events(); @@ -24,3 +28,15 @@ void IKeyListener::unsubscribe_events(int listenerId) { EventManager::get_instance().unsubscribe(key_released_handler , listenerId); std::cout << std::endl; } +void IKeyListener::activate_keys(int listenerId) { + if(this->active){ + return; + } + subscribe_events(listenerId); +} +void IKeyListener::deactivate_keys() { + if(!this->active){ + return; + } + unsubscribe_events(); +} diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index fc8de16..83949bf 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -4,18 +4,22 @@ #include "EventManager.h" class IKeyListener { public: + IKeyListener(int channel); IKeyListener(); virtual ~IKeyListener(); virtual bool on_key_pressed(const KeyPressEvent & event) = 0; virtual bool on_key_released(const KeyReleaseEvent & event) = 0; + void activate_keys(int listenerId = 0); + void deactivate_keys(); protected: void subscribe_events(int listenerId = 0); void unsubscribe_events(int listenerId = 0); - void activate(int listenerId = 0) { subscribe_events(listenerId); } - void deactivate(int listenerId = 0) { unsubscribe_events(listenerId); } + private: + bool active = true; + int channel = 0; EventHandler key_pressed_handler; EventHandler key_released_handler; }; diff --git a/src/makefile b/src/makefile index 03a2c0e..f6907f6 100644 --- a/src/makefile +++ b/src/makefile @@ -103,10 +103,9 @@ WOUTER += crepe/api/IKeyListener.cpp WOUTER += crepe/api/IKeyListener.h WOUTER += crepe/api/IMouseListener.cpp WOUTER += crepe/api/IMouseListener.h -FMT := $(WOUTER) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -# FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') # TODO: re-enable linter after all corrections -- cgit v1.2.3 From ab0c50d5b719863ab9e9821e6720bba450e174ce Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 12:19:58 +0100 Subject: added activate/deactive to listeners and doxygen comments --- src/crepe/api/EventManager.cpp | 36 ++++++------- src/crepe/api/EventManager.h | 88 ++++++++++++++++-------------- src/crepe/api/IKeyListener.cpp | 27 +++++----- src/crepe/api/IKeyListener.h | 77 +++++++++++++++++++++----- src/crepe/api/IMouseListener.cpp | 42 ++++++++++----- src/crepe/api/IMouseListener.h | 114 +++++++++++++++++++++++++++++++++++---- src/example/events.cpp | 3 -- 7 files changed, 276 insertions(+), 111 deletions(-) diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 3d8558b..872c6eb 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,24 +1,22 @@ #include "EventManager.h" - - void EventManager::dispatch_events() { - for (auto event_it = events_queue.begin(); event_it != events_queue.end();) { - auto& event = std::get<0>(*event_it); - int channel = std::get<1>(*event_it); - std::type_index event_type = std::get<2>(*event_it); + for (std::vector, int, std::type_index>>::iterator event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { + std::unique_ptr& event = std::get<0>(*event_it); + int channel = std::get<1>(*event_it); + std::type_index event_type = std::get<2>(*event_it); bool event_handled = false; if (channel) { - auto handlers_it = subscribers_by_event_id.find(event_type); + std::unordered_map>>>::iterator handlers_it = subscribers_by_event_id.find(event_type); if (handlers_it != subscribers_by_event_id.end()) { - auto& handlers_map = handlers_it->second; - auto handlers = handlers_map.find(channel); + std::unordered_map>>& handlers_map = handlers_it->second; + std::unordered_map>>::iterator handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { - auto& callbacks = handlers->second; - for (auto& handler : callbacks) { - if (handler->exec(*event)) { + std::vector>& callbacks = handlers->second; + for (std::vector>::iterator handler_it = callbacks.begin(); handler_it != callbacks.end(); ++handler_it) { + if ((*handler_it)->exec(*event)) { event_it = events_queue.erase(event_it); event_handled = true; break; @@ -28,12 +26,12 @@ void EventManager::dispatch_events() { } } else { // Handle event for all channels - auto handlers_it = subscribers.find(event_type); - if (handlers_it != subscribers.end()) { - auto& handlers = handlers_it->second; - for (auto& handler : handlers) { - if (handler->exec(*event)) { - event_it = events_queue.erase(event_it); + std::unordered_map>>::iterator handlers_it = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + std::vector>& handlers = handlers_it->second; + for (std::vector>::iterator handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) { + if ((*handler_it)->exec(*event)) { + event_it = this->events_queue.erase(event_it); event_handled = true; break; } @@ -46,5 +44,3 @@ void EventManager::dispatch_events() { } } } - - diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 9b69b29..d3a14da 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -1,18 +1,18 @@ #pragma once + #include #include #include #include -#include #include #include + #include "Event.h" #include "EventHandler.h" -//#include "keyCodes.h" /** - * @class EventManager - * @brief The EventManager class is responsible for managing the subscription, triggering, + * \class EventManager + * \brief The EventManager class is responsible for managing the subscription, triggering, * and queueing of events. It handles events and dispatches them to appropriate subscribers. */ class EventManager { @@ -23,16 +23,16 @@ public: EventManager(const EventManager &) = delete; /** - * @brief Deleted copy assignment operator to prevent assignment of the EventManager instance. + * \brief Deleted copy assignment operator to prevent assignment of the EventManager instance. */ const EventManager & operator=(const EventManager &) = delete; /** - * @brief Get the singleton instance of the EventManager. + * \brief Get the singleton instance of the EventManager. * * This method returns the unique instance of the EventManager, creating it on the first call. * - * @return Reference to the EventManager instance. + * \return Reference to the EventManager instance. */ static EventManager & get_instance() { static EventManager instance; @@ -40,56 +40,56 @@ public: } /** - * @brief Subscribe to an event. + * \brief Subscribe to an event. * * This method allows the registration of a callback for a specific event type and channel. * - * @tparam EventType The type of the event to subscribe to. - * @param callback The callback function to invoke when the event is triggered. - * @param channel The channel number to subscribe to (default is 0). + * \tparam EventType The type of the event to subscribe to. + * \param callback The callback function to invoke when the event is triggered. + * \param channel The channel number to subscribe to (default is 0). */ template void subscribe(EventHandler && callback, int channel = 0); /** - * @brief Unsubscribe from an event. + * \brief Unsubscribe from an event. * * This method removes a previously registered callback from an event. * - * @tparam EventType The type of the event to unsubscribe from. - * @param callback The callback function to remove from the subscription list. - * @param eventId The event ID to unsubscribe from. + * \tparam EventType The type of the event to unsubscribe from. + * \param callback The callback function to remove from the subscription list. + * \param channel The event ID to unsubscribe from. */ template - void unsubscribe(const EventHandler &, int eventId); + void unsubscribe(const EventHandler &, int channel); /** - * @brief Trigger an event. + * \brief Trigger an event. * * This method invokes the appropriate callback(s) for the specified event. * - * @tparam EventType The type of the event to trigger. - * @param event The event data to pass to the callback. - * @param channel The channel from which to trigger the event (default is 0). + * \tparam EventType The type of the event to trigger. + * \param event The event data to pass to the callback. + * \param channel The channel from which to trigger the event (default is 0). */ template void trigger_event(const EventType & event, int channel); /** - * @brief Queue an event for later processing. + * \brief Queue an event for later processing. * * This method adds an event to the event queue, which will be processed in the * dispatch_events function. * - * @tparam EventType The type of the event to queue. - * @param event The event to queue. - * @param channel The channel number for the event (default is 0). + * \tparam EventType The type of the event to queue. + * \param event The event to queue. + * \param channel The channel number for the event (default is 0). */ template void queue_event(EventType&& event, int channel); /** - * @brief Dispatch all queued events. + * \brief Dispatch all queued events. * * This method processes all events in the event queue and triggers the corresponding * callbacks for each event. @@ -98,7 +98,7 @@ public: private: /** - * @brief Default constructor for the EventManager. + * \brief Default constructor for the EventManager. * * This constructor is private to enforce the singleton pattern. */ @@ -118,7 +118,7 @@ void EventManager::subscribe(EventHandler && callback, int channel) { std::unique_ptr> handler = std::make_unique>(callback); if (channel) { - std::unordered_map>>& handlers_map = subscribers_by_event_id[event_type]; + std::unordered_map>>& handlers_map = this->subscribers_by_event_id[event_type]; std::unordered_map>>::iterator handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { handlers->second.emplace_back(std::move(handler)); @@ -126,7 +126,7 @@ void EventManager::subscribe(EventHandler && callback, int channel) { handlers_map[channel].emplace_back(std::move(handler)); } } else { - std::vector>& handlers = subscribers[event_type]; + std::vector>& handlers = this->subscribers[event_type]; handlers.emplace_back(std::move(handler)); } } @@ -142,22 +142,23 @@ void EventManager::queue_event(EventType&& event, int channel) { channel, event_type ); - events_queue.push_back(std::move(tuple)); + this->events_queue.push_back(std::move(tuple)); } template -void EventManager::trigger_event(const EventType & event, int eventId) { +void EventManager::trigger_event(const EventType & event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); - if (eventId > 0) { + if (channel > 0) { std::unordered_map>>& handlers_map = subscribers_by_event_id[event_type]; - std::unordered_map>>::iterator handlers_it = handlers_map.find(eventId); + std::unordered_map>>::iterator handlers_it = handlers_map.find(channel); if (handlers_it != handlers_map.end()) { - std::vector>& callbacks = handlers_it->second; - for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end();) { + std::vector>& handlers = handlers_it->second; + for (std::vector>::iterator it = handlers.begin(); it != handlers.end();) { + // erases callback if callback function returns true if ((*it)->exec(event)) { - it = callbacks.erase(it); + it = handlers.erase(it); } else { ++it; } @@ -165,8 +166,13 @@ void EventManager::trigger_event(const EventType & event, int eventId) { } } else { std::vector>& handlers = subscribers[event_type]; - for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { - (*it)->exec(event); + for (std::vector>::iterator it = handlers.begin(); it != handlers.end();) { + // erases callback if callback function returns true + if ((*it)->exec(event)) { + it = handlers.erase(it); + } else { + ++it; + } } } } @@ -177,8 +183,8 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha std::string handler_name = callback.target_type().name(); if (channel) { - std::unordered_map>>>::iterator subscriber_list = subscribers_by_event_id.find(event_type); - if (subscriber_list != subscribers_by_event_id.end()) { + std::unordered_map>>>::iterator subscriber_list = this->subscribers_by_event_id.find(event_type); + if (subscriber_list != this->subscribers_by_event_id.end()) { std::unordered_map>>& handlers_map = subscriber_list->second; std::unordered_map>>::iterator handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { @@ -192,8 +198,8 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha } } } else { - std::unordered_map>>::iterator handlers_it = subscribers.find(event_type); - if (handlers_it != subscribers.end()) { + std::unordered_map>>::iterator handlers_it = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { std::vector>& handlers = handlers_it->second; for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { if ((*it)->get_type() == handler_name) { diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index 3a06f77..17d8735 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -2,41 +2,44 @@ #include IKeyListener::IKeyListener(){ this->channel = channel; - subscribe_events(); + this->subscribe_events(); } IKeyListener::IKeyListener(int channel){ this->channel = channel; - subscribe_events(channel); + this->subscribe_events(); } IKeyListener::~IKeyListener() { - unsubscribe_events(); + this->unsubscribe_events(); } -void IKeyListener::subscribe_events(int listenerId) { +void IKeyListener::subscribe_events() { key_pressed_handler = [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }; key_released_handler = [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }; - EventManager::get_instance().subscribe(std::move(key_pressed_handler), listenerId); - EventManager::get_instance().subscribe(std::move(key_released_handler), listenerId); + EventManager::get_instance().subscribe(std::move(this->key_pressed_handler), this->channel); + EventManager::get_instance().subscribe(std::move(this->key_released_handler), this->channel); } -void IKeyListener::unsubscribe_events(int listenerId) { - EventManager::get_instance().unsubscribe(key_pressed_handler , listenerId); - EventManager::get_instance().unsubscribe(key_released_handler , listenerId); +void IKeyListener::unsubscribe_events() { + EventManager::get_instance().unsubscribe(this->key_pressed_handler , channel); + EventManager::get_instance().unsubscribe(this->key_released_handler , channel); std::cout << std::endl; } -void IKeyListener::activate_keys(int listenerId) { +void IKeyListener::activate_keys() { if(this->active){ return; } - subscribe_events(listenerId); + this->subscribe_events(); } void IKeyListener::deactivate_keys() { if(!this->active){ return; } - unsubscribe_events(); + this->unsubscribe_events(); +} +void IKeyListener::set_channel(int channel){ + this->channel = channel; } diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 83949bf..6d7915d 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -2,24 +2,77 @@ #include "Event.h" #include "EventHandler.h" #include "EventManager.h" + +/** + * \class IKeyListener + * \brief Interface for keyboard event handling in the application. + */ class IKeyListener { public: - IKeyListener(int channel); - IKeyListener(); + /** + * \brief Constructs an IKeyListener with a specified channel. + * \param channel The channel ID for event handling. + */ + IKeyListener(int channel); + + /** + * \brief Default constructor for IKeyListener. + */ + IKeyListener(); + + /** + * \brief Destructor. + */ virtual ~IKeyListener(); - virtual bool on_key_pressed(const KeyPressEvent & event) = 0; - virtual bool on_key_released(const KeyReleaseEvent & event) = 0; - void activate_keys(int listenerId = 0); + + /** + * \brief Pure virtual function to handle key press events. + * \param event The key press event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_key_pressed(const KeyPressEvent& event) = 0; + + /** + * \brief Pure virtual function to handle key release events. + * \param event The key release event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_key_released(const KeyReleaseEvent& event) = 0; + + /** + * \brief Activates key listening. + */ + void activate_keys(); + + /** + * \brief Deactivates key listening. + */ void deactivate_keys(); + /** + * \brief Sets the channel ID for event handling. + * \param channel The channel ID to set. + */ + void set_channel(int channel); + protected: - void subscribe_events(int listenerId = 0); - void unsubscribe_events(int listenerId = 0); - + /** + * \brief Subscribes to key events. + */ + void subscribe_events(); + + /** + * \brief Unsubscribes from key events. + */ + void unsubscribe_events(); private: - bool active = true; - int channel = 0; - EventHandler key_pressed_handler; - EventHandler key_released_handler; + //! Indicates whether key listening is active. + bool active = true; + //! Channel ID for event handling. + int channel = 0; + //! Key press event handler. + EventHandler key_pressed_handler; + //!< Key release event handler. + EventHandler key_released_handler; }; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index bb59664..68061be 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -1,12 +1,15 @@ #include "IMouseListener.h" +IMouseListener::IMouseListener(int channel){ + this->channel = channel; +} IMouseListener::IMouseListener(){ - subscribe_events(); + this->subscribe_events(); } IMouseListener::~IMouseListener() { - unsubscribe_events(); + this->unsubscribe_events(); } -void IMouseListener::subscribe_events(int listenerId) { +void IMouseListener::subscribe_events() { // Define handler lambdas and subscribe them mouse_click_handler = [this](const MouseClickEvent& event) { return this->on_mouse_clicked(event); @@ -20,15 +23,30 @@ void IMouseListener::subscribe_events(int listenerId) { mouse_move_handler = [this](const MouseMoveEvent& event) { return this->on_mouse_moved(event); }; - EventManager::get_instance().subscribe(std::move(mouse_click_handler), listenerId); - EventManager::get_instance().subscribe(std::move(mouse_press_handler), listenerId); - EventManager::get_instance().subscribe(std::move(mouse_release_handler), listenerId); - EventManager::get_instance().subscribe(std::move(mouse_move_handler), listenerId); + EventManager::get_instance().subscribe(std::move(this->mouse_click_handler), this->channel); + EventManager::get_instance().subscribe(std::move(this->mouse_press_handler), this->channel); + EventManager::get_instance().subscribe(std::move(this->mouse_release_handler), this->channel); + EventManager::get_instance().subscribe(std::move(this->mouse_move_handler), this->channel); } -void IMouseListener::unsubscribe_events(int listenerId) { - EventManager::get_instance().unsubscribe(mouse_click_handler, listenerId); - EventManager::get_instance().unsubscribe(mouse_press_handler, listenerId); - EventManager::get_instance().unsubscribe(mouse_release_handler, listenerId); - EventManager::get_instance().unsubscribe(mouse_move_handler, listenerId); +void IMouseListener::unsubscribe_events() { + EventManager::get_instance().unsubscribe(this->mouse_click_handler, this->channel); + EventManager::get_instance().unsubscribe(this->mouse_press_handler, this->channel); + EventManager::get_instance().unsubscribe(this->mouse_release_handler, this->channel); + EventManager::get_instance().unsubscribe(this->mouse_move_handler, this->channel); +} +void IMouseListener::activate_keys() { + if(this->active){ + return; + } + this->subscribe_events(); +} +void IMouseListener::deactivate_keys() { + if(!this->active){ + return; + } + this->unsubscribe_events(); +} +void IMouseListener::set_channel(int channel){ + this->channel = channel; } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 1078257..4493e7f 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -1,25 +1,117 @@ #pragma once + #include "Event.h" #include "EventHandler.h" #include "EventManager.h" +/** + * \class IMouseListener + * \brief Interface for mouse event handling in the application. + */ class IMouseListener { public: - IMouseListener(); + /** + * \brief Default constructor. + */ + IMouseListener(); + + /** + * \brief Constructs an IMouseListener with a specified channel. + * \param channel The channel ID for event handling. + */ + IMouseListener(int channel); + + /** + * \brief Destructor. + */ virtual ~IMouseListener(); - virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0; - virtual bool on_mouse_pressed(const MousePressEvent & event) = 0; - virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0; - virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; + /** + * \brief Copy constructor (deleted). + */ + IMouseListener(const IMouseListener&) = delete; + + /** + * \brief Copy assignment operator (deleted). + */ + IMouseListener& operator=(const IMouseListener&) = delete; + + /** + * \brief Move constructor (deleted). + */ + IMouseListener(IMouseListener&&) = delete; + + /** + * \brief Move assignment operator (deleted). + */ + IMouseListener& operator=(IMouseListener&&) = delete; + + /** + * \brief Handles a mouse click event. + * \param event The mouse click event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_mouse_clicked(const MouseClickEvent& event) = 0; + + /** + * \brief Handles a mouse press event. + * \param event The mouse press event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_mouse_pressed(const MousePressEvent& event) = 0; + + /** + * \brief Handles a mouse release event. + * \param event The mouse release event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_mouse_released(const MouseReleaseEvent& event) = 0; + + /** + * \brief Handles a mouse move event. + * \param event The mouse move event to handle. + * \return True if the event was handled, false otherwise. + */ + virtual bool on_mouse_moved(const MouseMoveEvent& event) = 0; + + /** + * \brief Activates mouse listening. + */ + void activate_keys(); + + /** + * \brief Deactivates mouse listening. + */ + void deactivate_keys(); + + /** + * \brief Sets the channel ID for event handling. + * \param channel The channel ID to set. + */ + void set_channel(int channel); protected: - void subscribe_events(int listenerId = 0); - void unsubscribe_events(int listenerId = 0); + /** + * \brief Subscribes to mouse events on the specified channel. + */ + void subscribe_events(); + + /** + * \brief Unsubscribes from mouse events on the specified channel. + */ + void unsubscribe_events(); private: - EventHandler mouse_click_handler; - EventHandler mouse_press_handler; - EventHandler mouse_release_handler; - EventHandler mouse_move_handler; + //! Indicates whether mouse listening is active. + bool active = true; + //! Channel ID for event handling. + int channel = 0; + //! Mouse click event handler. + EventHandler mouse_click_handler; + //! Mouse press event handler. + EventHandler mouse_press_handler; + //! Mouse release event handler. + EventHandler mouse_release_handler; + //! Mouse move event handler. + EventHandler mouse_move_handler; }; diff --git a/src/example/events.cpp b/src/example/events.cpp index 1f757e1..1a29d80 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -73,14 +73,11 @@ int main() { EventManager::get_instance().queue_event(std::move(key_press), 0); EventManager::get_instance().queue_event(std::move(click_event), 0); { - // Instantiate TestKeyListener, which subscribes to key events TestKeyListener testListener; - // Create game object with Transform and BehaviorScript components auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); obj.add_component().set_script(); - // Get ScriptSystem singleton instance (this would normally be done from the game loop) ScriptSystem sys; sys.update(); -- cgit v1.2.3 From ab69e41cabd6f5a84ebb2e2629d194b49b7039de Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 12:22:17 +0100 Subject: added some extra comments --- src/crepe/api/EventHandler.h | 2 +- src/example/events.cpp | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 5b9ac0d..dc06806 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -8,7 +8,7 @@ * \brief A type alias for an event handler function. * * The EventHandler is a std::function that takes an EventType reference and returns a boolean value - * indicating whether the event was successfully handled. + * indicating whether the event is handled. * * \tparam EventType The type of event this handler will handle. */ diff --git a/src/example/events.cpp b/src/example/events.cpp index 1a29d80..af5f747 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -91,13 +91,12 @@ int main() { return false; }; EventManager::get_instance().subscribe(std::move(event_handler),0); - // - + // testing trigger with testListener not in scope (unsubscribed) EventManager::get_instance().trigger_event(key_press, 0); - EventManager::get_instance().trigger_event(click_event, 0); + // dispatching queued events EventManager::get_instance().dispatch_events(); - + EventManager::get_instance().unsubscribe(event_handler,0); return EXIT_SUCCESS; } -- cgit v1.2.3 From f1ba2a5607f23ed0b23d74240ea66cb1f8d2c1ad Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 12:28:04 +0100 Subject: tested channel and deactivating in function --- src/crepe/api/IKeyListener.cpp | 3 +++ src/crepe/api/IMouseListener.cpp | 6 ++++-- src/crepe/api/IMouseListener.h | 4 ++-- src/example/events.cpp | 9 +++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index 17d8735..e759eca 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -41,5 +41,8 @@ void IKeyListener::deactivate_keys() { this->unsubscribe_events(); } void IKeyListener::set_channel(int channel){ + this->unsubscribe_events(); this->channel = channel; + this->subscribe_events(); + } diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 68061be..c6ea419 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -35,18 +35,20 @@ void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe(this->mouse_release_handler, this->channel); EventManager::get_instance().unsubscribe(this->mouse_move_handler, this->channel); } -void IMouseListener::activate_keys() { +void IMouseListener::activate_mouse() { if(this->active){ return; } this->subscribe_events(); } -void IMouseListener::deactivate_keys() { +void IMouseListener::deactivate_mouse() { if(!this->active){ return; } this->unsubscribe_events(); } void IMouseListener::set_channel(int channel){ + this->unsubscribe_events(); this->channel = channel; + this->subscribe_events(); } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 4493e7f..a7ad271 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -77,12 +77,12 @@ public: /** * \brief Activates mouse listening. */ - void activate_keys(); + void activate_mouse(); /** * \brief Deactivates mouse listening. */ - void deactivate_keys(); + void deactivate_mouse(); /** * \brief Sets the channel ID for event handling. diff --git a/src/example/events.cpp b/src/example/events.cpp index af5f747..ff97cf1 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -25,6 +25,7 @@ class MyScript : public Script, public IKeyListener,public IMouseListener{ bool on_key_pressed(const KeyPressEvent & event) override{ std::cout << "KeyPressed function" << std::endl; + this->deactivate_keys(); return false; } bool on_key_released(const KeyReleaseEvent & event) override{ @@ -74,7 +75,7 @@ int main() { EventManager::get_instance().queue_event(std::move(click_event), 0); { TestKeyListener testListener; - + testListener.set_channel(1); auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); obj.add_component().set_script(); @@ -82,8 +83,8 @@ int main() { sys.update(); // Trigger the events while `testListener` is in scope - EventManager::get_instance().trigger_event(key_press, 0); - EventManager::get_instance().trigger_event(click_event, 0); + EventManager::get_instance().trigger_event(key_press, 1); + EventManager::get_instance().trigger_event(click_event, 1); } // custom lambda event handler EventHandler event_handler = [](const KeyPressEvent& e) { @@ -96,7 +97,7 @@ int main() { EventManager::get_instance().trigger_event(click_event, 0); // dispatching queued events EventManager::get_instance().dispatch_events(); - + EventManager::get_instance().unsubscribe(event_handler,0); return EXIT_SUCCESS; } -- cgit v1.2.3 From e4b3162d28a635af37dd7ca1f71db16e0d196864 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 12:33:16 +0100 Subject: moved get_instance --- src/crepe/api/EventManager.cpp | 5 +++++ src/crepe/api/EventManager.h | 9 +++------ src/example/events.cpp | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 872c6eb..42bff12 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,5 +1,10 @@ #include "EventManager.h" +EventManager & EventManager::get_instance() { + static EventManager instance; + return instance; +} + void EventManager::dispatch_events() { for (std::vector, int, std::type_index>>::iterator event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { std::unique_ptr& event = std::get<0>(*event_it); diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index d3a14da..b963ce3 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -34,10 +34,7 @@ public: * * \return Reference to the EventManager instance. */ - static EventManager & get_instance() { - static EventManager instance; - return instance; - } + static EventManager & get_instance(); /** * \brief Subscribe to an event. @@ -150,7 +147,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); if (channel > 0) { - std::unordered_map>>& handlers_map = subscribers_by_event_id[event_type]; + std::unordered_map>>& handlers_map = this->subscribers_by_event_id[event_type]; std::unordered_map>>::iterator handlers_it = handlers_map.find(channel); if (handlers_it != handlers_map.end()) { @@ -165,7 +162,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { } } } else { - std::vector>& handlers = subscribers[event_type]; + std::vector>& handlers = this->subscribers[event_type]; for (std::vector>::iterator it = handlers.begin(); it != handlers.end();) { // erases callback if callback function returns true if ((*it)->exec(event)) { diff --git a/src/example/events.cpp b/src/example/events.cpp index ff97cf1..af6f294 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -9,13 +9,14 @@ #include #include #include - #include "crepe/api/Event.h" #include "crepe/api/EventManager.h" #include "crepe/api/IKeyListener.h" #include "crepe/api/IMouseListener.h" + using namespace crepe; using namespace std; + class MyScript : public Script, public IKeyListener,public IMouseListener{ void update() { // Retrieve component from the same GameObject this script is on -- cgit v1.2.3 From 408eb1fb5932a683a65db79afed6a300e18db5c6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 15:37:11 +0100 Subject: added keycodes --- .vscode/launch.json | 7 + .vscode/tasks.json | 28 ++++ mwe/events/include/event.h | 2 +- src/crepe/api/Event.h | 62 ++++----- src/crepe/api/EventHandler.h | 41 +++--- src/crepe/api/EventManager.cpp | 105 +++++++++------ src/crepe/api/EventManager.h | 274 +++++++++++++++++++++++---------------- src/crepe/api/IKeyListener.cpp | 51 ++++---- src/crepe/api/IKeyListener.h | 48 +++---- src/crepe/api/IMouseListener.cpp | 82 ++++++------ src/crepe/api/IMouseListener.h | 78 +++++------ src/crepe/api/KeyCodes.h | 147 +++++++++++++++++++++ src/example/events.cpp | 98 +++++++------- 13 files changed, 637 insertions(+), 386 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 src/crepe/api/KeyCodes.h diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..05054c5 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 16c75bf..3e70201 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -152,7 +152,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index c0de4fc..87c6068 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -1,68 +1,56 @@ #pragma once #include -//#include "keyCodes.h" -#include #include #include -#include -#include -using Keycode = uint16_t; -enum class MouseButton { - NONE = 0, - LEFT_MOUSE = 1, - RIGHT_MOUSE = 2, - MIDDLE_MOUSE = 3, - X1_MOUSE = 4, - X2_MOUSE = 5, - SCROLL_UP = 6, - SCROLL_DOWN = 7, -}; +#include "KeyCodes.h" + class Event { public: -bool handled = false; + bool handled = false; }; class KeyPressEvent : public Event { public: - int repeat = 0; - Keycode key = 0; + int repeat = 0; + Keycode key = Keycode::None; }; -class MouseClickEvent : public Event { -public: - int mouse_x = 0; - int mouse_y = 0; - MouseButton button; -}; class KeyReleaseEvent : public Event { public: - Keycode key = 0; + Keycode key = Keycode::None; }; class MousePressEvent : public Event { public: - int mouse_x = 0; - int mouse_y = 0; - MouseButton button; + int mouse_x = 0; + int mouse_y = 0; + MouseButton button; +}; + +class MouseClickEvent : public Event { +public: + int mouse_x = 0; + int mouse_y = 0; + MouseButton button; }; class MouseReleaseEvent : public Event { public: - int mouse_x = 0; - int mouse_y = 0; - MouseButton button; + int mouse_x = 0; + int mouse_y = 0; + MouseButton button = MouseButton::None; }; class MouseMoveEvent : public Event { public: - int mouse_x = 0; - int mouse_y = 0; + int mouse_x = 0; + int mouse_y = 0; }; -class CollisionEvent : public Event{ +class CollisionEvent : public Event { public: - //Collision collisionData; + //Collision collisionData; }; -class TextSubmitEvent : public Event{ +class TextSubmitEvent : public Event { public: - std::string text; + std::string text; }; class ShutDownEvent : public Event { public: diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index dc06806..46c6c7b 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -1,8 +1,8 @@ #pragma once +#include "Event.h" #include #include #include -#include "Event.h" /** * \brief A type alias for an event handler function. @@ -24,12 +24,12 @@ using EventHandler = std::function; */ class IEventHandlerWrapper { public: - /** + /** * \brief Virtual destructor for IEventHandlerWrapper. */ - virtual ~IEventHandlerWrapper() = default; + virtual ~IEventHandlerWrapper() = default; - /** + /** * \brief Executes the handler with the given event. * * This method calls the `call()` method of the derived class, passing the event to the handler. @@ -37,19 +37,19 @@ public: * \param e The event to be processed. * \return A boolean value indicating whether the event is handled. */ - bool exec(const Event & e); + bool exec(const Event & e); - /** + /** * \brief Get the type of the event handler. * * This method returns the type of the event handler as a string. * * \return A string representing the handler's type. */ - virtual std::string get_type() const = 0; + virtual std::string get_type() const = 0; private: - /** + /** * \brief The method responsible for handling the event. * * This method is implemented by derived classes to process the event. @@ -57,7 +57,7 @@ private: * \param e The event to be processed. * \return A boolean value indicating whether the event is handled. */ - virtual bool call(const Event & e) = 0; + virtual bool call(const Event & e) = 0; }; /** @@ -73,19 +73,18 @@ private: template class EventHandlerWrapper : public IEventHandlerWrapper { public: - /** + /** * \brief Constructs an EventHandlerWrapper with a given handler. * * The constructor takes an event handler function and stores it in the wrapper. * * \param handler The event handler function. */ - explicit EventHandlerWrapper(const EventHandler & handler) - : m_handler(handler), m_handler_type(m_handler.target_type().name()) { - } + explicit EventHandlerWrapper(const EventHandler & handler) + : m_handler(handler), m_handler_type(m_handler.target_type().name()) {} private: - /** + /** * \brief Calls the stored event handler with the event. * * This method casts the event to the appropriate type and calls the handler. @@ -93,20 +92,20 @@ private: * \param e The event to be handled. * \return A boolean value indicating whether the event is handled. */ - bool call(const Event & e) override { - return m_handler(static_cast(e)); - } + bool call(const Event & e) override { + return m_handler(static_cast(e)); + } - /** + /** * \brief Returns the type of the handler. * * This method returns a string representing the type of the event handler. * * \return The handler type as a string. */ - std::string get_type() const override { return m_handler_type; } + std::string get_type() const override { return m_handler_type; } //! The event handler function. - EventHandler m_handler; + EventHandler m_handler; //! The type name of the handler function. - const std::string m_handler_type; + const std::string m_handler_type; }; diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 42bff12..72cfd74 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -6,46 +6,73 @@ EventManager & EventManager::get_instance() { } void EventManager::dispatch_events() { - for (std::vector, int, std::type_index>>::iterator event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { - std::unique_ptr& event = std::get<0>(*event_it); - int channel = std::get<1>(*event_it); - std::type_index event_type = std::get<2>(*event_it); + for (std::vector, int, + std::type_index>>::iterator event_it + = this->events_queue.begin(); + event_it != this->events_queue.end();) { + std::unique_ptr & event = std::get<0>(*event_it); + int channel = std::get<1>(*event_it); + std::type_index event_type = std::get<2>(*event_it); - bool event_handled = false; + bool event_handled = false; - if (channel) { - std::unordered_map>>>::iterator handlers_it = subscribers_by_event_id.find(event_type); - if (handlers_it != subscribers_by_event_id.end()) { - std::unordered_map>>& handlers_map = handlers_it->second; - std::unordered_map>>::iterator handlers = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - std::vector>& callbacks = handlers->second; - for (std::vector>::iterator handler_it = callbacks.begin(); handler_it != callbacks.end(); ++handler_it) { - if ((*handler_it)->exec(*event)) { - event_it = events_queue.erase(event_it); - event_handled = true; - break; - } - } - } - } - } else { - // Handle event for all channels - std::unordered_map>>::iterator handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector>& handlers = handlers_it->second; - for (std::vector>::iterator handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) { - if ((*handler_it)->exec(*event)) { - event_it = this->events_queue.erase(event_it); - event_handled = true; - break; - } - } - } - } + if (channel) { + std::unordered_map< + std::type_index, + std::unordered_map< + int, std::vector>>>:: + iterator handlers_it + = subscribers_by_event_id.find(event_type); + if (handlers_it != subscribers_by_event_id.end()) { + std::unordered_map< + int, std::vector>> & + handlers_map + = handlers_it->second; + std::unordered_map< + int, std::vector>>:: + iterator handlers + = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + std::vector> & + callbacks + = handlers->second; + for (std::vector>:: + iterator handler_it + = callbacks.begin(); + handler_it != callbacks.end(); ++handler_it) { + if ((*handler_it)->exec(*event)) { + event_it = events_queue.erase(event_it); + event_handled = true; + break; + } + } + } + } + } else { + // Handle event for all channels + std::unordered_map< + std::type_index, + std::vector>>::iterator + handlers_it + = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + std::vector> & handlers + = handlers_it->second; + for (std::vector>:: + iterator handler_it + = handlers.begin(); + handler_it != handlers.end(); ++handler_it) { + if ((*handler_it)->exec(*event)) { + event_it = this->events_queue.erase(event_it); + event_handled = true; + break; + } + } + } + } - if (!event_handled) { - ++event_it; - } - } + if (!event_handled) { + ++event_it; + } + } } diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index b963ce3..26539ce 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -1,11 +1,11 @@ #pragma once +#include #include +#include +#include #include #include -#include -#include -#include #include "Event.h" #include "EventHandler.h" @@ -17,26 +17,26 @@ */ class EventManager { public: - /** + /** * \brief Deleted copy constructor to prevent copying of the EventManager instance. */ - EventManager(const EventManager &) = delete; + EventManager(const EventManager &) = delete; - /** + /** * \brief Deleted copy assignment operator to prevent assignment of the EventManager instance. */ - const EventManager & operator=(const EventManager &) = delete; + const EventManager & operator=(const EventManager &) = delete; - /** + /** * \brief Get the singleton instance of the EventManager. * * This method returns the unique instance of the EventManager, creating it on the first call. * * \return Reference to the EventManager instance. */ - static EventManager & get_instance(); + static EventManager & get_instance(); - /** + /** * \brief Subscribe to an event. * * This method allows the registration of a callback for a specific event type and channel. @@ -45,10 +45,10 @@ public: * \param callback The callback function to invoke when the event is triggered. * \param channel The channel number to subscribe to (default is 0). */ - template - void subscribe(EventHandler && callback, int channel = 0); + template + void subscribe(EventHandler && callback, int channel = 0); - /** + /** * \brief Unsubscribe from an event. * * This method removes a previously registered callback from an event. @@ -57,10 +57,10 @@ public: * \param callback The callback function to remove from the subscription list. * \param channel The event ID to unsubscribe from. */ - template - void unsubscribe(const EventHandler &, int channel); + template + void unsubscribe(const EventHandler &, int channel); - /** + /** * \brief Trigger an event. * * This method invokes the appropriate callback(s) for the specified event. @@ -69,10 +69,10 @@ public: * \param event The event data to pass to the callback. * \param channel The channel from which to trigger the event (default is 0). */ - template - void trigger_event(const EventType & event, int channel); + template + void trigger_event(const EventType & event, int channel); - /** + /** * \brief Queue an event for later processing. * * This method adds an event to the event queue, which will be processed in the @@ -82,128 +82,176 @@ public: * \param event The event to queue. * \param channel The channel number for the event (default is 0). */ - template - void queue_event(EventType&& event, int channel); + template + void queue_event(EventType && event, int channel); - /** + /** * \brief Dispatch all queued events. * * This method processes all events in the event queue and triggers the corresponding * callbacks for each event. */ - void dispatch_events(); + void dispatch_events(); private: - /** + /** * \brief Default constructor for the EventManager. * * This constructor is private to enforce the singleton pattern. */ - EventManager() = default; + EventManager() = default; - //! The queue of events to be processed. - std::vector, int, std::type_index>> events_queue; + //! The queue of events to be processed. + std::vector, int, std::type_index>> + events_queue; //! Registered event handlers. - std::unordered_map>> subscribers; + std::unordered_map>> + subscribers; //! Event handlers indexed by event ID. - std::unordered_map>>> subscribers_by_event_id; + std::unordered_map< + std::type_index, + std::unordered_map>>> + subscribers_by_event_id; }; - template void EventManager::subscribe(EventHandler && callback, int channel) { - std::type_index event_type = typeid(EventType); - std::unique_ptr> handler = std::make_unique>(callback); - - if (channel) { - std::unordered_map>>& handlers_map = this->subscribers_by_event_id[event_type]; - std::unordered_map>>::iterator handlers = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - handlers->second.emplace_back(std::move(handler)); - } else { - handlers_map[channel].emplace_back(std::move(handler)); - } - } else { - std::vector>& handlers = this->subscribers[event_type]; - handlers.emplace_back(std::move(handler)); - } + std::type_index event_type = typeid(EventType); + std::unique_ptr> handler + = std::make_unique>(callback); + + if (channel) { + std::unordered_map>> & + handlers_map + = this->subscribers_by_event_id[event_type]; + std::unordered_map< + int, std::vector>>::iterator + handlers + = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + handlers->second.emplace_back(std::move(handler)); + } else { + handlers_map[channel].emplace_back(std::move(handler)); + } + } else { + std::vector> & handlers + = this->subscribers[event_type]; + handlers.emplace_back(std::move(handler)); + } } template -void EventManager::queue_event(EventType&& event, int channel) { - std::type_index event_type = std::type_index(typeid(EventType)); - - std::unique_ptr event_ptr = std::make_unique(std::forward(event)); - - std::tuple, int, std::type_index> tuple( - std::move(event_ptr), - channel, - event_type - ); - this->events_queue.push_back(std::move(tuple)); +void EventManager::queue_event(EventType && event, int channel) { + std::type_index event_type = std::type_index(typeid(EventType)); + + std::unique_ptr event_ptr + = std::make_unique(std::forward(event)); + + std::tuple, int, std::type_index> tuple( + std::move(event_ptr), channel, event_type); + this->events_queue.push_back(std::move(tuple)); } template void EventManager::trigger_event(const EventType & event, int channel) { - std::type_index event_type = std::type_index(typeid(EventType)); - - if (channel > 0) { - std::unordered_map>>& handlers_map = this->subscribers_by_event_id[event_type]; - std::unordered_map>>::iterator handlers_it = handlers_map.find(channel); - - if (handlers_it != handlers_map.end()) { - std::vector>& handlers = handlers_it->second; - for (std::vector>::iterator it = handlers.begin(); it != handlers.end();) { + std::type_index event_type = std::type_index(typeid(EventType)); + + if (channel > 0) { + std::unordered_map>> & + handlers_map + = this->subscribers_by_event_id[event_type]; + std::unordered_map< + int, std::vector>>::iterator + handlers_it + = handlers_map.find(channel); + + if (handlers_it != handlers_map.end()) { + std::vector> & handlers + = handlers_it->second; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end();) { // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } - } else { - std::vector>& handlers = this->subscribers[event_type]; - for (std::vector>::iterator it = handlers.begin(); it != handlers.end();) { + if ((*it)->exec(event)) { + it = handlers.erase(it); + } else { + ++it; + } + } + } + } else { + std::vector> & handlers + = this->subscribers[event_type]; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end();) { // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } + if ((*it)->exec(event)) { + it = handlers.erase(it); + } else { + ++it; + } + } + } } template -void EventManager::unsubscribe(const EventHandler & callback, int channel) { - std::type_index event_type(typeid(EventType)); - std::string handler_name = callback.target_type().name(); - - if (channel) { - std::unordered_map>>>::iterator subscriber_list = this->subscribers_by_event_id.find(event_type); - if (subscriber_list != this->subscribers_by_event_id.end()) { - std::unordered_map>>& handlers_map = subscriber_list->second; - std::unordered_map>>::iterator handlers = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - std::vector>& callbacks = handlers->second; - for (std::vector>::iterator it = callbacks.begin(); it != callbacks.end(); ++it) { - if ((*it)->get_type() == handler_name) { - it = callbacks.erase(it); - return; - } - } - } - } - } else { - std::unordered_map>>::iterator handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector>& handlers = handlers_it->second; - for (std::vector>::iterator it = handlers.begin(); it != handlers.end(); ++it) { - if ((*it)->get_type() == handler_name) { - it = handlers.erase(it); - return; - } - } - } - } +void EventManager::unsubscribe(const EventHandler & callback, + int channel) { + std::type_index event_type(typeid(EventType)); + std::string handler_name = callback.target_type().name(); + + if (channel) { + std::unordered_map< + std::type_index, + std::unordered_map< + int, std::vector>>>:: + iterator subscriber_list + = this->subscribers_by_event_id.find(event_type); + if (subscriber_list != this->subscribers_by_event_id.end()) { + std::unordered_map< + int, std::vector>> & + handlers_map + = subscriber_list->second; + std::unordered_map< + int, + std::vector>>::iterator + handlers + = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + std::vector> & callbacks + = handlers->second; + for (std::vector< + std::unique_ptr>::iterator it + = callbacks.begin(); + it != callbacks.end(); ++it) { + if ((*it)->get_type() == handler_name) { + it = callbacks.erase(it); + return; + } + } + } + } + } else { + std::unordered_map>>:: + iterator handlers_it + = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + std::vector> & handlers + = handlers_it->second; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end(); ++it) { + if ((*it)->get_type() == handler_name) { + it = handlers.erase(it); + return; + } + } + } + } } + diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index e759eca..eb8f9af 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -1,48 +1,49 @@ #include "IKeyListener.h" #include -IKeyListener::IKeyListener(){ +IKeyListener::IKeyListener() { this->channel = channel; this->subscribe_events(); } -IKeyListener::IKeyListener(int channel){ +IKeyListener::IKeyListener(int channel) { this->channel = channel; this->subscribe_events(); } -IKeyListener::~IKeyListener() { - this->unsubscribe_events(); -} +IKeyListener::~IKeyListener() { this->unsubscribe_events(); } void IKeyListener::subscribe_events() { - key_pressed_handler = [this](const KeyPressEvent & event) { - return this->on_key_pressed(event); - }; - key_released_handler = [this](const KeyReleaseEvent & event) { - return this->on_key_released(event); - }; - EventManager::get_instance().subscribe(std::move(this->key_pressed_handler), this->channel); - EventManager::get_instance().subscribe(std::move(this->key_released_handler), this->channel); + key_pressed_handler = [this](const KeyPressEvent & event) { + return this->on_key_pressed(event); + }; + key_released_handler = [this](const KeyReleaseEvent & event) { + return this->on_key_released(event); + }; + EventManager::get_instance().subscribe( + std::move(this->key_pressed_handler), this->channel); + EventManager::get_instance().subscribe( + std::move(this->key_released_handler), this->channel); } void IKeyListener::unsubscribe_events() { - EventManager::get_instance().unsubscribe(this->key_pressed_handler , channel); - EventManager::get_instance().unsubscribe(this->key_released_handler , channel); + EventManager::get_instance().unsubscribe( + this->key_pressed_handler, channel); + EventManager::get_instance().unsubscribe( + this->key_released_handler, channel); std::cout << std::endl; } -void IKeyListener::activate_keys() { - if(this->active){ +void IKeyListener::activate_keys() { + if (this->active) { return; } - this->subscribe_events(); + this->subscribe_events(); } -void IKeyListener::deactivate_keys() { - if(!this->active){ +void IKeyListener::deactivate_keys() { + if (!this->active) { return; } - this->unsubscribe_events(); + this->unsubscribe_events(); } -void IKeyListener::set_channel(int channel){ - this->unsubscribe_events(); +void IKeyListener::set_channel(int channel) { + this->unsubscribe_events(); this->channel = channel; - this->subscribe_events(); - + this->subscribe_events(); } diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 6d7915d..839acbf 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -9,70 +9,70 @@ */ class IKeyListener { public: - /** + /** * \brief Constructs an IKeyListener with a specified channel. * \param channel The channel ID for event handling. */ - IKeyListener(int channel); + IKeyListener(int channel); - /** + /** * \brief Default constructor for IKeyListener. */ - IKeyListener(); + IKeyListener(); - /** + /** * \brief Destructor. */ - virtual ~IKeyListener(); + virtual ~IKeyListener(); - /** + /** * \brief Pure virtual function to handle key press events. * \param event The key press event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_key_pressed(const KeyPressEvent& event) = 0; + virtual bool on_key_pressed(const KeyPressEvent & event) = 0; - /** + /** * \brief Pure virtual function to handle key release events. * \param event The key release event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_key_released(const KeyReleaseEvent& event) = 0; + virtual bool on_key_released(const KeyReleaseEvent & event) = 0; - /** + /** * \brief Activates key listening. */ - void activate_keys(); + void activate_keys(); - /** + /** * \brief Deactivates key listening. */ - void deactivate_keys(); + void deactivate_keys(); - /** + /** * \brief Sets the channel ID for event handling. * \param channel The channel ID to set. */ - void set_channel(int channel); + void set_channel(int channel); protected: - /** + /** * \brief Subscribes to key events. */ - void subscribe_events(); + void subscribe_events(); - /** + /** * \brief Unsubscribes from key events. */ - void unsubscribe_events(); + void unsubscribe_events(); private: //! Indicates whether key listening is active. - bool active = true; + bool active = true; //! Channel ID for event handling. - int channel = 0; + int channel = 0; //! Key press event handler. - EventHandler key_pressed_handler; + EventHandler key_pressed_handler; //!< Key release event handler. - EventHandler key_released_handler; + EventHandler key_released_handler; }; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index c6ea419..683632c 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -1,54 +1,56 @@ #include "IMouseListener.h" -IMouseListener::IMouseListener(int channel){ - this->channel = channel; -} -IMouseListener::IMouseListener(){ - this->subscribe_events(); -} -IMouseListener::~IMouseListener() { - this->unsubscribe_events(); -} +IMouseListener::IMouseListener(int channel) { this->channel = channel; } +IMouseListener::IMouseListener() { this->subscribe_events(); } +IMouseListener::~IMouseListener() { this->unsubscribe_events(); } void IMouseListener::subscribe_events() { - // Define handler lambdas and subscribe them - mouse_click_handler = [this](const MouseClickEvent& event) { - return this->on_mouse_clicked(event); - }; - mouse_press_handler = [this](const MousePressEvent& event) { - return this->on_mouse_pressed(event); - }; - mouse_release_handler = [this](const MouseReleaseEvent& event) { - return this->on_mouse_released(event); - }; - mouse_move_handler = [this](const MouseMoveEvent& event) { - return this->on_mouse_moved(event); - }; - EventManager::get_instance().subscribe(std::move(this->mouse_click_handler), this->channel); - EventManager::get_instance().subscribe(std::move(this->mouse_press_handler), this->channel); - EventManager::get_instance().subscribe(std::move(this->mouse_release_handler), this->channel); - EventManager::get_instance().subscribe(std::move(this->mouse_move_handler), this->channel); + // Define handler lambdas and subscribe them + mouse_click_handler = [this](const MouseClickEvent & event) { + return this->on_mouse_clicked(event); + }; + mouse_press_handler = [this](const MousePressEvent & event) { + return this->on_mouse_pressed(event); + }; + mouse_release_handler = [this](const MouseReleaseEvent & event) { + return this->on_mouse_released(event); + }; + mouse_move_handler = [this](const MouseMoveEvent & event) { + return this->on_mouse_moved(event); + }; + EventManager::get_instance().subscribe( + std::move(this->mouse_click_handler), this->channel); + EventManager::get_instance().subscribe( + std::move(this->mouse_press_handler), this->channel); + EventManager::get_instance().subscribe( + std::move(this->mouse_release_handler), this->channel); + EventManager::get_instance().subscribe( + std::move(this->mouse_move_handler), this->channel); } void IMouseListener::unsubscribe_events() { - EventManager::get_instance().unsubscribe(this->mouse_click_handler, this->channel); - EventManager::get_instance().unsubscribe(this->mouse_press_handler, this->channel); - EventManager::get_instance().unsubscribe(this->mouse_release_handler, this->channel); - EventManager::get_instance().unsubscribe(this->mouse_move_handler, this->channel); -} -void IMouseListener::activate_mouse() { - if(this->active){ + EventManager::get_instance().unsubscribe( + this->mouse_click_handler, this->channel); + EventManager::get_instance().unsubscribe( + this->mouse_press_handler, this->channel); + EventManager::get_instance().unsubscribe( + this->mouse_release_handler, this->channel); + EventManager::get_instance().unsubscribe( + this->mouse_move_handler, this->channel); +} +void IMouseListener::activate_mouse() { + if (this->active) { return; } - this->subscribe_events(); + this->subscribe_events(); } -void IMouseListener::deactivate_mouse() { - if(!this->active){ +void IMouseListener::deactivate_mouse() { + if (!this->active) { return; } - this->unsubscribe_events(); + this->unsubscribe_events(); } -void IMouseListener::set_channel(int channel){ - this->unsubscribe_events(); +void IMouseListener::set_channel(int channel) { + this->unsubscribe_events(); this->channel = channel; - this->subscribe_events(); + this->subscribe_events(); } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index a7ad271..7e92956 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -10,108 +10,108 @@ */ class IMouseListener { public: - /** + /** * \brief Default constructor. */ - IMouseListener(); + IMouseListener(); - /** + /** * \brief Constructs an IMouseListener with a specified channel. * \param channel The channel ID for event handling. */ - IMouseListener(int channel); + IMouseListener(int channel); - /** + /** * \brief Destructor. */ - virtual ~IMouseListener(); + virtual ~IMouseListener(); - /** + /** * \brief Copy constructor (deleted). */ - IMouseListener(const IMouseListener&) = delete; + IMouseListener(const IMouseListener &) = delete; - /** + /** * \brief Copy assignment operator (deleted). */ - IMouseListener& operator=(const IMouseListener&) = delete; + IMouseListener & operator=(const IMouseListener &) = delete; - /** + /** * \brief Move constructor (deleted). */ - IMouseListener(IMouseListener&&) = delete; + IMouseListener(IMouseListener &&) = delete; - /** + /** * \brief Move assignment operator (deleted). */ - IMouseListener& operator=(IMouseListener&&) = delete; + IMouseListener & operator=(IMouseListener &&) = delete; - /** + /** * \brief Handles a mouse click event. * \param event The mouse click event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_mouse_clicked(const MouseClickEvent& event) = 0; + virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0; - /** + /** * \brief Handles a mouse press event. * \param event The mouse press event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_mouse_pressed(const MousePressEvent& event) = 0; + virtual bool on_mouse_pressed(const MousePressEvent & event) = 0; - /** + /** * \brief Handles a mouse release event. * \param event The mouse release event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_mouse_released(const MouseReleaseEvent& event) = 0; + virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0; - /** + /** * \brief Handles a mouse move event. * \param event The mouse move event to handle. * \return True if the event was handled, false otherwise. */ - virtual bool on_mouse_moved(const MouseMoveEvent& event) = 0; + virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; - /** + /** * \brief Activates mouse listening. */ - void activate_mouse(); + void activate_mouse(); - /** + /** * \brief Deactivates mouse listening. */ - void deactivate_mouse(); + void deactivate_mouse(); - /** + /** * \brief Sets the channel ID for event handling. * \param channel The channel ID to set. */ - void set_channel(int channel); + void set_channel(int channel); protected: - /** + /** * \brief Subscribes to mouse events on the specified channel. */ - void subscribe_events(); + void subscribe_events(); - /** + /** * \brief Unsubscribes from mouse events on the specified channel. */ - void unsubscribe_events(); + void unsubscribe_events(); private: //! Indicates whether mouse listening is active. - bool active = true; - //! Channel ID for event handling. - int channel = 0; + bool active = true; + //! Channel ID for event handling. + int channel = 0; //! Mouse click event handler. - EventHandler mouse_click_handler; + EventHandler mouse_click_handler; //! Mouse press event handler. - EventHandler mouse_press_handler; + EventHandler mouse_press_handler; //! Mouse release event handler. - EventHandler mouse_release_handler; + EventHandler mouse_release_handler; //! Mouse move event handler. - EventHandler mouse_move_handler; + EventHandler mouse_move_handler; }; diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h new file mode 100644 index 0000000..53be91b --- /dev/null +++ b/src/crepe/api/KeyCodes.h @@ -0,0 +1,147 @@ +#pragma once + +enum class MouseButton { + None = 0, + Left_Mouse = 1, + Right_Mouse = 2, + Middle_Mouse = 3, + X1_Mouse = 4, + X2_Mouse = 5, + Scroll_Up = 6, + Scroll_Down = 7, +}; + +enum class Keycode : int{ + // From glfw3.h + None = 0, + Space = 32, + Apostrophe = 39, /* ' */ + Comma = 44, /* , */ + Minus = 45, /* - */ + Period = 46, /* . */ + Slash = 47, /* / */ + + D0 = 48, /* 0 */ + D1 = 49, /* 1 */ + D2 = 50, /* 2 */ + D3 = 51, /* 3 */ + D4 = 52, /* 4 */ + D5 = 53, /* 5 */ + D6 = 54, /* 6 */ + D7 = 55, /* 7 */ + D8 = 56, /* 8 */ + D9 = 57, /* 9 */ + + Semicolon = 59, /* ; */ + Equal = 61, /* = */ + + A = 65, + B = 66, + C = 67, + D = 68, + E = 69, + F = 70, + G = 71, + H = 72, + I = 73, + J = 74, + K = 75, + L = 76, + M = 77, + N = 78, + O = 79, + P = 80, + Q = 81, + R = 82, + S = 83, + T = 84, + U = 85, + V = 86, + W = 87, + X = 88, + Y = 89, + Z = 90, + + LeftBracket = 91, /* [ */ + Backslash = 92, /* \ */ + RightBracket = 93, /* ] */ + GraveAccent = 96, /* ` */ + + World1 = 161, /* non-US #1 */ + World2 = 162, /* non-US #2 */ + + /* Function keys */ + Escape = 256, + Enter = 257, + Tab = 258, + Backspace = 259, + Insert = 260, + Delete = 261, + Right = 262, + Left = 263, + Down = 264, + Up = 265, + PageUp = 266, + PageDown = 267, + Home = 268, + End = 269, + CapsLock = 280, + ScrollLock = 281, + NumLock = 282, + PrintScreen = 283, + Pause = 284, + F1 = 290, + F2 = 291, + F3 = 292, + F4 = 293, + F5 = 294, + F6 = 295, + F7 = 296, + F8 = 297, + F9 = 298, + F10 = 299, + F11 = 300, + F12 = 301, + F13 = 302, + F14 = 303, + F15 = 304, + F16 = 305, + F17 = 306, + F18 = 307, + F19 = 308, + F20 = 309, + F21 = 310, + F22 = 311, + F23 = 312, + F24 = 313, + F25 = 314, + + /* Keypad */ + KP0 = 320, + KP1 = 321, + KP2 = 322, + KP3 = 323, + KP4 = 324, + KP5 = 325, + KP6 = 326, + KP7 = 327, + KP8 = 328, + KP9 = 329, + KPDecimal = 330, + KPDivide = 331, + KPMultiply = 332, + KPSubtract = 333, + KPAdd = 334, + KPEnter = 335, + KPEqual = 336, + + LeftShift = 340, + LeftControl = 341, + LeftAlt = 342, + LeftSuper = 343, + RightShift = 344, + RightControl = 345, + RightAlt = 346, + RightSuper = 347, + Menu = 348 +}; diff --git a/src/example/events.cpp b/src/example/events.cpp index af6f294..210c42c 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -4,103 +4,107 @@ #include #include +#include +#include +#include +#include #include #include #include #include #include -#include "crepe/api/Event.h" -#include "crepe/api/EventManager.h" -#include "crepe/api/IKeyListener.h" -#include "crepe/api/IMouseListener.h" +#include using namespace crepe; using namespace std; -class MyScript : public Script, public IKeyListener,public IMouseListener{ +class MyScript : public Script, public IKeyListener, public IMouseListener { void update() { // Retrieve component from the same GameObject this script is on Transform & test = get_component(); dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y); } - bool on_key_pressed(const KeyPressEvent & event) override{ + bool on_key_pressed(const KeyPressEvent & event) override { std::cout << "KeyPressed function" << std::endl; this->deactivate_keys(); return false; } - bool on_key_released(const KeyReleaseEvent & event) override{ + bool on_key_released(const KeyReleaseEvent & event) override { std::cout << "KeyRelease function" << std::endl; return false; } - bool on_mouse_clicked(const MouseClickEvent & event) override{ + bool on_mouse_clicked(const MouseClickEvent & event) override { std::cout << "MouseClick function" << std::endl; return false; } - bool on_mouse_pressed(const MousePressEvent & event) override { + bool on_mouse_pressed(const MousePressEvent & event) override { std::cout << "MousePress function" << std::endl; return false; } - bool on_mouse_released(const MouseReleaseEvent & event) override { + bool on_mouse_released(const MouseReleaseEvent & event) override { std::cout << "MouseRelease function" << std::endl; return false; } - bool on_mouse_moved(const MouseMoveEvent & event) override { + bool on_mouse_moved(const MouseMoveEvent & event) override { std::cout << "MouseMove function" << std::endl; return false; } - }; class TestKeyListener : public IKeyListener { public: - bool on_key_pressed(const KeyPressEvent &event) override { - std::cout << "TestKeyListener: Key Pressed - Code: " << event.key << std::endl; - return true; // Return true if the listener should remain active - } - bool on_key_released(const KeyReleaseEvent &event) override { - std::cout << "TestKeyListener: Key Released - Code: " << event.key << std::endl; - return true; - } + bool on_key_pressed(const KeyPressEvent & event) override { + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) + << std::endl; + return true; // Return true if the listener should remain active + } + bool on_key_released(const KeyReleaseEvent & event) override { + std::cout << "TestKeyListener: Key Released - Code: " << static_cast(event.key) + << std::endl; + return true; + } }; int main() { // two events to trigger KeyPressEvent key_press; - key_press.key = 1; - key_press.repeat = 0; + key_press.key = Keycode::A; + key_press.repeat = 0; MouseClickEvent click_event; - click_event.button = MouseButton::LEFT_MOUSE; - click_event.mouse_x = 100; - click_event.mouse_y = 200; + click_event.button = MouseButton::Left_Mouse; + click_event.mouse_x = 100; + click_event.mouse_y = 200; // queue events to test queue - EventManager::get_instance().queue_event(std::move(key_press), 0); - EventManager::get_instance().queue_event(std::move(click_event), 0); - { - TestKeyListener testListener; - testListener.set_channel(1); - auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); - obj.add_component().set_script(); + EventManager::get_instance().queue_event( + std::move(key_press), 0); + EventManager::get_instance().queue_event( + std::move(click_event), 0); + { + TestKeyListener test_listener; + test_listener.set_channel(1); + auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); + obj.add_component().set_script(); - ScriptSystem sys; - sys.update(); + ScriptSystem sys; + sys.update(); - // Trigger the events while `testListener` is in scope - EventManager::get_instance().trigger_event(key_press, 1); - EventManager::get_instance().trigger_event(click_event, 1); - } + // Trigger the events while `testListener` is in scope + EventManager::get_instance().trigger_event(key_press, 1); + EventManager::get_instance().trigger_event(click_event, + 1); + } // custom lambda event handler - EventHandler event_handler = [](const KeyPressEvent& e) { - std::cout << "lambda test" << std::endl; + EventHandler event_handler = [](const KeyPressEvent & e) { + std::cout << "lambda test" << std::endl; return false; }; - EventManager::get_instance().subscribe(std::move(event_handler),0); + EventManager::get_instance().subscribe( + std::move(event_handler), 0); // testing trigger with testListener not in scope (unsubscribed) - EventManager::get_instance().trigger_event(key_press, 0); - EventManager::get_instance().trigger_event(click_event, 0); + EventManager::get_instance().trigger_event(key_press, 0); + EventManager::get_instance().trigger_event(click_event, 0); // dispatching queued events EventManager::get_instance().dispatch_events(); - EventManager::get_instance().unsubscribe(event_handler,0); - return EXIT_SUCCESS; + EventManager::get_instance().unsubscribe(event_handler, 0); + return EXIT_SUCCESS; } - - -- cgit v1.2.3 From 9ce53b197953e66189febeaa434255b848647993 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 15:40:52 +0100 Subject: make format --- src/crepe/api/Event.h | 4 +- src/crepe/api/EventManager.h | 1 - src/crepe/api/KeyCodes.h | 118 +++++++++++++++++++++---------------------- src/example/events.cpp | 16 +++--- 4 files changed, 69 insertions(+), 70 deletions(-) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 87c6068..cffa5da 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -1,8 +1,8 @@ #pragma once -#include +#include "KeyCodes.h" #include #include -#include "KeyCodes.h" +#include class Event { public: diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 26539ce..e2665bd 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -254,4 +254,3 @@ void EventManager::unsubscribe(const EventHandler & callback, } } } - diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 53be91b..1cb1a8a 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,25 +1,25 @@ #pragma once enum class MouseButton { - None = 0, - Left_Mouse = 1, - Right_Mouse = 2, - Middle_Mouse = 3, - X1_Mouse = 4, - X2_Mouse = 5, - Scroll_Up = 6, - Scroll_Down = 7, + NONE = 0, + LEFT_MOUSE = 1, + RIGHT_MOUSE = 2, + MIDDLE_MOUSE = 3, + X1_MOUSE = 4, + X2_MOUSE = 5, + SCROLL_UP = 6, + SCROLL_DOWN = 7, }; -enum class Keycode : int{ +enum class Keycode : int { // From glfw3.h - None = 0, - Space = 32, - Apostrophe = 39, /* ' */ - Comma = 44, /* , */ - Minus = 45, /* - */ - Period = 46, /* . */ - Slash = 47, /* / */ + NONE = 0, + SPACE = 32, + APOSTROPHE = 39, /* ' */ + COMMA = 44, /* , */ + MINUS = 45, /* - */ + PERIOD = 46, /* . */ + SLASH = 47, /* / */ D0 = 48, /* 0 */ D1 = 49, /* 1 */ @@ -32,8 +32,8 @@ enum class Keycode : int{ D8 = 56, /* 8 */ D9 = 57, /* 9 */ - Semicolon = 59, /* ; */ - Equal = 61, /* = */ + SEMICOLON = 59, /* ; */ + EQUAL = 61, /* = */ A = 65, B = 66, @@ -62,34 +62,34 @@ enum class Keycode : int{ Y = 89, Z = 90, - LeftBracket = 91, /* [ */ - Backslash = 92, /* \ */ - RightBracket = 93, /* ] */ - GraveAccent = 96, /* ` */ + LEFT_BRACKET = 91, /* [ */ + BACKSLASH = 92, /* \ */ + RIGHT_BRACKET = 93, /* ] */ + GRAVE_ACCENT = 96, /* ` */ - World1 = 161, /* non-US #1 */ - World2 = 162, /* non-US #2 */ + WORLD1 = 161, /* non-US #1 */ + WORLD2 = 162, /* non-US #2 */ /* Function keys */ - Escape = 256, - Enter = 257, - Tab = 258, - Backspace = 259, - Insert = 260, - Delete = 261, - Right = 262, - Left = 263, - Down = 264, - Up = 265, - PageUp = 266, - PageDown = 267, - Home = 268, - End = 269, - CapsLock = 280, - ScrollLock = 281, - NumLock = 282, - PrintScreen = 283, - Pause = 284, + ESCAPE = 256, + ENTER = 257, + TAB = 258, + BACKSPACE = 259, + INSERT = 260, + DELETE = 261, + RIGHT = 262, + LEFT = 263, + DOWN = 264, + UP = 265, + PAGE_UP = 266, + PAGE_DOWN = 267, + HOME = 268, + END = 269, + CAPS_LOCK = 280, + SCROLL_LOCK = 281, + NUM_LOCK = 282, + PRINT_SCREEN = 283, + PAUSE = 284, F1 = 290, F2 = 291, F3 = 292, @@ -127,21 +127,21 @@ enum class Keycode : int{ KP7 = 327, KP8 = 328, KP9 = 329, - KPDecimal = 330, - KPDivide = 331, - KPMultiply = 332, - KPSubtract = 333, - KPAdd = 334, - KPEnter = 335, - KPEqual = 336, + KP_DECIMAL = 330, + KP_DIVIDE = 331, + KP_MULTIPLY = 332, + KP_SUBTRACT = 333, + KP_ADD = 334, + KP_ENTER = 335, + KP_EQUAL = 336, - LeftShift = 340, - LeftControl = 341, - LeftAlt = 342, - LeftSuper = 343, - RightShift = 344, - RightControl = 345, - RightAlt = 346, - RightSuper = 347, - Menu = 348 + LEFT_SHIFT = 340, + LEFT_CONTROL = 341, + LEFT_ALT = 342, + LEFT_SUPER = 343, + RIGHT_SHIFT = 344, + RIGHT_CONTROL = 345, + RIGHT_ALT = 346, + RIGHT_SUPER = 347, + MENU = 348 }; diff --git a/src/example/events.cpp b/src/example/events.cpp index 210c42c..51c9a37 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -4,16 +4,16 @@ #include #include +#include +#include #include #include +#include #include #include -#include -#include -#include +#include #include #include -#include using namespace crepe; using namespace std; @@ -54,13 +54,13 @@ class MyScript : public Script, public IKeyListener, public IMouseListener { class TestKeyListener : public IKeyListener { public: bool on_key_pressed(const KeyPressEvent & event) override { - std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) - << std::endl; + std::cout << "TestKeyListener: Key Pressed - Code: " + << static_cast(event.key) << std::endl; return true; // Return true if the listener should remain active } bool on_key_released(const KeyReleaseEvent & event) override { - std::cout << "TestKeyListener: Key Released - Code: " << static_cast(event.key) - << std::endl; + std::cout << "TestKeyListener: Key Released - Code: " + << static_cast(event.key) << std::endl; return true; } }; -- cgit v1.2.3 From d6c48b5b955336f7e2832bae7b7eca4afdb02cc4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 14 Nov 2024 13:42:35 +0100 Subject: removed unwanted files --- .vscode/launch.json | 7 ------- .vscode/tasks.json | 28 ---------------------------- 2 files changed, 35 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 5c7247b..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [] -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 05054c5..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: g++ build active file", - "command": "/usr/bin/g++", - "args": [ - "-fdiagnostics-color=always", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file -- cgit v1.2.3 From cab672ce6258aec563e7f0e0549281156c0c8d21 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 15 Nov 2024 12:43:38 +0100 Subject: feedback --- src/crepe/api/CMakeLists.txt | 14 ++++ src/crepe/api/Event.h | 13 ++-- src/crepe/api/EventHandler.h | 1 + src/crepe/api/EventManager.cpp | 3 + src/crepe/api/EventManager.h | 142 ++------------------------------------- src/crepe/api/EventManager.hpp | 140 ++++++++++++++++++++++++++++++++++++++ src/crepe/api/IKeyListener.cpp | 4 +- src/crepe/api/IMouseListener.cpp | 10 ++- src/crepe/api/KeyCodes.h | 1 - src/example/events.cpp | 10 ++- 10 files changed, 187 insertions(+), 151 deletions(-) create mode 100644 src/crepe/api/EventManager.hpp diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 87cbb09..53b3041 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -18,6 +18,12 @@ target_sources(crepe PUBLIC Vector2.cpp Camera.cpp Animator.cpp + EventManager.cpp + EventHandler.cpp + IKeyListener.cpp + IMouseListener.cpp + LoopManager.cpp + LoopTimer.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -42,4 +48,12 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SceneManager.hpp Camera.h Animator.h + EventManager.h + EventManager.hpp + EventHandler.h + Event.h + IKeyListener.h + IMouseListener.h + LoopManager.h + LoopTimer.h ) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index cffa5da..701ecdf 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -6,38 +6,37 @@ class Event { public: - bool handled = false; }; class KeyPressEvent : public Event { public: int repeat = 0; - Keycode key = Keycode::None; + Keycode key = Keycode::NONE; }; class KeyReleaseEvent : public Event { public: - Keycode key = Keycode::None; + Keycode key = Keycode::NONE; }; class MousePressEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button; + MouseButton button = MouseButton::NONE; }; class MouseClickEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button; + MouseButton button = MouseButton::NONE; }; class MouseReleaseEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button = MouseButton::None; + MouseButton button = MouseButton::NONE; }; class MouseMoveEvent : public Event { public: @@ -50,7 +49,7 @@ public: }; class TextSubmitEvent : public Event { public: - std::string text; + std::string text = ""; }; class ShutDownEvent : public Event { public: diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 46c6c7b..0ab90de 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -12,6 +12,7 @@ * * \tparam EventType The type of event this handler will handle. */ +// TODO: typedef template using EventHandler = std::function; diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 72cfd74..e881d49 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,5 +1,7 @@ #include "EventManager.h" +using namespace crepe; + EventManager & EventManager::get_instance() { static EventManager instance; return instance; @@ -62,6 +64,7 @@ void EventManager::dispatch_events() { iterator handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) { + // remove event from queue since and continue when callback returns true if ((*handler_it)->exec(*event)) { event_it = this->events_queue.erase(event_it); event_handled = true; diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index e2665bd..38d2e64 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -10,6 +10,8 @@ #include "Event.h" #include "EventHandler.h" + +namespace crepe { /** * \class EventManager * \brief The EventManager class is responsible for managing the subscription, triggering, @@ -115,142 +117,6 @@ private: std::vector>>> subscribers_by_event_id; }; -template -void EventManager::subscribe(EventHandler && callback, int channel) { - std::type_index event_type = typeid(EventType); - std::unique_ptr> handler - = std::make_unique>(callback); - - if (channel) { - std::unordered_map>> & - handlers_map - = this->subscribers_by_event_id[event_type]; - std::unordered_map< - int, std::vector>>::iterator - handlers - = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - handlers->second.emplace_back(std::move(handler)); - } else { - handlers_map[channel].emplace_back(std::move(handler)); - } - } else { - std::vector> & handlers - = this->subscribers[event_type]; - handlers.emplace_back(std::move(handler)); - } -} - -template -void EventManager::queue_event(EventType && event, int channel) { - std::type_index event_type = std::type_index(typeid(EventType)); - - std::unique_ptr event_ptr - = std::make_unique(std::forward(event)); - - std::tuple, int, std::type_index> tuple( - std::move(event_ptr), channel, event_type); - this->events_queue.push_back(std::move(tuple)); -} - -template -void EventManager::trigger_event(const EventType & event, int channel) { - std::type_index event_type = std::type_index(typeid(EventType)); - - if (channel > 0) { - std::unordered_map>> & - handlers_map - = this->subscribers_by_event_id[event_type]; - std::unordered_map< - int, std::vector>>::iterator - handlers_it - = handlers_map.find(channel); - - if (handlers_it != handlers_map.end()) { - std::vector> & handlers - = handlers_it->second; - for (std::vector>::iterator it - = handlers.begin(); - it != handlers.end();) { - // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } - } else { - std::vector> & handlers - = this->subscribers[event_type]; - for (std::vector>::iterator it - = handlers.begin(); - it != handlers.end();) { - // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } -} - -template -void EventManager::unsubscribe(const EventHandler & callback, - int channel) { - std::type_index event_type(typeid(EventType)); - std::string handler_name = callback.target_type().name(); - if (channel) { - std::unordered_map< - std::type_index, - std::unordered_map< - int, std::vector>>>:: - iterator subscriber_list - = this->subscribers_by_event_id.find(event_type); - if (subscriber_list != this->subscribers_by_event_id.end()) { - std::unordered_map< - int, std::vector>> & - handlers_map - = subscriber_list->second; - std::unordered_map< - int, - std::vector>>::iterator - handlers - = handlers_map.find(channel); - if (handlers != handlers_map.end()) { - std::vector> & callbacks - = handlers->second; - for (std::vector< - std::unique_ptr>::iterator it - = callbacks.begin(); - it != callbacks.end(); ++it) { - if ((*it)->get_type() == handler_name) { - it = callbacks.erase(it); - return; - } - } - } - } - } else { - std::unordered_map>>:: - iterator handlers_it - = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector> & handlers - = handlers_it->second; - for (std::vector>::iterator it - = handlers.begin(); - it != handlers.end(); ++it) { - if ((*it)->get_type() == handler_name) { - it = handlers.erase(it); - return; - } - } - } - } -} +} // namespace crepe +#include "EventManager.hpp" diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp new file mode 100644 index 0000000..b509097 --- /dev/null +++ b/src/crepe/api/EventManager.hpp @@ -0,0 +1,140 @@ +#include "EventManager.h" +namespace crepe { + +template +void EventManager::subscribe(EventHandler && callback, int channel) { + std::type_index event_type = typeid(EventType); + std::unique_ptr> handler + = std::make_unique>(callback); + + if (channel) { + std::unordered_map>> & + handlers_map + = this->subscribers_by_event_id[event_type]; + std::unordered_map< + int, std::vector>>::iterator + handlers + = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + handlers->second.emplace_back(std::move(handler)); + } else { + handlers_map[channel].emplace_back(std::move(handler)); + } + } else { + std::vector> & handlers + = this->subscribers[event_type]; + handlers.emplace_back(std::move(handler)); + } +} + +template +void EventManager::queue_event(EventType && event, int channel) { + std::type_index event_type = std::type_index(typeid(EventType)); + + std::unique_ptr event_ptr + = std::make_unique(std::forward(event)); + + std::tuple, int, std::type_index> tuple( + std::move(event_ptr), channel, event_type); + this->events_queue.push_back(std::move(tuple)); +} + +template +void EventManager::trigger_event(const EventType & event, int channel) { + std::type_index event_type = std::type_index(typeid(EventType)); + + if (channel > 0) { + std::unordered_map>> & + handlers_map + = this->subscribers_by_event_id[event_type]; + std::unordered_map< + int, std::vector>>::iterator + handlers_it + = handlers_map.find(channel); + + if (handlers_it != handlers_map.end()) { + std::vector> & handlers + = handlers_it->second; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end();++it) { + // stops when callback returns true + if((*it)->exec(event)){ + break; + } + } + } + } else { + std::vector> & handlers + = this->subscribers[event_type]; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end();++it) { + // stops when callback returns true + if((*it)->exec(event)){ + break; + } + } + } +} + +template +void EventManager::unsubscribe(const EventHandler & callback, + int channel) { + std::type_index event_type(typeid(EventType)); + std::string handler_name = callback.target_type().name(); + + if (channel) { + std::unordered_map< + std::type_index, + std::unordered_map< + int, std::vector>>>:: + iterator subscriber_list + = this->subscribers_by_event_id.find(event_type); + if (subscriber_list != this->subscribers_by_event_id.end()) { + std::unordered_map< + int, std::vector>> & + handlers_map + = subscriber_list->second; + std::unordered_map< + int, + std::vector>>::iterator + handlers + = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + std::vector> & callbacks + = handlers->second; + for (std::vector< + std::unique_ptr>::iterator it + = callbacks.begin(); + it != callbacks.end(); ++it) { + if ((*it)->get_type() == handler_name) { + it = callbacks.erase(it); + return; + } + } + } + } + } else { + std::unordered_map>>:: + iterator handlers_it + = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + std::vector> & handlers + = handlers_it->second; + for (std::vector>::iterator it + = handlers.begin(); + it != handlers.end(); ++it) { + if ((*it)->get_type() == handler_name) { + it = handlers.erase(it); + return; + } + } + } + } +} + +} diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index eb8f9af..4fd9855 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -1,5 +1,7 @@ #include "IKeyListener.h" -#include + +using namespace crepe; + IKeyListener::IKeyListener() { this->channel = channel; this->subscribe_events(); diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 683632c..489e55b 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -1,6 +1,11 @@ #include "IMouseListener.h" + +using namespace crepe; + IMouseListener::IMouseListener(int channel) { this->channel = channel; } + IMouseListener::IMouseListener() { this->subscribe_events(); } + IMouseListener::~IMouseListener() { this->unsubscribe_events(); } void IMouseListener::subscribe_events() { @@ -26,7 +31,7 @@ void IMouseListener::subscribe_events() { EventManager::get_instance().subscribe( std::move(this->mouse_move_handler), this->channel); } - +// TODO: reference voor singleton void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe( this->mouse_click_handler, this->channel); @@ -37,18 +42,21 @@ void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe( this->mouse_move_handler, this->channel); } + void IMouseListener::activate_mouse() { if (this->active) { return; } this->subscribe_events(); } + void IMouseListener::deactivate_mouse() { if (!this->active) { return; } this->unsubscribe_events(); } + void IMouseListener::set_channel(int channel) { this->unsubscribe_events(); this->channel = channel; diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 1cb1a8a..e5a91fc 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -12,7 +12,6 @@ enum class MouseButton { }; enum class Keycode : int { - // From glfw3.h NONE = 0, SPACE = 32, APOSTROPHE = 39, /* ' */ diff --git a/src/example/events.cpp b/src/example/events.cpp index 51c9a37..9c59bb7 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -70,7 +70,7 @@ int main() { key_press.key = Keycode::A; key_press.repeat = 0; MouseClickEvent click_event; - click_event.button = MouseButton::Left_Mouse; + click_event.button = MouseButton::LEFT_MOUSE; click_event.mouse_x = 100; click_event.mouse_y = 200; // queue events to test queue @@ -89,8 +89,12 @@ int main() { // Trigger the events while `testListener` is in scope EventManager::get_instance().trigger_event(key_press, 1); - EventManager::get_instance().trigger_event(click_event, - 1); + // EventManager::get_instance().trigger_event(MouseClickEvent{ + // .button = MouseButton::LEFT_MOUSE, + // .mouse_y = 100, + // .mouse_x = 100, + + // },1); } // custom lambda event handler EventHandler event_handler = [](const KeyPressEvent & e) { -- cgit v1.2.3 From 337a957a9e605f16287506b6afda56950e562db3 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 15 Nov 2024 12:50:30 +0100 Subject: events example working --- src/example/events.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/example/events.cpp b/src/example/events.cpp index 9c59bb7..ed519ff 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -89,12 +89,11 @@ int main() { // Trigger the events while `testListener` is in scope EventManager::get_instance().trigger_event(key_press, 1); - // EventManager::get_instance().trigger_event(MouseClickEvent{ - // .button = MouseButton::LEFT_MOUSE, - // .mouse_y = 100, - // .mouse_x = 100, - - // },1); + EventManager::get_instance().trigger_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 100, + .button = MouseButton::LEFT_MOUSE, + },1); } // custom lambda event handler EventHandler event_handler = [](const KeyPressEvent & e) { -- cgit v1.2.3