any key command

This commit is contained in:
Frank DeMarco 2020-07-21 01:09:34 -04:00
parent 0ccc00f201
commit 8eeeb82f4c
7 changed files with 54 additions and 22 deletions

View File

@ -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", "."}

View File

@ -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 << "<KeyCombination " << combination.command << ", " <<
combination.key << ", ctrl " << combination.ctrl << ", shift " <<
@ -55,7 +57,7 @@ void Input::load_key_map()
}
}
int Input::get_key_code(std::string name)
int Input::get_key_code(const std::string& name)
{
if (key_ids.count(name) > 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<std::string>& system_any_key_ignore = get_configuration()["input"]["system-any-key-ignore-commands"];
const std::vector<std::string>& 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);
}

View File

@ -5,6 +5,7 @@
#include <map>
#include <list>
#include <functional>
#include <algorithm>
#include "SDL.h"
@ -44,14 +45,16 @@ struct Input : Node
{"space", SDLK_SPACE}
};
std::vector<KeyCombination> 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"; }
};

View File

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

View File

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

View File

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

View File

@ -31,6 +31,12 @@ namespace sfw
fs::path get_next_file_name(
fs::path, int = 0, std::string = "", std::string = "");
template<typename T1, typename T2>
bool is_in_container(T1& container, T2& member)
{
return std::find(container.begin(), container.end(), member) != container.end();
}
template<typename T>
std::string pad(T end, int width, char fill = '0')
{