diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-15 12:43:38 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-15 12:43:38 +0100 |
commit | cab672ce6258aec563e7f0e0549281156c0c8d21 (patch) | |
tree | e4b825c5fed4282672aeb18d4bd405710f20349d /src/crepe/api | |
parent | 22a3ba4d0773d6010704ec494a08f5d95f564a94 (diff) |
feedback
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 14 | ||||
-rw-r--r-- | src/crepe/api/Event.h | 13 | ||||
-rw-r--r-- | src/crepe/api/EventHandler.h | 1 | ||||
-rw-r--r-- | src/crepe/api/EventManager.cpp | 3 | ||||
-rw-r--r-- | src/crepe/api/EventManager.h | 142 | ||||
-rw-r--r-- | src/crepe/api/EventManager.hpp | 140 | ||||
-rw-r--r-- | src/crepe/api/IKeyListener.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/IMouseListener.cpp | 10 | ||||
-rw-r--r-- | src/crepe/api/KeyCodes.h | 1 |
9 files changed, 180 insertions, 148 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 87cbb09..53b3041 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -18,6 +18,12 @@ target_sources(crepe PUBLIC Vector2.cpp Camera.cpp Animator.cpp + EventManager.cpp + EventHandler.cpp + IKeyListener.cpp + IMouseListener.cpp + LoopManager.cpp + LoopTimer.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -42,4 +48,12 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SceneManager.hpp Camera.h Animator.h + EventManager.h + EventManager.hpp + EventHandler.h + Event.h + IKeyListener.h + IMouseListener.h + LoopManager.h + LoopTimer.h ) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index cffa5da..701ecdf 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -6,38 +6,37 @@ class Event { public: - bool handled = false; }; class KeyPressEvent : public Event { public: int repeat = 0; - Keycode key = Keycode::None; + Keycode key = Keycode::NONE; }; class KeyReleaseEvent : public Event { public: - Keycode key = Keycode::None; + Keycode key = Keycode::NONE; }; class MousePressEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button; + MouseButton button = MouseButton::NONE; }; class MouseClickEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button; + MouseButton button = MouseButton::NONE; }; class MouseReleaseEvent : public Event { public: int mouse_x = 0; int mouse_y = 0; - MouseButton button = MouseButton::None; + MouseButton button = MouseButton::NONE; }; class MouseMoveEvent : public Event { public: @@ -50,7 +49,7 @@ public: }; class TextSubmitEvent : public Event { public: - std::string text; + std::string text = ""; }; class ShutDownEvent : public Event { public: diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h index 46c6c7b..0ab90de 100644 --- a/src/crepe/api/EventHandler.h +++ b/src/crepe/api/EventHandler.h @@ -12,6 +12,7 @@ * * \tparam EventType The type of event this handler will handle. */ +// TODO: typedef template <typename EventType> using EventHandler = std::function<bool(const EventType & e)>; diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 72cfd74..e881d49 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -1,5 +1,7 @@ #include "EventManager.h" +using namespace crepe; + EventManager & EventManager::get_instance() { static EventManager instance; return instance; @@ -62,6 +64,7 @@ void EventManager::dispatch_events() { iterator 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; diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index e2665bd..38d2e64 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -10,6 +10,8 @@ #include "Event.h" #include "EventHandler.h" + +namespace crepe { /** * \class EventManager * \brief The EventManager class is responsible for managing the subscription, triggering, @@ -115,142 +117,6 @@ private: std::vector<std::unique_ptr<IEventHandlerWrapper>>>> subscribers_by_event_id; }; -template <typename EventType> -void EventManager::subscribe(EventHandler<EventType> && callback, int channel) { - 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)); -} - -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();) { - // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } - } 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();) { - // erases callback if callback function returns true - if ((*it)->exec(event)) { - it = handlers.erase(it); - } else { - ++it; - } - } - } -} - -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; - } - } - } - } -} +} // namespace crepe +#include "EventManager.hpp" diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp new file mode 100644 index 0000000..b509097 --- /dev/null +++ b/src/crepe/api/EventManager.hpp @@ -0,0 +1,140 @@ +#include "EventManager.h" +namespace crepe { + +template <typename EventType> +void EventManager::subscribe(EventHandler<EventType> && callback, int channel) { + 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)); +} + +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; + } + } + } +} + +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; + } + } + } + } +} + +} diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index eb8f9af..4fd9855 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -1,5 +1,7 @@ #include "IKeyListener.h" -#include <iostream> + +using namespace crepe; + IKeyListener::IKeyListener() { this->channel = channel; this->subscribe_events(); diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 683632c..489e55b 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -1,6 +1,11 @@ #include "IMouseListener.h" + +using namespace crepe; + IMouseListener::IMouseListener(int channel) { this->channel = channel; } + IMouseListener::IMouseListener() { this->subscribe_events(); } + IMouseListener::~IMouseListener() { this->unsubscribe_events(); } void IMouseListener::subscribe_events() { @@ -26,7 +31,7 @@ void IMouseListener::subscribe_events() { EventManager::get_instance().subscribe<MouseMoveEvent>( std::move(this->mouse_move_handler), this->channel); } - +// TODO: reference voor singleton void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe<MouseClickEvent>( this->mouse_click_handler, this->channel); @@ -37,18 +42,21 @@ void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe<MouseMoveEvent>( this->mouse_move_handler, this->channel); } + void IMouseListener::activate_mouse() { if (this->active) { return; } this->subscribe_events(); } + void IMouseListener::deactivate_mouse() { if (!this->active) { return; } this->unsubscribe_events(); } + void IMouseListener::set_channel(int channel) { this->unsubscribe_events(); this->channel = channel; diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 1cb1a8a..e5a91fc 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -12,7 +12,6 @@ enum class MouseButton { }; enum class Keycode : int { - // From glfw3.h NONE = 0, SPACE = 32, APOSTROPHE = 39, /* ' */ |