From 8eeeb82f4c36af63f5bb7660c6b71cdcc00059e9 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Tue, 21 Jul 2020 01:09:34 -0400 Subject: [PATCH] any key command --- src/Configuration.cpp | 6 ++++++ src/Input.cpp | 45 +++++++++++++++++++++++++++++++------------ src/Input.hpp | 7 +++++-- src/Node.cpp | 6 +----- src/Node.hpp | 4 ++-- src/Sprite.cpp | 2 +- src/extension.hpp | 6 ++++++ 7 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/Configuration.cpp b/src/Configuration.cpp index bbab862..6df8f73 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -25,6 +25,12 @@ void Configuration::set_defaults() {"fullscreen", {"ALT", "enter"}}, {"toggle-framerate", {"CTRL", "f"}} }; + sys_config["input"] = { + {"suppress-any-key-on-mods", true}, + {"system-any-key-ignore-commands", + {"fullscreen", "screenshot", "toggle-framerate", "record", "quit"}}, + {"any-key-ignore-commands", {}} + }; sys_config["path"] = { {"screenshots", "."}, {"video", "."} diff --git a/src/Input.cpp b/src/Input.cpp index aa0ed6c..6592097 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -1,5 +1,7 @@ #include "Input.hpp" +std::string Input::any = "any"; + Input::Input(Node *parent) : Node(parent) { load_key_map(); @@ -11,7 +13,7 @@ Input::Input(Node *parent) : Node(parent) } } -void Input::print_key_combination(KeyCombination &combination) +void Input::print_key_combination(const KeyCombination &combination) const { std::cout << " 0) { @@ -76,19 +78,38 @@ void Input::add_to_key_map( void Input::respond(SDL_Event &event) { SDL_Keymod mod = SDL_GetModState(); + SDL_Keycode sym = event.key.keysym.sym; + bool found_command = false, cancel = event.type != SDL_KEYDOWN, ctrl = mod & KMOD_CTRL, + shift = mod & KMOD_SHIFT, alt = mod & KMOD_ALT, + suppress_any_key = get_configuration()["input"]["suppress-any-key-on-mods"] && (ctrl || alt); + const std::vector& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"]; + const std::vector& any_key_ignore = get_configuration()["input"]["any-key-ignore-commands"]; for (KeyCombination& combination : key_map) { - if (event.key.keysym.sym == combination.key and - (not combination.ctrl || mod & KMOD_CTRL) and - (not combination.shift || mod & KMOD_SHIFT) and - (not combination.alt || mod & KMOD_ALT)) + if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && + (!combination.alt || alt)) { - SDL_Event relay; - bool* cancel = new bool(event.type == SDL_KEYDOWN ? false : true); - relay.type = Delegate::command_event_type; - relay.user.data1 = &combination.command; - relay.user.data2 = cancel; - SDL_PushEvent(&relay); + post_command(combination.command, cancel); + if (!sfw::is_in_container(system_any_key_ignore, combination.command) && !found_command && + !sfw::is_in_container(any_key_ignore, combination.command) && !suppress_any_key) + { + post_command(any, cancel); + } + found_command = true; } } + if (!found_command && !suppress_any_key) + { + post_command(any, cancel); + } +} + +void Input::post_command(std::string& name, const bool& cancel) const +{ + SDL_Event relay; + bool* cancel_pointer = new bool(cancel); + relay.type = Delegate::command_event_type; + relay.user.data1 = &name; + relay.user.data2 = cancel_pointer; + SDL_PushEvent(&relay); } diff --git a/src/Input.hpp b/src/Input.hpp index 04e75d2..b12eab7 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "SDL.h" @@ -44,14 +45,16 @@ struct Input : Node {"space", SDLK_SPACE} }; std::vector key_map; + static std::string any; Input(Node*); void respond(SDL_Event&); void load_key_map(); - SDL_Keycode get_key_code(std::string); + 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(KeyCombination&); + void print_key_combination(const KeyCombination&) const; + void post_command(std::string&, const bool&) const; std::string get_class_name() { return "Input"; } }; diff --git a/src/Node.cpp b/src/Node.cpp index a1fe1fe..139a136 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -3,12 +3,8 @@ Node::Node() : Node(NULL) {} -Node::Node(Node *parent, bool active) : parent(parent) +Node::Node(Node *parent) : parent(parent) { - if (active) - { - activate(); - } std::cout << "Constructing "; print_branch(); } diff --git a/src/Node.hpp b/src/Node.hpp index 0edab5f..f584fc2 100644 --- a/src/Node.hpp +++ b/src/Node.hpp @@ -16,10 +16,10 @@ struct Node { Node *parent; - bool active; + bool active = true; Node(); - Node(Node*, bool=true); + Node(Node*); void set_parent(Node*); void activate(); void deactivate(); diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 8fd914a..bed6af5 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -4,7 +4,7 @@ Sprite::Sprite() : Sprite(NULL) {} Sprite::Sprite(Node* parent) : - Node(parent, true), current_frameset_name(get_configuration()["animation"]["all-frames-frameset-name"]) + Node(parent), current_frameset_name(get_configuration()["animation"]["all-frames-frameset-name"]) { add_frameset(current_frameset_name); frame_animation.play(); diff --git a/src/extension.hpp b/src/extension.hpp index 1e6ee57..7ed52b7 100644 --- a/src/extension.hpp +++ b/src/extension.hpp @@ -31,6 +31,12 @@ namespace sfw fs::path get_next_file_name( fs::path, int = 0, std::string = "", std::string = ""); + template + bool is_in_container(T1& container, T2& member) + { + return std::find(container.begin(), container.end(), member) != container.end(); + } + template std::string pad(T end, int width, char fill = '0') {