diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 18:48:18 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 18:48:18 +0100 |
commit | 9f882131f09410113d757d96e5aa0322aa5584bd (patch) | |
tree | 9ee5d3b3b8e83f3281797ec4c9c4be47dd603848 /src/crepe/api | |
parent | 9a46acde813e00e574e70439795dedcdc9a8192a (diff) |
git remake
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/EventManager.cpp | 60 | ||||
-rw-r--r-- | src/crepe/api/EventManager.h | 19 | ||||
-rw-r--r-- | src/crepe/api/EventManager.hpp | 87 | ||||
-rw-r--r-- | src/crepe/api/IKeyListener.cpp | 8 | ||||
-rw-r--r-- | src/crepe/api/IKeyListener.h | 1 | ||||
-rw-r--r-- | src/crepe/api/IMouseListener.cpp | 3 | ||||
-rw-r--r-- | src/crepe/api/IMouseListener.h | 1 |
7 files changed, 87 insertions, 92 deletions
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 = (*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<CallbackEntry>& 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 = (*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<CallbackEntry> & 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 <typename EventType> - void subscribe(const EventHandler<EventType> & callback, int channel = CHANNEL_ALL, int priority = 0); + void subscribe(const EventHandler<EventType> & 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 <typename EventType> - 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> event; + std::unique_ptr<Event> event; int channel = CHANNEL_ALL; std::type_index type; int priority = 0; - }; + }; struct CallbackEntry { - std::unique_ptr<IEventHandlerWrapper> callback; + std::unique_ptr<IEventHandlerWrapper> callback; int channel = CHANNEL_ALL; int priority = 0; - }; + }; //! The queue of events to be processed. std::vector<QueueEntry> events_queue; //! Registered event handlers. - std::unordered_map<std::type_index,std::vector<CallbackEntry>> subscribers; - + std::unordered_map<std::type_index, std::vector<CallbackEntry>> 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 <typename EventType> -void EventManager::subscribe(const EventHandler<EventType> & callback, int channel, int priority) { +void EventManager::subscribe(const EventHandler<EventType> & callback, int channel, + int priority) { std::type_index event_type = typeid(EventType); std::unique_ptr<EventHandlerWrapper<EventType>> handler @@ -14,69 +15,65 @@ void EventManager::subscribe(const EventHandler<EventType> & 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 <typename EventType> -void EventManager::queue_event(const EventType & event, int channel,int priority) { - static_assert(std::is_base_of<Event, EventType>::value, "EventType must derive from Event"); +void EventManager::queue_event(const EventType & event, int channel, int priority) { + static_assert(std::is_base_of<Event, EventType>::value, + "EventType must derive from Event"); std::type_index event_type = typeid(EventType); auto event_ptr = std::make_unique<EventType>(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 <typename EventType> 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<CallbackEntry> &handlers = handlers_it->second; + auto handlers_it = this->subscribers.find(event_type); + if (handlers_it != this->subscribers.end()) { + const std::vector<CallbackEntry> & 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 <typename EventType> void EventManager::unsubscribe(const EventHandler<EventType> & 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<CallbackEntry> & 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<CallbackEntry> & 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<KeyPressEvent>(this->key_pressed_handler, - this->channel); - event_manager.subscribe<KeyReleaseEvent>(this->key_released_handler, - this->channel); + event_manager.subscribe<KeyPressEvent>(this->key_pressed_handler, this->channel); + event_manager.subscribe<KeyReleaseEvent>(this->key_released_handler, this->channel); } // Unsubscribe from key events @@ -32,4 +29,3 @@ void IKeyListener::unsubscribe_events() { event_manager.unsubscribe<KeyPressEvent>(this->key_pressed_handler, this->channel); event_manager.unsubscribe<KeyReleaseEvent>(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<MouseClickEvent>(mouse_click_handler, this->channel); event_manager.subscribe<MousePressEvent>(mouse_press_handler, this->channel); - event_manager.subscribe<MouseReleaseEvent>(mouse_release_handler, - this->channel); + event_manager.subscribe<MouseReleaseEvent>(mouse_release_handler, this->channel); event_manager.subscribe<MouseMoveEvent>(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. |