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;
}
}
}