diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-11 10:50:40 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-11 10:50:40 +0100 |
commit | 880a7b1fb233a67303ad5a4155dc459f79734762 (patch) | |
tree | 955faa9c02480f17cb4a674ae9a1b82dab741a63 /src/crepe/api/EventManager.h | |
parent | 3a690f7d0c91b92b9cdfe62f44dba8db90142abc (diff) |
big cleanup events + events are now structs
Diffstat (limited to 'src/crepe/api/EventManager.h')
-rw-r--r-- | src/crepe/api/EventManager.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h new file mode 100644 index 0000000..8c0685a --- /dev/null +++ b/src/crepe/api/EventManager.h @@ -0,0 +1,76 @@ +#pragma once +#include <memory> +#include <unordered_map> +#include <vector> +#include <functional> +#include <iostream> +#include <typeindex> +#include <type_traits> +#include "Event.h" +#include "EventHandler.h" +//#include "keyCodes.h" + +class EventManager { +public: + EventManager(const EventManager &) = delete; + const EventManager & operator=(const EventManager &) = delete; + static EventManager & get_instance() { + static EventManager instance; + return instance; + } + template <typename EventType> + void subscribe(EventHandler<EventType> && callback, int channel = 0); + void unsubscribe(std::type_index eventType, const std::string & handlerName,int channel); + template <typename EventType> + void trigger_event(const EventType & event, int channel); + void queue_event(std::unique_ptr<Event> && event, int channel); + void dispatch_events(); + +private: + EventManager() = default; + std::vector<std::pair<std::unique_ptr<Event>, int>> events_queue; + std::unordered_map<std::type_index, std::vector<std::unique_ptr<IEventHandlerWrapper>>> subscribers; + std::unordered_map<std::type_index, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>> subscribers_by_event_id; +}; + +template <typename EventType> +void EventManager::subscribe(EventHandler<EventType> && callback, int channel){ + std::type_index event_type = typeid(EventType); + auto handler = std::make_unique<EventHandlerWrapper<EventType>>(callback); + + if (channel) { + auto & handlers_map = subscribers_by_event_id[event_type]; + auto handlers = handlers_map.find(channel); + if (handlers != handlers_map.end()) { + handlers->second.emplace_back(std::move(handler)); + } else { + handlers_map[channel].emplace_back(std::move(handler)); + } + } else { + subscribers[event_type].emplace_back(std::move(handler)); + } +} + +template <typename EventType> +void EventManager::trigger_event(const EventType & event, int eventId) { + std::type_index event_type = std::type_index(typeid(EventType)); + + if (eventId > 0) { + auto handlers_it = subscribers_by_event_id[event_type].find(eventId); + if (handlers_it != subscribers_by_event_id[event_type].end()) { + auto & callbacks = handlers_it->second; + for (auto it = callbacks.begin(); it != callbacks.end();) { + if ((*it)->exec(event)) { + it = callbacks.erase(it); + } else { + ++it; + } + } + } + } else { + auto & handlers = subscribers[event_type]; + for (auto & handler : handlers) { + handler->exec(event); + } + } +} |