diff --git a/src/window/detail/input_sdl.cpp b/src/window/detail/input_sdl.cpp index 673b1d690..bbc825286 100644 --- a/src/window/detail/input_sdl.cpp +++ b/src/window/detail/input_sdl.cpp @@ -17,6 +17,79 @@ static debug::Logger logger("input"); +static std::unordered_map keycodes { + {"enter", SDL_SCANCODE_RETURN}, + {"space", SDL_SCANCODE_SPACE}, + {"backspace", SDL_SCANCODE_BACKSPACE}, + {"caps-lock", SDL_SCANCODE_CAPSLOCK}, + {"escape", SDL_SCANCODE_ESCAPE}, + {"delete", SDL_SCANCODE_DELETE}, + {"home", SDL_SCANCODE_HOME}, + {"end", SDL_SCANCODE_END}, + {"tab", SDL_SCANCODE_TAB}, + {"insert", SDL_SCANCODE_INSERT}, + {"page-down", SDL_SCANCODE_PAGEDOWN}, + {"page-up", SDL_SCANCODE_PAGEUP}, + {"left-shift", SDL_SCANCODE_LSHIFT}, + {"right-shift", SDL_SCANCODE_RSHIFT}, + {"left-ctrl", SDL_SCANCODE_LCTRL}, + {"right-ctrl", SDL_SCANCODE_RCTRL}, + {"left-alt", SDL_SCANCODE_LALT}, + {"right-alt", SDL_SCANCODE_RALT}, + {"left-super", SDL_SCANCODE_LGUI}, + {"right-super", SDL_SCANCODE_RGUI}, + {"grave-accent", SDL_SCANCODE_GRAVE}, + {"left", SDL_SCANCODE_LEFT}, + {"right", SDL_SCANCODE_RIGHT}, + {"down", SDL_SCANCODE_DOWN}, + {"up", SDL_SCANCODE_UP}, +}; +static std::unordered_map mousecodes { + {"left", SDL_BUTTON_LEFT}, + {"right", SDL_BUTTON_RIGHT}, + {"middle", SDL_BUTTON_MIDDLE}, + {"side1", SDL_BUTTON_X1}, + {"side2", SDL_BUTTON_X2}, +}; + +static std::unordered_map keynames {}; +static std::unordered_map buttonsnames {}; + +std::string input_util::get_name(Mousecode code) { + const auto found = buttonsnames.find(static_cast(code)); + if (found == buttonsnames.end()) { + return "unknown"; + } + return found->second; +} + +std::string input_util::get_name(Keycode code) { + const auto found = keynames.find(static_cast(code)); + if (found == keynames.end()) { + return "unknown"; + } + return found->second; +} + +void input_util::initialize() { + keycodes[std::to_string(0)] = SDL_SCANCODE_0; + for (int i = 1; i <= 9; i++) { + keycodes[std::to_string(i)] = SDL_SCANCODE_1 + i; + } + for (int i = 0; i < 25; i++) { + keycodes["f" + std::to_string(i + 1)] = SDL_SCANCODE_F1 + i; + } + for (char i = 'a'; i <= 'z'; i++) { + keycodes[std::string({i})] = SDL_SCANCODE_A - 'a' + i; + } + for (const auto& entry : keycodes) { + keynames[entry.second] = entry.first; + } + for (const auto& entry : mousecodes) { + buttonsnames[entry.second] = entry.first; + } +} + std::string input_util::to_string(Keycode code) { auto icode_repr = static_cast(code); const char* name = SDL_GetKeyName( @@ -26,9 +99,25 @@ std::string input_util::to_string(Keycode code) { } Keycode input_util::keycode_from(const std::string& name) { + auto found = std::find_if( + std::begin(keynames), std::end(keynames), [&name](auto&& p) { + return p.second == name; + } + ); + // Compatibility with old names + if (found != keynames.end()) return static_cast(found->first); + return static_cast(SDL_GetScancodeFromName(name.c_str())); } +Mousecode input_util::mousecode_from(const std::string& name) { + const auto& found = mousecodes.find(name); + if (found == mousecodes.end()) { + return Mousecode::UNKNOWN; + } + return static_cast(found->second); +} + input_sdl::input_sdl(window_sdl& window) : window(window) { input_util::initialize(); // We should always get char stream (maybe) diff --git a/src/window/input.cpp b/src/window/input.cpp index 04cb6df42..195f5ca82 100644 --- a/src/window/input.cpp +++ b/src/window/input.cpp @@ -4,20 +4,9 @@ #include "debug/Logger.hpp" #include "util/stringutil.hpp" -#include debug::Logger logger("input"); -static std::unordered_map buttonsnames {}; - -static std::unordered_map mousecodes { - {"left", SDL_BUTTON_LEFT}, - {"right", SDL_BUTTON_RIGHT}, - {"middle", SDL_BUTTON_MIDDLE}, - {"side1", SDL_BUTTON_X1}, - {"side2", SDL_BUTTON_X2}, -}; - void Binding::reset(InputType type, int code) { this->type = type; this->code = code; @@ -31,32 +20,6 @@ void Binding::reset(Mousecode code) { reset(InputType::MOUSE, static_cast(code)); } -Mousecode input_util::mousecode_from(const std::string& name) { - const auto& found = mousecodes.find(name); - if (found == mousecodes.end()) { - return Mousecode::UNKNOWN; - } - return static_cast(found->second); -} - -void input_util::initialize() { - // for (int i = 0; i <= 9; i++) { - // keycodes[std::to_string(i)] = GLFW_KEY_0 + i; - // } - // for (int i = 0; i < 25; i++) { - // keycodes["f" + std::to_string(i + 1)] = GLFW_KEY_F1 + i; - // } - // for (char i = 'a'; i <= 'z'; i++) { - // keycodes[std::string({i})] = GLFW_KEY_A - 'a' + i; - // } - // for (const auto& entry : keycodes) { - // keynames[entry.second] = entry.first; - // } - for (const auto& entry : mousecodes) { - buttonsnames[entry.second] = entry.first; - } -} - std::string input_util::to_string(Mousecode code) { switch (code) { case Mousecode::BUTTON_1: @@ -126,12 +89,12 @@ std::string Bindings::write() const { case InputType::KEYBOARD: value = "key:" + - input_util::to_string(static_cast(binding.code)); + input_util::get_name(static_cast(binding.code)); break; case InputType::MOUSE: value = "mouse:" + - input_util::to_string(static_cast(binding.code)); + input_util::get_name(static_cast(binding.code)); break; default: throw std::runtime_error("unsupported control type"); diff --git a/src/window/input.hpp b/src/window/input.hpp index a6fdcb355..7ae082dd3 100644 --- a/src/window/input.hpp +++ b/src/window/input.hpp @@ -124,6 +124,7 @@ inline Mousecode mousecodes_all[] { Mousecode::BUTTON_5, }; + namespace input_util { void initialize(); Keycode keycode_from(const std::string& name); @@ -133,6 +134,11 @@ namespace input_util { std::string to_string(Keycode code); /// @return Mouse button label by keycode std::string to_string(Mousecode code); + + /// @return Key name by keycode + std::string get_name(Keycode code); + /// @return Mouse button name by keycode + std::string get_name(Mousecode code); } enum class InputType {