aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/EventManager.cpp')
-rw-r--r--src/crepe/api/EventManager.cpp78
1 files changed, 32 insertions, 46 deletions
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp
index b465e89..20f0dd3 100644
--- a/src/crepe/api/EventManager.cpp
+++ b/src/crepe/api/EventManager.cpp
@@ -1,6 +1,7 @@
#include "EventManager.h"
using namespace crepe;
+using namespace std;
EventManager & EventManager::get_instance() {
static EventManager instance;
@@ -8,53 +9,38 @@ EventManager & EventManager::get_instance() {
}
void EventManager::dispatch_events() {
- using HandlersMap
- = std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>;
- using HandlersVec = std::vector<std::unique_ptr<IEventHandlerWrapper>>;
-
- for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) {
- std::unique_ptr<Event> & event = std::get<0>(*event_it);
- int channel = std::get<1>(*event_it);
- std::type_index event_type = std::get<2>(*event_it);
-
- bool event_handled = false;
-
- if (channel) {
- auto handlers_it = subscribers_by_event_id.find(event_type);
- if (handlers_it != subscribers_by_event_id.end()) {
- HandlersMap & handlers_map = handlers_it->second;
- auto handlers = handlers_map.find(channel);
- if (handlers != handlers_map.end()) {
- HandlersVec & callbacks = handlers->second;
- for (auto handler_it = callbacks.begin(); handler_it != callbacks.end();
- ++handler_it) {
- if ((*handler_it)->exec(*event)) {
- event_it = events_queue.erase(event_it);
- event_handled = true;
- break;
- }
- }
- }
- }
- } else {
- // Handle event for all channels
- auto handlers_it = this->subscribers.find(event_type);
- if (handlers_it != this->subscribers.end()) {
- HandlersVec & handlers = handlers_it->second;
- for (auto handler_it = handlers.begin(); handler_it != handlers.end();
- ++handler_it) {
- // remove event from queue since and continue when callback returns true
- if ((*handler_it)->exec(*event)) {
- event_it = this->events_queue.erase(event_it);
- event_handled = true;
- break;
- }
- }
- }
- }
+ 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();
+}
- if (!event_handled) {
- ++event_it;
+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;
}
}
}