- gamepad_manager.cpp (12 logs): connect/disconnect com a info, errors d'open/init com a error, warning per BD no carregada. - key_config.cpp (5 logs): error per arxius malformats, info per al recompte de bindings carregats. - gamepad_bindings.cpp (4 logs): error/info anàleg. - audio_effects.cpp (2 errors de format). - sound_effects_config.cpp (3 logs): warn quan no hi ha presets, error de parse. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
52 lines
1.8 KiB
C++
52 lines
1.8 KiB
C++
#include "core/input/key_config.hpp"
|
|
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
#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<const char*>(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<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) {
|
|
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;
|
|
}
|