From f1ba2a5607f23ed0b23d74240ea66cb1f8d2c1ad Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 13 Nov 2024 12:28:04 +0100 Subject: tested channel and deactivating in function --- src/crepe/api/IKeyListener.cpp | 3 +++ src/crepe/api/IMouseListener.cpp | 6 ++++-- src/crepe/api/IMouseListener.h | 4 ++-- src/example/events.cpp | 9 +++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/crepe/api/IKeyListener.cpp b/src/crepe/api/IKeyListener.cpp index 17d8735..e759eca 100644 --- a/src/crepe/api/IKeyListener.cpp +++ b/src/crepe/api/IKeyListener.cpp @@ -41,5 +41,8 @@ void IKeyListener::deactivate_keys() { this->unsubscribe_events(); } void IKeyListener::set_channel(int channel){ + this->unsubscribe_events(); this->channel = channel; + this->subscribe_events(); + } diff --git a/src/crepe/api/IMouseListener.cpp b/src/crepe/api/IMouseListener.cpp index 68061be..c6ea419 100644 --- a/src/crepe/api/IMouseListener.cpp +++ b/src/crepe/api/IMouseListener.cpp @@ -35,18 +35,20 @@ void IMouseListener::unsubscribe_events() { EventManager::get_instance().unsubscribe(this->mouse_release_handler, this->channel); EventManager::get_instance().unsubscribe(this->mouse_move_handler, this->channel); } -void IMouseListener::activate_keys() { +void IMouseListener::activate_mouse() { if(this->active){ return; } this->subscribe_events(); } -void IMouseListener::deactivate_keys() { +void IMouseListener::deactivate_mouse() { if(!this->active){ return; } this->unsubscribe_events(); } void IMouseListener::set_channel(int channel){ + this->unsubscribe_events(); this->channel = channel; + this->subscribe_events(); } diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h index 4493e7f..a7ad271 100644 --- a/src/crepe/api/IMouseListener.h +++ b/src/crepe/api/IMouseListener.h @@ -77,12 +77,12 @@ public: /** * \brief Activates mouse listening. */ - void activate_keys(); + void activate_mouse(); /** * \brief Deactivates mouse listening. */ - void deactivate_keys(); + void deactivate_mouse(); /** * \brief Sets the channel ID for event handling. diff --git a/src/example/events.cpp b/src/example/events.cpp index af5f747..ff97cf1 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -25,6 +25,7 @@ class MyScript : public Script, public IKeyListener,public IMouseListener{ bool on_key_pressed(const KeyPressEvent & event) override{ std::cout << "KeyPressed function" << std::endl; + this->deactivate_keys(); return false; } bool on_key_released(const KeyReleaseEvent & event) override{ @@ -74,7 +75,7 @@ int main() { EventManager::get_instance().queue_event(std::move(click_event), 0); { TestKeyListener testListener; - + testListener.set_channel(1); auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); obj.add_component().set_script(); @@ -82,8 +83,8 @@ int main() { sys.update(); // Trigger the events while `testListener` is in scope - EventManager::get_instance().trigger_event(key_press, 0); - EventManager::get_instance().trigger_event(click_event, 0); + EventManager::get_instance().trigger_event(key_press, 1); + EventManager::get_instance().trigger_event(click_event, 1); } // custom lambda event handler EventHandler event_handler = [](const KeyPressEvent& e) { @@ -96,7 +97,7 @@ int main() { EventManager::get_instance().trigger_event(click_event, 0); // dispatching queued events EventManager::get_instance().dispatch_events(); - + EventManager::get_instance().unsubscribe(event_handler,0); return EXIT_SUCCESS; } -- cgit v1.2.3