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