Loading...
Searching...
No Matches
EventManager.h
Go to the documentation of this file.
1//
2// EventsManager.h
3//
4
5#ifndef _EVENTSMANAGER_H
6#define _EVENTSMANAGER_H
7
8#include <map>
9#include <string>
10#include <vector>
14#include "SFML/Window/Event.hpp"
15
16namespace ml
17{
18 class Fireable;
19 class EventReceiver;
20 class Core;
21 class Unsubscribable;
22
40 class EventManager : public DeferredOperationsManager<EventManager>
41 {
43 struct Subscriber
44 {
45 EventReceiver* receiver;
46 Core* core;
47 };
48
49 inline static std::map<
50 std::string,
51 std::vector<Subscriber>
52 > _subscribers;
54
55 public:
63 template<typename EnumType>
64 static void subscribe(EnumType eventEnum, EventReceiver* component)
65 {
66 doSubscribe(EnumKey::get(eventEnum), component);
67 }
68
78 template<typename EnumType>
79 static void unsubscribe(EnumType eventEnum, Core* core)
80 {
81 deferOrExecute([key = EnumKey::get(eventEnum), core]()
82 {
83 doUnsubscribe(key, core);
84 });
85 }
86
95 static void unsubscribeAll(Core* core);
96
102 static void clear();
103
114 template<typename EnumType>
115 static void fire(EnumType eventEnum,
116 Fireable* dispatcher,
117 const std::optional<sf::Event>& event,
118 SystemCallback resolve = nullptr,
119 SystemCallback reject = nullptr)
120 {
121 doFire(EnumKey::get(eventEnum), dispatcher, event, resolve, reject);
122 }
123
124 friend class AppManager;
125 friend class Fireable;
126 friend class Core;
127 friend class Unsubscribable;
128
129 private:
131 static void unsubscribe(const std::string& key, Core* core)
132 {
133 deferOrExecute([key, core]() { doUnsubscribe(key, core); });
134 }
135
136
137
138 static void doSubscribe(const std::string& key, EventReceiver* component);
139 static void doFire(const std::string& key,
140 Fireable* dispatcher,
141 const std::optional<sf::Event>& event,
142 SystemCallback resolve,
143 SystemCallback reject);
144 static void doUnsubscribe(const std::string& key, Core* core);
145 static void doUnsubscribeAll(Core* core);
146
147 public:
148 static void forceUnsubscribeAll(Core* core);
150 };
151
152} // namespace ml
153
154#endif // _EVENTSMANAGER_H
Virtual base class for all Malena framework objects.
Definition Core.h:67
CRTP base that gives a manager safe deferred-operation support.
static void deferOrExecute(std::function< void()> operation)
Centralized event bus for the Malena trait-based event system.
static void clear()
Remove all subscriptions for all events.
static void unsubscribe(EnumType eventEnum, Core *core)
Remove a component's subscription to one event.
friend class Core
friend class AppManager
static void fire(EnumType eventEnum, Fireable *dispatcher, const std::optional< sf::Event > &event, SystemCallback resolve=nullptr, SystemCallback reject=nullptr)
Fire an event to all matching subscribers.
static void subscribe(EnumType eventEnum, EventReceiver *component)
Register a component for an enum-keyed event.
friend class Fireable
static void unsubscribeAll(Core *core)
Remove all subscriptions for a component.
friend class Unsubscribable
Base class for all event-receiving traits.
Trait that gives components the ability to unsubscribe from events.
Definition Component.h:18
static std::string get(EnumType value)
Generate a unique string key for an enum value.
Definition EnumKey.h:63