Pasaeta de linters
This commit is contained in:
110
source/input.cpp
110
source/input.cpp
@@ -1,13 +1,12 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_GetGamepa...
|
||||
#include <SDL3/SDL.h> // Para SDL_GamepadButton, SDL_GetGamepadAxis, SDL_GetError, SDL_GamepadAxis, SDL_JoystickID, SDL_AddGamepadMappingsFromFile, SDL_Event, SDL_EventType, SDL_GetGamepadButton, SDL_GetKeyboardState, SDL_INIT_GAMEPAD, SDL_InitSubSystem, SDL_LogCategory, SDL_LogError, SDL_LogInfo, SDL_OpenGamepad, SDL_PollEvent, SDL_WasInit, SDL_Gamepad, SDL_Scancode
|
||||
|
||||
#include <algorithm> // Para find
|
||||
#include <cstddef> // Para size_t
|
||||
#include <iterator> // Para distance
|
||||
#include <memory> // Para std::unique_ptr
|
||||
#include <unordered_map> // Para unordered_map, _Node_const_iterator, operat...
|
||||
#include <utility> // Para pair
|
||||
#include <algorithm> // Para find_if, remove_if
|
||||
#include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr
|
||||
#include <memory> // Para shared_ptr, __shared_ptr_access, allocator, operator==, make_shared
|
||||
#include <unordered_map> // Para unordered_map, operator==, _Node_iterator_base, _Node_iterator, _Node_const_iterator
|
||||
#include <utility> // Para pair, move
|
||||
|
||||
// Singleton
|
||||
Input *Input::instance = nullptr;
|
||||
@@ -29,9 +28,6 @@ Input::Input(std::string game_controller_db_path, std::string gamepad_configs_fi
|
||||
gamepad_configs_file_(std::move(gamepad_configs_file)) {
|
||||
// Inicializa el subsistema SDL_INIT_GAMEPAD
|
||||
initSDLGamePad();
|
||||
|
||||
// Listado de los inputs para jugar que utilizan botones, ni palancas ni crucetas
|
||||
button_inputs_ = {Action::FIRE_LEFT, Action::FIRE_CENTER, Action::FIRE_RIGHT, Action::START};
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
@@ -47,13 +43,13 @@ void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action ac
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source) {
|
||||
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action action_target, Action action_source) {
|
||||
if (gamepad != nullptr) {
|
||||
gamepad->bindings[input_target].button = gamepad->bindings[input_source].button;
|
||||
gamepad->bindings[action_target].button = gamepad->bindings[action_source].button;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
// Comprueba si alguna acción está activa
|
||||
auto Input::checkAction(Action action, bool repeat, bool check_keyboard, std::shared_ptr<Gamepad> gamepad) -> bool {
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
@@ -81,7 +77,7 @@ auto Input::checkAction(Action action, bool repeat, bool check_keyboard, std::sh
|
||||
return (success_keyboard || success_controller);
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
// Comprueba si hay almenos una acción activa
|
||||
auto Input::checkAnyInput(bool check_keyboard, std::shared_ptr<Gamepad> gamepad) -> bool {
|
||||
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
|
||||
|
||||
@@ -115,7 +111,7 @@ auto Input::checkAnyInput(bool check_keyboard, std::shared_ptr<Gamepad> gamepad)
|
||||
// Comprueba si hay algún botón pulsado
|
||||
auto Input::checkAnyButton(bool repeat) -> bool {
|
||||
// Solo comprueba los botones definidos previamente
|
||||
for (auto bi : button_inputs_) {
|
||||
for (auto bi : BUTTON_INPUTS) {
|
||||
// Comprueba el teclado
|
||||
if (checkAction(bi, repeat, CHECK_KEYBOARD)) {
|
||||
return true;
|
||||
@@ -136,7 +132,7 @@ auto Input::checkAnyButton(bool repeat) -> bool {
|
||||
auto Input::gameControllerFound() const -> bool { return !gamepads_.empty(); }
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
auto Input::getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string { return gamepad == nullptr ? std::string() : gamepad->name; }
|
||||
auto Input::getControllerName(std::shared_ptr<Gamepad> gamepad) -> std::string { return gamepad == nullptr ? std::string() : gamepad->name; }
|
||||
|
||||
// Obtiene la lista de nombres de mandos
|
||||
auto Input::getControllerNames() const -> std::vector<std::string> {
|
||||
@@ -151,7 +147,7 @@ auto Input::getControllerNames() const -> std::vector<std::string> {
|
||||
auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
|
||||
|
||||
// Obtiene el gamepad a partir de un event.id
|
||||
std::shared_ptr<Input::Gamepad> Input::getGamepad(SDL_JoystickID id) const {
|
||||
auto Input::getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Input::Gamepad> {
|
||||
for (const auto &gamepad : gamepads_) {
|
||||
if (gamepad->instance_id == id) {
|
||||
return gamepad;
|
||||
@@ -160,7 +156,7 @@ std::shared_ptr<Input::Gamepad> Input::getGamepad(SDL_JoystickID id) const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<Input::Gamepad> Input::getGamepadByName(const std::string &name) const {
|
||||
auto Input::getGamepadByName(const std::string &name) const -> std::shared_ptr<Input::Gamepad> {
|
||||
for (const auto &gamepad : gamepads_) {
|
||||
if (gamepad && gamepad->name == name) {
|
||||
return gamepad;
|
||||
@@ -169,14 +165,14 @@ std::shared_ptr<Input::Gamepad> Input::getGamepadByName(const std::string &name)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Obtiene el SDL_GamepadButton asignado a un input
|
||||
auto Input::getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton {
|
||||
return gamepad->bindings[input].button;
|
||||
// Obtiene el SDL_GamepadButton asignado a un action
|
||||
auto Input::getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action action) -> SDL_GamepadButton {
|
||||
return gamepad->bindings[action].button;
|
||||
}
|
||||
|
||||
// Convierte un InputAction a std::string
|
||||
auto Input::inputToString(Action input) -> std::string {
|
||||
switch (input) {
|
||||
auto Input::inputToString(Action action) -> std::string {
|
||||
switch (action) {
|
||||
case Action::FIRE_LEFT:
|
||||
return "input_fire_left";
|
||||
case Action::FIRE_CENTER:
|
||||
@@ -206,11 +202,11 @@ auto Input::stringToInput(const std::string &name) -> Action {
|
||||
}
|
||||
|
||||
// Comprueba el eje del mando
|
||||
auto Input::checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool {
|
||||
auto Input::checkAxisInput(Action action, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool {
|
||||
// Umbral para considerar el eje como activo
|
||||
bool axis_active_now = false;
|
||||
|
||||
switch (input) {
|
||||
switch (action) {
|
||||
case Action::LEFT:
|
||||
axis_active_now = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_LEFTX) < -AXIS_THRESHOLD;
|
||||
break;
|
||||
@@ -228,7 +224,7 @@ auto Input::checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool
|
||||
}
|
||||
|
||||
// Referencia al binding correspondiente
|
||||
auto &binding = gamepad->bindings[input];
|
||||
auto &binding = gamepad->bindings[action];
|
||||
|
||||
if (repeat) {
|
||||
// Si se permite repetir, simplemente devolvemos el estado actual
|
||||
@@ -315,17 +311,17 @@ void Input::update() {
|
||||
void Input::handleEvent(const SDL_Event &event) {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_GAMEPAD_ADDED:
|
||||
add_gamepad(event.gdevice.which);
|
||||
addGamepad(event.gdevice.which);
|
||||
break;
|
||||
case SDL_EVENT_GAMEPAD_REMOVED:
|
||||
remove_gamepad(event.gdevice.which);
|
||||
removeGamepad(event.gdevice.which);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Input::add_gamepad(int device_index) {
|
||||
void Input::addGamepad(int device_index) {
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(device_index);
|
||||
if (!pad) {
|
||||
if (pad == nullptr) {
|
||||
std::cerr << "Error al abrir el gamepad: " << SDL_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
@@ -337,7 +333,7 @@ void Input::add_gamepad(int device_index) {
|
||||
gamepads_.push_back(std::move(gamepad));
|
||||
}
|
||||
|
||||
void Input::remove_gamepad(SDL_JoystickID id) {
|
||||
void Input::removeGamepad(SDL_JoystickID id) {
|
||||
auto it = std::remove_if(gamepads_.begin(), gamepads_.end(), [id](const std::shared_ptr<Gamepad> &gamepad) {
|
||||
return gamepad->instance_id == id;
|
||||
});
|
||||
@@ -358,7 +354,7 @@ void Input::printConnectedGamepads() const {
|
||||
|
||||
std::cout << "Gamepads conectados:\n";
|
||||
for (const auto &gamepad : gamepads_) {
|
||||
std::string name = gamepad->name == "" ? "Desconocido" : gamepad->name;
|
||||
std::string name = gamepad->name.empty() ? "Desconocido" : gamepad->name;
|
||||
std::cout << " - ID: " << gamepad->instance_id
|
||||
<< ", Nombre: " << name << ")" << std::endl;
|
||||
}
|
||||
@@ -375,20 +371,19 @@ void Input::saveGamepadConfigs() {
|
||||
}
|
||||
|
||||
void Input::applyGamepadConfig(std::shared_ptr<Gamepad> gamepad) {
|
||||
if (!gamepad || gamepad->path.empty()) { // No podemos aplicar config sin una ruta
|
||||
if (!gamepad || gamepad->path.empty()) { // No podemos aplicar config sin una ruta
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 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;
|
||||
auto config_it = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad](const GamepadConfig &config) {
|
||||
return config.path == gamepad->path;
|
||||
});
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
if (config_it != gamepad_configs_.end()) {
|
||||
// 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) {
|
||||
for (const auto &[action, button] : config_it->bindings) {
|
||||
if (gamepad->bindings.find(action) != gamepad->bindings.end()) {
|
||||
gamepad->bindings[action].button = button;
|
||||
}
|
||||
@@ -398,31 +393,30 @@ void Input::applyGamepadConfig(std::shared_ptr<Gamepad> gamepad) {
|
||||
}
|
||||
|
||||
void Input::saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad) {
|
||||
if (!gamepad || gamepad->path.empty()) { // No podemos guardar una config sin una ruta
|
||||
if (!gamepad || gamepad->path.empty()) { // No podemos guardar una config sin una ruta
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 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;
|
||||
auto config_it = 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, incluyendo nombre y ruta
|
||||
GamepadConfig newConfig(gamepad->name, gamepad->path); // <--- CAMBIO: Pasamos ambos
|
||||
newConfig.bindings.clear();
|
||||
GamepadConfig new_config(gamepad->name, gamepad->path); // <--- CAMBIO: Pasamos ambos
|
||||
new_config.bindings.clear();
|
||||
|
||||
// Copiar todos los bindings actuales del gamepad
|
||||
for (const auto &[action, buttonState] : gamepad->bindings) {
|
||||
newConfig.bindings[action] = buttonState.button;
|
||||
new_config.bindings[action] = buttonState.button;
|
||||
}
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
if (config_it != gamepad_configs_.end()) {
|
||||
// Sobreescribir configuración existente para esta ruta
|
||||
*configIt = newConfig;
|
||||
*config_it = new_config;
|
||||
} else {
|
||||
// Añadir nueva configuración
|
||||
gamepad_configs_.push_back(newConfig);
|
||||
gamepad_configs_.push_back(new_config);
|
||||
}
|
||||
|
||||
// Guardar cambios inmediatamente
|
||||
@@ -436,22 +430,22 @@ void Input::setGamepadConfigsFile(const std::string &filename) {
|
||||
}
|
||||
|
||||
// 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;
|
||||
auto Input::getGamepadConfig(const std::string &gamepad_name) -> GamepadConfig * {
|
||||
auto config_it = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad_name](const GamepadConfig &config) {
|
||||
return config.name == gamepad_name;
|
||||
});
|
||||
|
||||
return (configIt != gamepad_configs_.end()) ? &(*configIt) : nullptr;
|
||||
return (config_it != gamepad_configs_.end()) ? &(*config_it) : 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;
|
||||
auto Input::removeGamepadConfig(const std::string &gamepad_name) -> bool {
|
||||
auto config_it = std::find_if(gamepad_configs_.begin(), gamepad_configs_.end(), [&gamepad_name](const GamepadConfig &config) {
|
||||
return config.name == gamepad_name;
|
||||
});
|
||||
|
||||
if (configIt != gamepad_configs_.end()) {
|
||||
gamepad_configs_.erase(configIt);
|
||||
if (config_it != gamepad_configs_.end()) {
|
||||
gamepad_configs_.erase(config_it);
|
||||
saveGamepadConfigs();
|
||||
return true;
|
||||
}
|
||||
@@ -459,7 +453,7 @@ bool Input::removeGamepadConfig(const std::string &gamepadName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<Input::Gamepad> Input::findAvailableGamepadByName(const std::string &gamepad_name) {
|
||||
auto Input::findAvailableGamepadByName(const std::string &gamepad_name) -> std::shared_ptr<Input::Gamepad> {
|
||||
// Si no hay gamepads disponibles, devolver gamepad por defecto
|
||||
if (gamepads_.empty()) {
|
||||
return nullptr;
|
||||
|
||||
Reference in New Issue
Block a user