bindings.toml, config/bindings.toml file

This commit is contained in:
MihailRis
2024-05-20 04:06:49 +03:00
parent 7fdacffe53
commit 66476ee642
11 changed files with 79 additions and 31 deletions
+27
View File
@@ -1,6 +1,7 @@
#include "Events.hpp"
#include "Window.hpp"
#include "../debug/Logger.hpp"
#include "../util/stringutil.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@@ -148,6 +149,7 @@ void Events::setPosition(float xpos, float ypos) {
#include "../data/dynamic.hpp"
#include "../coders/json.hpp"
#include "../coders/toml.hpp"
std::string Events::writeBindings() {
dynamic::Map obj;
@@ -189,3 +191,28 @@ void Events::loadBindings(const std::string& filename, const std::string& source
jentry->num("code", binding.code);
}
}
void Events::loadBindingsToml(const std::string& filename, const std::string& source) {
auto map = toml::parse(filename, source);
for (auto& entry : map->values) {
if (auto value = std::get_if<std::string>(&entry.second)) {
auto [prefix, codename] = util::split_at(*value, ':');
inputtype type;
int code;
if (prefix == "key") {
type = inputtype::keyboard;
code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") {
type = inputtype::mouse;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error() << "unknown input type: " << prefix
<< " (binding " << util::quote(entry.first) << ")";
continue;
}
Events::bind(entry.first, type, code);
} else {
logger.error() << "invalid binding entry: " << entry.first;
}
}
}
+2 -1
View File
@@ -2,11 +2,11 @@
#define WINDOW_EVENTS_HPP_
#include "input.hpp"
#include "../typedefs.hpp"
#include <string>
#include <vector>
#include <unordered_map>
#include "../typedefs.hpp"
inline constexpr short KEYS_BUFFER_SIZE = 1036;
@@ -51,6 +51,7 @@ public:
static std::string writeBindings();
static void loadBindings(const std::string& filename, const std::string& source);
static void loadBindingsToml(const std::string& filename, const std::string& source);
};
#endif // WINDOW_EVENTS_HPP_
+3 -1
View File
@@ -15,6 +15,7 @@ static std::unordered_map<std::string, int> keycodes {
{"delete", GLFW_KEY_DELETE},
{"home", GLFW_KEY_HOME},
{"end", GLFW_KEY_END},
{"tab", GLFW_KEY_TAB},
{"insert", GLFW_KEY_INSERT},
{"page-down", GLFW_KEY_PAGE_DOWN},
{"page-up", GLFW_KEY_PAGE_UP},
@@ -26,6 +27,7 @@ static std::unordered_map<std::string, int> keycodes {
{"right-alt", GLFW_KEY_RIGHT_ALT},
{"left-super", GLFW_KEY_LEFT_SUPER},
{"right-super", GLFW_KEY_RIGHT_SUPER},
{"grave-accent", GLFW_KEY_GRAVE_ACCENT},
{"left", GLFW_KEY_LEFT},
{"right", GLFW_KEY_RIGHT},
{"down", GLFW_KEY_DOWN},
@@ -59,7 +61,7 @@ void input_util::initialize() {
keycodes["f"+std::to_string(i)] = GLFW_KEY_F1+i;
}
for (char i = 'a'; i <= 'z'; i++) {
keycodes[std::to_string(i)] = GLFW_KEY_A-'a'+i;
keycodes[std::string({i})] = GLFW_KEY_A-'a'+i;
}
}