diff --git a/src/Input.cpp b/src/Input.cpp index 08d11da..e6173cc 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -30,45 +30,60 @@ void Input::print_key_combination(const KeyCombination &combination) const void Input::load_key_map() { const nlohmann::json& config = configuration()(); - for (auto& entry : config.at("keys").items()) + for (const auto& [command, input] : config.at("keys").items()) { - bool ctrl = false, alt = false, shift = false; - int key; - if (not entry.value().is_string()) + if (input.is_array()) { - for (std::string part : entry.value()) + if (input[0].is_array()) { - if (part == "CTRL") + for (nlohmann::json version : input) { - ctrl = true; - } - else if (part == "SHIFT") - { - shift = true; - } - else if (part == "ALT") - { - alt = true; - } - else - { - key = get_key_code(part); + key_map.push_back(parse_key(command, version)); } } + else + { + key_map.push_back(parse_key(command, input)); + } } else { - key = get_key_code(entry.value()); + key_map.push_back((KeyCombination){command, get_key_code(input.get()), false, false, false}); } - add_to_key_map(entry.key(), key, ctrl, shift, alt); } } -int Input::get_key_code(const std::string& name) +KeyCombination Input::parse_key(const std::string& command, const nlohmann::json& json) const +{ + SDL_Keycode key_code; + bool ctrl = false, alt = false, shift = false; + for (const std::string& component : json) + { + if (component == "CTRL") + { + ctrl = true; + } + else if (component == "SHIFT") + { + shift = true; + } + else if (component == "ALT") + { + alt = true; + } + else + { + key_code = get_key_code(component); + } + } + return (KeyCombination){command, key_code, ctrl, shift, alt}; +} + +int Input::get_key_code(const std::string& name) const { if (key_ids.count(name) > 0) { - return key_ids[name]; + return key_ids.at(name); } else { diff --git a/src/Input.hpp b/src/Input.hpp index 32e4166..4d9f1ef 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -56,6 +56,8 @@ private: std::vector key_map; bool suppressed = false; + KeyCombination parse_key(const std::string& command, const nlohmann::json& json) const; + public: inline static std::string any = "any"; @@ -64,7 +66,7 @@ public: Input(Node*); void respond(SDL_Event&); void load_key_map(); - SDL_Keycode get_key_code(const std::string&); + SDL_Keycode get_key_code(const std::string&) const; void add_to_key_map(std::string, SDL_Keycode, bool = false, bool = false, bool = false); void print_key_combination(const KeyCombination&) const; void suppress();