From 6e7003c73110cf9ad7e4c72de703dfdc2292d8ca Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 16 Nov 2024 13:55:47 +0100 Subject: added hpp files and auto --- src/crepe/api/EventHandler.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/crepe/api/EventHandler.hpp (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp new file mode 100644 index 0000000..1b5702e --- /dev/null +++ b/src/crepe/api/EventHandler.hpp @@ -0,0 +1,25 @@ + +#include + +#include "EventHandler.h" + +namespace crepe { + +// Implementation of EventHandlerWrapper constructor +template +EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) + : m_handler(handler), m_handler_type(m_handler.target_type().name()) {} + +// Implementation of EventHandlerWrapper::call +template +bool EventHandlerWrapper::call(const Event & e) { + return m_handler(static_cast(e)); +} + +// Implementation of EventHandlerWrapper::get_type +template +std::string EventHandlerWrapper::get_type() const { + return m_handler_type; +} + +} //namespace crepe -- cgit v1.2.3 From 027e67b32a9cca3cac4a186e73fdcc42faeab8c4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 16 Nov 2024 15:21:49 +0100 Subject: added eventManager reference to interfaces --- src/crepe/api/Event.h | 67 ++++----- src/crepe/api/EventHandler.cpp | 4 +- src/crepe/api/EventHandler.h | 36 ++--- src/crepe/api/EventHandler.hpp | 10 +- src/crepe/api/EventManager.cpp | 49 ++---- src/crepe/api/EventManager.h | 12 +- src/crepe/api/EventManager.hpp | 69 ++++----- src/crepe/api/IKeyListener.cpp | 81 ++++++---- src/crepe/api/IKeyListener.h | 3 + src/crepe/api/IMouseListener.cpp | 45 +++--- src/crepe/api/IMouseListener.h | 2 + src/crepe/api/KeyCodes.h | 318 ++++++++++++++++++++++----------------- src/example/events.cpp | 12 +- 13 files changed, 365 insertions(+), 343 deletions(-) (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index e7cac25..d5ddf0a 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -1,10 +1,9 @@ #pragma once -#include #include -#include #include "KeyCodes.h" + /** * \brief Base class for all event types in the system. */ @@ -17,11 +16,11 @@ public: */ class KeyPressEvent : public Event { public: - //! Number of times the key press is repeated (e.g., for long presses). - int repeat = 0; + //! Number of times the key press is repeated (e.g., for long presses). + int repeat = 0; - //! The key that was pressed. - Keycode key = Keycode::NONE; + //! The key that was pressed. + Keycode key = Keycode::NONE; }; /** @@ -29,8 +28,8 @@ public: */ class KeyReleaseEvent : public Event { public: - //! The key that was released. - Keycode key = Keycode::NONE; + //! The key that was released. + Keycode key = Keycode::NONE; }; /** @@ -38,14 +37,14 @@ public: */ class MousePressEvent : public Event { public: - //! X-coordinate of the mouse position at the time of the event. - int mouse_x = 0; + //! X-coordinate of the mouse position at the time of the event. + int mouse_x = 0; - //! Y-coordinate of the mouse position at the time of the event. - int mouse_y = 0; + //! Y-coordinate of the mouse position at the time of the event. + int mouse_y = 0; - //! The mouse button that was pressed. - MouseButton button = MouseButton::NONE; + //! The mouse button that was pressed. + MouseButton button = MouseButton::NONE; }; /** @@ -53,14 +52,14 @@ public: */ class MouseClickEvent : public Event { public: - //! X-coordinate of the mouse position at the time of the event. - int mouse_x = 0; + //! X-coordinate of the mouse position at the time of the event. + int mouse_x = 0; - //! Y-coordinate of the mouse position at the time of the event. - int mouse_y = 0; + //! Y-coordinate of the mouse position at the time of the event. + int mouse_y = 0; - //! The mouse button that was clicked. - MouseButton button = MouseButton::NONE; + //! The mouse button that was clicked. + MouseButton button = MouseButton::NONE; }; /** @@ -68,14 +67,14 @@ public: */ class MouseReleaseEvent : public Event { public: - //! X-coordinate of the mouse position at the time of the event. - int mouse_x = 0; + //! X-coordinate of the mouse position at the time of the event. + int mouse_x = 0; - //! Y-coordinate of the mouse position at the time of the event. - int mouse_y = 0; + //! Y-coordinate of the mouse position at the time of the event. + int mouse_y = 0; - //! The mouse button that was released. - MouseButton button = MouseButton::NONE; + //! The mouse button that was released. + MouseButton button = MouseButton::NONE; }; /** @@ -83,11 +82,11 @@ public: */ class MouseMoveEvent : public Event { public: - //! X-coordinate of the mouse position at the time of the event. - int mouse_x = 0; + //! X-coordinate of the mouse position at the time of the event. + int mouse_x = 0; - //! Y-coordinate of the mouse position at the time of the event. - int mouse_y = 0; + //! Y-coordinate of the mouse position at the time of the event. + int mouse_y = 0; }; /** @@ -95,8 +94,8 @@ public: */ class CollisionEvent : public Event { public: - //! Data describing the collision (currently not implemented). - // Collision collisionData; + //! Data describing the collision (currently not implemented). + // Collision collisionData; }; /** @@ -104,8 +103,8 @@ public: */ class TextSubmitEvent : public Event { public: - //! The submitted text. - std::string text = ""; + //! The submitted text. + std::string text = ""; }; /** diff --git a/src/crepe/api/EventHandler.cpp b/src/crepe/api/EventHandler.cpp index d90d765..186ec9c 100644 --- a/src/crepe/api/EventHandler.cpp +++ b/src/crepe/api/EventHandler.cpp @@ -3,6 +3,4 @@ using namespace crepe; // Implementation of IEventHandlerWrapper::exec -bool IEventHandlerWrapper::exec(const Event & e) { - return call(e); -} +bool IEventHandlerWrapper::exec(const Event & e) { return call(e); } diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 2a684c0..db51d04 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -26,12 +26,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. @@ -39,19 +39,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. @@ -59,7 +59,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; }; /** @@ -75,17 +75,17 @@ 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); + explicit EventHandlerWrapper(const EventHandler & handler); private: - /** + /** * \brief Calls the stored event handler with the event. * * This method casts the event to the appropriate type and calls the handler. @@ -93,21 +93,21 @@ private: * \param e The event to be handled. * \return A boolean value indicating whether the event is handled. */ - bool call(const Event & e) override; + bool call(const Event & e) override; - /** + /** * \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; + std::string get_type() const override; - //! The event handler function. - EventHandler m_handler; - //! The type name of the handler function. - 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; }; } // namespace crepe diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index 1b5702e..564d3d7 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -7,19 +7,21 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template -EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) - : m_handler(handler), m_handler_type(m_handler.target_type().name()) {} +EventHandlerWrapper::EventHandlerWrapper( + const EventHandler & handler) + : m_handler(handler), + m_handler_type(m_handler.target_type().name()) {} // Implementation of EventHandlerWrapper::call template bool EventHandlerWrapper::call(const Event & e) { - return m_handler(static_cast(e)); + return m_handler(static_cast(e)); } // Implementation of EventHandlerWrapper::get_type template std::string EventHandlerWrapper::get_type() const { - return m_handler_type; + return m_handler_type; } } //namespace crepe diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index e881d49..a04f08b 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -8,10 +8,10 @@ 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();) { + using HandlersMap = std::unordered_map>>; + using HandlersVec = std::vector>; + + for (auto 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); @@ -19,29 +19,13 @@ void EventManager::dispatch_events() { bool event_handled = false; if (channel) { - std::unordered_map< - std::type_index, - std::unordered_map< - int, std::vector>>>:: - iterator handlers_it - = subscribers_by_event_id.find(event_type); + auto 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); + HandlersMap & handlers_map = handlers_it->second; + auto 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) { + HandlersVec & callbacks = handlers->second; + for (auto 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; @@ -52,18 +36,10 @@ void EventManager::dispatch_events() { } } else { // Handle event for all channels - std::unordered_map< - std::type_index, - std::vector>>::iterator - handlers_it - = this->subscribers.find(event_type); + auto 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) { + HandlersVec & handlers = handlers_it->second; + for (auto 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); @@ -79,3 +55,4 @@ void EventManager::dispatch_events() { } } } + diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 38d2e64..1ff4031 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -10,8 +10,8 @@ #include "Event.h" #include "EventHandler.h" - namespace crepe { + /** * \class EventManager * \brief The EventManager class is responsible for managing the subscription, triggering, @@ -19,16 +19,6 @@ namespace crepe { */ class EventManager { public: - /** - * \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. * diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index 70f0a31..1e505f4 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -1,17 +1,18 @@ #include "EventManager.h" + namespace crepe { template void EventManager::subscribe(EventHandler && callback, int channel) { + using HandlersMap = std::unordered_map>>; + using HandlersVec = std::vector>; + 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]; + HandlersMap & handlers_map = this->subscribers_by_event_id[event_type]; auto handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { handlers->second.emplace_back(std::move(handler)); @@ -19,8 +20,7 @@ void EventManager::subscribe(EventHandler && callback, int channel) { handlers_map[channel].emplace_back(std::move(handler)); } } else { - std::vector> & handlers - = this->subscribers[event_type]; + HandlersVec & handlers = this->subscribers[event_type]; handlers.emplace_back(std::move(handler)); } } @@ -29,8 +29,7 @@ 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)); + auto event_ptr = std::make_unique(std::forward(event)); std::tuple, int, std::type_index> tuple( std::move(event_ptr), channel, event_type); @@ -39,59 +38,51 @@ void EventManager::queue_event(EventType && event, int channel) { template void EventManager::trigger_event(const EventType & event, int channel) { + using HandlersMap = std::unordered_map>>; + using HandlersVec = std::vector>; + 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]; + HandlersMap & handlers_map = this->subscribers_by_event_id[event_type]; auto handlers_it = handlers_map.find(channel); if (handlers_it != handlers_map.end()) { - std::vector> & handlers - = handlers_it->second; - for (auto it = handlers.begin(); - it != handlers.end();++it) { + HandlersVec & handlers = handlers_it->second; + for (auto it = handlers.begin(); it != handlers.end(); ++it) { // stops when callback returns true - if((*it)->exec(event)){ + if ((*it)->exec(event)) { break; } } } } else { - std::vector> & handlers - = this->subscribers[event_type]; - for (auto it - = handlers.begin(); - it != handlers.end();++it) { + HandlersVec & handlers = this->subscribers[event_type]; + for (auto it = handlers.begin(); it != handlers.end(); ++it) { // stops when callback returns true - if((*it)->exec(event)){ - break; - } + if ((*it)->exec(event)) { + break; + } } } } template -void EventManager::unsubscribe(const EventHandler & callback, - int channel) { +void EventManager::unsubscribe(const EventHandler & callback, int channel) { + using HandlersMap = std::unordered_map>>; + using HandlersVec = std::vector>; + std::type_index event_type(typeid(EventType)); std::string handler_name = callback.target_type().name(); if (channel) { auto 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; - auto handlers = handlers_map.find(channel); + HandlersMap & handlers_map = subscriber_list->second; + auto handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { - std::vector> & callbacks - = handlers->second; - for (auto it = callbacks.begin(); - it != callbacks.end(); ++it) { + HandlersVec & callbacks = handlers->second; + for (auto it = callbacks.begin(); it != callbacks.end(); ++it) { if ((*it)->get_type() == handler_name) { it = callbacks.erase(it); return; @@ -102,10 +93,8 @@ void EventManager::unsubscribe(const EventHandler & callback, } else { auto handlers_it = this->subscribers.find(event_type); if (handlers_it != this->subscribers.end()) { - std::vector> & handlers - = handlers_it->second; - for (auto it = handlers.begin(); - it != handlers.end(); ++it) { + HandlersVec & handlers = handlers_it->second; + for (auto 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 4fd9855..81d30b8 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -2,50 +2,65 @@ using namespace crepe; -IKeyListener::IKeyListener() { - this->channel = channel; - this->subscribe_events(); +// Constructor with default channel +IKeyListener::IKeyListener() : channel(0), active(true), event_manager(EventManager::get_instance()) { + this->subscribe_events(); } -IKeyListener::IKeyListener(int channel) { - this->channel = channel; - this->subscribe_events(); + +// Constructor with specified channel +IKeyListener::IKeyListener(int channel) : channel(channel), active(true), event_manager(EventManager::get_instance()) { + this->subscribe_events(); +} + +// Destructor, unsubscribe events +IKeyListener::~IKeyListener() { + this->unsubscribe_events(); } -IKeyListener::~IKeyListener() { this->unsubscribe_events(); } +// Subscribe to key 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); + }; + + event_manager.subscribe( + std::move(this->key_pressed_handler), this->channel); + event_manager.subscribe( + std::move(this->key_released_handler), this->channel); } +// Unsubscribe from key events 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; + event_manager.unsubscribe( + this->key_pressed_handler, this->channel); + event_manager.unsubscribe( + this->key_released_handler, this->channel); } + +// Activate key listening void IKeyListener::activate_keys() { - if (this->active) { - return; - } - this->subscribe_events(); + if (this->active) { + return; + } + this->active = true; + this->subscribe_events(); } + +// Deactivate key listening void IKeyListener::deactivate_keys() { - if (!this->active) { - return; - } - this->unsubscribe_events(); + if (!this->active) { + return; + } + this->active = false; + this->unsubscribe_events(); } + +// Set a new channel for key events void IKeyListener::set_channel(int channel) { - this->unsubscribe_events(); - this->channel = channel; - this->subscribe_events(); + this->unsubscribe_events(); + this->channel = channel; + this->subscribe_events(); } diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 5a2cafa..77fbf1e 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -1,9 +1,11 @@ #pragma once + #include "Event.h" #include "EventHandler.h" #include "EventManager.h" namespace crepe { + /** * \class IKeyListener * \brief Interface for keyboard event handling in the application. @@ -76,6 +78,7 @@ private: EventHandler key_pressed_handler; //!< Key release event handler. EventHandler key_released_handler; + EventManager& event_manager; }; } // namespace crepe diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 489e55b..f49004a 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -2,11 +2,19 @@ using namespace crepe; -IMouseListener::IMouseListener(int channel) { this->channel = channel; } +IMouseListener::IMouseListener(int channel) + : event_manager(EventManager::get_instance()), channel(channel) { + this->subscribe_events(); +} -IMouseListener::IMouseListener() { this->subscribe_events(); } +IMouseListener::IMouseListener() + : event_manager(EventManager::get_instance()) { + this->subscribe_events(); +} -IMouseListener::~IMouseListener() { this->unsubscribe_events(); } +IMouseListener::~IMouseListener() { + this->unsubscribe_events(); +} void IMouseListener::subscribe_events() { // Define handler lambdas and subscribe them @@ -22,25 +30,20 @@ void IMouseListener::subscribe_events() { 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); + + // Subscribe event handlers (no need for std::move) + event_manager.subscribe(std::move(mouse_click_handler), this->channel); + event_manager.subscribe(std::move(mouse_press_handler), this->channel); + event_manager.subscribe(std::move(mouse_release_handler), this->channel); + event_manager.subscribe(std::move(mouse_move_handler), this->channel); } -// TODO: reference voor singleton + 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); + // Unsubscribe event handlers + event_manager.unsubscribe(mouse_click_handler, this->channel); + event_manager.unsubscribe(mouse_press_handler, this->channel); + event_manager.unsubscribe(mouse_release_handler, this->channel); + event_manager.unsubscribe(mouse_move_handler, this->channel); } void IMouseListener::activate_mouse() { @@ -48,6 +51,7 @@ void IMouseListener::activate_mouse() { return; } this->subscribe_events(); + this->active = true; } void IMouseListener::deactivate_mouse() { @@ -55,6 +59,7 @@ void IMouseListener::deactivate_mouse() { return; } this->unsubscribe_events(); + this->active = false; } void IMouseListener::set_channel(int channel) { diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 1df55af..3b9e317 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -5,6 +5,7 @@ #include "EventManager.h" namespace crepe { + /** * \class IMouseListener * \brief Interface for mouse event handling in the application. @@ -115,6 +116,7 @@ private: EventHandler mouse_release_handler; //! Mouse move event handler. EventHandler mouse_move_handler; + EventManager& event_manager; }; } //namespace crepe diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index e5a91fc..a326527 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,146 +1,186 @@ #pragma once +//! \file InputEnums.h +//! \brief Defines enums for mouse buttons and keyboard keycodes used in the application. + +//! \enum MouseButton +//! \brief Enumeration for mouse button inputs, including standard and extended buttons. 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, + //! No mouse button input. + NONE = 0, + //! Left mouse button. + LEFT_MOUSE = 1, + //! Right mouse button. + RIGHT_MOUSE = 2, + //! Middle mouse button (scroll wheel press). + MIDDLE_MOUSE = 3, + //! First extended mouse button. + X1_MOUSE = 4, + //! Second extended mouse button. + X2_MOUSE = 5, + //! Scroll wheel upward movement. + SCROLL_UP = 6, + //! Scroll wheel downward movement. + SCROLL_DOWN = 7 }; +//! \enum Keycode +//! \brief Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys. enum class Keycode : int { - 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, - - LEFT_BRACKET = 91, /* [ */ - BACKSLASH = 92, /* \ */ - RIGHT_BRACKET = 93, /* ] */ - GRAVE_ACCENT = 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, - 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, - 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, - KP_DECIMAL = 330, - KP_DIVIDE = 331, - KP_MULTIPLY = 332, - KP_SUBTRACT = 333, - KP_ADD = 334, - KP_ENTER = 335, - KP_EQUAL = 336, - - 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 + //! No key input. + NONE = 0, + //! Spacebar. + SPACE = 32, + //! Apostrophe ('). + APOSTROPHE = 39, + //! Comma (,). + COMMA = 44, + //! Minus (-). + MINUS = 45, + //! Period (.). + PERIOD = 46, + //! Slash (/). + SLASH = 47, + //! Digit 0. + D0 = 48, + //! Digit 1. + D1 = 49, + //! Digit 2. + D2 = 50, + //! Digit 3. + D3 = 51, + //! Digit 4. + D4 = 52, + //! Digit 5. + D5 = 53, + //! Digit 6. + D6 = 54, + //! Digit 7. + D7 = 55, + //! Digit 8. + D8 = 56, + //! Digit 9. + D9 = 57, + //! Semicolon (;). + SEMICOLON = 59, + //! Equal sign (=). + EQUAL = 61, + //! Key 'A'. + A = 65, + //! Key 'B'. + B = 66, + //! Key 'C'. + C = 67, + //! Key 'D'. + D = 68, + //! Key 'E'. + E = 69, + //! Key 'F'. + F = 70, + //! Key 'G'. + G = 71, + //! Key 'H'. + H = 72, + //! Key 'I'. + I = 73, + //! Key 'J'. + J = 74, + //! Key 'K'. + K = 75, + //! Key 'L'. + L = 76, + //! Key 'M'. + M = 77, + //! Key 'N'. + N = 78, + //! Key 'O'. + O = 79, + //! Key 'P'. + P = 80, + //! Key 'Q'. + Q = 81, + //! Key 'R'. + R = 82, + //! Key 'S'. + S = 83, + //! Key 'T'. + T = 84, + //! Key 'U'. + U = 85, + //! Key 'V'. + V = 86, + //! Key 'W'. + W = 87, + //! Key 'X'. + X = 88, + //! Key 'Y'. + Y = 89, + //! Key 'Z'. + Z = 90, + //! Left bracket ([). + LEFT_BRACKET = 91, + //! Backslash (\). + BACKSLASH = 92, + //! Right bracket (]). + RIGHT_BRACKET = 93, + //! Grave accent (`). + GRAVE_ACCENT = 96, + //! Non-US key #1. + WORLD1 = 161, + //! Non-US key #2. + WORLD2 = 162, + //! Escape key. + ESCAPE = 256, + //! Enter key. + ENTER = 257, + //! Tab key. + TAB = 258, + //! Backspace key. + BACKSPACE = 259, + //! Insert key. + INSERT = 260, + //! Delete key. + DELETE = 261, + //! Right arrow key. + RIGHT = 262, + //! Left arrow key. + LEFT = 263, + //! Down arrow key. + DOWN = 264, + //! Up arrow key. + UP = 265, + //! Page Up key. + PAGE_UP = 266, + //! Page Down key. + PAGE_DOWN = 267, + //! Home key. + HOME = 268, + //! End key. + END = 269, + //! Caps Lock key. + CAPS_LOCK = 280, + //! Scroll Lock key. + SCROLL_LOCK = 281, + //! Num Lock key. + NUM_LOCK = 282, + //! Print Screen key. + PRINT_SCREEN = 283, + //! Pause key. + PAUSE = 284, + //! Function keys (F1-F25). + 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 digits and operators. + KP0 = 320, KP1 = 321, KP2 = 322, KP3 = 323, KP4 = 324, + KP5 = 325, KP6 = 326, KP7 = 327, KP8 = 328, KP9 = 329, + KP_DECIMAL = 330, KP_DIVIDE = 331, KP_MULTIPLY = 332, + KP_SUBTRACT = 333, KP_ADD = 334, KP_ENTER = 335, KP_EQUAL = 336, + //! Modifier keys. + 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 key. + MENU = 348 }; diff --git a/src/example/events.cpp b/src/example/events.cpp index ed519ff..5a0a748 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -89,11 +89,13 @@ int main() { // Trigger the events while `testListener` is in scope EventManager::get_instance().trigger_event(key_press, 1); - EventManager::get_instance().trigger_event(MouseClickEvent{ - .mouse_x = 100, - .mouse_y = 100, - .button = MouseButton::LEFT_MOUSE, - },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 From 71a614e1f87141c4d062a2a466377322fc3d9ff0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 18 Nov 2024 15:40:11 +0100 Subject: make format --- mwe/events/include/event.h | 12 ++++-------- src/crepe/api/EventHandler.hpp | 3 +-- src/crepe/api/EventManager.cpp | 15 +++++++-------- src/crepe/api/EventManager.h | 9 +++------ src/crepe/api/EventManager.hpp | 22 ++++++++++----------- src/crepe/api/IKeyListener.cpp | 20 ++++++++------------ src/crepe/api/IKeyListener.h | 2 +- src/crepe/api/IMouseListener.cpp | 41 +++++++++++++++------------------------- src/crepe/api/IMouseListener.h | 1 - src/example/events.cpp | 17 +++++++---------- 10 files changed, 56 insertions(+), 86 deletions(-) (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 16c75bf..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -20,9 +20,7 @@ public: \ static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \ return typeID; \ } \ - virtual std::uint32_t getEventType() const override { \ - return getStaticEventType(); \ - } + virtual std::uint32_t getEventType() const override { return getStaticEventType(); } class Event { public: Event(std::string eventType); @@ -32,16 +30,14 @@ public: void addArgument(const std::string & key, const std::variant & value); - std::variant - getArgument(const std::string & key) const; + std::variant getArgument(const std::string & key) const; std::string getType() const; bool getHandled() const; void markHandled(); private: - std::unordered_map> - eventData; + std::unordered_map> eventData; bool isHandled = false; }; @@ -152,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index 564d3d7..9c47da2 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -7,8 +7,7 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template -EventHandlerWrapper::EventHandlerWrapper( - const EventHandler & handler) +EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) : m_handler(handler), m_handler_type(m_handler.target_type().name()) {} diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 7f47938..b465e89 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -8,12 +8,11 @@ EventManager & EventManager::get_instance() { } void EventManager::dispatch_events() { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; - for (auto event_it = this->events_queue.begin(); - event_it != this->events_queue.end();) { + for (auto 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); @@ -27,8 +26,8 @@ void EventManager::dispatch_events() { auto handlers = handlers_map.find(channel); if (handlers != handlers_map.end()) { HandlersVec & callbacks = handlers->second; - for (auto handler_it = callbacks.begin(); - handler_it != callbacks.end(); ++handler_it) { + for (auto 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; @@ -42,8 +41,8 @@ void EventManager::dispatch_events() { auto handlers_it = this->subscribers.find(event_type); if (handlers_it != this->subscribers.end()) { HandlersVec & handlers = handlers_it->second; - for (auto handler_it = handlers.begin(); - handler_it != handlers.end(); ++handler_it) { + for (auto 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); diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 783db62..c7c4744 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -94,17 +94,14 @@ private: EventManager() = default; //! The queue of events to be processed. - std::vector, int, std::type_index>> - events_queue; + std::vector, int, std::type_index>> events_queue; //! Registered event handlers. - std::unordered_map>> + std::unordered_map>> subscribers; //! Event handlers indexed by event ID. std::unordered_map< std::type_index, - std::unordered_map>>> + std::unordered_map>>> subscribers_by_event_id; }; diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index d901492..b20b88f 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -4,8 +4,8 @@ namespace crepe { template void EventManager::subscribe(EventHandler && callback, int channel) { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type = typeid(EventType); @@ -30,18 +30,17 @@ template void EventManager::queue_event(EventType && event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); - auto event_ptr - = std::make_unique(std::forward(event)); + auto event_ptr = std::make_unique(std::forward(event)); - std::tuple, int, std::type_index> tuple( - std::move(event_ptr), channel, event_type); + 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) { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type = std::type_index(typeid(EventType)); @@ -71,10 +70,9 @@ void EventManager::trigger_event(const EventType & event, int channel) { } template -void EventManager::unsubscribe(const EventHandler & callback, - int channel) { - using HandlersMap = std::unordered_map< - int, std::vector>>; +void EventManager::unsubscribe(const EventHandler & callback, int channel) { + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type(typeid(EventType)); diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index cd255df..f5426be 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -23,25 +23,21 @@ IKeyListener::~IKeyListener() { this->unsubscribe_events(); } // Subscribe to key 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); - }; + 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); }; event_manager.subscribe(std::move(this->key_pressed_handler), this->channel); - event_manager.subscribe( - std::move(this->key_released_handler), this->channel); + event_manager.subscribe(std::move(this->key_released_handler), + this->channel); } // Unsubscribe from key events void IKeyListener::unsubscribe_events() { - event_manager.unsubscribe(this->key_pressed_handler, - this->channel); - event_manager.unsubscribe(this->key_released_handler, - this->channel); + event_manager.unsubscribe(this->key_pressed_handler, this->channel); + event_manager.unsubscribe(this->key_released_handler, this->channel); } // Activate key listening diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 3e0e2cb..d492387 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -22,7 +22,7 @@ public: IKeyListener(const IKeyListener &) = delete; IKeyListener & operator=(const IKeyListener &) = delete; IKeyListener(IKeyListener &&) = delete; - + /** * \brief Pure virtual function to handle key press events. * \param event The key press event to handle. diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index bfa49f8..5ee2814 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -16,40 +16,29 @@ 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); - }; + 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); }; // Subscribe event handlers (no need for std::move) - event_manager.subscribe(std::move(mouse_click_handler), - this->channel); - event_manager.subscribe(std::move(mouse_press_handler), - this->channel); + event_manager.subscribe(std::move(mouse_click_handler), this->channel); + event_manager.subscribe(std::move(mouse_press_handler), this->channel); event_manager.subscribe(std::move(mouse_release_handler), this->channel); - event_manager.subscribe(std::move(mouse_move_handler), - this->channel); + event_manager.subscribe(std::move(mouse_move_handler), this->channel); } void IMouseListener::unsubscribe_events() { // Unsubscribe event handlers - event_manager.unsubscribe(mouse_click_handler, - this->channel); - event_manager.unsubscribe(mouse_press_handler, - this->channel); - event_manager.unsubscribe(mouse_release_handler, - this->channel); - event_manager.unsubscribe(mouse_move_handler, - this->channel); + event_manager.unsubscribe(mouse_click_handler, this->channel); + event_manager.unsubscribe(mouse_press_handler, this->channel); + event_manager.unsubscribe(mouse_release_handler, this->channel); + event_manager.unsubscribe(mouse_move_handler, this->channel); } void IMouseListener::activate_mouse() { diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 2ec156f..c315c35 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -12,7 +12,6 @@ namespace crepe { */ class IMouseListener { public: - IMouseListener(); /** * \brief Constructs an IMouseListener with a specified channel. diff --git a/src/example/events.cpp b/src/example/events.cpp index 5a0a748..6431c67 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -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; } }; @@ -74,10 +74,8 @@ int main() { 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); + 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); @@ -102,8 +100,7 @@ int main() { 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); -- cgit v1.2.3 From ce14236bd08469737185962d0be11d72c442b60e Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 20 Nov 2024 15:39:27 +0100 Subject: most tests done --- src/crepe/api/Event.h | 12 +- src/crepe/api/EventHandler.cpp | 3 +- src/crepe/api/EventHandler.h | 6 +- src/crepe/api/EventHandler.hpp | 8 +- src/crepe/api/EventManager.cpp | 11 +- src/crepe/api/EventManager.h | 32 ++- src/crepe/api/EventManager.hpp | 24 +- src/crepe/api/IKeyListener.cpp | 35 +-- src/crepe/api/IKeyListener.h | 21 +- src/crepe/api/IMouseListener.cpp | 34 +-- src/crepe/api/IMouseListener.h | 22 +- src/crepe/facade/SDLContext.cpp | 1 - src/example/events.cpp | 74 +++--- src/test/CMakeLists.txt | 1 + src/test/EventTest.cpp | 470 +++++++++++++++++++++++++++++++++++++++ 15 files changed, 584 insertions(+), 170 deletions(-) create mode 100644 src/test/EventTest.cpp (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index d5ddf0a..33f3add 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -7,17 +7,15 @@ /** * \brief Base class for all event types in the system. */ -class Event { -public: -}; +class Event {}; /** * \brief Event triggered when a key is pressed. */ class KeyPressEvent : public Event { public: - //! Number of times the key press is repeated (e.g., for long presses). - int repeat = 0; + //! false if first time press, true if key is repeated + bool repeat = false; //! The key that was pressed. Keycode key = Keycode::NONE; @@ -110,6 +108,4 @@ public: /** * \brief Event triggered to indicate the application is shutting down. */ -class ShutDownEvent : public Event { -public: -}; +class ShutDownEvent : public Event {}; diff --git a/src/crepe/api/EventHandler.cpp b/src/crepe/api/EventHandler.cpp index 186ec9c..4dc232f 100644 --- a/src/crepe/api/EventHandler.cpp +++ b/src/crepe/api/EventHandler.cpp @@ -2,5 +2,4 @@ using namespace crepe; -// Implementation of IEventHandlerWrapper::exec -bool IEventHandlerWrapper::exec(const Event & e) { return call(e); } +bool IEventHandlerWrapper::exec(const Event & e) { return this->call(e); } diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index db51d04..90886aa 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -13,6 +13,8 @@ namespace crepe { * indicating whether the event is handled. * * \tparam EventType The type of event this handler will handle. + * + * Returning \c false from an event handler results in the event being propogated to other listeners for the same event type, while returning \c true stops propogation altogether. */ template using EventHandler = std::function; @@ -105,9 +107,9 @@ private: std::string get_type() const override; //! The event handler function. - EventHandler m_handler; + EventHandler handler; //! The type name of the handler function. - const std::string m_handler_type; + const std::string handler_type; }; } // namespace crepe diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index 9c47da2..a1e774d 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -8,19 +8,19 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) - : m_handler(handler), - m_handler_type(m_handler.target_type().name()) {} + : handler(handler), + handler_type(handler.target_type().name()) {} // Implementation of EventHandlerWrapper::call template bool EventHandlerWrapper::call(const Event & e) { - return m_handler(static_cast(e)); + return this->handler(static_cast(e)); } // Implementation of EventHandlerWrapper::get_type template std::string EventHandlerWrapper::get_type() const { - return m_handler_type; + return this->handler_type; } } //namespace crepe diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index b465e89..27a304f 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -13,9 +13,9 @@ void EventManager::dispatch_events() { using HandlersVec = std::vector>; for (auto 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); + std::unique_ptr & event = (*event_it).event; + int channel = (*event_it).channel; + std::type_index event_type = (*event_it).type; bool event_handled = false; @@ -58,3 +58,8 @@ void EventManager::dispatch_events() { } } } +void EventManager::clear(){ + this->subscribers.clear(); + this->events_queue.clear(); + this->subscribers_by_event_id.clear(); +} diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 92273df..8fd22f4 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -11,7 +11,7 @@ #include "EventHandler.h" namespace crepe { - +static constexpr int CHANNEL_ALL = -1; /** * \class EventManager * \brief The EventManager class is responsible for managing the subscription, triggering, @@ -19,6 +19,7 @@ namespace crepe { */ class EventManager { public: + /** * \brief Get the singleton instance of the EventManager. * @@ -38,7 +39,7 @@ public: * \param channel The channel number to subscribe to (default is 0). */ template - void subscribe(EventHandler && callback, int channel = 0); + void subscribe(const EventHandler & callback, int channel = CHANNEL_ALL, int priority = 0); /** * \brief Unsubscribe from an event. @@ -50,7 +51,7 @@ public: * \param channel The event ID to unsubscribe from. */ template - void unsubscribe(const EventHandler &, int channel = 0); + void unsubscribe(const EventHandler &, int channel = CHANNEL_ALL); /** * \brief Trigger an event. @@ -62,7 +63,7 @@ public: * \param channel The channel from which to trigger the event (default is 0). */ template - void trigger_event(const EventType & event, int channel = 0); + void trigger_event(const EventType & event, int channel = CHANNEL_ALL); /** * \brief Queue an event for later processing. @@ -75,7 +76,7 @@ public: * \param channel The channel number for the event (default is 0). */ template - void queue_event(EventType && event, int channel = 0); + void queue_event(const EventType & event, int channel = CHANNEL_ALL,int priority = 0); /** * \brief Dispatch all queued events. @@ -84,8 +85,24 @@ public: * callbacks for each event. */ void dispatch_events(); - + /** + * \brief clears all subscribers + * + */ + void clear(); private: + struct QueueEntry { + std::unique_ptr event; + int channel = 0; + std::type_index type; + int priority = 0; + }; + struct CallbackEntry { + std::unique_ptr callback; + int channel = 0; + std::type_index type; + int priority = 0; + }; /** * \brief Default constructor for the EventManager. * @@ -94,7 +111,7 @@ private: EventManager() = default; //! The queue of events to be processed. - std::vector, int, std::type_index>> events_queue; + std::vector events_queue; //! Registered event handlers. std::unordered_map>> subscribers; @@ -103,6 +120,7 @@ private: std::type_index, std::unordered_map>>> subscribers_by_event_id; + }; } // namespace crepe diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index b20b88f..9090a3f 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -3,7 +3,7 @@ namespace crepe { template -void EventManager::subscribe(EventHandler && callback, int channel) { +void EventManager::subscribe(const EventHandler & callback, int channel, int priority) { using HandlersMap = std::unordered_map>>; using HandlersVec = std::vector>; @@ -27,14 +27,20 @@ void EventManager::subscribe(EventHandler && callback, int channel) { } template -void EventManager::queue_event(EventType && event, int channel) { - std::type_index event_type = std::type_index(typeid(EventType)); +void EventManager::queue_event(const EventType & event, int channel,int priority) { + static_assert(std::is_base_of::value, "EventType must derive from Event"); + std::type_index event_type = typeid(EventType); - auto event_ptr = std::make_unique(std::forward(event)); + auto event_ptr = std::make_unique(event); - std::tuple, int, std::type_index> tuple(std::move(event_ptr), - channel, event_type); - this->events_queue.push_back(std::move(tuple)); + + this->events_queue.push_back( + QueueEntry{ + .event = std::move(event_ptr), + .channel = channel, + .type = event_type + } + ); } template @@ -43,7 +49,7 @@ void EventManager::trigger_event(const EventType & event, int channel) { = std::unordered_map>>; using HandlersVec = std::vector>; - std::type_index event_type = std::type_index(typeid(EventType)); + std::type_index event_type = typeid(EventType); if (channel > 0) { HandlersMap & handlers_map = this->subscribers_by_event_id[event_type]; @@ -75,7 +81,7 @@ void EventManager::unsubscribe(const EventHandler & callback, int cha = std::unordered_map>>; using HandlersVec = std::vector>; - std::type_index event_type(typeid(EventType)); + std::type_index event_type = typeid(EventType); std::string handler_name = callback.target_type().name(); if (channel) { diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index f5426be..5e7d9bb 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -2,13 +2,6 @@ using namespace crepe; -// Constructor with default channel -IKeyListener::IKeyListener() - : channel(0), - active(true), - event_manager(EventManager::get_instance()) { - this->subscribe_events(); -} // Constructor with specified channel IKeyListener::IKeyListener(int channel) @@ -28,9 +21,9 @@ void IKeyListener::subscribe_events() { key_released_handler = [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }; - event_manager.subscribe(std::move(this->key_pressed_handler), + event_manager.subscribe(this->key_pressed_handler, this->channel); - event_manager.subscribe(std::move(this->key_released_handler), + event_manager.subscribe(this->key_released_handler, this->channel); } @@ -40,27 +33,3 @@ void IKeyListener::unsubscribe_events() { event_manager.unsubscribe(this->key_released_handler, this->channel); } -// Activate key listening -void IKeyListener::activate_keys() { - if (this->active) { - return; - } - this->active = true; - this->subscribe_events(); -} - -// Deactivate key listening -void IKeyListener::deactivate_keys() { - if (!this->active) { - return; - } - this->active = false; - this->unsubscribe_events(); -} - -// Set a new channel for key events -void IKeyListener::set_channel(int channel) { - this->unsubscribe_events(); - this->channel = channel; - this->subscribe_events(); -} diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index d492387..70243b4 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -16,11 +16,11 @@ public: * \brief Constructs an IKeyListener with a specified channel. * \param channel The channel ID for event handling. */ - IKeyListener(int channel); - IKeyListener(); + IKeyListener(int channel = CHANNEL_ALL); virtual ~IKeyListener(); IKeyListener(const IKeyListener &) = delete; IKeyListener & operator=(const IKeyListener &) = delete; + IKeyListener & operator=(IKeyListener &&) = delete; IKeyListener(IKeyListener &&) = delete; /** @@ -36,23 +36,6 @@ public: * \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: /** * \brief Subscribes to key events. diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 5ee2814..6fd6c4b 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -8,10 +8,6 @@ IMouseListener::IMouseListener(int channel) this->subscribe_events(); } -IMouseListener::IMouseListener() : event_manager(EventManager::get_instance()) { - this->subscribe_events(); -} - IMouseListener::~IMouseListener() { this->unsubscribe_events(); } void IMouseListener::subscribe_events() { @@ -26,11 +22,11 @@ void IMouseListener::subscribe_events() { = [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }; // Subscribe event handlers (no need for std::move) - event_manager.subscribe(std::move(mouse_click_handler), this->channel); - event_manager.subscribe(std::move(mouse_press_handler), this->channel); - event_manager.subscribe(std::move(mouse_release_handler), + event_manager.subscribe(mouse_click_handler, this->channel); + event_manager.subscribe(mouse_press_handler, this->channel); + event_manager.subscribe(mouse_release_handler, this->channel); - event_manager.subscribe(std::move(mouse_move_handler), this->channel); + event_manager.subscribe(mouse_move_handler, this->channel); } void IMouseListener::unsubscribe_events() { @@ -40,25 +36,3 @@ void IMouseListener::unsubscribe_events() { event_manager.unsubscribe(mouse_release_handler, this->channel); event_manager.unsubscribe(mouse_move_handler, this->channel); } - -void IMouseListener::activate_mouse() { - if (this->active) { - return; - } - this->subscribe_events(); - this->active = true; -} - -void IMouseListener::deactivate_mouse() { - if (!this->active) { - return; - } - this->unsubscribe_events(); - this->active = false; -} - -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 921a760..1195a4e 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -12,16 +12,15 @@ namespace crepe { */ class IMouseListener { public: - IMouseListener(); /** * \brief Constructs an IMouseListener with a specified channel. * \param channel The channel ID for event handling. */ - IMouseListener(int channel); + IMouseListener(int channel = CHANNEL_ALL); virtual ~IMouseListener(); IMouseListener & operator=(const IMouseListener &) = delete; IMouseListener(const IMouseListener &) = delete; - IMouseListener && operator=(const IMouseListener &&) = delete; + IMouseListener & operator=(const IMouseListener &&) = delete; IMouseListener(IMouseListener &&) = delete; /** @@ -56,23 +55,6 @@ public: * \return True if the event was handled, false otherwise. */ virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; - - /** - * \brief Activates mouse listening. - */ - void activate_mouse(); - - /** - * \brief Deactivates mouse listening. - */ - void deactivate_mouse(); - - /** - * \brief Sets the channel ID for event handling. - * \param channel The channel ID to set. - */ - void set_channel(int channel); - protected: /** * \brief Subscribes to mouse events on the specified channel. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 83e91f8..9464c31 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -78,7 +78,6 @@ SDLContext::~SDLContext() { IMG_Quit(); SDL_Quit(); } - void SDLContext::handle_events(bool & running) { //TODO: wouter i need events /* diff --git a/src/example/events.cpp b/src/example/events.cpp index 6431c67..402a857 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -27,7 +26,6 @@ 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 { @@ -65,48 +63,60 @@ public: } }; int main() { - // two events to trigger - KeyPressEvent key_press; - 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; - // 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); + { + // two events to trigger + KeyPressEvent key_press; + 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; + // queue events to test queue + EventManager::get_instance().queue_event(key_press); + EventManager::get_instance().queue_event(click_event); 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(); + //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( - MouseClickEvent{ - .mouse_x = 100, - .mouse_y = 100, - .button = MouseButton::LEFT_MOUSE, - }, - 1); + //EventManager::get_instance().trigger_event(key_press, 1); + // EventManager::get_instance().trigger_event( + // MouseClickEvent{ + // .mouse_x = 100, + // .mouse_y = 100, + // .button = MouseButton::LEFT_MOUSE, + // }, + // 1); + //EventManager::get_instance().trigger_event(click_event, 0); } // custom lambda event handler EventHandler event_handler = [](const KeyPressEvent & e) { - std::cout << "lambda test" << std::endl; + std::cout << "key lambda test" << std::endl; + return true; + }; + EventHandler event_handler2 = [](const MouseClickEvent & e) { + std::cout << "mouse lambda test" << std::endl; return false; }; - EventManager::get_instance().subscribe(std::move(event_handler), 0); + EventManager::get_instance().subscribe(event_handler, CHANNEL_ALL); + EventManager::get_instance().subscribe(event_handler, CHANNEL_ALL); + EventManager::get_instance().subscribe(event_handler2, CHANNEL_ALL); + EventManager::get_instance().trigger_event(KeyPressEvent{ + .repeat = false, + .key = Keycode::A + }); + //EventManager::get_instance().unsubscribe(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().dispatch_events(); - EventManager::get_instance().unsubscribe(event_handler, 0); + EventManager::get_instance().unsubscribe(event_handler); return EXIT_SUCCESS; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 49c8151..6f6ad79 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,5 +3,6 @@ target_sources(test_main PUBLIC PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp + EventTest.cpp ) diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp new file mode 100644 index 0000000..19f6d2e --- /dev/null +++ b/src/test/EventTest.cpp @@ -0,0 +1,470 @@ + +#include "api/EventManager.h" +#include "api/Event.h" +#include "api/IKeyListener.h" +#include "api/IMouseListener.h" +#include +#include +#include +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + + +class EventManagerTest : public ::testing::Test { +protected: + void SetUp() override { + // Clear any existing subscriptions or events before each test + EventManager::get_instance().clear(); + } + + void TearDown() override { + // Ensure cleanup after each test + EventManager::get_instance().clear(); + } +}; +class MockKeyListener : public IKeyListener { +public: + MOCK_METHOD(bool, on_key_pressed, (const KeyPressEvent& event), (override)); + MOCK_METHOD(bool, on_key_released, (const KeyReleaseEvent& event), (override)); +}; + +class MockMouseListener : public IMouseListener { +public: + MOCK_METHOD(bool, on_mouse_clicked, (const MouseClickEvent& event), (override)); + MOCK_METHOD(bool, on_mouse_pressed, (const MousePressEvent& event), (override)); + MOCK_METHOD(bool, on_mouse_released, (const MouseReleaseEvent& event), (override)); + MOCK_METHOD(bool, on_mouse_moved, (const MouseMoveEvent& event), (override)); +}; +TEST_F(EventManagerTest, EventSubscription) { + EventHandler key_handler = [](const KeyPressEvent& e) { + std::cout << "Key Event Triggered" << std::endl; + return true; + }; + + // Subscribe to KeyPressEvent + EventManager::get_instance().subscribe(key_handler, 1); + + // Verify subscription (not directly verifiable; test by triggering event) + + EventManager::get_instance().trigger_event(KeyPressEvent{ + .repeat = true, + .key = Keycode::A, + }, 1); + EventManager::get_instance().trigger_event(KeyPressEvent{ + .repeat = true, + .key = Keycode::A, + + }, CHANNEL_ALL); + +} +TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { + bool triggered = false; + + EventHandler mouse_handler = [&](const MouseClickEvent& e) { + triggered = true; + std::cout << "mouse handled" <(mouse_handler, CHANNEL_ALL); + + MouseClickEvent click_event{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }; + EventManager::get_instance().trigger_event(click_event, CHANNEL_ALL); + + EXPECT_TRUE(triggered); +} + +TEST_F(EventManagerTest, EventManagerTest_priority_order) { + EventManager& event_manager = EventManager::get_instance(); + + // Vector to track call order + std::vector call_order; + + // Handlers with different priorities + EventHandler handler_priority_3 = [&](const MouseClickEvent& e) { + call_order.push_back(3); + return false; // Allow propagation + }; + + EventHandler handler_priority_1 = [&](const MouseClickEvent& e) { + call_order.push_back(1); + return false; // Allow propagation + }; + + EventHandler handler_priority_2 = [&](const MouseClickEvent& e) { + call_order.push_back(2); + return false; // Allow propagation + }; + + // Subscribe handlers with different priorities + event_manager.subscribe(handler_priority_1, CHANNEL_ALL, 1); + event_manager.subscribe(handler_priority_2, CHANNEL_ALL, 2); + event_manager.subscribe(handler_priority_3, CHANNEL_ALL, 3); + + // Trigger the event + event_manager.trigger_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }, CHANNEL_ALL); + + // Check the call order matches the expected priority order + std::vector expected_order = {3, 2, 1}; + EXPECT_EQ(call_order, expected_order); + +} + +TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { + EventManager& event_manager = EventManager::get_instance(); + + // Flags to track handler calls + bool triggered_true = false; + bool triggered_false = false; + + // Handlers + EventHandler mouse_handler_true = [&](const MouseClickEvent& e) { + triggered_true = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return true; // Stops propagation + }; + + EventHandler mouse_handler_false = [&](const MouseClickEvent& e) { + triggered_false = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + + // Test event + MouseClickEvent click_event{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }; + + // First Scenario: True handler has higher priority + event_manager.subscribe(mouse_handler_true, CHANNEL_ALL, 1); + event_manager.subscribe(mouse_handler_false, CHANNEL_ALL, 0); + + // Trigger event + event_manager.trigger_event(click_event, CHANNEL_ALL); + + // Check that only the true handler was triggered + EXPECT_TRUE(triggered_true); + EXPECT_FALSE(triggered_false); + + // Reset and clear + triggered_true = false; + triggered_false = false; + event_manager.clear(); + + // Second Scenario: False handler has higher priority + event_manager.subscribe(mouse_handler_true, CHANNEL_ALL, 0); + event_manager.subscribe(mouse_handler_false, CHANNEL_ALL, 1); + + // Trigger event again + event_manager.trigger_event(click_event, CHANNEL_ALL); + + // Check that both handlers were triggered + EXPECT_TRUE(triggered_true); + EXPECT_TRUE(triggered_false); +} + +TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { + EventManager& event_manager = EventManager::get_instance(); + bool triggered1 = false; + bool triggered2 = false; + int test_channel = 1; + EventHandler mouse_handler1 = [&](const MouseClickEvent& e) { + triggered1 = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + EventHandler mouse_handler2 = [&](const MouseClickEvent& e) { + triggered2 = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + event_manager.subscribe(mouse_handler1); + event_manager.subscribe(mouse_handler2,test_channel); + + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }); + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + },test_channel); + event_manager.dispatch_events(); + EXPECT_TRUE(triggered1); + EXPECT_TRUE(triggered2); +} + +TEST_F(EventManagerTest, EventManagerTest_dispatch_priority) { + EventManager& event_manager = EventManager::get_instance(); + std::vector call_order; + int test_channel = 1; + EventHandler mouse_handler1 = [&](const MouseClickEvent& e) { + call_order.push_back(1); + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + EventHandler mouse_handler2 = [&](const MouseClickEvent& e) { + call_order.push_back(2); + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + EventHandler mouse_handler3 = [&](const MouseClickEvent& e) { + call_order.push_back(3); + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + event_manager.subscribe(mouse_handler1,CHANNEL_ALL,1); + event_manager.subscribe(mouse_handler2,CHANNEL_ALL,2); + event_manager.subscribe(mouse_handler2,CHANNEL_ALL,3); + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }); + event_manager.dispatch_events(); + std::vector expected_order = {3, 2, 1}; + EXPECT_EQ(call_order, expected_order); +} + +TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { + EventManager& event_manager = EventManager::get_instance(); + + // Flags to track if handlers are triggered + bool triggered1 = false; + bool triggered2 = false; + + // Define EventHandlers + EventHandler mouse_handler1 = [&](const MouseClickEvent& e) { + triggered1 = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + + EventHandler mouse_handler2 = [&](const MouseClickEvent& e) { + triggered2 = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; // Allows propagation + }; + + // Subscribe handlers + event_manager.subscribe(mouse_handler1); + event_manager.subscribe(mouse_handler2); + + // Queue events + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }); + + // Dispatch events - both handlers should be triggered + event_manager.dispatch_events(); + EXPECT_TRUE(triggered1); // Handler 1 should be triggered + EXPECT_TRUE(triggered2); // Handler 2 should be triggered + + // Reset flags + triggered1 = false; + triggered2 = false; + + // Unsubscribe handler1 + event_manager.unsubscribe(mouse_handler1); + + // Queue the same event again + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }); + + // Dispatch events - only handler 2 should be triggered, handler 1 should NOT + event_manager.dispatch_events(); + EXPECT_FALSE(triggered1); // Handler 1 should NOT be triggered + EXPECT_TRUE(triggered2); // Handler 2 should be triggered + + // Reset flags + triggered2 = false; + + // Unsubscribe handler2 + event_manager.unsubscribe(mouse_handler2); + + // Queue the event again + event_manager.queue_event(MouseClickEvent{ + .mouse_x = 100, + .mouse_y = 200, + .button = MouseButton::LEFT_MOUSE + }); + + // Dispatch events - no handler should be triggered + event_manager.dispatch_events(); + EXPECT_FALSE(triggered1); // Handler 1 should NOT be triggered + EXPECT_FALSE(triggered2); // Handler 2 should NOT be triggered +} + +// TEST_F(EventManagerTest, OnKeyPressedTest) { +// MockKeyListener mock_key_listener; +// EventManager& event_manager = EventManager::get_instance(); + +// // Create the KeyPressEvent object +// KeyPressEvent key_event = KeyPressEvent{ +// .key = Keycode::A, +// .repeat = false +// }; + +// // Set up the mock expectation with direct object passing +// EXPECT_CALL(mock_key_listener, on_key_pressed(key_event)) +// .Times(1) // Expect it to be called exactly once +// .WillOnce(::testing::Return(true)); // Return value (can be true/false) + + +// // Queue the event +// event_manager.queue_event(key_event); + +// // Dispatch the event to trigger the mock +// event_manager.dispatch_events(); // Trigger event dispatch + +// // The mock will ensure the on_key_pressed method is called +// } + + + +// TEST_F(EventManagerTest, OnKeyReleaseTest) { +// MockKeyListener mock_key_listener; +// EventManager& event_manager = EventManager::get_instance(); + +// // Create the KeyPressEvent object +// KeyReleaseEvent key_event = KeyReleaseEvent{ +// .key = Keycode::A, +// }; + +// // Set up the mock expectation with direct object passing +// EXPECT_CALL(mock_key_listener, on_key_released(key_event)) +// .Times(1) // Expect it to be called exactly once +// .WillOnce(::testing::Return(true)); // Return value (can be true/false) + + +// // Queue the event +// event_manager.queue_event(key_event); + +// // Dispatch the event to trigger the mock +// event_manager.dispatch_events(); // Trigger event dispatch + +// // The mock will ensure the on_key_pressed method is called +// } + +// TEST_F(EventManagerTest, UnsubscribeEventsTest) { +// EventManager& event_manager = EventManager::get_instance(); +// KeyPressEvent key_event{ +// .key = Keycode::A, +// .repeat = false +// }; + +// // Create the mock listener +// MockKeyListener mock_key_listener; + +// // Set up the mock expectation that on_key_pressed will be called once initially +// EXPECT_CALL(mock_key_listener, on_key_pressed(key_event)) +// .Times(1) // Expect it to be called exactly once +// .WillOnce(::testing::Return(true)); // Return value (can be true/false) + +// event_manager.queue_event(key_event); +// event_manager.dispatch_events(); // Should trigger on_key_pressed once + +// // Now unsubscribe the listener +// event_manager.unsubscribe(std::bind(&MockKeyListener::on_key_pressed, &mock_key_listener, std::placeholders::_1)); + +// // Set up the expectation that on_key_pressed will NOT be called after unsubscribing +// EXPECT_CALL(mock_key_listener, on_key_pressed(key_event)) +// .Times(0); // Should not be called after unsubscribe + +// // Queue and dispatch the event again (after unsubscribe) +// event_manager.queue_event(key_event); +// event_manager.dispatch_events(); // Should NOT trigger on_key_pressed after unsubscribe +// } + +// TEST_F(EventManagerTest, OnMouseButtonPressedTest) { +// MockMouseListener mock_mouse_listener; +// EventManager& event_manager = EventManager::get_instance(); + +// // Create the MouseButtonPressEvent object +// MousePressEvent mouse_event{ +// .mouse_x = 100, +// .mouse_y = 150, +// .button = MouseButton::LEFT_MOUSE +// }; + +// // Set up the mock expectation with direct object passing +// EXPECT_CALL(mock_mouse_listener, on_mouse_pressed(mouse_event)) +// .Times(1) // Expect it to be called exactly once +// .WillOnce(::testing::Return(true)); // Return value (can be true/false) + +// // Queue the event +// event_manager.queue_event(mouse_event); + +// // Dispatch the event to trigger the mock +// event_manager.dispatch_events(); // Should trigger on_mouse_button_pressed once +// } + +// TEST_F(EventManagerTest, UnsubscribeMouseEventsTest) { +// EventManager& event_manager = EventManager::get_instance(); +// MousePressEvent mouse_event{ +// .mouse_x = 100, +// .mouse_y = 150, +// .button = MouseButton::LEFT_MOUSE +// }; + +// // Create the mock listener +// MockMouseListener mock_mouse_listener; + +// // Set up the mock expectation that on_mouse_button_pressed will be called once initially +// EXPECT_CALL(mock_mouse_listener, on_mouse_pressed(mouse_event)) +// .Times(1) // Expect it to be called exactly once +// .WillOnce(::testing::Return(true)); // Return value (can be true/false) + +// // Queue and dispatch the event (should trigger on_mouse_button_pressed) +// event_manager.queue_event(mouse_event); +// event_manager.dispatch_events(); // Should trigger on_mouse_button_pressed once + +// // Now unsubscribe the listener +// event_manager.unsubscribe(std::bind(&MockMouseListener::on_mouse_pressed, &mock_mouse_listener, std::placeholders::_1)); + +// // Set up the expectation that on_mouse_button_pressed will NOT be called after unsubscribing +// EXPECT_CALL(mock_mouse_listener, on_mouse_pressed(mouse_event)) +// .Times(0); // Should not be called after unsubscribe + +// // Queue and dispatch the event again (after unsubscribe) +// event_manager.queue_event(mouse_event); +// event_manager.dispatch_events(); // Should NOT trigger on_mouse_button_pressed after unsubscribe +// } -- cgit v1.2.3 From 397da65e03ec681922aeea3881918026d36068a7 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 20 Nov 2024 20:24:17 +0100 Subject: result of loeks temper tantrum --- src/crepe/api/EventHandler.h | 23 +----- src/crepe/api/EventHandler.hpp | 9 +-- src/crepe/api/EventManager.cpp | 42 +++++++---- src/crepe/api/EventManager.h | 158 ++++++++++++++++++++++----------------- src/crepe/api/EventManager.hpp | 38 ++-------- src/crepe/api/IKeyListener.cpp | 26 ++----- src/crepe/api/IKeyListener.h | 26 ++----- src/crepe/api/IMouseListener.cpp | 47 +++++------- src/crepe/api/IMouseListener.h | 34 +++------ src/example/events.cpp | 120 ----------------------------- src/test/EventTest.cpp | 118 ++++++++--------------------- 11 files changed, 198 insertions(+), 443 deletions(-) delete mode 100644 src/example/events.cpp (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 90886aa..ef659fd 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -24,7 +24,7 @@ using EventHandler = std::function; * \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. + * `call()` method to process events */ class IEventHandlerWrapper { public: @@ -43,15 +43,6 @@ public: */ 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. @@ -96,20 +87,8 @@ private: * \return A boolean value indicating whether the event is handled. */ bool call(const Event & e) override; - - /** - * \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; - //! The event handler function. EventHandler handler; - //! The type name of the handler function. - const std::string handler_type; }; } // namespace crepe diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index a1e774d..8d8136b 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -1,4 +1,3 @@ - #include #include "EventHandler.h" @@ -8,8 +7,7 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) - : handler(handler), - handler_type(handler.target_type().name()) {} + : handler(handler){} // Implementation of EventHandlerWrapper::call template @@ -17,10 +15,5 @@ bool EventHandlerWrapper::call(const Event & e) { return this->handler(static_cast(e)); } -// Implementation of EventHandlerWrapper::get_type -template -std::string EventHandlerWrapper::get_type() const { - return this->handler_type; -} } //namespace crepe diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index dbdb0c3..64d7c26 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -15,22 +15,18 @@ void EventManager::dispatch_events() { bool event_handled = false; auto handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector & handlers = handlers_it->second; - - std::sort(handlers.begin(), handlers.end(), - [](const CallbackEntry & a, const CallbackEntry & b) { - return a.priority > b.priority; - }); - - for (auto handler_it = handlers.begin(); handler_it != handlers.end(); - ++handler_it) { - // If callback is executed and returns true, remove the event from the queue - if ((*handler_it).callback->exec(*event)) { - event_it = this->events_queue.erase(event_it); - event_handled = true; - break; - } + if (handlers_it == this->subscribers.end()) { + continue; + } + std::vector & handlers = handlers_it->second; + + for (auto handler_it = handlers.begin(); handler_it != handlers.end(); + ++handler_it) { + // If callback is executed and returns true, remove the event from the queue + if ((*handler_it).callback->exec(*event)) { + event_it = this->events_queue.erase(event_it); + event_handled = true; + break; } } @@ -44,3 +40,17 @@ void EventManager::clear() { this->subscribers.clear(); this->events_queue.clear(); } + +void EventManager::unsubscribe(subscription_t event_id) { + for (auto& [event_type, handlers] : this->subscribers) { + for (auto it = handlers.begin(); it != handlers.end();) { + if (it->id == event_id) { + it = handlers.erase(it); + return; + } else { + ++it; + } + } + } +} + diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 133d72d..93e9ca2 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -11,109 +11,131 @@ #include "EventHandler.h" namespace crepe { -static constexpr int CHANNEL_ALL = -1; +//! typedef for subscription value +typedef int subscription_t; + /** * \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. + * \brief Manages event subscriptions, triggers, and queues, enabling decoupled event handling. + * + * The `EventManager` acts as a centralized event system. It allows for registering callbacks + * for specific event types, triggering events synchronously, queueing events for later + * processing, and managing subscriptions via unique identifiers. */ class EventManager { public: + static constexpr int CHANNEL_ALL = -1; + /** - * \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. - */ + * \brief Get the singleton instance of the EventManager. + * + * This method returns the unique instance of the EventManager, creating it if it + * doesn't already exist. Ensures only one instance is active in the program. + * + * \return Reference to the singleton instance of the EventManager. + */ static EventManager & get_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). - */ + * \brief Subscribe to a specific event type. + * + * Registers a callback for a given event type and optional channel. Each callback + * is assigned a unique subscription ID that can be used for later unsubscription. + * + * \tparam EventType The type of the event to subscribe to. + * \param callback The callback function to be invoked when the event is triggered. + * \param channel The channel number to subscribe to (default is CHANNEL_ALL, which listens to all channels). + * \return A unique subscription ID associated with the registered callback. + */ template - void subscribe(const EventHandler & callback, int channel = CHANNEL_ALL, - int priority = 0); + subscription_t subscribe(const EventHandler & callback, int channel = CHANNEL_ALL); /** - * \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 channel The event ID to unsubscribe from. - */ - template - void unsubscribe(const EventHandler &, int channel = CHANNEL_ALL); + * \brief Unsubscribe a previously registered callback. + * + * Removes a callback from the subscription list based on its unique subscription ID. + * + * \param event_id The unique subscription ID of the callback to remove. + */ + void unsubscribe(subscription_t event_id); /** - * \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). - */ + * \brief Trigger an event immediately. + * + * Synchronously invokes all registered callbacks for the given event type on the specified channel. + * + * \tparam EventType The type of the event to trigger. + * \param event The event instance to pass to the callbacks. + * \param channel The channel to trigger the event on (default is CHANNEL_ALL, which triggers on all channels). + */ template void trigger_event(const EventType & event, int channel = CHANNEL_ALL); /** - * \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). - */ + * \brief Queue an event for later processing. + * + * Adds an event to the event queue to be processed during the next call to `dispatch_events`. + * + * \tparam EventType The type of the event to queue. + * \param event The event instance to queue. + * \param channel The channel to associate with the event (default is CHANNEL_ALL). + */ template - void queue_event(const EventType & event, int channel = CHANNEL_ALL, int priority = 0); + void queue_event(const EventType & event, int channel = CHANNEL_ALL); /** - * \brief Dispatch all queued events. - * - * This method processes all events in the event queue and triggers the corresponding - * callbacks for each event. - */ + * \brief Process all queued events. + * + * Iterates through the event queue and triggers callbacks for each queued event. + * Events are removed from the queue once processed. + */ void dispatch_events(); + /** - * \brief clears all subscribers - * - */ + * \brief Clear all subscriptions. + * + * Removes all registered event handlers and clears the subscription list. + */ void clear(); private: /** - * \brief Default constructor for the EventManager. - * - * This constructor is private to enforce the singleton pattern. - */ + * \brief Default constructor for the EventManager. + * + * Constructor is private to enforce the singleton pattern. + */ EventManager() = default; + + /** + * \struct QueueEntry + * \brief Represents an entry in the event queue. + */ struct QueueEntry { - std::unique_ptr event; - int channel = CHANNEL_ALL; - std::type_index type; - int priority = 0; + std::unique_ptr event; ///< The event instance. + int channel = CHANNEL_ALL; ///< The channel associated with the event. + std::type_index type; ///< The type of the event. }; + + /** + * \struct CallbackEntry + * \brief Represents a registered event handler callback. + */ struct CallbackEntry { - std::unique_ptr callback; - int channel = CHANNEL_ALL; - int priority = 0; + std::unique_ptr callback; ///< The callback function wrapper. + int channel = CHANNEL_ALL; ///< The channel this callback listens to. + subscription_t id = -1; ///< Unique subscription ID. }; - //! The queue of events to be processed. + + //! The queue of events to be processed during dispatch. std::vector events_queue; - //! Registered event handlers. + + //! A map of event type to registered callbacks. std::unordered_map> subscribers; + + //! Counter to generate unique subscription IDs. + subscription_t subscription_counter = 0; }; } // namespace crepe + #include "EventManager.hpp" diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index a04a43a..d7afa9f 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -3,9 +3,8 @@ namespace crepe { template -void EventManager::subscribe(const EventHandler & callback, int channel, - int priority) { - +subscription_t EventManager::subscribe(const EventHandler & callback, int channel) { + subscription_counter++; std::type_index event_type = typeid(EventType); std::unique_ptr> handler = std::make_unique>(callback); @@ -13,17 +12,13 @@ void EventManager::subscribe(const EventHandler & callback, int chann handlers.emplace_back(CallbackEntry{ .callback = std::move(handler), .channel = channel, - .priority = priority, + .id = subscription_counter }); - // Sort handlers by priority (highest first) - std::sort(handlers.begin(), handlers.end(), - [](const CallbackEntry & a, const CallbackEntry & b) { - return a.priority > b.priority; - }); + return subscription_counter; } template -void EventManager::queue_event(const EventType & event, int channel, int priority) { +void EventManager::queue_event(const EventType & event, int channel) { static_assert(std::is_base_of::value, "EventType must derive from Event"); std::type_index event_type = typeid(EventType); @@ -32,8 +27,7 @@ void EventManager::queue_event(const EventType & event, int channel, int priorit this->events_queue.push_back(QueueEntry{.event = std::move(event_ptr), .channel = channel, - .type = event_type, - .priority = priority}); + .type = event_type}); } template @@ -55,25 +49,5 @@ void EventManager::trigger_event(const EventType & event, int channel) { } } -template -void EventManager::unsubscribe(const EventHandler & callback, int channel) { - std::type_index event_type = typeid(EventType); - std::string handler_name = callback.target_type().name(); - - // Find the list of handlers for this event type - auto handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector & handlers = handlers_it->second; - - for (auto it = handlers.begin(); it != handlers.end();) { - // Match based on handler type and channel - if (it->callback->get_type() == handler_name && it->channel == channel) { - it = handlers.erase(it); - return; - } - ++it; - } - } -} } // namespace crepe diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index 6a522c1..ebbf486 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -3,29 +3,15 @@ using namespace crepe; // Constructor with specified channel -IKeyListener::IKeyListener(int channel) - : channel(channel), - active(true), +IKeyListener::IKeyListener(int channel) : event_manager(EventManager::get_instance()) { - this->subscribe_events(); + press_id = event_manager.subscribe([this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel); + release_id = event_manager.subscribe([this](const KeyReleaseEvent & event) { return this->on_key_released(event); }, channel); } // Destructor, unsubscribe events -IKeyListener::~IKeyListener() { this->unsubscribe_events(); } - -// Subscribe to key 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); }; - - event_manager.subscribe(this->key_pressed_handler, this->channel); - event_manager.subscribe(this->key_released_handler, this->channel); +IKeyListener::~IKeyListener() { + event_manager.unsubscribe(press_id); + event_manager.unsubscribe(release_id); } -// Unsubscribe from key events -void IKeyListener::unsubscribe_events() { - event_manager.unsubscribe(this->key_pressed_handler, this->channel); - event_manager.unsubscribe(this->key_released_handler, this->channel); -} diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 9402cce..4726aa7 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -16,7 +16,7 @@ public: * \brief Constructs an IKeyListener with a specified channel. * \param channel The channel ID for event handling. */ - IKeyListener(int channel = CHANNEL_ALL); + IKeyListener(int channel = EventManager::CHANNEL_ALL); virtual ~IKeyListener(); IKeyListener(const IKeyListener &) = delete; IKeyListener & operator=(const IKeyListener &) = delete; @@ -36,27 +36,11 @@ public: * \return True if the event was handled, false otherwise. */ virtual bool on_key_released(const KeyReleaseEvent & event) = 0; - -protected: - /** - * \brief Subscribes to key events. - */ - void subscribe_events(); - - /** - * \brief Unsubscribes from key events. - */ - void unsubscribe_events(); - private: - //! 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; + //! Key press event id + subscription_t press_id = -1; + //!< Key release event id + subscription_t release_id = -1; EventManager & event_manager; }; diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index f3ceb84..a6cb163 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -3,35 +3,28 @@ using namespace crepe; IMouseListener::IMouseListener(int channel) - : event_manager(EventManager::get_instance()), - channel(channel) { - this->subscribe_events(); -} + : event_manager(EventManager::get_instance()) { + click_id = event_manager.subscribe( + [this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); }, + channel); -IMouseListener::~IMouseListener() { this->unsubscribe_events(); } + press_id = event_manager.subscribe( + [this](const MousePressEvent & event) { return this->on_mouse_pressed(event); }, + channel); -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); }; + release_id = event_manager.subscribe( + [this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); }, + channel); - // Subscribe event handlers (no need for std::move) - event_manager.subscribe(mouse_click_handler, this->channel); - event_manager.subscribe(mouse_press_handler, this->channel); - event_manager.subscribe(mouse_release_handler, this->channel); - event_manager.subscribe(mouse_move_handler, this->channel); + move_id = event_manager.subscribe( + [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, + channel); } -void IMouseListener::unsubscribe_events() { - // Unsubscribe event handlers - event_manager.unsubscribe(mouse_click_handler, this->channel); - event_manager.unsubscribe(mouse_press_handler, this->channel); - event_manager.unsubscribe(mouse_release_handler, this->channel); - event_manager.unsubscribe(mouse_move_handler, this->channel); -} +IMouseListener::~IMouseListener() { + // Unsubscribe event handlers + event_manager.unsubscribe(click_id); + event_manager.unsubscribe(press_id); + event_manager.unsubscribe(release_id); + event_manager.unsubscribe(move_id); + } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 6bc5716..91b33e1 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -16,7 +16,7 @@ public: * \brief Constructs an IMouseListener with a specified channel. * \param channel The channel ID for event handling. */ - IMouseListener(int channel = CHANNEL_ALL); + IMouseListener(int channel = EventManager::CHANNEL_ALL); virtual ~IMouseListener(); IMouseListener & operator=(const IMouseListener &) = delete; IMouseListener(const IMouseListener &) = delete; @@ -56,30 +56,16 @@ public: */ virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; -protected: - /** - * \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: - //! 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; + //! Mouse click event id + subscription_t click_id = -1; + //! Mouse press event id + subscription_t press_id = -1; + //! Mouse release event id + subscription_t release_id = -1; + //! Mouse move event id + subscription_t move_id = -1; + //! EventManager reference EventManager & event_manager; }; diff --git a/src/example/events.cpp b/src/example/events.cpp deleted file mode 100644 index 3dee9fa..0000000 --- a/src/example/events.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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) override { - std::cout << "MouseClick function" << std::endl; - return false; - } - bool on_mouse_pressed(const MousePressEvent & event) override { - std::cout << "MousePress function" << std::endl; - return false; - } - bool on_mouse_released(const MouseReleaseEvent & event) override { - std::cout << "MouseRelease function" << std::endl; - return false; - } - 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: " << 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 = 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; - // queue events to test queue - EventManager::get_instance().queue_event(key_press); - EventManager::get_instance().queue_event(click_event); - TestKeyListener test_listener; - //auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); - //obj.add_component().set_script(); - - //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( - // MouseClickEvent{ - // .mouse_x = 100, - // .mouse_y = 100, - // .button = MouseButton::LEFT_MOUSE, - // }, - // 1); - //EventManager::get_instance().trigger_event(click_event, 0); - } - // custom lambda event handler - EventHandler event_handler = [](const KeyPressEvent & e) { - std::cout << "key lambda test" << std::endl; - return true; - }; - EventHandler event_handler2 = [](const MouseClickEvent & e) { - std::cout << "mouse lambda test" << std::endl; - return false; - }; - EventManager::get_instance().subscribe(event_handler, CHANNEL_ALL); - EventManager::get_instance().subscribe(event_handler, CHANNEL_ALL); - EventManager::get_instance().subscribe(event_handler2, CHANNEL_ALL); - EventManager::get_instance().trigger_event( - KeyPressEvent{.repeat = false, .key = Keycode::A}); - //EventManager::get_instance().unsubscribe(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); - return EXIT_SUCCESS; -} diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 9dc2360..d42e742 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -34,6 +34,7 @@ public: MOCK_METHOD(bool, on_mouse_released, (const MouseReleaseEvent & event), (override)); MOCK_METHOD(bool, on_mouse_moved, (const MouseMoveEvent & event), (override)); }; + TEST_F(EventManagerTest, EventSubscription) { EventHandler key_handler = [](const KeyPressEvent & e) { std::cout << "Key Event Triggered" << std::endl; @@ -57,65 +58,47 @@ TEST_F(EventManagerTest, EventSubscription) { .key = Keycode::A, }, - CHANNEL_ALL); + EventManager::CHANNEL_ALL); } TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { bool triggered = false; EventHandler mouse_handler = [&](const MouseClickEvent & e) { triggered = true; - std::cout << "mouse handled" << std::endl; EXPECT_EQ(e.mouse_x, 100); EXPECT_EQ(e.mouse_y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; - EventManager::get_instance().subscribe(mouse_handler, CHANNEL_ALL); + EventManager::get_instance().subscribe(mouse_handler, EventManager::CHANNEL_ALL); MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - EventManager::get_instance().trigger_event(click_event, CHANNEL_ALL); + EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); } - -TEST_F(EventManagerTest, EventManagerTest_priority_order) { - EventManager & event_manager = EventManager::get_instance(); - - // Vector to track call order - std::vector call_order; - - // Handlers with different priorities - EventHandler handler_priority_3 = [&](const MouseClickEvent & e) { - call_order.push_back(3); - return false; // Allow propagation - }; - - EventHandler handler_priority_1 = [&](const MouseClickEvent & e) { - call_order.push_back(1); - return false; // Allow propagation - }; - - EventHandler handler_priority_2 = [&](const MouseClickEvent & e) { - call_order.push_back(2); - return false; // Allow propagation +TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { + bool triggered = false; + int test_channel = 1; + EventHandler mouse_handler = [&](const MouseClickEvent & e) { + triggered = true; + EXPECT_EQ(e.mouse_x, 100); + EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; }; + EventManager::get_instance().subscribe(mouse_handler, test_channel); - // Subscribe handlers with different priorities - event_manager.subscribe(handler_priority_1, CHANNEL_ALL, 1); - event_manager.subscribe(handler_priority_3, CHANNEL_ALL, 3); - event_manager.subscribe(handler_priority_2, CHANNEL_ALL, 2); - - // Trigger the event - event_manager.trigger_event( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}, - CHANNEL_ALL); + MouseClickEvent click_event{ + .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); - // Check the call order matches the expected priority order - std::vector expected_order = {3, 2, 1}; - EXPECT_EQ(call_order, expected_order); + EXPECT_FALSE(triggered); + EventManager::get_instance().trigger_event(click_event, test_channel); } + TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { EventManager & event_manager = EventManager::get_instance(); @@ -143,13 +126,13 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Test event MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - - // First Scenario: True handler has higher priority - event_manager.subscribe(mouse_handler_true, CHANNEL_ALL, 1); - event_manager.subscribe(mouse_handler_false, CHANNEL_ALL, 0); + event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); + event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); + + // Trigger event - event_manager.trigger_event(click_event, CHANNEL_ALL); + event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); // Check that only the true handler was triggered EXPECT_TRUE(triggered_true); @@ -159,13 +142,12 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { triggered_true = false; triggered_false = false; event_manager.clear(); - - // Second Scenario: False handler has higher priority - event_manager.subscribe(mouse_handler_true, CHANNEL_ALL, 0); - event_manager.subscribe(mouse_handler_false, CHANNEL_ALL, 1); + event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); + event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); + // Trigger event again - event_manager.trigger_event(click_event, CHANNEL_ALL); + event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); // Check that both handlers were triggered EXPECT_TRUE(triggered_true); @@ -204,40 +186,6 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { EXPECT_TRUE(triggered2); } -TEST_F(EventManagerTest, EventManagerTest_dispatch_priority) { - EventManager & event_manager = EventManager::get_instance(); - std::vector call_order; - int test_channel = 1; - EventHandler mouse_handler1 = [&](const MouseClickEvent & e) { - call_order.push_back(1); - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); - EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); - return false; // Allows propagation - }; - EventHandler mouse_handler2 = [&](const MouseClickEvent & e) { - call_order.push_back(2); - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); - EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); - return false; // Allows propagation - }; - EventHandler mouse_handler3 = [&](const MouseClickEvent & e) { - call_order.push_back(3); - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); - EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); - return false; // Allows propagation - }; - event_manager.subscribe(mouse_handler2, CHANNEL_ALL, 2); - event_manager.subscribe(mouse_handler1, CHANNEL_ALL, 1); - event_manager.subscribe(mouse_handler3, CHANNEL_ALL, 3); - event_manager.queue_event( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); - event_manager.dispatch_events(); - std::vector expected_order = {3, 2, 1}; - EXPECT_EQ(call_order, expected_order); -} TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { EventManager & event_manager = EventManager::get_instance(); @@ -263,8 +211,8 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { return false; // Allows propagation }; // Subscribe handlers - event_manager.subscribe(mouse_handler1); - event_manager.subscribe(mouse_handler2); + subscription_t handler1_id = event_manager.subscribe(mouse_handler1); + subscription_t handler2_id = event_manager.subscribe(mouse_handler2); // Queue events event_manager.queue_event( @@ -280,7 +228,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { triggered2 = false; // Unsubscribe handler1 - event_manager.unsubscribe(mouse_handler1); + event_manager.unsubscribe(handler1_id); // Queue the same event again event_manager.queue_event( @@ -295,7 +243,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { triggered2 = false; // Unsubscribe handler2 - event_manager.unsubscribe(mouse_handler2); + event_manager.unsubscribe(handler2_id); // Queue the event again event_manager.queue_event( -- cgit v1.2.3 From 5ec30d8915debef409f6db6f8ee9b822fb24d46b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 20 Nov 2024 20:30:33 +0100 Subject: make format --- src/crepe/api/EventHandler.hpp | 3 +-- src/crepe/api/EventManager.cpp | 24 +++++++++++------------- src/crepe/api/EventManager.h | 11 ++++++----- src/crepe/api/EventManager.hpp | 11 +++-------- src/crepe/api/IKeyListener.cpp | 17 +++++++++-------- src/crepe/api/IKeyListener.h | 6 ++++-- src/crepe/api/IMouseListener.cpp | 26 ++++++++++++-------------- src/test/EventTest.cpp | 14 ++++++-------- 8 files changed, 52 insertions(+), 60 deletions(-) (limited to 'src/crepe/api/EventHandler.hpp') diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index 8d8136b..391dcca 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -7,7 +7,7 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) - : handler(handler){} + : handler(handler) {} // Implementation of EventHandlerWrapper::call template @@ -15,5 +15,4 @@ bool EventHandlerWrapper::call(const Event & e) { return this->handler(static_cast(e)); } - } //namespace crepe diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 64d7c26..993db86 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -20,8 +20,7 @@ void EventManager::dispatch_events() { } std::vector & handlers = handlers_it->second; - for (auto handler_it = handlers.begin(); handler_it != handlers.end(); - ++handler_it) { + for (auto handler_it = handlers.begin(); handler_it != handlers.end(); ++handler_it) { // If callback is executed and returns true, remove the event from the queue if ((*handler_it).callback->exec(*event)) { event_it = this->events_queue.erase(event_it); @@ -42,15 +41,14 @@ void EventManager::clear() { } void EventManager::unsubscribe(subscription_t event_id) { - for (auto& [event_type, handlers] : this->subscribers) { - for (auto it = handlers.begin(); it != handlers.end();) { - if (it->id == event_id) { - it = handlers.erase(it); - return; - } else { - ++it; - } - } - } + for (auto & [event_type, handlers] : this->subscribers) { + for (auto it = handlers.begin(); it != handlers.end();) { + if (it->id == event_id) { + it = handlers.erase(it); + return; + } else { + ++it; + } + } + } } - diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 93e9ca2..bd9772a 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -48,7 +48,8 @@ public: * \return A unique subscription ID associated with the registered callback. */ template - subscription_t subscribe(const EventHandler & callback, int channel = CHANNEL_ALL); + subscription_t subscribe(const EventHandler & callback, + int channel = CHANNEL_ALL); /** * \brief Unsubscribe a previously registered callback. @@ -112,8 +113,8 @@ private: */ struct QueueEntry { std::unique_ptr event; ///< The event instance. - int channel = CHANNEL_ALL; ///< The channel associated with the event. - std::type_index type; ///< The type of the event. + int channel = CHANNEL_ALL; ///< The channel associated with the event. + std::type_index type; ///< The type of the event. }; /** @@ -122,8 +123,8 @@ private: */ struct CallbackEntry { std::unique_ptr callback; ///< The callback function wrapper. - int channel = CHANNEL_ALL; ///< The channel this callback listens to. - subscription_t id = -1; ///< Unique subscription ID. + int channel = CHANNEL_ALL; ///< The channel this callback listens to. + subscription_t id = -1; ///< Unique subscription ID. }; //! The queue of events to be processed during dispatch. diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index d7afa9f..b2a94bd 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -10,10 +10,7 @@ subscription_t EventManager::subscribe(const EventHandler & callback, = std::make_unique>(callback); std::vector & handlers = this->subscribers[event_type]; handlers.emplace_back(CallbackEntry{ - .callback = std::move(handler), - .channel = channel, - .id = subscription_counter - }); + .callback = std::move(handler), .channel = channel, .id = subscription_counter}); return subscription_counter; } @@ -25,9 +22,8 @@ void EventManager::queue_event(const EventType & event, int channel) { auto event_ptr = std::make_unique(event); - this->events_queue.push_back(QueueEntry{.event = std::move(event_ptr), - .channel = channel, - .type = event_type}); + this->events_queue.push_back( + QueueEntry{.event = std::move(event_ptr), .channel = channel, .type = event_type}); } template @@ -49,5 +45,4 @@ void EventManager::trigger_event(const EventType & event, int channel) { } } - } // namespace crepe diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index ebbf486..7aefaf7 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -3,15 +3,16 @@ using namespace crepe; // Constructor with specified channel -IKeyListener::IKeyListener(int channel) : - event_manager(EventManager::get_instance()) { - press_id = event_manager.subscribe([this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel); - release_id = event_manager.subscribe([this](const KeyReleaseEvent & event) { return this->on_key_released(event); }, channel); +IKeyListener::IKeyListener(int channel) : event_manager(EventManager::get_instance()) { + this->press_id = event_manager.subscribe( + [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }, channel); + this->release_id = event_manager.subscribe( + [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }, + channel); } // Destructor, unsubscribe events -IKeyListener::~IKeyListener() { - event_manager.unsubscribe(press_id); - event_manager.unsubscribe(release_id); +IKeyListener::~IKeyListener() { + event_manager.unsubscribe(this->press_id); + event_manager.unsubscribe(this->release_id); } - diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 4726aa7..2a89cbc 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -36,12 +36,14 @@ public: * \return True if the event was handled, false otherwise. */ virtual bool on_key_released(const KeyReleaseEvent & event) = 0; + private: //! Key press event id subscription_t press_id = -1; - //!< Key release event id + //! Key release event id subscription_t release_id = -1; - EventManager & event_manager; + //! EventManager reference + EventManager & event_manager;; }; } // namespace crepe diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index a6cb163..7d38280 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -2,29 +2,27 @@ using namespace crepe; -IMouseListener::IMouseListener(int channel) - : event_manager(EventManager::get_instance()) { - click_id = event_manager.subscribe( +IMouseListener::IMouseListener(int channel) : event_manager(EventManager::get_instance()) { + this->click_id = event_manager.subscribe( [this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); }, channel); - press_id = event_manager.subscribe( + this->press_id = event_manager.subscribe( [this](const MousePressEvent & event) { return this->on_mouse_pressed(event); }, channel); - release_id = event_manager.subscribe( + this->release_id = event_manager.subscribe( [this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); }, channel); - move_id = event_manager.subscribe( - [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, - channel); + this->move_id = event_manager.subscribe( + [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }, channel); } IMouseListener::~IMouseListener() { - // Unsubscribe event handlers - event_manager.unsubscribe(click_id); - event_manager.unsubscribe(press_id); - event_manager.unsubscribe(release_id); - event_manager.unsubscribe(move_id); - } + // Unsubscribe event handlers + event_manager.unsubscribe(this->click_id); + event_manager.unsubscribe(this->press_id); + event_manager.unsubscribe(this->release_id); + event_manager.unsubscribe(this->move_id); +} diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index d42e742..b0e6c9c 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -70,11 +70,13 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; - EventManager::get_instance().subscribe(mouse_handler, EventManager::CHANNEL_ALL); + EventManager::get_instance().subscribe(mouse_handler, + EventManager::CHANNEL_ALL); MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); + EventManager::get_instance().trigger_event(click_event, + EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); } @@ -92,13 +94,13 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { MouseClickEvent click_event{ .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); + EventManager::get_instance().trigger_event(click_event, + EventManager::CHANNEL_ALL); EXPECT_FALSE(triggered); EventManager::get_instance().trigger_event(click_event, test_channel); } - TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { EventManager & event_manager = EventManager::get_instance(); @@ -128,8 +130,6 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); - - // Trigger event event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); @@ -144,7 +144,6 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { event_manager.clear(); event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); - // Trigger event again event_manager.trigger_event(click_event, EventManager::CHANNEL_ALL); @@ -186,7 +185,6 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { EXPECT_TRUE(triggered2); } - TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { EventManager & event_manager = EventManager::get_instance(); -- cgit v1.2.3