From 71a614e1f87141c4d062a2a466377322fc3d9ff0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 18 Nov 2024 15:40:11 +0100 Subject: make format --- mwe/events/include/event.h | 12 ++++-------- src/crepe/api/EventHandler.hpp | 3 +-- src/crepe/api/EventManager.cpp | 15 +++++++-------- src/crepe/api/EventManager.h | 9 +++------ src/crepe/api/EventManager.hpp | 22 ++++++++++----------- src/crepe/api/IKeyListener.cpp | 20 ++++++++------------ src/crepe/api/IKeyListener.h | 2 +- src/crepe/api/IMouseListener.cpp | 41 +++++++++++++++------------------------- src/crepe/api/IMouseListener.h | 1 - src/example/events.cpp | 17 +++++++---------- 10 files changed, 56 insertions(+), 86 deletions(-) diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index 16c75bf..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -20,9 +20,7 @@ public: \ static std::uint32_t typeID = UUIDGenerator::getUniqueID(); \ return typeID; \ } \ - virtual std::uint32_t getEventType() const override { \ - return getStaticEventType(); \ - } + virtual std::uint32_t getEventType() const override { return getStaticEventType(); } class Event { public: Event(std::string eventType); @@ -32,16 +30,14 @@ public: void addArgument(const std::string & key, const std::variant & value); - std::variant - getArgument(const std::string & key) const; + std::variant getArgument(const std::string & key) const; std::string getType() const; bool getHandled() const; void markHandled(); private: - std::unordered_map> - eventData; + std::unordered_map> eventData; bool isHandled = false; }; @@ -152,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/EventHandler.hpp b/src/crepe/api/EventHandler.hpp index 564d3d7..9c47da2 100644 --- a/src/crepe/api/EventHandler.hpp +++ b/src/crepe/api/EventHandler.hpp @@ -7,8 +7,7 @@ namespace crepe { // Implementation of EventHandlerWrapper constructor template -EventHandlerWrapper::EventHandlerWrapper( - const EventHandler & handler) +EventHandlerWrapper::EventHandlerWrapper(const EventHandler & handler) : m_handler(handler), m_handler_type(m_handler.target_type().name()) {} diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index 7f47938..b465e89 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -8,12 +8,11 @@ EventManager & EventManager::get_instance() { } void EventManager::dispatch_events() { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; - for (auto event_it = this->events_queue.begin(); - event_it != this->events_queue.end();) { + for (auto event_it = this->events_queue.begin(); event_it != this->events_queue.end();) { std::unique_ptr & event = std::get<0>(*event_it); int channel = std::get<1>(*event_it); std::type_index event_type = std::get<2>(*event_it); @@ -27,8 +26,8 @@ void EventManager::dispatch_events() { 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) { + 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; @@ -42,8 +41,8 @@ void EventManager::dispatch_events() { 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) { + 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); diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h index 783db62..c7c4744 100644 --- a/src/crepe/api/EventManager.h +++ b/src/crepe/api/EventManager.h @@ -94,17 +94,14 @@ private: EventManager() = default; //! The queue of events to be processed. - std::vector, int, std::type_index>> - events_queue; + std::vector, int, std::type_index>> events_queue; //! Registered event handlers. - std::unordered_map>> + std::unordered_map>> subscribers; //! Event handlers indexed by event ID. std::unordered_map< std::type_index, - std::unordered_map>>> + std::unordered_map>>> subscribers_by_event_id; }; diff --git a/src/crepe/api/EventManager.hpp b/src/crepe/api/EventManager.hpp index d901492..b20b88f 100644 --- a/src/crepe/api/EventManager.hpp +++ b/src/crepe/api/EventManager.hpp @@ -4,8 +4,8 @@ namespace crepe { template void EventManager::subscribe(EventHandler && callback, int channel) { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type = typeid(EventType); @@ -30,18 +30,17 @@ template void EventManager::queue_event(EventType && event, int channel) { std::type_index event_type = std::type_index(typeid(EventType)); - auto event_ptr - = std::make_unique(std::forward(event)); + auto event_ptr = std::make_unique(std::forward(event)); - std::tuple, int, std::type_index> tuple( - std::move(event_ptr), channel, event_type); + std::tuple, int, std::type_index> tuple(std::move(event_ptr), + channel, event_type); this->events_queue.push_back(std::move(tuple)); } template void EventManager::trigger_event(const EventType & event, int channel) { - using HandlersMap = std::unordered_map< - int, std::vector>>; + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type = std::type_index(typeid(EventType)); @@ -71,10 +70,9 @@ void EventManager::trigger_event(const EventType & event, int channel) { } template -void EventManager::unsubscribe(const EventHandler & callback, - int channel) { - using HandlersMap = std::unordered_map< - int, std::vector>>; +void EventManager::unsubscribe(const EventHandler & callback, int channel) { + using HandlersMap + = std::unordered_map>>; using HandlersVec = std::vector>; std::type_index event_type(typeid(EventType)); diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index cd255df..f5426be 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -23,25 +23,21 @@ IKeyListener::~IKeyListener() { this->unsubscribe_events(); } // Subscribe to key events void IKeyListener::subscribe_events() { - key_pressed_handler = [this](const KeyPressEvent & event) { - return this->on_key_pressed(event); - }; - key_released_handler = [this](const KeyReleaseEvent & event) { - return this->on_key_released(event); - }; + key_pressed_handler + = [this](const KeyPressEvent & event) { return this->on_key_pressed(event); }; + key_released_handler + = [this](const KeyReleaseEvent & event) { return this->on_key_released(event); }; event_manager.subscribe(std::move(this->key_pressed_handler), this->channel); - event_manager.subscribe( - std::move(this->key_released_handler), this->channel); + event_manager.subscribe(std::move(this->key_released_handler), + this->channel); } // Unsubscribe from key events void IKeyListener::unsubscribe_events() { - event_manager.unsubscribe(this->key_pressed_handler, - this->channel); - event_manager.unsubscribe(this->key_released_handler, - this->channel); + event_manager.unsubscribe(this->key_pressed_handler, this->channel); + event_manager.unsubscribe(this->key_released_handler, this->channel); } // Activate key listening diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h index 3e0e2cb..d492387 100644 --- a/src/crepe/api/IKeyListener.h +++ b/src/crepe/api/IKeyListener.h @@ -22,7 +22,7 @@ public: IKeyListener(const IKeyListener &) = delete; IKeyListener & operator=(const IKeyListener &) = delete; IKeyListener(IKeyListener &&) = delete; - + /** * \brief Pure virtual function to handle key press events. * \param event The key press event to handle. diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index bfa49f8..5ee2814 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -16,40 +16,29 @@ IMouseListener::~IMouseListener() { this->unsubscribe_events(); } void IMouseListener::subscribe_events() { // Define handler lambdas and subscribe them - mouse_click_handler = [this](const MouseClickEvent & event) { - return this->on_mouse_clicked(event); - }; - mouse_press_handler = [this](const MousePressEvent & event) { - return this->on_mouse_pressed(event); - }; - mouse_release_handler = [this](const MouseReleaseEvent & event) { - return this->on_mouse_released(event); - }; - mouse_move_handler = [this](const MouseMoveEvent & event) { - return this->on_mouse_moved(event); - }; + mouse_click_handler + = [this](const MouseClickEvent & event) { return this->on_mouse_clicked(event); }; + mouse_press_handler + = [this](const MousePressEvent & event) { return this->on_mouse_pressed(event); }; + mouse_release_handler + = [this](const MouseReleaseEvent & event) { return this->on_mouse_released(event); }; + mouse_move_handler + = [this](const MouseMoveEvent & event) { return this->on_mouse_moved(event); }; // Subscribe event handlers (no need for std::move) - event_manager.subscribe(std::move(mouse_click_handler), - this->channel); - event_manager.subscribe(std::move(mouse_press_handler), - this->channel); + event_manager.subscribe(std::move(mouse_click_handler), this->channel); + event_manager.subscribe(std::move(mouse_press_handler), this->channel); event_manager.subscribe(std::move(mouse_release_handler), this->channel); - event_manager.subscribe(std::move(mouse_move_handler), - this->channel); + event_manager.subscribe(std::move(mouse_move_handler), this->channel); } void IMouseListener::unsubscribe_events() { // Unsubscribe event handlers - event_manager.unsubscribe(mouse_click_handler, - this->channel); - event_manager.unsubscribe(mouse_press_handler, - this->channel); - event_manager.unsubscribe(mouse_release_handler, - this->channel); - event_manager.unsubscribe(mouse_move_handler, - this->channel); + event_manager.unsubscribe(mouse_click_handler, this->channel); + event_manager.unsubscribe(mouse_press_handler, this->channel); + event_manager.unsubscribe(mouse_release_handler, this->channel); + event_manager.unsubscribe(mouse_move_handler, this->channel); } void IMouseListener::activate_mouse() { diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 2ec156f..c315c35 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -12,7 +12,6 @@ namespace crepe { */ class IMouseListener { public: - IMouseListener(); /** * \brief Constructs an IMouseListener with a specified channel. diff --git a/src/example/events.cpp b/src/example/events.cpp index 5a0a748..6431c67 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -54,13 +54,13 @@ class MyScript : public Script, public IKeyListener, public IMouseListener { class TestKeyListener : public IKeyListener { public: bool on_key_pressed(const KeyPressEvent & event) override { - std::cout << "TestKeyListener: Key Pressed - Code: " - << static_cast(event.key) << std::endl; + std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast(event.key) + << std::endl; return true; // Return true if the listener should remain active } bool on_key_released(const KeyReleaseEvent & event) override { - std::cout << "TestKeyListener: Key Released - Code: " - << static_cast(event.key) << std::endl; + std::cout << "TestKeyListener: Key Released - Code: " << static_cast(event.key) + << std::endl; return true; } }; @@ -74,10 +74,8 @@ int main() { click_event.mouse_x = 100; click_event.mouse_y = 200; // queue events to test queue - EventManager::get_instance().queue_event( - std::move(key_press), 0); - EventManager::get_instance().queue_event( - std::move(click_event), 0); + EventManager::get_instance().queue_event(std::move(key_press), 0); + EventManager::get_instance().queue_event(std::move(click_event), 0); { TestKeyListener test_listener; test_listener.set_channel(1); @@ -102,8 +100,7 @@ int main() { std::cout << "lambda test" << std::endl; return false; }; - EventManager::get_instance().subscribe( - std::move(event_handler), 0); + EventManager::get_instance().subscribe(std::move(event_handler), 0); // testing trigger with testListener not in scope (unsubscribed) EventManager::get_instance().trigger_event(key_press, 0); EventManager::get_instance().trigger_event(click_event, 0); -- cgit v1.2.3