key_config.cpp i keys.yaml per a centralitzar i no hardcodejar tecles

This commit is contained in:
2026-04-12 15:21:43 +02:00
parent 999cf53821
commit 848b658611
19 changed files with 521 additions and 115 deletions

View File

@@ -10,6 +10,7 @@
#include <vector> // Para vector
#include "core/audio/audio.hpp" // Para Audio
#include "core/input/key_config.hpp" // Para KeyConfig
#include "core/locale/locale.hpp" // Para Locale
#include "core/rendering/render_info.hpp" // Para RenderInfo
#include "core/rendering/screen.hpp" // Para Screen
@@ -1092,7 +1093,9 @@ void CommandRegistry::registerHandlers() { // NOLINT(readability-function-cogni
// HELP: lista de comandos visibles en el scope activo
dynamic_providers_["HELP"] = [this]() -> std::vector<std::string> {
return getVisibleKeywords();
auto kws = getVisibleKeywords();
kws.insert(kws.begin(), "KEYS");
return kws;
};
dynamic_providers_["SET ITEMCOLOR1"] = color_provider;
dynamic_providers_["SET ITEMCOLOR2"] = color_provider;
@@ -1237,6 +1240,10 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
// Registrar el handler de HELP (captura this)
handlers_["cmd_help"] = [this](const std::vector<std::string>& args) -> std::string {
if (!args.empty()) {
// HELP KEYS [scope]: referencia de atajos de teclado
if (args[0] == "KEYS") {
return generateKeysHelp(args.size() > 1 ? args[1] : "");
}
// HELP <command>: mostrar ayuda detallada de un comando
const auto* cmd = findCommand(args[0]);
if (cmd != nullptr) {
@@ -1268,6 +1275,44 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
completions_map_[path] = opts;
}
}
// Proveedor dinámico para HELP KEYS <scope> (usa KeyConfig)
dynamic_providers_["HELP KEYS"] = []() -> std::vector<std::string> {
std::vector<std::string> names;
if (KeyConfig::get() != nullptr) {
for (const auto& scope : KeyConfig::get()->getScopes()) {
names.push_back(scope.name);
}
}
return names;
};
}
auto CommandRegistry::generateKeysHelp(const std::string& scope_filter) -> std::string {
if (KeyConfig::get() == nullptr) { return "KeyConfig not loaded"; }
// Sin argumento: mostrar solo GLOBAL
const std::string FILTER = scope_filter.empty() ? "GLOBAL" : scope_filter;
const auto* scope = KeyConfig::get()->getScope(FILTER);
if (scope == nullptr) {
std::string filter_lower = FILTER;
std::ranges::transform(filter_lower, filter_lower.begin(), ::tolower);
return "Unknown scope: " + filter_lower;
}
// Cabecera del scope
std::string name_lower = scope->name;
std::ranges::transform(name_lower, name_lower.begin(), ::tolower);
std::string result = '[' + name_lower + "]\n";
// Una tecla por línea: key = desc
for (size_t i = 0; i < scope->entries.size(); ++i) {
const auto& entry = scope->entries[i];
result += entry.display_key + " = " + entry.desc;
if (i + 1 < scope->entries.size()) { result += '\n'; }
}
return result;
}
auto CommandRegistry::findCommand(const std::string& keyword) const -> const CommandDef* {
@@ -1346,7 +1391,6 @@ auto CommandRegistry::generateConsoleHelp() const -> std::string { // NOLINT(re
if (active_scope_ == "editor" && !editor_cmds.empty()) {
result += "Editor:\n" + editor_cmds + "\n";
result += "keys: 9=editor g=grid 8=collision e=eraser m=map\n";
}
if (!debug_cmds.empty()) {