summaryrefslogtreecommitdiff
path: root/src/port/hooks/impl/EventSystem.cpp
diff options
context:
space:
mode:
authorKiritoDv <kiritodev01@gmail.com>2024-12-29 00:46:19 -0600
committerLywx <kiritodev01@gmail.com>2025-01-03 16:56:49 -0600
commit8462aee14e3732d482c5457569d61e3f8f60e9d9 (patch)
treee3374523c4a1f52d3fb5e906ce410d31b36a2dda /src/port/hooks/impl/EventSystem.cpp
parent4d28f402e51a74e5af1e4f85a53de92a9363baf6 (diff)
Moved to a dynamic id system and increased id size to uint32_t
Diffstat (limited to 'src/port/hooks/impl/EventSystem.cpp')
-rw-r--r--src/port/hooks/impl/EventSystem.cpp22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/port/hooks/impl/EventSystem.cpp b/src/port/hooks/impl/EventSystem.cpp
index 71eaffb5..3418b6ef 100644
--- a/src/port/hooks/impl/EventSystem.cpp
+++ b/src/port/hooks/impl/EventSystem.cpp
@@ -4,8 +4,16 @@
EventSystem* EventSystem::Instance = new EventSystem();
+EventID EventSystem::RegisterEvent() {
+ return this->mInternalEventID++;
+}
+
ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
- auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)];
+ if(id == -1) {
+ throw std::runtime_error("Trying to register listener for unregistered event");
+ }
+
+ auto& listeners = this->mEventListeners[id];
if(std::find_if(listeners.begin(), listeners.end(), [callback](EventListener listener) {
return listener.function == callback;
@@ -24,23 +32,23 @@ ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, Eve
}
void EventSystem::UnregisterListener(EventID id, ListenerID listenerId) {
- auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)];
+ auto& listeners = this->mEventListeners[id];
listeners.erase(listeners.begin() + listenerId);
}
void EventSystem::CallEvent(EventID id, IEvent* event) {
- auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)];
-
- if (listeners.empty()) {
- return;
- }
+ auto& listeners = this->mEventListeners[id];
for (auto& listener : listeners) {
listener.function(event);
}
}
+extern "C" EventID EventSystem_RegisterEvent() {
+ return EventSystem::Instance->RegisterEvent();
+}
+
extern "C" size_t EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
return EventSystem::Instance->RegisterListener(id, callback, priority);
}