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

@@ -43,7 +43,7 @@ static auto parseTokens(const std::string& input) -> std::vector<std::string> {
// Calcula la altura total de la consola para N líneas de mensaje (+ 1 línea de input)
static auto calcTargetHeight(int num_msg_lines) -> float {
constexpr int PADDING_IN_V = Console::TEXT_SIZE / 2;
return static_cast<float>((Console::TEXT_SIZE * (num_msg_lines + 1)) + (PADDING_IN_V * 2));
return static_cast<float>((Console::LINE_HEIGHT * (num_msg_lines + 1)) + (PADDING_IN_V * 2));
}
// Divide text en líneas respetando los \n existentes y haciendo word-wrap por ancho en píxeles
@@ -173,7 +173,7 @@ void Console::redrawText() {
const int VISIBLE = std::min(remaining, static_cast<int>(line.size()));
text_->writeColored(PADDING_IN_H, y_pos, line.substr(0, VISIBLE), MSG_COLOR);
remaining -= VISIBLE;
y_pos += TEXT_SIZE;
y_pos += LINE_HEIGHT;
}
// Línea de input (siempre la última): prompt en PROMPT_COLOR, comando + cursor en COMMAND_COLOR

View File

@@ -29,7 +29,8 @@ class Console {
void handleEvent(const SDL_Event& event);
// Constantes públicas
static constexpr int TEXT_SIZE = 6; // Tamaño de carácter del font de la consola
static constexpr int TEXT_SIZE = 6; // Tamaño de carácter del font de la consola
static constexpr int LINE_HEIGHT = 7; // TEXT_SIZE + 1px de interlineado
// Consultas
auto isActive() -> bool; // true si RISING, ACTIVE o VANISHING

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()) {

View File

@@ -58,4 +58,5 @@ class CommandRegistry {
void registerHandlers();
[[nodiscard]] auto isCommandVisible(const CommandDef& cmd) const -> bool;
[[nodiscard]] static auto generateKeysHelp(const std::string& scope_filter) -> std::string;
};