Loading...
Searching...
No Matches
EventManager.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith. All rights reserved.
2// Malena Framework — Proprietary Software. See LICENSE for terms.
3
4//
5// EventManager.h
6//
7
8#ifndef MALENA_EVENTSMANAGER_H
9#define MALENA_EVENTSMANAGER_H
10
12#include <map>
13#include <string>
14#include <vector>
18#include <SFML/Window/Event.hpp>
19#include <optional>
20
21namespace ml
22{
23 class Fireable;
24 class EventReceiver;
25 class Core;
26 class Unsubscribable;
27
46 {
48 struct Subscriber
49 {
50 EventReceiver* receiver;
51 Core* core;
52 };
53
54 inline static std::map<
55 std::string,
56 std::vector<Subscriber>
57 > _subscribers;
59
60 public:
68 template<typename EnumType>
69 static void subscribe(EnumType eventEnum, EventReceiver* component)
70 {
71 doSubscribe(EnumKey::get(eventEnum), component);
72 }
73
83 template<typename EnumType>
84 static void unsubscribe(EnumType eventEnum, Core* core)
85 {
86 deferOrExecute([key = EnumKey::get(eventEnum), core]()
87 {
88 doUnsubscribe(key, core);
89 });
90 }
91
100 static void unsubscribeAll(Core* core);
101
107 static void clear();
108
119 template<typename EnumType>
120 static void fire(EnumType eventEnum,
121 Fireable* dispatcher,
122 const std::optional<sf::Event>& event,
123 SystemCallback resolve = nullptr,
124 SystemCallback reject = nullptr)
125 {
126 doFire(EnumKey::get(eventEnum), dispatcher, event, resolve, reject);
127 }
128
129 friend class AppManager;
130 friend class Fireable;
131 friend class Core;
132 friend class Unsubscribable;
133
134 private:
136 static void unsubscribe(const std::string& key, Core* core)
137 {
138 deferOrExecute([key, core]() { doUnsubscribe(key, core); });
139 }
140
141
142
143 static void doSubscribe(const std::string& key, EventReceiver* component);
144 static void doFire(const std::string& key,
145 Fireable* dispatcher,
146 const std::optional<sf::Event>& event,
147 SystemCallback resolve,
148 SystemCallback reject);
149 static void doUnsubscribe(const std::string& key, Core* core);
150 static void doUnsubscribeAll(Core* core);
151
152 public:
153 static void forceUnsubscribeAll(Core* core);
155 };
156
157} // namespace ml
158
159#endif // MALENA_EVENTSMANAGER_H
Virtual base class for all Malena framework objects.
Definition Core.h:69
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.
#define MALENA_API
Definition Component.h:22
static std::string get(EnumType value)
Generate a unique string key for an enum value.
Definition EnumKey.h:67