aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/eventManager.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 18:41:30 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 18:41:30 +0100
commit8e3367b186e60eb1e33bf58a066823cb00a7566e (patch)
treec4038a31993767276efec5fa1b1a37dff3b79465 /mwe/events/include/eventManager.h
parentb7df77d6cc26cb9ee46891d7108f01734b3104dd (diff)
parent35ef3ba91ce9e00466508f2388f4c1dd2321b505 (diff)
Merge branch 'master' into poc/audio-miniaudio
Diffstat (limited to 'mwe/events/include/eventManager.h')
-rw-r--r--mwe/events/include/eventManager.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/mwe/events/include/eventManager.h b/mwe/events/include/eventManager.h
new file mode 100644
index 0000000..508a5e2
--- /dev/null
+++ b/mwe/events/include/eventManager.h
@@ -0,0 +1,66 @@
+#pragma once
+#include "event.h"
+#include "eventHandler.h"
+#include "keyCodes.h"
+#include <memory>
+#include <unordered_map>
+#include <vector>
+// using EventType = std::uint32_t;
+// using EventId = std::uint64_t;
+
+class EventManager {
+public:
+ EventManager(const EventManager &) = delete;
+ const EventManager & operator=(const EventManager &) = delete;
+ static EventManager & getInstance() {
+ static EventManager instance;
+ return instance;
+ }
+
+ void shutdown();
+ void subscribe(int eventType,
+ std::unique_ptr<IEventHandlerWrapper> && handler,
+ int eventId);
+ void unsubscribe(int eventType, const std::string & handlerName,
+ int eventId);
+ void triggerEvent(const Event & event_, int eventId);
+ void queueEvent(std::unique_ptr<Event> && event_, int eventId);
+ void dispatchEvents();
+
+private:
+ EventManager() = default;
+ std::vector<std::pair<std::unique_ptr<Event>, int>> eventsQueue;
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>
+ subscribers;
+ std::unordered_map<
+ int, std::unordered_map<
+ int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>
+ subscribersByEventId;
+};
+
+template <typename EventType>
+inline void subscribe(const EventHandler<EventType> & callback, int eventId = 0,
+ const bool unsubscribeOnSuccess = false) {
+ std::unique_ptr<IEventHandlerWrapper> handler
+ = std::make_unique<EventHandlerWrapper<EventType>>(
+ callback, unsubscribeOnSuccess);
+ EventManager::getInstance().subscribe(EventType::getStaticEventType(),
+ std::move(handler), eventId);
+}
+
+template <typename EventType>
+inline void unsubscribe(const EventHandler<EventType> & callback,
+ int eventId = 0) {
+ const std::string handlerName = callback.target_type().name();
+ EventManager::getInstance().unsubscribe(EventType::getStaticEventType(),
+ handlerName, eventId);
+}
+
+inline void triggerEvent(const Event & triggeredEvent, int eventId = 0) {
+ EventManager::getInstance().triggerEvent(triggeredEvent, eventId);
+}
+
+inline void queueEvent(std::unique_ptr<Event> && queuedEvent, int eventId = 0) {
+ EventManager::getInstance().queueEvent(
+ std::forward<std::unique_ptr<Event>>(queuedEvent), eventId);
+}