Pasaeta de linters
This commit is contained in:
158
source/options.h
158
source/options.h
@@ -1,19 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_GamepadButton, SDL_ScaleMode
|
||||
#include <SDL3/SDL.h> // Para SDL_ScaleMode
|
||||
|
||||
#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
|
||||
#include <cstddef> // Para size_t
|
||||
#include <exception> // Para exception
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream, basic_ostream::operator<<, ofstream
|
||||
#include <memory> // Para shared_ptr, swap
|
||||
#include <stdexcept> // Para out_of_range, invalid_argument
|
||||
#include <string> // Para char_traits, string, allocator, operator==, swap, operator<<, basic_string, stoi
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "difficulty.h" // Para Code
|
||||
#include "input.h" // Para InputAction, InputDevice
|
||||
#include "input.h" // Para Input
|
||||
#include "lang.h" // Para Code
|
||||
#include "manage_hiscore_table.h" // Para HiScoreEntry
|
||||
#include "manage_hiscore_table.h" // Para ManageHiScoreTable, Table
|
||||
#include "player.h" // Para Player
|
||||
|
||||
namespace Options {
|
||||
@@ -106,51 +108,39 @@ struct Gamepad {
|
||||
|
||||
// --- 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);
|
||||
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)];
|
||||
auto getGamepad(Player::Id player_id) -> Gamepad& {
|
||||
return gamepads_[playerIdToIndex(player_id)];
|
||||
}
|
||||
|
||||
const Gamepad& getGamepad(Player::Id player_id) const {
|
||||
return gamepads[playerIdToIndex(player_id)];
|
||||
[[nodiscard]] auto getGamepad(Player::Id player_id) const -> const Gamepad& {
|
||||
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];
|
||||
auto operator[](size_t index) -> Gamepad& {
|
||||
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];
|
||||
auto operator[](size_t index) const -> const Gamepad& {
|
||||
if (index >= MAX_PLAYERS) {
|
||||
throw std::out_of_range("Invalid gamepad index");
|
||||
}
|
||||
return gamepads_[index];
|
||||
}
|
||||
|
||||
bool assignGamepadToPlayer(Player::Id player_id,
|
||||
auto assignGamepadToPlayer(Player::Id player_id,
|
||||
std::shared_ptr<Input::Gamepad> instance,
|
||||
const std::string& name) {
|
||||
const std::string& name) -> bool {
|
||||
try {
|
||||
auto& gamepad = getGamepad(player_id);
|
||||
gamepad.instance = instance;
|
||||
@@ -162,15 +152,15 @@ class GamepadManager {
|
||||
}
|
||||
|
||||
void swapPlayers() {
|
||||
std::swap(gamepads[0].instance, gamepads[1].instance);
|
||||
std::swap(gamepads[0].name, gamepads[1].name);
|
||||
std::swap(gamepads[0].path, gamepads[1].path);
|
||||
std::swap(gamepads_[0].instance, gamepads_[1].instance);
|
||||
std::swap(gamepads_[0].name, gamepads_[1].name);
|
||||
std::swap(gamepads_[0].path, gamepads_[1].path);
|
||||
}
|
||||
|
||||
// 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];
|
||||
const auto& gamepad = gamepads_[i];
|
||||
file << "controller." << i << ".name=" << gamepad.name << "\n";
|
||||
file << "controller." << i << ".path=" << gamepad.path << "\n";
|
||||
file << "controller." << i << ".player=" << static_cast<int>(gamepad.player_id) << "\n";
|
||||
@@ -178,28 +168,33 @@ class GamepadManager {
|
||||
}
|
||||
|
||||
// Método helper para parseAndSetController
|
||||
bool setControllerProperty(size_t controller_index,
|
||||
auto setControllerProperty(size_t controller_index,
|
||||
const std::string& property,
|
||||
const std::string& value) {
|
||||
if (controller_index >= MAX_PLAYERS) return false;
|
||||
const std::string& value) -> bool {
|
||||
if (controller_index >= MAX_PLAYERS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& gamepad = gamepads[controller_index];
|
||||
auto& gamepad = gamepads_[controller_index];
|
||||
|
||||
if (property == "name") {
|
||||
gamepad.name = value;
|
||||
return true;
|
||||
} else if (property == "path") {
|
||||
}
|
||||
if (property == "path") {
|
||||
gamepad.path = value;
|
||||
return true;
|
||||
} else if (property == "player") {
|
||||
}
|
||||
if (property == "player") {
|
||||
try {
|
||||
int player_int = std::stoi(value);
|
||||
if (player_int == 1)
|
||||
if (player_int == 1) {
|
||||
gamepad.player_id = Player::Id::PLAYER1;
|
||||
else if (player_int == 2)
|
||||
} else if (player_int == 2) {
|
||||
gamepad.player_id = Player::Id::PLAYER2;
|
||||
else
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception&) {
|
||||
return false;
|
||||
@@ -213,12 +208,39 @@ class GamepadManager {
|
||||
void assignAndLinkGamepads();
|
||||
|
||||
// Iteradores
|
||||
auto begin() { return gamepads.begin(); }
|
||||
auto end() { return gamepads.end(); }
|
||||
auto begin() const { return gamepads.begin(); }
|
||||
auto end() const { return gamepads.end(); }
|
||||
auto begin() { return gamepads_.begin(); }
|
||||
auto end() { return gamepads_.end(); }
|
||||
[[nodiscard]] auto begin() const { return gamepads_.begin(); }
|
||||
[[nodiscard]] auto end() const { return gamepads_.end(); }
|
||||
|
||||
size_t size() const { return MAX_PLAYERS; }
|
||||
[[nodiscard]] static auto size() -> size_t { return MAX_PLAYERS; }
|
||||
|
||||
private:
|
||||
static constexpr size_t MAX_PLAYERS = 2;
|
||||
std::array<Gamepad, MAX_PLAYERS> gamepads_;
|
||||
|
||||
// Convierte Player::Id a índice del array
|
||||
[[nodiscard]] static auto playerIdToIndex(Player::Id player_id) -> size_t {
|
||||
switch (player_id) {
|
||||
case Player::Id::PLAYER1:
|
||||
return 0;
|
||||
case Player::Id::PLAYER2:
|
||||
return 1;
|
||||
default:
|
||||
throw std::invalid_argument("Invalid player ID");
|
||||
}
|
||||
}
|
||||
|
||||
void assignGamepadsByPath(
|
||||
const std::array<std::string, MAX_PLAYERS>& desired_paths,
|
||||
const std::vector<std::shared_ptr<Input::Gamepad>>& physical_gamepads,
|
||||
std::vector<std::shared_ptr<Input::Gamepad>>& assigned_instances);
|
||||
void assignRemainingGamepads(
|
||||
const std::vector<std::shared_ptr<Input::Gamepad>>& physical_gamepads,
|
||||
std::vector<std::shared_ptr<Input::Gamepad>>& assigned_instances);
|
||||
[[nodiscard]] static auto isGamepadAssigned(
|
||||
const std::shared_ptr<Input::Gamepad>& physical_gamepad,
|
||||
const std::vector<std::shared_ptr<Input::Gamepad>>& assigned_instances) -> bool;
|
||||
};
|
||||
|
||||
struct Keyboard {
|
||||
@@ -245,16 +267,16 @@ 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
|
||||
bool assignGamepadByName(const std::string& gamepad_name, Player::Id player_id); // Buscar y asignar un mando disponible por nombre
|
||||
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
|
||||
auto assignGamepadByName(const std::string& gamepad_name, Player::Id player_id) -> bool; // Buscar y asignar un mando disponible por nombre
|
||||
} // namespace Options
|
||||
Reference in New Issue
Block a user