diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index a77bba3..551bd14 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -41,7 +41,7 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) auto gamepad = input_->getGamepad(event.which); // Asegúrate de que el gamepad sea válido y sea el que corresponde - if (!gamepad) { + if (!gamepad || gamepad != options_gamepad_->instance) { return; } diff --git a/source/gamepad_config_manager.h b/source/gamepad_config_manager.h index eea87b9..7d88eb6 100644 --- a/source/gamepad_config_manager.h +++ b/source/gamepad_config_manager.h @@ -8,22 +8,24 @@ #include "input_types.h" // Solo incluimos los tipos compartidos struct GamepadConfig { - std::string name; // Nombre del dispositivo - std::unordered_map bindings; // Asociación acción-botón + std::string name; // Nombre del dispositivo + std::string path; // Ruta física del dispositivo + std::unordered_map bindings; // Asociación acción-botón - GamepadConfig(std::string name = "") - : name(name), - bindings{ - {InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST}, - {InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH}, - {InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST}, - {InputAction::START, SDL_GAMEPAD_BUTTON_START}, - {InputAction::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {} + GamepadConfig(const std::string& name, const std::string& path) + : name(name), + path(path), + bindings{ + {InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST}, + {InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH}, + {InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST}, + {InputAction::START, SDL_GAMEPAD_BUTTON_START}, + {InputAction::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {} - // Reasigna un botón a una acción - void rebindAction(InputAction action, SDL_GamepadButton new_button) { - bindings[action] = new_button; - } + // Reasigna un botón a una acción + void rebindAction(InputAction action, SDL_GamepadButton new_button) { + bindings[action] = new_button; + } }; using GamepadConfigs = std::vector; @@ -39,13 +41,14 @@ class GamepadConfigManager { for (const auto& config : configs) { nlohmann::json gamepadJson; gamepadJson["name"] = config.name; + gamepadJson["path"] = config.path; gamepadJson["bindings"] = nlohmann::json::object(); // Convertir bindings a JSON for (const auto& [action, button] : config.bindings) { auto actionIt = actionToString.find(action); auto buttonIt = buttonToString.find(button); - + if (actionIt != actionToString.end() && buttonIt != buttonToString.end()) { gamepadJson["bindings"][actionIt->second] = buttonIt->second; } @@ -93,7 +96,9 @@ class GamepadConfigManager { 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() : ""; + GamepadConfig config(gamepadJson["name"], path); // Limpiar bindings por defecto para cargar los del archivo config.bindings.clear(); diff --git a/source/input.cpp b/source/input.cpp index 48b0ed8..0ab3173 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -357,46 +357,50 @@ void Input::saveGamepadConfigs() { } void Input::applyGamepadConfig(std::shared_ptr gamepad) { - if (!gamepad) { + if (!gamepad || gamepad->path.empty()) { // No podemos aplicar config sin una ruta return; } - // Buscar configuración por nombre del gamepad - auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad](const GamepadConfig &config) { - return config.name == gamepad->name; + // --- Buscar configuración por RUTA (path) --- + auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), + [&gamepad](const GamepadConfig &config) { + return config.path == gamepad->path; }); 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) { if (gamepad->bindings.find(action) != gamepad->bindings.end()) { 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) { - if (!gamepad) { + if (!gamepad || gamepad->path.empty()) { // No podemos guardar una config sin una ruta return; } - // Buscar si ya existe una configuración con este nombre - auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad](const GamepadConfig &config) { - return config.name == gamepad->name; + // --- 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) { + return config.path == gamepad->path; }); - // Crear nueva configuración desde el gamepad - GamepadConfig newConfig(gamepad->name); - newConfig.bindings.clear(); // Limpiar bindings por defecto + // Crear nueva configuración desde el gamepad, incluyendo nombre y ruta + GamepadConfig newConfig(gamepad->name, gamepad->path); // <--- CAMBIO: Pasamos ambos + newConfig.bindings.clear(); - // Copiar todos los bindings del gamepad + // Copiar todos los bindings actuales del gamepad for (const auto &[action, buttonState] : gamepad->bindings) { newConfig.bindings[action] = buttonState.button; } if (configIt != gamepad_configs_.end()) { - // Sobreescribir configuración existente + // Sobreescribir configuración existente para esta ruta *configIt = newConfig; } else { // Añadir nueva configuración