aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:33:14 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:33:14 +0100
commit48f69cedaf16038d980071d43d32e22caae44c17 (patch)
treed52db1e10dc82eb02c7d22dbe3fa8648c457c2ce /src/crepe/api/EventManager.hpp
parent6808307cf65a4b686621f08a58effecc0a9c6bb8 (diff)
parentda379a58033c0ef3c9c854326a3fca25d6e54319 (diff)
merge `loek/scripts` into `loek/collision-system`
Diffstat (limited to 'src/crepe/api/EventManager.hpp')
-rw-r--r--src/crepe/api/EventManager.hpp146
1 files changed, 21 insertions, 125 deletions
diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp
index b509097..a5f4556 100644
--- a/src/crepe/api/EventManager.hpp
+++ b/src/crepe/api/EventManager.hpp
@@ -1,140 +1,36 @@
+#pragma once
+
#include "EventManager.h"
+
namespace crepe {
template <typename EventType>
-void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
+subscription_t EventManager::subscribe(const EventHandler<EventType> & callback,
+ event_channel_t channel) {
+ subscription_counter++;
std::type_index event_type = typeid(EventType);
std::unique_ptr<EventHandlerWrapper<EventType>> handler
= std::make_unique<EventHandlerWrapper<EventType>>(callback);
-
- if (channel) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
- = handlers_map.find(channel);
- if (handlers != handlers_map.end()) {
- handlers->second.emplace_back(std::move(handler));
- } else {
- handlers_map[channel].emplace_back(std::move(handler));
- }
- } else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
- handlers.emplace_back(std::move(handler));
- }
-}
-
-template <typename EventType>
-void EventManager::queue_event(EventType && event, int channel) {
- std::type_index event_type = std::type_index(typeid(EventType));
-
- std::unique_ptr<EventType> event_ptr
- = std::make_unique<EventType>(std::forward<EventType>(event));
-
- std::tuple<std::unique_ptr<Event>, int, std::type_index> tuple(
- std::move(event_ptr), channel, event_type);
- this->events_queue.push_back(std::move(tuple));
+ std::vector<CallbackEntry> & handlers = this->subscribers[event_type];
+ handlers.emplace_back(CallbackEntry{
+ .callback = std::move(handler), .channel = channel, .id = subscription_counter});
+ return subscription_counter;
}
template <typename EventType>
-void EventManager::trigger_event(const EventType & event, int channel) {
- std::type_index event_type = std::type_index(typeid(EventType));
-
- if (channel > 0) {
- std::unordered_map<int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = this->subscribers_by_event_id[event_type];
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers_it
- = handlers_map.find(channel);
-
- if (handlers_it != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
- = handlers.begin();
- it != handlers.end();++it) {
- // stops when callback returns true
- if((*it)->exec(event)){
- break;
- }
- }
- }
- } else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = this->subscribers[event_type];
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
- = handlers.begin();
- it != handlers.end();++it) {
- // stops when callback returns true
- if((*it)->exec(event)){
- break;
- }
- }
- }
+void EventManager::queue_event(const EventType & event, event_channel_t channel) {
+ static_assert(std::is_base_of<Event, EventType>::value,
+ "EventType must derive from Event");
+ this->events_queue.push_back(QueueEntry{
+ .event = std::make_unique<EventType>(event),
+ .channel = channel,
+ .type = typeid(EventType),
+ });
}
template <typename EventType>
-void EventManager::unsubscribe(const EventHandler<EventType> & callback,
- int channel) {
- std::type_index event_type(typeid(EventType));
- std::string handler_name = callback.target_type().name();
-
- if (channel) {
- std::unordered_map<
- std::type_index,
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::
- iterator subscriber_list
- = this->subscribers_by_event_id.find(event_type);
- if (subscriber_list != this->subscribers_by_event_id.end()) {
- std::unordered_map<
- int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> &
- handlers_map
- = subscriber_list->second;
- std::unordered_map<
- int,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator
- handlers
- = handlers_map.find(channel);
- if (handlers != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & callbacks
- = handlers->second;
- for (std::vector<
- std::unique_ptr<IEventHandlerWrapper>>::iterator it
- = callbacks.begin();
- it != callbacks.end(); ++it) {
- if ((*it)->get_type() == handler_name) {
- it = callbacks.erase(it);
- return;
- }
- }
- }
- }
- } else {
- std::unordered_map<std::type_index,
- std::vector<std::unique_ptr<IEventHandlerWrapper>>>::
- iterator handlers_it
- = this->subscribers.find(event_type);
- if (handlers_it != this->subscribers.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>> & handlers
- = handlers_it->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it
- = handlers.begin();
- it != handlers.end(); ++it) {
- if ((*it)->get_type() == handler_name) {
- it = handlers.erase(it);
- return;
- }
- }
- }
- }
+void EventManager::trigger_event(const EventType & event, event_channel_t channel) {
+ this->handle_event(typeid(EventType), channel, event);
}
-}
+} // namespace crepe