any key command
This commit is contained in:
parent
0ccc00f201
commit
8eeeb82f4c
|
@ -25,6 +25,12 @@ void Configuration::set_defaults()
|
||||||
{"fullscreen", {"ALT", "enter"}},
|
{"fullscreen", {"ALT", "enter"}},
|
||||||
{"toggle-framerate", {"CTRL", "f"}}
|
{"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"] = {
|
sys_config["path"] = {
|
||||||
{"screenshots", "."},
|
{"screenshots", "."},
|
||||||
{"video", "."}
|
{"video", "."}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "Input.hpp"
|
#include "Input.hpp"
|
||||||
|
|
||||||
|
std::string Input::any = "any";
|
||||||
|
|
||||||
Input::Input(Node *parent) : Node(parent)
|
Input::Input(Node *parent) : Node(parent)
|
||||||
{
|
{
|
||||||
load_key_map();
|
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 << ", " <<
|
std::cout << "<KeyCombination " << combination.command << ", " <<
|
||||||
combination.key << ", ctrl " << combination.ctrl << ", shift " <<
|
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)
|
if (key_ids.count(name) > 0)
|
||||||
{
|
{
|
||||||
|
@ -76,19 +78,38 @@ void Input::add_to_key_map(
|
||||||
void Input::respond(SDL_Event &event)
|
void Input::respond(SDL_Event &event)
|
||||||
{
|
{
|
||||||
SDL_Keymod mod = SDL_GetModState();
|
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)
|
for (KeyCombination& combination : key_map)
|
||||||
{
|
{
|
||||||
if (event.key.keysym.sym == combination.key and
|
if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) &&
|
||||||
(not combination.ctrl || mod & KMOD_CTRL) and
|
(!combination.alt || alt))
|
||||||
(not combination.shift || mod & KMOD_SHIFT) and
|
|
||||||
(not combination.alt || mod & KMOD_ALT))
|
|
||||||
{
|
{
|
||||||
SDL_Event relay;
|
post_command(combination.command, cancel);
|
||||||
bool* cancel = new bool(event.type == SDL_KEYDOWN ? false : true);
|
if (!sfw::is_in_container(system_any_key_ignore, combination.command) && !found_command &&
|
||||||
relay.type = Delegate::command_event_type;
|
!sfw::is_in_container(any_key_ignore, combination.command) && !suppress_any_key)
|
||||||
relay.user.data1 = &combination.command;
|
{
|
||||||
relay.user.data2 = cancel;
|
post_command(any, cancel);
|
||||||
SDL_PushEvent(&relay);
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
|
@ -44,14 +45,16 @@ struct Input : Node
|
||||||
{"space", SDLK_SPACE}
|
{"space", SDLK_SPACE}
|
||||||
};
|
};
|
||||||
std::vector<KeyCombination> key_map;
|
std::vector<KeyCombination> key_map;
|
||||||
|
static std::string any;
|
||||||
|
|
||||||
Input(Node*);
|
Input(Node*);
|
||||||
void respond(SDL_Event&);
|
void respond(SDL_Event&);
|
||||||
void load_key_map();
|
void load_key_map();
|
||||||
SDL_Keycode get_key_code(std::string);
|
SDL_Keycode get_key_code(const std::string&);
|
||||||
void add_to_key_map(
|
void add_to_key_map(
|
||||||
std::string, SDL_Keycode, bool = false, bool = false, bool = false);
|
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"; }
|
std::string get_class_name() { return "Input"; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,12 +3,8 @@
|
||||||
|
|
||||||
Node::Node() : Node(NULL) {}
|
Node::Node() : Node(NULL) {}
|
||||||
|
|
||||||
Node::Node(Node *parent, bool active) : parent(parent)
|
Node::Node(Node *parent) : parent(parent)
|
||||||
{
|
{
|
||||||
if (active)
|
|
||||||
{
|
|
||||||
activate();
|
|
||||||
}
|
|
||||||
std::cout << "Constructing ";
|
std::cout << "Constructing ";
|
||||||
print_branch();
|
print_branch();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,10 @@ struct Node
|
||||||
{
|
{
|
||||||
|
|
||||||
Node *parent;
|
Node *parent;
|
||||||
bool active;
|
bool active = true;
|
||||||
|
|
||||||
Node();
|
Node();
|
||||||
Node(Node*, bool=true);
|
Node(Node*);
|
||||||
void set_parent(Node*);
|
void set_parent(Node*);
|
||||||
void activate();
|
void activate();
|
||||||
void deactivate();
|
void deactivate();
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
Sprite::Sprite() : Sprite(NULL) {}
|
Sprite::Sprite() : Sprite(NULL) {}
|
||||||
|
|
||||||
Sprite::Sprite(Node* parent) :
|
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);
|
add_frameset(current_frameset_name);
|
||||||
frame_animation.play();
|
frame_animation.play();
|
||||||
|
|
|
@ -31,6 +31,12 @@ namespace sfw
|
||||||
fs::path get_next_file_name(
|
fs::path get_next_file_name(
|
||||||
fs::path, int = 0, std::string = "", std::string = "");
|
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>
|
template<typename T>
|
||||||
std::string pad(T end, int width, char fill = '0')
|
std::string pad(T end, int width, char fill = '0')
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue