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

@@ -0,0 +1,53 @@
#pragma once
#include <SDL3/SDL.h>
#include <string> // Para string
#include <unordered_map> // Para unordered_map
#include <vector> // Para vector
// Entrada de tecla cargada desde keys.yaml
struct KeyEntry {
std::string id; // Identificador usado en código (ej. "grid", "tile_picker")
std::string display_key; // Texto para mostrar en HELP KEYS (ej. "G", "T")
std::string desc; // Descripción corta
SDL_Keycode keycode{SDLK_UNKNOWN}; // Tecla SDL asignada
std::string action; // (Opcional) Nombre de InputAction para scope GLOBAL
};
// Grupo de teclas por ámbito
struct KeyConfigScope {
std::string name; // "GLOBAL", "EDITOR", "MINIMAP", "DEBUG"
std::vector<KeyEntry> entries;
std::unordered_map<std::string, size_t> index; // id → posición en entries
};
// Registro centralizado de teclas — fuente única de verdad (keys.yaml)
class KeyConfig {
public:
// Singleton
static void init(const std::string& yaml_path);
static void destroy();
static auto get() -> KeyConfig*;
// Consulta la SDL_Keycode asignada a un id dentro de un scope
[[nodiscard]] auto key(const std::string& scope, const std::string& id) const -> SDL_Keycode;
// Aplica las teclas del scope GLOBAL al sistema de Input (Input::bindKey)
void applyGlobalBindings() const;
// Acceso a scopes para HELP KEYS y para aplicar bindings globales
[[nodiscard]] auto getScopes() const -> const std::vector<KeyConfigScope>&;
[[nodiscard]] auto getScope(const std::string& name) const -> const KeyConfigScope*;
private:
static KeyConfig* instance_;
KeyConfig() = default;
~KeyConfig() = default;
void load(const std::string& yaml_path);
std::vector<KeyConfigScope> scopes_;
std::unordered_map<std::string, size_t> scope_index_; // nombre → posición en scopes_
};