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);
// Asegúrate de que el gamepad sea válido y sea el que corresponde
if (!gamepad) {
if (!gamepad || gamepad != options_gamepad_->instance) {
return;
}

View File

@@ -8,22 +8,24 @@
#include "input_types.h" // Solo incluimos los tipos compartidos
struct GamepadConfig {
std::string name; // Nombre del dispositivo
std::unordered_map<InputAction, SDL_GamepadButton> bindings; // Asociación acción-botón
std::string name; // Nombre del dispositivo
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 = "")
: 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<GamepadConfig>;
@@ -39,6 +41,7 @@ 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
@@ -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<std::string>() : "";
GamepadConfig config(gamepadJson["name"], path);
// Limpiar bindings por defecto para cargar los del archivo
config.bindings.clear();

View File

@@ -357,46 +357,50 @@ void Input::saveGamepadConfigs() {
}
void Input::applyGamepadConfig(std::shared_ptr<Gamepad> 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> 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