49 lines
1.7 KiB
C++
49 lines
1.7 KiB
C++
#include "core/input/key_config.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "external/fkyaml_node.hpp"
|
|
|
|
auto KeyConfig::get() -> KeyConfig& {
|
|
static KeyConfig instance_;
|
|
return instance_;
|
|
}
|
|
|
|
void KeyConfig::load(const std::string& file_path) {
|
|
std::ifstream file(file_path);
|
|
if (!file.is_open()) {
|
|
std::cerr << "KeyConfig: no s'ha pogut obrir " << file_path << '\n';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
auto yaml = fkyaml::node::deserialize(file);
|
|
if (!yaml.contains("keys") || !yaml["keys"].is_sequence()) {
|
|
std::cerr << "KeyConfig: falta la secció 'keys' a " << file_path << '\n';
|
|
return;
|
|
}
|
|
bindings_.clear();
|
|
for (const auto& entry : yaml["keys"]) {
|
|
if (!entry.contains("id") || !entry.contains("code")) { continue; }
|
|
const auto ID = entry["id"].get_value<std::string>();
|
|
const auto CODE = entry["code"].get_value<std::string>();
|
|
const SDL_Scancode SC = SDL_GetScancodeFromName(CODE.c_str());
|
|
if (SC == SDL_SCANCODE_UNKNOWN) {
|
|
std::cerr << "KeyConfig: codi SDL desconegut '" << CODE << "' per id '" << ID << "'\n";
|
|
continue;
|
|
}
|
|
bindings_[ID] = SC;
|
|
}
|
|
std::cout << "KeyConfig: " << bindings_.size() << " bindings des de " << file_path << '\n';
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "KeyConfig: error parsejant " << file_path << ": " << e.what() << '\n';
|
|
}
|
|
}
|
|
|
|
auto KeyConfig::scancode(const std::string& id) const -> SDL_Scancode {
|
|
auto it = bindings_.find(id);
|
|
return it != bindings_.end() ? it->second : SDL_SCANCODE_UNKNOWN;
|
|
}
|