#include "core/input/key_config.hpp" #include #include #include "core/resources/resource_helper.hpp" #include "external/fkyaml_node.hpp" #include "utils/log.hpp" auto KeyConfig::get() -> KeyConfig& { static KeyConfig instance_; return instance_; } void KeyConfig::load(const std::string& file_path) { auto bytes = Resource::Helper::loadFile(file_path); if (bytes.empty()) { Log::error("KeyConfig", "no s'ha pogut obrir {}", file_path); return; } try { const auto* begin = reinterpret_cast(bytes.data()); const auto* end = begin + bytes.size(); auto yaml = fkyaml::node::deserialize(begin, end); if (!yaml.contains("keys") || !yaml["keys"].is_sequence()) { Log::error("KeyConfig", "falta la secció 'keys' a {}", file_path); return; } bindings_.clear(); for (const auto& entry : yaml["keys"]) { if (!entry.contains("id") || !entry.contains("code")) { continue; } const auto ID = entry["id"].get_value(); const auto CODE = entry["code"].get_value(); const SDL_Scancode SC = SDL_GetScancodeFromName(CODE.c_str()); if (SC == SDL_SCANCODE_UNKNOWN) { Log::warn("KeyConfig", "codi SDL desconegut '{}' per id '{}'", CODE, ID); continue; } bindings_[ID] = SC; } Log::info("KeyConfig", "{} bindings des de {}", bindings_.size(), file_path); } catch (const std::exception& e) { Log::error("KeyConfig", "error parsejant {}: {}", file_path, e.what()); } } auto KeyConfig::scancode(const std::string& id) const -> SDL_Scancode { auto it = bindings_.find(id); return it != bindings_.end() ? it->second : SDL_SCANCODE_UNKNOWN; }