2020-11-12 09:38:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
enum class Events {
|
2020-11-27 13:44:52 +01:00
|
|
|
FileLoaded,
|
2020-11-15 00:46:38 +01:00
|
|
|
DataChanged,
|
2020-11-17 13:58:50 +01:00
|
|
|
PatternChanged,
|
2020-11-21 00:12:58 +01:00
|
|
|
FileDropped,
|
2020-11-28 15:53:11 +01:00
|
|
|
RegionSelected,
|
2020-11-23 13:10:14 +01:00
|
|
|
|
2020-11-28 00:33:26 +01:00
|
|
|
SelectionChangeRequest,
|
|
|
|
|
|
|
|
AddBookmark
|
2020-11-12 09:38:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EventHandler {
|
|
|
|
void *sender;
|
|
|
|
Events eventType;
|
2020-11-17 13:58:50 +01:00
|
|
|
std::function<void(const void*)> callback;
|
2020-11-12 09:38:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class EventManager {
|
|
|
|
public:
|
|
|
|
|
2020-11-17 13:58:50 +01:00
|
|
|
void post(Events eventType, const void *userData) {
|
2020-11-12 09:38:52 +01:00
|
|
|
for (auto &handler : this->m_eventHandlers)
|
|
|
|
if (eventType == handler.eventType)
|
|
|
|
handler.callback(userData);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:58:50 +01:00
|
|
|
void subscribe(Events eventType, void *sender, std::function<void(const void*)> callback) {
|
2020-11-12 09:38:52 +01:00
|
|
|
for (auto &handler : this->m_eventHandlers)
|
|
|
|
if (eventType == handler.eventType && sender == handler.sender)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->m_eventHandlers.push_back(EventHandler { sender, eventType, callback });
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsubscribe(Events eventType, void *sender) {
|
|
|
|
std::erase_if(this->m_eventHandlers, [&eventType, &sender](EventHandler handler) {
|
|
|
|
return eventType == handler.eventType && sender == handler.sender;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<EventHandler> m_eventHandlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|