migrant input: controllers.json guarda ara el path

migrant input: define_buttons no agarra inputs del mando que no toca
falta: Options ha de comprovar que el gamepad que te en la configuracio estiga o no estiga conectat
This commit is contained in:
2025-08-04 22:16:16 +02:00
parent f2d827daa4
commit 12e3226f17
3 changed files with 40 additions and 31 deletions

View File

@@ -41,7 +41,7 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event)
auto gamepad = input_->getGamepad(event.which); auto gamepad = input_->getGamepad(event.which);
// Asegúrate de que el gamepad sea válido y sea el que corresponde // Asegúrate de que el gamepad sea válido y sea el que corresponde
if (!gamepad) { if (!gamepad || gamepad != options_gamepad_->instance) {
return; return;
} }

View File

@@ -8,22 +8,24 @@
#include "input_types.h" // Solo incluimos los tipos compartidos #include "input_types.h" // Solo incluimos los tipos compartidos
struct GamepadConfig { struct GamepadConfig {
std::string name; // Nombre del dispositivo std::string name; // Nombre del dispositivo
std::unordered_map<InputAction, SDL_GamepadButton> bindings; // Asociación acción-botón std::string path; // Ruta física del dispositivo
std::unordered_map<InputAction, SDL_GamepadButton> bindings; // Asociación acción-botón
GamepadConfig(std::string name = "") GamepadConfig(const std::string& name, const std::string& path)
: name(name), : name(name),
bindings{ path(path),
{InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST}, bindings{
{InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH}, {InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST},
{InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST}, {InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH},
{InputAction::START, SDL_GAMEPAD_BUTTON_START}, {InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST},
{InputAction::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {} {InputAction::START, SDL_GAMEPAD_BUTTON_START},
{InputAction::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {}
// Reasigna un botón a una acción // Reasigna un botón a una acción
void rebindAction(InputAction action, SDL_GamepadButton new_button) { void rebindAction(InputAction action, SDL_GamepadButton new_button) {
bindings[action] = new_button; bindings[action] = new_button;
} }
}; };
using GamepadConfigs = std::vector<GamepadConfig>; using GamepadConfigs = std::vector<GamepadConfig>;
@@ -39,13 +41,14 @@ class GamepadConfigManager {
for (const auto& config : configs) { for (const auto& config : configs) {
nlohmann::json gamepadJson; nlohmann::json gamepadJson;
gamepadJson["name"] = config.name; gamepadJson["name"] = config.name;
gamepadJson["path"] = config.path;
gamepadJson["bindings"] = nlohmann::json::object(); gamepadJson["bindings"] = nlohmann::json::object();
// Convertir bindings a JSON // Convertir bindings a JSON
for (const auto& [action, button] : config.bindings) { for (const auto& [action, button] : config.bindings) {
auto actionIt = actionToString.find(action); auto actionIt = actionToString.find(action);
auto buttonIt = buttonToString.find(button); auto buttonIt = buttonToString.find(button);
if (actionIt != actionToString.end() && buttonIt != buttonToString.end()) { if (actionIt != actionToString.end() && buttonIt != buttonToString.end()) {
gamepadJson["bindings"][actionIt->second] = buttonIt->second; gamepadJson["bindings"][actionIt->second] = buttonIt->second;
} }
@@ -93,7 +96,9 @@ class GamepadConfigManager {
continue; // Saltar configuraciones malformadas continue; // Saltar configuraciones malformadas
} }
GamepadConfig config(gamepadJson["name"]); // Leer el campo path si existe, si no dejarlo vacío
std::string path = gamepadJson.contains("path") ? gamepadJson["path"].get<std::string>() : "";
GamepadConfig config(gamepadJson["name"], path);
// Limpiar bindings por defecto para cargar los del archivo // Limpiar bindings por defecto para cargar los del archivo
config.bindings.clear(); config.bindings.clear();

View File

@@ -357,46 +357,50 @@ void Input::saveGamepadConfigs() {
} }
void Input::applyGamepadConfig(std::shared_ptr<Gamepad> gamepad) { void Input::applyGamepadConfig(std::shared_ptr<Gamepad> gamepad) {
if (!gamepad) { if (!gamepad || gamepad->path.empty()) { // No podemos aplicar config sin una ruta
return; return;
} }
// Buscar configuración por nombre del gamepad // --- Buscar configuración por RUTA (path) ---
auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad](const GamepadConfig &config) { auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(),
return config.name == gamepad->name; [&gamepad](const GamepadConfig &config) {
return config.path == gamepad->path;
}); });
if (configIt != gamepad_configs_.end()) { if (configIt != gamepad_configs_.end()) {
// Aplicar la configuración encontrada al gamepad // Se encontró una configuración específica para este puerto/dispositivo. La aplicamos.
std::cout << "Applying custom config for gamepad at path: " << gamepad->path << std::endl;
for (const auto &[action, button] : configIt->bindings) { for (const auto &[action, button] : configIt->bindings) {
if (gamepad->bindings.find(action) != gamepad->bindings.end()) { if (gamepad->bindings.find(action) != gamepad->bindings.end()) {
gamepad->bindings[action].button = button; gamepad->bindings[action].button = button;
} }
} }
} }
// Opcional: Podrías añadir un fallback para buscar por nombre si no se encuentra por ruta.
} }
void Input::saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad) { void Input::saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad) {
if (!gamepad) { if (!gamepad || gamepad->path.empty()) { // No podemos guardar una config sin una ruta
return; return;
} }
// Buscar si ya existe una configuración con este nombre // --- CAMBIO CLAVE: Buscar si ya existe una configuración por RUTA (path) ---
auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad](const GamepadConfig &config) { auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(),
return config.name == gamepad->name; [&gamepad](const GamepadConfig &config) {
return config.path == gamepad->path;
}); });
// Crear nueva configuración desde el gamepad // Crear nueva configuración desde el gamepad, incluyendo nombre y ruta
GamepadConfig newConfig(gamepad->name); GamepadConfig newConfig(gamepad->name, gamepad->path); // <--- CAMBIO: Pasamos ambos
newConfig.bindings.clear(); // Limpiar bindings por defecto newConfig.bindings.clear();
// Copiar todos los bindings del gamepad // Copiar todos los bindings actuales del gamepad
for (const auto &[action, buttonState] : gamepad->bindings) { for (const auto &[action, buttonState] : gamepad->bindings) {
newConfig.bindings[action] = buttonState.button; newConfig.bindings[action] = buttonState.button;
} }
if (configIt != gamepad_configs_.end()) { if (configIt != gamepad_configs_.end()) {
// Sobreescribir configuración existente // Sobreescribir configuración existente para esta ruta
*configIt = newConfig; *configIt = newConfig;
} else { } else {
// Añadir nueva configuración // Añadir nueva configuración