move post command into delegate

This commit is contained in:
ohsqueezy 2023-07-19 12:47:18 -04:00
parent cd66f70d0c
commit 27d2128e1f
4 changed files with 24 additions and 17 deletions

View File

@ -54,6 +54,7 @@ void Delegate::dispatch()
}
if (event.type == command_event_type)
{
delete static_cast<std::string*>(event.user.data1);
delete static_cast<bool*>(event.user.data2);
}
}
@ -102,3 +103,14 @@ bool Delegate::get_event_cancel_state(SDL_Event& event)
{
return *static_cast<bool*>(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);
}

View File

@ -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.
*

View File

@ -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;

View File

@ -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();