translate key down to command event

This commit is contained in:
Frank DeMarco 2019-05-04 20:30:39 -04:00
parent 41dbfb8498
commit ebd8f34266
4 changed files with 31 additions and 8 deletions

View File

@ -4,8 +4,6 @@ Configuration::Configuration(Node *parent) : Configuration(parent, "config.json"
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
{
// std::cout << "Constructing Configuration with parent " << parent <<
// " and path " << path << std::endl;
config_path = path;
set_defaults();
load();
@ -15,8 +13,8 @@ Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
void Configuration::set_defaults()
{
sys_config["keys"] = {
{"record", {"CTRL", "SHIFT", "F10"}},
{"screenshot", "F9"},
{"record", {"CTRL", "SHIFT", "f10"}},
{"screenshot", "f9"},
{"action", " "},
{"up", "up"},
{"right", "right"},

View File

@ -1,8 +1,10 @@
#include "Delegate.hpp"
int Delegate::command_event_type = SDL_RegisterEvents(1);
Delegate::Delegate(Node *parent) : Node(parent) {}
void Delegate::add_subscriber(subscriber s, SDL_EventType type)
void Delegate::add_subscriber(subscriber s, int type)
{
if (subscribers.count(type) == 0)
{

View File

@ -14,10 +14,11 @@ typedef std::function<void(SDL_Event&)> subscriber;
struct Delegate : Node
{
std::map<SDL_EventType, std::list<subscriber>> subscribers;
std::map<int, std::list<subscriber>> subscribers;
static int command_event_type;
Delegate(Node*);
void add_subscriber(subscriber, SDL_EventType);
void add_subscriber(subscriber, int);
void dispatch();
};

View File

@ -4,6 +4,7 @@ Input::Input(Node *parent) : Node(parent)
{
load_key_map();
get_delegate()->add_subscriber(std::bind(&Input::respond, this, std::placeholders::_1), SDL_KEYDOWN);
get_delegate()->add_subscriber(std::bind(&Input::respond, this, std::placeholders::_1), Delegate::command_event_type);
for (KeyCombination& combination : key_map)
{
print_key_combination(combination);
@ -74,5 +75,26 @@ void Input::add_to_key_map(
void Input::respond(SDL_Event &event)
{
std::cout << event.key.keysym.sym << std::endl;
if (event.type == SDL_KEYDOWN)
{
std::cout << event.key.keysym.sym << std::endl;
SDL_Keymod mod = SDL_GetModState();
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))
{
SDL_Event event;
event.type = Delegate::command_event_type;
event.user.data1 = &combination.command;
SDL_PushEvent(&event);
}
}
}
else if (event.type == Delegate::command_event_type)
{
std::cout << event.type << " " << *((std::string*) event.user.data1) << std::endl;
}
}