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