From 41dbfb849803657863247d2c7af27ef0dd8073ba Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Sat, 4 May 2019 03:25:35 -0400 Subject: [PATCH] key map --- demo/Demo.cpp | 6 ++-- demo/{config => config.json} | 1 + src/Configuration.cpp | 36 ++++++++++++-------- src/Configuration.hpp | 2 ++ src/Delegate.cpp | 2 +- src/Delegate.hpp | 2 +- src/Game.cpp | 4 +-- src/Game.hpp | 2 +- src/Input.cpp | 64 ++++++++++++++++++++++++++++++++++-- src/Input.hpp | 14 +++++--- src/Node.cpp | 4 +-- src/Node.hpp | 6 ++-- 12 files changed, 110 insertions(+), 33 deletions(-) rename demo/{config => config.json} (77%) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index a1a002d..5368e60 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -179,8 +179,6 @@ struct Demo : Game Input *input = new Input(this); input->print_branch(); mushroom->print_branch(); - // Game* root = get_root(); - // std::cout << root << std::endl; } std::string get_class_name() @@ -343,6 +341,10 @@ struct Demo : Game get_framerate_indicator_surface(frame_count), GL_LINEAR); } + void respond(SDL_Event& event) + { + } + void update() { // while (SDL_PollEvent(&event)) diff --git a/demo/config b/demo/config.json similarity index 77% rename from demo/config rename to demo/config.json index 59faf0b..6812142 100644 --- a/demo/config +++ b/demo/config.json @@ -10,5 +10,6 @@ "keys": { + "screenshot": ["CTRL", "s"] } } diff --git a/src/Configuration.cpp b/src/Configuration.cpp index d99b3a5..dfd874a 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -1,6 +1,6 @@ #include "Configuration.hpp" -Configuration::Configuration(Node *parent) : Configuration(parent, "config") {} +Configuration::Configuration(Node *parent) : Configuration(parent, "config.json") {} Configuration::Configuration(Node *parent, fs::path path) : Node(parent) { @@ -9,15 +9,20 @@ Configuration::Configuration(Node *parent, fs::path path) : Node(parent) config_path = path; set_defaults(); load(); + merge(); } void Configuration::set_defaults() { sys_config["keys"] = { {"record", {"CTRL", "SHIFT", "F10"}}, - {"screenshot", "F9"} + {"screenshot", "F9"}, + {"action", " "}, + {"up", "up"}, + {"right", "right"}, + {"down", "down"}, + {"left", "left"} }; - config = sys_config; } void Configuration::load() @@ -27,16 +32,21 @@ void Configuration::load() void Configuration::load(fs::path path) { - std::ifstream contents(path); - contents >> game_config; - // config["blob"] = false; - // config["slime"][1] = {420, 69}; - // config["goo"] = "yum"; - // std::cout << std::setw(4) << config << std::endl; - // std::cout << std::setw(4) << sys_config << std::endl; - config.update(game_config); + if (fs::exists(path)) + { + std::ifstream contents(path); + contents >> game_config; + } +} + +void Configuration::merge() +{ + config = sys_config; + if (not game_config.empty()) + { + config["keys"].update(game_config["keys"]); + } std::cout << std::setw(4) << config << std::endl; - std::cout << std::setw(4) << game_config << std::endl; } void Configuration::write() @@ -47,5 +57,5 @@ void Configuration::write() void Configuration::write(fs::path path) { std::ofstream output(path); - output << std::setw(tab_width) << config << std::endl; + output << std::setw(tab_width) << game_config << std::endl; } diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 43d28bb..6dbe277 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -8,6 +8,7 @@ #include "json/json.hpp" +#include "filesystem.hpp" #include "Node.hpp" struct Configuration : Node @@ -21,6 +22,7 @@ struct Configuration : Node void set_defaults(); void load(); void load(fs::path path); + void merge(); void write(); void write(fs::path path); std::string get_class_name() { return "Configuration"; } diff --git a/src/Delegate.cpp b/src/Delegate.cpp index 1d7c71e..d7a3fea 100644 --- a/src/Delegate.cpp +++ b/src/Delegate.cpp @@ -22,7 +22,7 @@ void Delegate::dispatch() { for (subscriber s : iter->second) { - s(&event); + s(event); } } } diff --git a/src/Delegate.hpp b/src/Delegate.hpp index b27806f..a6fda19 100644 --- a/src/Delegate.hpp +++ b/src/Delegate.hpp @@ -9,7 +9,7 @@ #include "SDL.h" -typedef std::function subscriber; +typedef std::function subscriber; struct Delegate : Node { diff --git a/src/Game.cpp b/src/Game.cpp index b345438..cabcde4 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -152,9 +152,9 @@ void Game::set_framerate(int fps) frame_length = 1000.0 / framerate; } -void Game::handle_quit_event(SDL_Event *event) +void Game::handle_quit_event(SDL_Event &event) { - if (event->type == SDL_QUIT) + if (event.type == SDL_QUIT) { flag_to_end(); } diff --git a/src/Game.hpp b/src/Game.hpp index 5491ee2..c77a595 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -48,7 +48,7 @@ struct Game : Node void flag_to_end(); virtual void update() {}; void set_framerate(int); - void handle_quit_event(SDL_Event*); + void handle_quit_event(SDL_Event&); void quit(); std::string get_class_name() { return "Game"; } diff --git a/src/Input.cpp b/src/Input.cpp index 1d27eb4..bcb69c4 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -4,17 +4,75 @@ Input::Input(Node *parent) : Node(parent) { load_key_map(); get_delegate()->add_subscriber(std::bind(&Input::respond, this, std::placeholders::_1), SDL_KEYDOWN); + for (KeyCombination& combination : key_map) + { + print_key_combination(combination); + } +} + +void Input::print_key_combination(KeyCombination &combination) +{ + std::cout << "" << std::endl; } void Input::load_key_map() { + nlohmann::json &config = get_configuration(); + for (auto& entry : config.at("keys").items()) + { + bool ctrl = false, alt = false, shift = false; + int key; + if (not entry.value().is_string()) + { + for (std::string part : entry.value()) + { + if (part == "CTRL") + { + ctrl = true; + } + else if (part == "SHIFT") + { + shift = true; + } + else if (part == "ALT") + { + alt = true; + } + else + { + key = get_key_code(part); + } + } + } + else + { + key = get_key_code(entry.value()); + } + add_to_key_map(entry.key(), key, ctrl, shift, alt); + } } -void Input::add_to_key_map() +int Input::get_key_code(std::string name) { + if (key_ids.count(name) > 0) + { + return key_ids[name]; + } + else + { + return (SDL_Keycode) name[0]; + } } -void Input::respond(SDL_Event *event) +void Input::add_to_key_map( + std::string command, SDL_Keycode key_code, bool ctrl, bool shift, bool alt) { - std::cout << event->key.keysym.sym << std::endl; + key_map.push_back((KeyCombination){command, key_code, ctrl, shift, alt}); +} + +void Input::respond(SDL_Event &event) +{ + std::cout << event.key.keysym.sym << std::endl; } diff --git a/src/Input.hpp b/src/Input.hpp index ef5df49..3101b09 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -11,16 +11,17 @@ struct KeyCombination { - bool alt; + std::string command; + int key; bool ctrl; bool shift; - unsigned int key; + bool alt; }; struct Input : Node { - std::map key_ids + std::map key_ids { {"up", SDLK_UP}, {"right", SDLK_RIGHT}, @@ -42,10 +43,13 @@ struct Input : Node std::list key_map; Input(Node*); - void respond(SDL_Event*); + void respond(SDL_Event&); std::string get_class_name() { return "Input"; } void load_key_map(); - void add_to_key_map(); + SDL_Keycode get_key_code(std::string); + void add_to_key_map( + std::string, SDL_Keycode, bool = false, bool = false, bool = false); + void print_key_combination(KeyCombination&); }; diff --git a/src/Node.cpp b/src/Node.cpp index bdba8e6..d857479 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -9,9 +9,9 @@ Node::Node(Node *parent) : parent(parent) print_branch(); } -Configuration* Node::get_configuration() +nlohmann::json& Node::get_configuration() { - return get_root()->configuration; + return get_root()->configuration->config; } Delegate* Node::get_delegate() diff --git a/src/Node.hpp b/src/Node.hpp index 458c643..d2967ff 100644 --- a/src/Node.hpp +++ b/src/Node.hpp @@ -3,12 +3,12 @@ #include +#include "json/json.hpp" #include "SDL.h" #include "filesystem.hpp" struct Game; -struct Configuration; struct Delegate; struct Node @@ -18,8 +18,8 @@ struct Node Node(); Node(Node*); - Game *get_root(); - Configuration* get_configuration(); + Game* get_root(); + nlohmann::json& get_configuration(); Delegate* get_delegate(); void print_branch(); virtual std::string get_class_name() { return "Node"; };