2020-12-22 18:10:01 +01:00
|
|
|
#include "helpers/event.hpp"
|
|
|
|
|
2021-01-04 00:19:56 +01:00
|
|
|
#include <helpers/shared_data.hpp>
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
void EventManager::post(Events eventType, const void *userData) {
|
2021-01-04 00:19:56 +01:00
|
|
|
for (auto &handler : *SharedData::get().eventHandlers)
|
2020-12-22 18:10:01 +01:00
|
|
|
if (eventType == handler.eventType)
|
|
|
|
handler.callback(userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::subscribe(Events eventType, void *owner, std::function<void(const void*)> callback) {
|
2021-01-04 00:19:56 +01:00
|
|
|
for (auto &handler : *SharedData::get().eventHandlers)
|
2020-12-22 18:10:01 +01:00
|
|
|
if (eventType == handler.eventType && owner == handler.owner)
|
|
|
|
return;
|
|
|
|
|
2021-01-04 00:19:56 +01:00
|
|
|
SharedData::get().eventHandlers->push_back(EventHandler { owner, eventType, callback });
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::unsubscribe(Events eventType, void *sender) {
|
2021-01-04 00:19:56 +01:00
|
|
|
std::erase_if(*SharedData::get().eventHandlers, [&eventType, &sender](EventHandler handler) {
|
2020-12-22 18:10:01 +01:00
|
|
|
return eventType == handler.eventType && sender == handler.owner;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|