From 27d2128e1f8bc815c2fc600cfa2a2e26bff50a0f Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 19 Jul 2023 12:47:18 -0400 Subject: [PATCH] move post command into delegate --- src/Delegate.cpp | 12 ++++++++++++ src/Delegate.hpp | 12 +++++++++--- src/Input.cpp | 16 +++------------- src/Input.hpp | 1 - 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Delegate.cpp b/src/Delegate.cpp index 40354fa..244f7d3 100644 --- a/src/Delegate.cpp +++ b/src/Delegate.cpp @@ -54,6 +54,7 @@ void Delegate::dispatch() } if (event.type == command_event_type) { + delete static_cast(event.user.data1); delete static_cast(event.user.data2); } } @@ -102,3 +103,14 @@ bool Delegate::get_event_cancel_state(SDL_Event& event) { return *static_cast(event.user.data2); } + +void Delegate::post(const std::string& command, bool cancel) +{ + SDL_Event relay; + bool* cancel_pointer = new bool(cancel); + std::string* command_pointer = new std::string(command); + relay.type = command_event_type; + relay.user.data1 = command_pointer; + relay.user.data2 = cancel_pointer; + SDL_PushEvent(&relay); +} diff --git a/src/Delegate.hpp b/src/Delegate.hpp index 8a032c2..39ac269 100644 --- a/src/Delegate.hpp +++ b/src/Delegate.hpp @@ -50,6 +50,15 @@ namespace sb static const std::string& get_event_command(SDL_Event&); static bool get_event_cancel_state(SDL_Event&); + /*! + * Post a custom command to the queue. There is not currently support for passing custom data along with the command. The string + * and bool arguments will be copied onto the heap and deleted when the event is retrieved from the queue. + * + * @param command name of the custom command + * @param cancel whether or not the event is a cancellation + */ + static void post(const std::string& command, bool cancel = false); + /*! * Subscribe a sb::Node or extention of sb::Node object's member function to receive a reference to an SDL_Event whenever * an event of the specified type is triggered. The type can be an SDL event type from https://wiki.libsdl.org/SDL2/SDL_EventType, @@ -61,9 +70,6 @@ namespace sb * events that are mapped to keys in an sb::Configuration object's "keys" field. For example, by default `ALT+enter` is configured * to send an event named `fullscreen`. Using a JSON file, `z` could be mapped to an event named `jump`. * - * Currently, there is no method in the framework for sending custom sb::Delegate::command_event_type events, but they can be - * posted using `SDL_Event` and `SDL_PushEvent`. - * * The subscribing object must be unsubscribed with Delegate::unsubscribe(const Type*) before it is destroyed, otherwise it will * be null referenced. * diff --git a/src/Input.cpp b/src/Input.cpp index ade2680..08d11da 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -99,33 +99,23 @@ void Input::respond(SDL_Event &event) { if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && (!combination.alt || alt)) { - post_command(combination.command, cancel); + sb::Delegate::post(combination.command, cancel); if (!sb::is_in_container(system_any_key_ignore, combination.command) && !found_command && !sb::is_in_container(any_key_ignore, combination.command) && !suppress_any_key) { - post_command(any, cancel); + sb::Delegate::post(any, cancel); } found_command = true; } } if (!found_command && !suppress_any_key) { - post_command(any, cancel); + sb::Delegate::post(any, cancel); } } } } -void Input::post_command(std::string& name, const bool& cancel) -{ - SDL_Event relay; - bool* cancel_pointer = new bool(cancel); - relay.type = sb::Delegate::command_event_type; - relay.user.data1 = &name; - relay.user.data2 = cancel_pointer; - SDL_PushEvent(&relay); -} - void Input::suppress() { suppressed = true; diff --git a/src/Input.hpp b/src/Input.hpp index 42a9f1a..32e4166 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -67,7 +67,6 @@ public: SDL_Keycode get_key_code(const std::string&); void add_to_key_map(std::string, SDL_Keycode, bool = false, bool = false, bool = false); void print_key_combination(const KeyCombination&) const; - static void post_command(std::string&, const bool&); void suppress(); void unsuppress(); bool is_suppressed();