mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
fix bug with keycodes
This commit is contained in:
parent
3843226497
commit
706fe0c40b
3 changed files with 97 additions and 39 deletions
|
|
@ -17,6 +17,79 @@
|
|||
|
||||
static debug::Logger logger("input");
|
||||
|
||||
static std::unordered_map<std::string, std::uint32_t> 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<std::string, std::uint32_t> 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<int, std::string> keynames {};
|
||||
static std::unordered_map<int, std::string> buttonsnames {};
|
||||
|
||||
std::string input_util::get_name(Mousecode code) {
|
||||
const auto found = buttonsnames.find(static_cast<int>(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<int>(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<SDL_Scancode>(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<Keycode>(found->first);
|
||||
|
||||
return static_cast<Keycode>(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<Mousecode>(found->second);
|
||||
}
|
||||
|
||||
input_sdl::input_sdl(window_sdl& window) : window(window) {
|
||||
input_util::initialize();
|
||||
// We should always get char stream (maybe)
|
||||
|
|
|
|||
|
|
@ -4,20 +4,9 @@
|
|||
#include "debug/Logger.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
#include <SDL3/SDL_mouse.h>
|
||||
|
||||
debug::Logger logger("input");
|
||||
|
||||
static std::unordered_map<int, std::string> buttonsnames {};
|
||||
|
||||
static std::unordered_map<std::string, int> 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<int>(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<Mousecode>(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<Keycode>(binding.code));
|
||||
input_util::get_name(static_cast<Keycode>(binding.code));
|
||||
break;
|
||||
case InputType::MOUSE:
|
||||
value =
|
||||
"mouse:" +
|
||||
input_util::to_string(static_cast<Mousecode>(binding.code));
|
||||
input_util::get_name(static_cast<Mousecode>(binding.code));
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("unsupported control type");
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue