This repository was archived by the owner on Jan 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTitan_Events.hpp
More file actions
118 lines (97 loc) · 2.41 KB
/
Copy pathTitan_Events.hpp
File metadata and controls
118 lines (97 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Titan Events Header
*
* Event bus system
*/
#ifndef TITAN_EVENTS_HPP
#define TITAN_EVENTS_HPP
#include "Titan_Core.hpp"
#include <unordered_map>
#include <vector>
#include <functional>
namespace Titan {
// ============================================================================
// Event Types
// ============================================================================
using EventID = uint32;
struct EventContext
{
uint32 SenderID = 0;
union
{
uint64 DataU64;
void* Ptr;
};
};
using EventHandler = std::function<void(const EventContext&)>;
// ============================================================================
// Event Bus
// ============================================================================
struct EventBus
{
void Init()
{
Listeners.clear();
}
void Shutdown()
{
Listeners.clear();
}
/**
* Subscribe to an event
* @param ID - Event identifier
* @param Handler - Callback function
*/
void Subscribe(EventID ID, EventHandler Handler)
{
Listeners[ID].push_back(Handler);
}
/**
* Emit an event to all subscribers
* @param ID - Event identifier
* @param Context - Event data
*/
void Emit(EventID ID, const EventContext& Context)
{
auto It = Listeners.find(ID);
if (It != Listeners.end())
{
for (auto& Handler : It->second)
{
Handler(Context);
}
}
}
/**
* Clear all listeners for an event
*/
void Clear(EventID ID)
{
Listeners.erase(ID);
}
/**
* Clear all listeners
*/
void ClearAll()
{
Listeners.clear();
}
private:
std::unordered_map<EventID, std::vector<EventHandler>> Listeners;
};
// ============================================================================
// Common Event IDs
// ============================================================================
namespace Events {
constexpr EventID OnEngineStart = 1;
constexpr EventID OnEngineShutdown = 2;
constexpr EventID OnSceneLoad = 10;
constexpr EventID OnSceneUnload = 11;
constexpr EventID OnEntityCreated = 20;
constexpr EventID OnEntityDestroyed = 21;
constexpr EventID OnCollision = 30;
constexpr EventID OnTriggerEnter = 31;
constexpr EventID OnTriggerExit = 32;
} // namespace Events
} // namespace Titan
#endif // TITAN_EVENTS_HPP