migrant input: commit abans que gemini destroçe algo
This commit is contained in:
150
source/options.h
150
source/options.h
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <algorithm> // Para copy
|
||||
#include <array> // Para array
|
||||
#include <fstream> // Para ofstream
|
||||
#include <stdexcept> // Para excepciones
|
||||
#include <string> // Para allocator, string
|
||||
#include <unordered_map>
|
||||
#include <vector> // Para vector
|
||||
@@ -15,6 +17,7 @@
|
||||
#include "player.h" // Para Player
|
||||
|
||||
namespace Options {
|
||||
|
||||
// --- Opciones de ventana ---
|
||||
struct Window {
|
||||
std::string caption; // Texto que aparece en la barra de título de la ventana
|
||||
@@ -90,6 +93,7 @@ struct Settings {
|
||||
}
|
||||
};
|
||||
|
||||
// --- Estructura para gamepad individual ---
|
||||
struct Gamepad {
|
||||
std::shared_ptr<Input::Gamepad> instance = nullptr; // Referencia al mando
|
||||
std::string name; // Nombre del mando
|
||||
@@ -99,6 +103,114 @@ struct Gamepad {
|
||||
: player_id(custom_player_id) {}
|
||||
};
|
||||
|
||||
// --- Manager para los gamepads ---
|
||||
class GamepadManager {
|
||||
private:
|
||||
static constexpr size_t MAX_PLAYERS = 2;
|
||||
std::array<Gamepad, MAX_PLAYERS> gamepads;
|
||||
|
||||
// Convierte Player::Id a índice del array
|
||||
size_t playerIdToIndex(Player::Id player_id) const {
|
||||
switch (player_id) {
|
||||
case Player::Id::PLAYER1:
|
||||
return 0;
|
||||
case Player::Id::PLAYER2:
|
||||
return 1;
|
||||
default:
|
||||
throw std::invalid_argument("Invalid player ID");
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void init() {
|
||||
gamepads[0] = Gamepad(Player::Id::PLAYER1);
|
||||
gamepads[1] = Gamepad(Player::Id::PLAYER2);
|
||||
}
|
||||
|
||||
// Acceso directo por player_id (más intuitivo)
|
||||
Gamepad& getGamepad(Player::Id player_id) {
|
||||
return gamepads[playerIdToIndex(player_id)];
|
||||
}
|
||||
|
||||
const Gamepad& getGamepad(Player::Id player_id) const {
|
||||
return gamepads[playerIdToIndex(player_id)];
|
||||
}
|
||||
|
||||
// Acceso por índice (más eficiente si ya tienes el índice)
|
||||
Gamepad& operator[](size_t index) {
|
||||
if (index >= MAX_PLAYERS) throw std::out_of_range("Invalid gamepad index");
|
||||
return gamepads[index];
|
||||
}
|
||||
|
||||
const Gamepad& operator[](size_t index) const {
|
||||
if (index >= MAX_PLAYERS) throw std::out_of_range("Invalid gamepad index");
|
||||
return gamepads[index];
|
||||
}
|
||||
|
||||
bool assignGamepadToPlayer(Player::Id player_id,
|
||||
std::shared_ptr<Input::Gamepad> instance,
|
||||
const std::string& name) {
|
||||
try {
|
||||
auto& gamepad = getGamepad(player_id);
|
||||
gamepad.instance = instance;
|
||||
gamepad.name = name;
|
||||
return true;
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void swapPlayers() {
|
||||
std::swap(gamepads[0].player_id, gamepads[1].player_id);
|
||||
}
|
||||
|
||||
// Para serialización/deserialización
|
||||
void saveToFile(std::ofstream& file) const {
|
||||
for (size_t i = 0; i < MAX_PLAYERS; ++i) {
|
||||
const auto& gamepad = gamepads[i];
|
||||
file << "controller." << i << ".name=" << gamepad.name << "\n";
|
||||
file << "controller." << i << ".player=" << static_cast<int>(gamepad.player_id) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Método helper para parseAndSetController
|
||||
bool setControllerProperty(size_t controller_index,
|
||||
const std::string& property,
|
||||
const std::string& value) {
|
||||
if (controller_index >= MAX_PLAYERS) return false;
|
||||
|
||||
auto& gamepad = gamepads[controller_index];
|
||||
|
||||
if (property == "name") {
|
||||
gamepad.name = value;
|
||||
return true;
|
||||
} else if (property == "player") {
|
||||
try {
|
||||
int player_int = std::stoi(value);
|
||||
if (player_int == 1)
|
||||
gamepad.player_id = Player::Id::PLAYER1;
|
||||
else if (player_int == 2)
|
||||
gamepad.player_id = Player::Id::PLAYER2;
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Iteradores
|
||||
auto begin() { return gamepads.begin(); }
|
||||
auto end() { return gamepads.end(); }
|
||||
auto begin() const { return gamepads.begin(); }
|
||||
auto end() const { return gamepads.end(); }
|
||||
|
||||
size_t size() const { return MAX_PLAYERS; }
|
||||
};
|
||||
|
||||
struct Keyboard {
|
||||
Player::Id player_id = Player::Id::PLAYER1; // Jugador asociado al teclado
|
||||
};
|
||||
@@ -114,25 +226,25 @@ struct PendingChanges {
|
||||
};
|
||||
|
||||
// --- Variables globales ---
|
||||
extern Window window; // Opciones de la ventana
|
||||
extern Settings settings; // Opciones del juego
|
||||
extern Video video; // Opciones de vídeo
|
||||
extern Audio audio; // Opciones de audio
|
||||
extern std::vector<std::shared_ptr<Gamepad>> gamepads; // Opciones de mando para cada jugador
|
||||
extern Keyboard keyboard; // Opciones para el teclado
|
||||
extern PendingChanges pending_changes; // Opciones que se aplican al cerrar
|
||||
extern Window window; // Opciones de la ventana
|
||||
extern Settings settings; // Opciones del juego
|
||||
extern Video video; // Opciones de vídeo
|
||||
extern Audio audio; // Opciones de audio
|
||||
extern GamepadManager gamepads; // Manager de mandos para cada jugador
|
||||
extern Keyboard keyboard; // Opciones para el teclado
|
||||
extern PendingChanges pending_changes; // Opciones que se aplican al cerrar
|
||||
|
||||
// --- Funciones de configuración ---
|
||||
void init(); // Inicializa las opciones del programa
|
||||
void setConfigFile(const std::string &file_path); // Establece el fichero de configuración
|
||||
void setControllersFile(const std::string &file_path); // Establece el fichero de configuración de mandos
|
||||
auto loadFromFile() -> bool; // Carga el fichero de configuración
|
||||
auto saveToFile() -> bool; // Guarda el fichero de configuración
|
||||
void setKeyboardToPlayer(Player::Id player_id); // Asigna el teclado al jugador
|
||||
void swapKeyboard(); // Intercambia el teclado de jugador
|
||||
void swapControllers(); // Intercambia los jugadores asignados a los dos primeros mandos
|
||||
auto getPlayerWhoUsesKeyboard() -> Player::Id; // Averigua quién está usando el teclado
|
||||
void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables
|
||||
void checkPendingChanges(); // Verifica si hay cambios pendientes
|
||||
|
||||
void init(); // Inicializa las opciones del programa
|
||||
void setConfigFile(const std::string& file_path); // Establece el fichero de configuración
|
||||
void setControllersFile(const std::string& file_path); // Establece el fichero de configuración de mandos
|
||||
auto loadFromFile() -> bool; // Carga el fichero de configuración
|
||||
auto saveToFile() -> bool; // Guarda el fichero de configuración
|
||||
void setKeyboardToPlayer(Player::Id player_id); // Asigna el teclado al jugador
|
||||
void swapKeyboard(); // Intercambia el teclado de jugador
|
||||
void swapControllers(); // Intercambia los jugadores asignados a los dos primeros mandos
|
||||
auto getPlayerWhoUsesKeyboard() -> Player::Id; // Averigua quién está usando el teclado
|
||||
void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables
|
||||
void checkPendingChanges(); // Verifica si hay cambios pendientes
|
||||
bool assignGamepadByName(const std::string& gamepad_name, Player::Id player_id); // Buscar y asignar un mando disponible por nombre
|
||||
} // namespace Options
|
||||
Reference in New Issue
Block a user