blob: 17fe528c6dc9dd9ad160349752506c3cee24a372 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include "EventManager.h"
#include "util/Log.h"
using namespace crepe;
using namespace std;
EventManager & EventManager::get_instance() {
dbg_trace();
static EventManager instance;
return instance;
}
void EventManager::dispatch_events() {
for (auto & event : this->events_queue) {
this->handle_event(event.type, event.channel, *event.event.get());
}
this->events_queue.clear();
}
void EventManager::handle_event(type_index type, event_channel_t channel, const Event & data) {
auto handlers_it = this->subscribers.find(type);
if (handlers_it == this->subscribers.end()) return;
vector<CallbackEntry> & handlers = handlers_it->second;
for (auto & handler : handlers) {
bool check_channel = handler.channel != CHANNEL_ALL || channel != CHANNEL_ALL;
if (check_channel && handler.channel != channel) continue;
bool handled = handler.callback->exec(data);
if (handled) return;
}
}
void EventManager::clear() {
this->subscribers.clear();
this->events_queue.clear();
}
void EventManager::unsubscribe(subscription_t id) {
for (auto & [event_type, handlers] : this->subscribers) {
for (auto it = handlers.begin(); it != handlers.end(); it++) {
// find listener with subscription id
if ((*it).id != id) continue;
it = handlers.erase(it);
return;
}
}
}
|