migrant input: ja guarda i llig configuracions de gamepad en fitxer
This commit is contained in:
@@ -76,6 +76,7 @@ set(APP_SOURCES
|
||||
source/color.cpp
|
||||
source/define_buttons.cpp
|
||||
source/difficulty.cpp
|
||||
source/input_types.cpp
|
||||
source/mouse.cpp
|
||||
source/options.cpp
|
||||
source/stage.cpp
|
||||
|
||||
@@ -95,7 +95,9 @@ void Director::init() {
|
||||
Screen::init(); // Inicializa la pantalla y el sistema de renderizado
|
||||
Audio::init(); // Activa el sistema de audio
|
||||
Resource::init(); // Inicializa el sistema de gestión de recursos
|
||||
Input::init(Asset::get()->get("gamecontrollerdb.txt")); // Carga configuración de controles
|
||||
Input::init(
|
||||
Asset::get()->get("gamecontrollerdb.txt"),
|
||||
Asset::get()->get("controllers.json")); // Carga configuración de controles
|
||||
bindInputs(); // Asigna los controles a la entrada del sistema
|
||||
|
||||
auto gamepads = Input::get()->getGamepads();
|
||||
|
||||
@@ -26,10 +26,12 @@ struct GamepadConfig {
|
||||
}
|
||||
};
|
||||
|
||||
using GamepadConfigs = std::vector<GamepadConfig>;
|
||||
|
||||
class GamepadConfigManager {
|
||||
public:
|
||||
// Escribir vector de GamepadConfig a archivo JSON
|
||||
static bool writeToJson(const std::vector<GamepadConfig>& configs, const std::string& filename) {
|
||||
static bool writeToJson(const GamepadConfigs& configs, const std::string& filename) {
|
||||
try {
|
||||
nlohmann::json j;
|
||||
j["gamepads"] = nlohmann::json::array();
|
||||
@@ -69,7 +71,7 @@ class GamepadConfigManager {
|
||||
}
|
||||
|
||||
// Leer vector de GamepadConfig desde archivo JSON
|
||||
static bool readFromJson(std::vector<GamepadConfig>& configs, const std::string& filename) {
|
||||
static bool readFromJson(GamepadConfigs& configs, const std::string& filename) {
|
||||
try {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
|
||||
126
source/input.cpp
126
source/input.cpp
@@ -13,7 +13,9 @@
|
||||
Input *Input::instance = nullptr;
|
||||
|
||||
// Inicializa la instancia única del singleton
|
||||
void Input::init(const std::string &game_controller_db_path) { Input::instance = new Input(game_controller_db_path); }
|
||||
void Input::init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file) {
|
||||
Input::instance = new Input(game_controller_db_path, gamepad_configs_file);
|
||||
}
|
||||
|
||||
// Libera la instancia
|
||||
void Input::destroy() { delete Input::instance; }
|
||||
@@ -22,8 +24,9 @@ void Input::destroy() { delete Input::instance; }
|
||||
auto Input::get() -> Input * { return Input::instance; }
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string game_controller_db_path)
|
||||
: game_controller_db_path_(std::move(game_controller_db_path)) {
|
||||
Input::Input(std::string game_controller_db_path, std::string gamepad_configs_file)
|
||||
: game_controller_db_path_(std::move(game_controller_db_path)),
|
||||
gamepad_configs_file_(std::move(gamepad_configs_file)) {
|
||||
// Inicializa el subsistema SDL_INIT_GAMEPAD
|
||||
initSDLGamePad();
|
||||
|
||||
@@ -226,15 +229,27 @@ auto Input::checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool
|
||||
return false;
|
||||
}
|
||||
|
||||
void Input::addGamepadMappingsFromFile() {
|
||||
if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0) {
|
||||
std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Input::discoverGamepads() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
handleEvent(event); // Comprueba mandos conectados
|
||||
}
|
||||
}
|
||||
|
||||
void Input::initSDLGamePad() {
|
||||
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1) {
|
||||
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD could not initialize! SDL Error: %s", SDL_GetError());
|
||||
} else {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
handleEvent(event); // Comprueba mandos conectados
|
||||
}
|
||||
addGamepadMappingsFromFile();
|
||||
loadGamepadConfigs();
|
||||
discoverGamepads();
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Input System initialized successfully\n");
|
||||
}
|
||||
}
|
||||
@@ -299,6 +314,8 @@ void Input::add_gamepad(int device_index) {
|
||||
|
||||
auto gamepad = std::make_shared<Gamepad>(pad);
|
||||
std::cout << "Gamepad connected (" << gamepad->name << ")" << std::endl;
|
||||
applyGamepadConfig(gamepad);
|
||||
saveGamepadConfigFromGamepad(gamepad);
|
||||
gamepads_.push_back(std::move(gamepad));
|
||||
}
|
||||
|
||||
@@ -328,3 +345,98 @@ void Input::printConnectedGamepads() const {
|
||||
<< ", Nombre: " << name << ")" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Input::loadGamepadConfigs() {
|
||||
if (GamepadConfigManager::fileExists(gamepad_configs_file_)) {
|
||||
GamepadConfigManager::readFromJson(gamepad_configs_, gamepad_configs_file_);
|
||||
}
|
||||
}
|
||||
|
||||
void Input::saveGamepadConfigs() {
|
||||
GamepadConfigManager::writeToJson(gamepad_configs_, gamepad_configs_file_);
|
||||
}
|
||||
|
||||
void Input::applyGamepadConfig(std::shared_ptr<Gamepad> gamepad) {
|
||||
if (!gamepad) {
|
||||
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;
|
||||
});
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
// Aplicar la configuración encontrada al gamepad
|
||||
for (const auto& [action, button] : configIt->bindings) {
|
||||
if (gamepad->bindings.find(action) != gamepad->bindings.end()) {
|
||||
gamepad->bindings[action].button = button;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Input::saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad) {
|
||||
if (!gamepad) {
|
||||
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;
|
||||
});
|
||||
|
||||
// Crear nueva configuración desde el gamepad
|
||||
GamepadConfig newConfig(gamepad->name);
|
||||
newConfig.bindings.clear(); // Limpiar bindings por defecto
|
||||
|
||||
// Copiar todos los bindings del gamepad
|
||||
for (const auto& [action, buttonState] : gamepad->bindings) {
|
||||
newConfig.bindings[action] = buttonState.button;
|
||||
}
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
// Sobreescribir configuración existente
|
||||
*configIt = newConfig;
|
||||
} else {
|
||||
// Añadir nueva configuración
|
||||
gamepad_configs_.push_back(newConfig);
|
||||
}
|
||||
|
||||
// Guardar cambios inmediatamente
|
||||
saveGamepadConfigs();
|
||||
}
|
||||
|
||||
// Método para establecer el archivo de configuración (opcional)
|
||||
void Input::setGamepadConfigsFile(const std::string& filename) {
|
||||
gamepad_configs_file_ = filename;
|
||||
loadGamepadConfigs(); // Recargar con el nuevo archivo
|
||||
}
|
||||
|
||||
// Método para obtener configuración de un gamepad específico (opcional)
|
||||
GamepadConfig* Input::getGamepadConfig(const std::string& gamepadName) {
|
||||
auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(),
|
||||
[&gamepadName](const GamepadConfig& config) {
|
||||
return config.name == gamepadName;
|
||||
});
|
||||
|
||||
return (configIt != gamepad_configs_.end()) ? &(*configIt) : nullptr;
|
||||
}
|
||||
|
||||
// Método para eliminar configuración de gamepad (opcional)
|
||||
bool Input::removeGamepadConfig(const std::string& gamepadName) {
|
||||
auto configIt = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(),
|
||||
[&gamepadName](const GamepadConfig& config) {
|
||||
return config.name == gamepadName;
|
||||
});
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
gamepad_configs_.erase(configIt);
|
||||
saveGamepadConfigs();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,17 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "gamepad_config_manager.h"
|
||||
#include "input_types.h"
|
||||
|
||||
// Forward declaration para evitar dependencia circular
|
||||
struct GamepadConfig;
|
||||
class GamepadConfigManager;
|
||||
// struct GamepadConfig;
|
||||
// class GamepadConfigManager;
|
||||
|
||||
// Clase Input: gestiona la entrada de teclado y mandos (singleton)
|
||||
class Input {
|
||||
@@ -121,10 +123,15 @@ class Input {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
|
||||
// Reasigna un botón a una acción
|
||||
void rebindAction(InputAction action, SDL_GamepadButton new_button) {
|
||||
bindings[action] = new_button;
|
||||
}
|
||||
};
|
||||
|
||||
// --- Métodos de singleton ---
|
||||
static void init(const std::string &game_controller_db_path);
|
||||
static void init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file);
|
||||
static void destroy();
|
||||
static auto get() -> Input *;
|
||||
|
||||
@@ -160,11 +167,6 @@ class Input {
|
||||
|
||||
[[nodiscard]] auto getGamepads() const -> const std::vector<std::shared_ptr<Gamepad>> & { return gamepads_; }
|
||||
|
||||
// --- Métodos para integración con GamepadConfigManager ---
|
||||
void loadGamepadConfigs(const std::string& filename);
|
||||
void saveGamepadConfigs(const std::string& filename) const;
|
||||
void applyConfigToGamepad(std::shared_ptr<Gamepad> gamepad, const GamepadConfig& config);
|
||||
|
||||
private:
|
||||
// --- Constantes ---
|
||||
static constexpr Sint16 AXIS_THRESHOLD = 30000;
|
||||
@@ -174,15 +176,30 @@ class Input {
|
||||
Keyboard keyboard_;
|
||||
std::vector<Action> button_inputs_;
|
||||
std::string game_controller_db_path_;
|
||||
std::string gamepad_configs_file_;
|
||||
GamepadConfigs gamepad_configs_;
|
||||
|
||||
// --- Métodos internos ---
|
||||
void initSDLGamePad();
|
||||
auto checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool;
|
||||
void add_gamepad(int device_index);
|
||||
void remove_gamepad(SDL_JoystickID id);
|
||||
void addGamepadMappingsFromFile();
|
||||
void discoverGamepads();
|
||||
|
||||
// --- Métodos para integración con GamepadConfigManager ---
|
||||
void loadGamepadConfigs();
|
||||
void saveGamepadConfigs();
|
||||
void applyGamepadConfig(std::shared_ptr<Gamepad> gamepad);
|
||||
void saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad);
|
||||
|
||||
// Métodos auxiliares opcionales
|
||||
void setGamepadConfigsFile(const std::string& filename);
|
||||
GamepadConfig* getGamepadConfig(const std::string& gamepadName);
|
||||
bool removeGamepadConfig(const std::string& gamepadName);
|
||||
|
||||
// --- Constructor y destructor ---
|
||||
explicit Input(std::string game_controller_db_path);
|
||||
explicit Input(std::string game_controller_db_path, std::string gamepad_configs_file);
|
||||
~Input() = default;
|
||||
|
||||
// --- Singleton ---
|
||||
|
||||
@@ -47,7 +47,7 @@ void init() {
|
||||
controllers.emplace_back(GamepadOptions(2));
|
||||
setKeyboardToPlayer(1);
|
||||
|
||||
// Opciones pendientes
|
||||
// Opciones de cambios pendientes
|
||||
pending_changes.new_language = settings.language;
|
||||
pending_changes.new_difficulty = settings.difficulty;
|
||||
pending_changes.has_pending_changes = false;
|
||||
|
||||
@@ -30,8 +30,7 @@ void initParam() {
|
||||
param.game.play_area.rect = {0, 0, param.game.width, 216};
|
||||
param.game.name_entry_idle_time = 10;
|
||||
param.game.name_entry_total_time = 60;
|
||||
// param.game.speed = 15;
|
||||
param.game.speed = 1000 / 60;
|
||||
param.game.speed = 15;
|
||||
param.game.hit_stop = true;
|
||||
param.game.hit_stop_ms = 300;
|
||||
precalculateZones();
|
||||
|
||||
Reference in New Issue
Block a user