canvi de pc

This commit is contained in:
2025-08-01 09:20:22 +02:00
parent fe818d6a10
commit 6385e413da
4 changed files with 281 additions and 238 deletions

View File

@@ -1,69 +1,29 @@
#pragma once
#include <external/json.hpp>
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "input.h" // Para Input
// Mapas para convertir entre enums y strings
std::unordered_map<Input::Action, std::string> actionToString = {
{Input::Action::FIRE_LEFT, "FIRE_LEFT"},
{Input::Action::FIRE_CENTER, "FIRE_CENTER"},
{Input::Action::FIRE_RIGHT, "FIRE_RIGHT"},
{Input::Action::START, "START"},
{Input::Action::SERVICE, "SERVICE"}};
std::unordered_map<std::string, Input::Action> stringToAction = {
{"FIRE_LEFT", Input::Action::FIRE_LEFT},
{"FIRE_CENTER", Input::Action::FIRE_CENTER},
{"FIRE_RIGHT", Input::Action::FIRE_RIGHT},
{"START", Input::Action::START},
{"SERVICE", Input::Action::SERVICE}};
std::unordered_map<SDL_GamepadButton, std::string> buttonToString = {
{SDL_GAMEPAD_BUTTON_WEST, "WEST"},
{SDL_GAMEPAD_BUTTON_NORTH, "NORTH"},
{SDL_GAMEPAD_BUTTON_EAST, "EAST"},
{SDL_GAMEPAD_BUTTON_SOUTH, "SOUTH"},
{SDL_GAMEPAD_BUTTON_START, "START"},
{SDL_GAMEPAD_BUTTON_BACK, "BACK"},
{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "LEFT_SHOULDER"},
{SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "RIGHT_SHOULDER"}
// Añade todos los botones que necesites
};
std::unordered_map<std::string, SDL_GamepadButton> stringToButton = {
{"WEST", SDL_GAMEPAD_BUTTON_WEST},
{"NORTH", SDL_GAMEPAD_BUTTON_NORTH},
{"EAST", SDL_GAMEPAD_BUTTON_EAST},
{"SOUTH", SDL_GAMEPAD_BUTTON_SOUTH},
{"START", SDL_GAMEPAD_BUTTON_START},
{"BACK", SDL_GAMEPAD_BUTTON_BACK},
{"LEFT_SHOULDER", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
{"RIGHT_SHOULDER", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}
// Añade todos los botones que necesites
};
#include "input_types.h" // Solo incluimos los tipos compartidos
struct GamepadConfig {
std::string name; // Nombre del dispositivo
std::unordered_map<Input::Action, SDL_GamepadButton> bindings; // Asociación acción-botón
std::string name; // Nombre del dispositivo
std::unordered_map<InputAction, SDL_GamepadButton> bindings; // Asociación acción-botón
GamepadConfig(std::string name = "")
: name(name),
bindings{
{Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST},
{Input::Action::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH},
{Input::Action::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST},
{Input::Action::START, SDL_GAMEPAD_BUTTON_START},
{Input::Action::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {}
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}} {}
// Reasigna un botón a una acción
void rebindAction(Input::Action 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;
}
};
class GamepadConfigManager {
@@ -81,9 +41,12 @@ class GamepadConfigManager {
// Convertir bindings a JSON
for (const auto& [action, button] : config.bindings) {
std::string actionStr = actionToString[action];
std::string buttonStr = buttonToString[button];
gamepadJson["bindings"][actionStr] = buttonStr;
auto actionIt = actionToString.find(action);
auto buttonIt = buttonToString.find(button);
if (actionIt != actionToString.end() && buttonIt != buttonToString.end()) {
gamepadJson["bindings"][actionIt->second] = buttonIt->second;
}
}
j["gamepads"].push_back(gamepadJson);
@@ -159,33 +122,4 @@ class GamepadConfigManager {
std::ifstream file(filename);
return file.good();
}
};
/*
// Ejemplo de uso
void ejemploUso() {
std::vector<GamepadConfig> configs;
// Crear algunas configuraciones de ejemplo
GamepadConfig config1("Xbox Controller");
config1.rebindAction(Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_SOUTH);
GamepadConfig config2("PS4 Controller");
config2.rebindAction(Input::Action::START, SDL_GAMEPAD_BUTTON_OPTIONS);
configs.push_back(config1);
configs.push_back(config2);
// Escribir a archivo
if (GamepadConfigManager::writeToJson(configs, "gamepad_config.json")) {
std::cout << "Configuración guardada exitosamente" << std::endl;
}
// Leer desde archivo
std::vector<GamepadConfig> loadedConfigs;
if (GamepadConfigManager::readFromJson(loadedConfigs, "gamepad_config.json")) {
std::cout << "Configuración cargada exitosamente" << std::endl;
std::cout << "Número de configuraciones: " << loadedConfigs.size() << std::endl;
}
}
*/
};