From 9f882131f09410113d757d96e5aa0322aa5584bd Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 20 Nov 2024 18:48:18 +0100 Subject: git remake --- src/crepe/api/EventManager.cpp | 60 +++++++++++++-------------- src/crepe/api/EventManager.h | 19 +++++---- src/crepe/api/EventManager.hpp | 87 +++++++++++++++++++--------------------- src/crepe/api/IKeyListener.cpp | 8 +--- src/crepe/api/IKeyListener.h | 1 + src/crepe/api/IMouseListener.cpp | 3 +- src/crepe/api/IMouseListener.h | 1 + 7 files changed, 87 insertions(+), 92 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 4f88e97..dbdb0c3 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -8,37 +8,39 @@ EventManager & EventManager::get_instance() { } void EventManager::dispatch_events() { - for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { - std::unique_ptr& event = (*event_it).event; - int channel = (*event_it).channel; - std::type_index event_type = (*event_it).type; - - 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 (!event_handled) { - ++event_it; - } - } + for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { + std::unique_ptr & event = (*event_it).event; + int channel = (*event_it).channel; + std::type_index event_type = (*event_it).type; + + 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 (!event_handled) { + ++event_it; + } + } } -void EventManager::clear(){ +void EventManager::clear() { this->subscribers.clear(); this->events_queue.clear(); } diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index eccb0bf..133d72d 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -19,7 +19,6 @@ static constexpr int CHANNEL_ALL = -1; */ class EventManager { public: - /** * \brief Get the singleton instance of the EventManager. * @@ -39,7 +38,8 @@ public: * \param channel The channel number to subscribe to (default is 0). */ template - void subscribe(const EventHandler & callback, int channel = CHANNEL_ALL, int priority = 0); + void subscribe(const EventHandler & callback, int channel = CHANNEL_ALL, + int priority = 0); /** * \brief Unsubscribe from an event. @@ -76,7 +76,7 @@ public: * \param channel The channel number for the event (default is 0). */ template - void queue_event(const EventType & event, int channel = CHANNEL_ALL,int priority = 0); + void queue_event(const EventType & event, int channel = CHANNEL_ALL, int priority = 0); /** * \brief Dispatch all queued events. @@ -90,8 +90,8 @@ public: * */ void clear(); + private: - /** * \brief Default constructor for the EventManager. * @@ -99,21 +99,20 @@ private: */ EventManager() = default; struct QueueEntry { - std::unique_ptr event; + std::unique_ptr event; int channel = CHANNEL_ALL; std::type_index type; int priority = 0; - }; + }; struct CallbackEntry { - std::unique_ptr callback; + std::unique_ptr callback; int channel = CHANNEL_ALL; int priority = 0; - }; + }; //! The queue of events to be processed. std::vector events_queue; //! Registered event handlers. - std::unordered_map> subscribers; - + std::unordered_map> subscribers; }; } // namespace crepe diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index 481017d..a04a43a 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -3,7 +3,8 @@ namespace crepe { template -void EventManager::subscribe(const EventHandler & callback, int channel, int priority) { +void EventManager::subscribe(const EventHandler & callback, int channel, + int priority) { std::type_index event_type = typeid(EventType); std::unique_ptr> handler @@ -14,69 +15,65 @@ void EventManager::subscribe(const EventHandler & callback, int chann .channel = channel, .priority = priority, }); - // Sort handlers by priority (highest first) - std::sort(handlers.begin(), handlers.end(), [](const CallbackEntry &a, const CallbackEntry &b) { - return a.priority > b.priority; - }); + // Sort handlers by priority (highest first) + std::sort(handlers.begin(), handlers.end(), + [](const CallbackEntry & a, const CallbackEntry & b) { + return a.priority > b.priority; + }); } template -void EventManager::queue_event(const EventType & event, int channel,int priority) { - static_assert(std::is_base_of::value, "EventType must derive from Event"); +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(event); - this->events_queue.push_back( - QueueEntry{ - .event = std::move(event_ptr), - .channel = channel, - .type = event_type, - .priority = priority - } - ); + this->events_queue.push_back(QueueEntry{.event = std::move(event_ptr), + .channel = channel, + .type = event_type, + .priority = priority}); } template void EventManager::trigger_event(const EventType & event, int channel) { - std::type_index event_type = typeid(EventType); + std::type_index event_type = typeid(EventType); - auto handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - const std::vector &handlers = handlers_it->second; + auto handlers_it = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + const std::vector & handlers = handlers_it->second; - for (const CallbackEntry &handler : handlers) { - if (handler.channel != channel && handler.channel != CHANNEL_ALL) { - continue; - } - if (handler.callback->exec(event)) { - break; - } - } - } + for (const CallbackEntry & handler : handlers) { + if (handler.channel != channel && handler.channel != CHANNEL_ALL) { + continue; + } + if (handler.callback->exec(event)) { + break; + } + } + } } - template void EventManager::unsubscribe(const EventHandler & callback, int channel) { - std::type_index event_type = typeid(EventType); - std::string handler_name = callback.target_type().name(); + 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; + // 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; - } - } + 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 5e7d9bb..6a522c1 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -2,7 +2,6 @@ using namespace crepe; - // Constructor with specified channel IKeyListener::IKeyListener(int channel) : channel(channel), @@ -21,10 +20,8 @@ void IKeyListener::subscribe_events() { 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); + event_manager.subscribe(this->key_pressed_handler, this->channel); + event_manager.subscribe(this->key_released_handler, this->channel); } // Unsubscribe from key events @@ -32,4 +29,3 @@ 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 70243b4..9402cce 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -36,6 +36,7 @@ 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. diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 6fd6c4b..f3ceb84 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -24,8 +24,7 @@ void IMouseListener::subscribe_events() { // 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_release_handler, this->channel); event_manager.subscribe(mouse_move_handler, this->channel); } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 1195a4e..6bc5716 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -55,6 +55,7 @@ public: * \return True if the event was handled, false otherwise. */ virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0; + protected: /** * \brief Subscribes to mouse events on the specified channel. -- cgit v1.2.3