canvi de pc
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
*/
|
||||
};
|
||||
262
source/input.h
262
source/input.h
@@ -1,21 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_GamepadButton, Uint8, SDL_Gamepad, SDL_Joystick, SDL_JoystickID, SDL_Scancode, Sint16
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <iostream>
|
||||
#include <memory> // Para std::unique_ptr
|
||||
#include <string> // Para basic_string, string
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector> // Para vector
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
connectedControllers es un vector donde están todos los mandos encontrados [0 .. n]
|
||||
checkInput requiere de un índice para comprobar las pulsaciones de un controlador en concreto [0 .. n]
|
||||
device contiene el tipo de dispositivo a comprobar:
|
||||
InputDeviceToUse::KEYBOARD solo mirará el teclado
|
||||
InputDeviceToUse::CONTROLLER solo mirará el controlador especificado (Si no se especifica, el primero)
|
||||
InputDeviceToUse::ANY mirará tanto el teclado como el PRIMER controlador
|
||||
*/
|
||||
#include "input_types.h"
|
||||
|
||||
// Forward declaration para evitar dependencia circular
|
||||
struct GamepadConfig;
|
||||
class GamepadConfigManager;
|
||||
|
||||
// Clase Input: gestiona la entrada de teclado y mandos (singleton)
|
||||
class Input {
|
||||
@@ -27,201 +23,167 @@ class Input {
|
||||
static constexpr bool CHECK_KEYBOARD = true;
|
||||
static constexpr bool DO_NOT_CHECK_KEYBOARD = false;
|
||||
|
||||
// Acciones de entrada posibles en el juego
|
||||
enum class Action : int {
|
||||
// Inputs de movimiento
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
|
||||
// Inputs personalizados
|
||||
FIRE_LEFT,
|
||||
FIRE_CENTER,
|
||||
FIRE_RIGHT,
|
||||
START,
|
||||
|
||||
// Service Menu
|
||||
SM_SELECT,
|
||||
SM_BACK,
|
||||
|
||||
// Inputs de control
|
||||
BACK,
|
||||
EXIT,
|
||||
PAUSE,
|
||||
SERVICE,
|
||||
WINDOW_FULLSCREEN,
|
||||
WINDOW_INC_SIZE,
|
||||
WINDOW_DEC_SIZE,
|
||||
TOGGLE_VIDEO_SHADERS,
|
||||
TOGGLE_VIDEO_INTEGER_SCALE,
|
||||
TOGGLE_VIDEO_VSYNC,
|
||||
RESET,
|
||||
TOGGLE_AUDIO,
|
||||
CHANGE_LANG,
|
||||
SHOW_INFO,
|
||||
CONFIG,
|
||||
SWAP_CONTROLLERS,
|
||||
TOGGLE_AUTO_FIRE,
|
||||
|
||||
// Input obligatorio
|
||||
NONE,
|
||||
SIZE,
|
||||
};
|
||||
// Alias para mantener compatibilidad con el código existente
|
||||
using Action = InputAction;
|
||||
|
||||
// --- Estructuras ---
|
||||
struct KeyState {
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
|
||||
KeyState(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
|
||||
: scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
|
||||
KeyState(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
|
||||
: scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
|
||||
};
|
||||
|
||||
struct ButtonState {
|
||||
SDL_GamepadButton button; // GameControllerButton asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
bool axis_active; // Estado del eje
|
||||
SDL_GamepadButton button; // GameControllerButton asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
bool axis_active; // Estado del eje
|
||||
|
||||
ButtonState(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
|
||||
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
|
||||
ButtonState(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
|
||||
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
|
||||
};
|
||||
|
||||
struct Keyboard {
|
||||
std::unordered_map<Input::Action, KeyState> bindings;
|
||||
std::unordered_map<InputAction, KeyState> bindings;
|
||||
|
||||
Keyboard()
|
||||
: bindings{
|
||||
// Teclado - Movimiento del jugador
|
||||
{Input::Action::UP, KeyState(SDL_SCANCODE_UP)},
|
||||
{Input::Action::DOWN, KeyState(SDL_SCANCODE_DOWN)},
|
||||
{Input::Action::LEFT, KeyState(SDL_SCANCODE_LEFT)},
|
||||
{Input::Action::RIGHT, KeyState(SDL_SCANCODE_RIGHT)},
|
||||
Keyboard()
|
||||
: bindings{
|
||||
// Teclado - Movimiento del jugador
|
||||
{InputAction::UP, KeyState(SDL_SCANCODE_UP)},
|
||||
{InputAction::DOWN, KeyState(SDL_SCANCODE_DOWN)},
|
||||
{InputAction::LEFT, KeyState(SDL_SCANCODE_LEFT)},
|
||||
{InputAction::RIGHT, KeyState(SDL_SCANCODE_RIGHT)},
|
||||
|
||||
// Teclado - Disparo del jugador
|
||||
{Input::Action::FIRE_LEFT, KeyState(SDL_SCANCODE_Q)},
|
||||
{Input::Action::FIRE_CENTER, KeyState(SDL_SCANCODE_W)},
|
||||
{Input::Action::FIRE_RIGHT, KeyState(SDL_SCANCODE_E)},
|
||||
// Teclado - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, KeyState(SDL_SCANCODE_Q)},
|
||||
{InputAction::FIRE_CENTER, KeyState(SDL_SCANCODE_W)},
|
||||
{InputAction::FIRE_RIGHT, KeyState(SDL_SCANCODE_E)},
|
||||
|
||||
// Teclado - Interfaz
|
||||
{Input::Action::START, KeyState(SDL_SCANCODE_RETURN)},
|
||||
// Teclado - Interfaz
|
||||
{InputAction::START, KeyState(SDL_SCANCODE_RETURN)},
|
||||
|
||||
// Teclado - Menu de servicio
|
||||
{Input::Action::SERVICE, KeyState(SDL_SCANCODE_0)},
|
||||
{Input::Action::SM_SELECT, KeyState(SDL_SCANCODE_RETURN)},
|
||||
{Input::Action::SM_BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
// Teclado - Menu de servicio
|
||||
{InputAction::SERVICE, KeyState(SDL_SCANCODE_0)},
|
||||
{InputAction::SM_SELECT, KeyState(SDL_SCANCODE_RETURN)},
|
||||
{InputAction::SM_BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
|
||||
// Teclado - Control del programa
|
||||
{Input::Action::EXIT, KeyState(SDL_SCANCODE_ESCAPE)},
|
||||
{Input::Action::PAUSE, KeyState(SDL_SCANCODE_P)},
|
||||
{Input::Action::BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
// Teclado - Control del programa
|
||||
{InputAction::EXIT, KeyState(SDL_SCANCODE_ESCAPE)},
|
||||
{InputAction::PAUSE, KeyState(SDL_SCANCODE_P)},
|
||||
{InputAction::BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
|
||||
{Input::Action::WINDOW_DEC_SIZE, KeyState(SDL_SCANCODE_F1)},
|
||||
{Input::Action::WINDOW_INC_SIZE, KeyState(SDL_SCANCODE_F2)},
|
||||
{Input::Action::WINDOW_FULLSCREEN, KeyState(SDL_SCANCODE_F3)},
|
||||
{Input::Action::TOGGLE_VIDEO_SHADERS, KeyState(SDL_SCANCODE_F4)},
|
||||
{Input::Action::TOGGLE_VIDEO_INTEGER_SCALE, KeyState(SDL_SCANCODE_F5)},
|
||||
{Input::Action::TOGGLE_VIDEO_VSYNC, KeyState(SDL_SCANCODE_F6)},
|
||||
{InputAction::WINDOW_DEC_SIZE, KeyState(SDL_SCANCODE_F1)},
|
||||
{InputAction::WINDOW_INC_SIZE, KeyState(SDL_SCANCODE_F2)},
|
||||
{InputAction::WINDOW_FULLSCREEN, KeyState(SDL_SCANCODE_F3)},
|
||||
{InputAction::TOGGLE_VIDEO_SHADERS, KeyState(SDL_SCANCODE_F4)},
|
||||
{InputAction::TOGGLE_VIDEO_INTEGER_SCALE, KeyState(SDL_SCANCODE_F5)},
|
||||
{InputAction::TOGGLE_VIDEO_VSYNC, KeyState(SDL_SCANCODE_F6)},
|
||||
|
||||
{Input::Action::TOGGLE_AUDIO, KeyState(SDL_SCANCODE_F7)},
|
||||
{Input::Action::TOGGLE_AUTO_FIRE, KeyState(SDL_SCANCODE_F8)},
|
||||
{Input::Action::CHANGE_LANG, KeyState(SDL_SCANCODE_F9)},
|
||||
{InputAction::TOGGLE_AUDIO, KeyState(SDL_SCANCODE_F7)},
|
||||
{InputAction::TOGGLE_AUTO_FIRE, KeyState(SDL_SCANCODE_F8)},
|
||||
{InputAction::CHANGE_LANG, KeyState(SDL_SCANCODE_F9)},
|
||||
|
||||
{Input::Action::RESET, KeyState(SDL_SCANCODE_F10)},
|
||||
{Input::Action::SHOW_INFO, KeyState(SDL_SCANCODE_F12)}} {}
|
||||
{InputAction::RESET, KeyState(SDL_SCANCODE_F10)},
|
||||
{InputAction::SHOW_INFO, KeyState(SDL_SCANCODE_F12)}} {}
|
||||
};
|
||||
|
||||
struct Gamepad {
|
||||
SDL_Gamepad *pad;
|
||||
SDL_JoystickID instance_id;
|
||||
std::string name;
|
||||
std::unordered_map<Input::Action, ButtonState> bindings;
|
||||
SDL_Gamepad *pad;
|
||||
SDL_JoystickID instance_id;
|
||||
std::string name;
|
||||
std::unordered_map<InputAction, ButtonState> bindings;
|
||||
|
||||
Gamepad(SDL_Gamepad *gamepad)
|
||||
: pad(gamepad),
|
||||
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
|
||||
name(std::string(SDL_GetGamepadName(gamepad)) + " #" + std::to_string(instance_id)),
|
||||
bindings{
|
||||
// Mando - Movimiento del jugador
|
||||
{Input::Action::UP, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_UP)},
|
||||
{Input::Action::DOWN, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_DOWN)},
|
||||
{Input::Action::LEFT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_LEFT)},
|
||||
{Input::Action::RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)},
|
||||
Gamepad(SDL_Gamepad *gamepad)
|
||||
: pad(gamepad),
|
||||
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
|
||||
name(std::string(SDL_GetGamepadName(gamepad))),
|
||||
bindings{
|
||||
// Mando - Movimiento del jugador
|
||||
{InputAction::UP, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_UP)},
|
||||
{InputAction::DOWN, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_DOWN)},
|
||||
{InputAction::LEFT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_LEFT)},
|
||||
{InputAction::RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)},
|
||||
|
||||
// Mando - Disparo del jugador
|
||||
{Input::Action::FIRE_LEFT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
|
||||
{Input::Action::FIRE_CENTER, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)},
|
||||
{Input::Action::FIRE_RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_EAST)},
|
||||
// Mando - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
|
||||
{InputAction::FIRE_CENTER, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)},
|
||||
{InputAction::FIRE_RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_EAST)},
|
||||
|
||||
// Mando - Interfaz
|
||||
{Input::Action::START, ButtonState(SDL_GAMEPAD_BUTTON_START)},
|
||||
{Input::Action::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)}} {}
|
||||
// Mando - Interfaz
|
||||
{InputAction::START, ButtonState(SDL_GAMEPAD_BUTTON_START)},
|
||||
{InputAction::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)}} {}
|
||||
|
||||
~Gamepad() {
|
||||
if (pad) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
~Gamepad() {
|
||||
if (pad) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// --- Métodos de singleton ---
|
||||
static void init(const std::string &game_controller_db_path); // Inicializa el singleton
|
||||
static void destroy(); // Libera el singleton
|
||||
static auto get() -> Input *; // Obtiene la instancia
|
||||
static void init(const std::string &game_controller_db_path);
|
||||
static void destroy();
|
||||
static auto get() -> Input *;
|
||||
|
||||
// --- Métodos de configuración de controles ---
|
||||
void bindKey(Action input, SDL_Scancode code); // Asigna inputs a teclas
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button); // Asigna inputs a botones del mando
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source); // Asigna inputs a otros inputs del mando
|
||||
void bindKey(Action input, SDL_Scancode code);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source);
|
||||
|
||||
// --- Métodos de consulta de entrada ---
|
||||
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
|
||||
auto checkAction(Action input, bool repeat = true, bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool; // Comprueba si un input está activo
|
||||
auto checkAnyInput(bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool; // Comprueba si hay al menos un input activo
|
||||
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool; // Comprueba si hay algún botón pulsado
|
||||
void update();
|
||||
auto checkAction(Action input, bool repeat = true, bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyInput(bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
|
||||
|
||||
// --- Métodos de gestión de mandos ---
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool; // Comprueba si hay algún mando conectado
|
||||
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string; // Obten el nombre de un mando de juego
|
||||
[[nodiscard]] auto getNumControllers() const -> int; // Obtiene el número de mandos conectados
|
||||
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int; // Obtiene el índice del controlador a partir de un event.id
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool;
|
||||
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
|
||||
[[nodiscard]] auto getNumControllers() const -> int;
|
||||
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int;
|
||||
|
||||
// --- Métodos de consulta y utilidades ---
|
||||
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton; // Obtiene el SDL_GamepadButton asignado a un input
|
||||
[[nodiscard]] static auto inputToString(Action input) -> std::string; // Convierte un InputAction a std::string
|
||||
[[nodiscard]] static auto stringToInput(const std::string &name) -> Action; // Convierte un std::string a InputAction
|
||||
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;
|
||||
[[nodiscard]] static auto inputToString(Action input) -> std::string;
|
||||
[[nodiscard]] static auto stringToInput(const std::string &name) -> Action;
|
||||
|
||||
// --- Métodos de reseteo de estado de entrada ---
|
||||
void resetInputStates(); // Pone todos los KeyBindings.active y ControllerBindings.active a false
|
||||
void resetInputStates();
|
||||
|
||||
// --- Eventos ---
|
||||
void handleEvent(const SDL_Event &event); // Comprueba si se conecta algun mando
|
||||
void handleEvent(const SDL_Event &event);
|
||||
|
||||
void printConnectedGamepads() const;
|
||||
|
||||
[[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;
|
||||
|
||||
// --- Variables internas ---
|
||||
std::vector<std::shared_ptr<Gamepad>> gamepads_; // Mandos conectados
|
||||
Keyboard keyboard_; // Estructura con las asociaciones de teclas a acciones
|
||||
std::vector<Action> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
|
||||
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
|
||||
std::vector<std::shared_ptr<Gamepad>> gamepads_;
|
||||
Keyboard keyboard_;
|
||||
std::vector<Action> button_inputs_;
|
||||
std::string game_controller_db_path_;
|
||||
|
||||
// --- Métodos internos ---
|
||||
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
|
||||
auto checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool; // Comprueba el eje del mando
|
||||
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);
|
||||
|
||||
// --- Constructor y destructor ---
|
||||
explicit Input(std::string game_controller_db_path); // Constructor privado
|
||||
~Input() = default; // Destructor privado
|
||||
explicit Input(std::string game_controller_db_path);
|
||||
~Input() = default;
|
||||
|
||||
// --- Singleton ---
|
||||
static Input *instance;
|
||||
|
||||
94
source/input_types.cpp
Normal file
94
source/input_types.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "input_types.h"
|
||||
|
||||
// Definición de los mapas
|
||||
const std::unordered_map<InputAction, std::string> actionToString = {
|
||||
{InputAction::FIRE_LEFT, "FIRE_LEFT"},
|
||||
{InputAction::FIRE_CENTER, "FIRE_CENTER"},
|
||||
{InputAction::FIRE_RIGHT, "FIRE_RIGHT"},
|
||||
{InputAction::START, "START"},
|
||||
{InputAction::SERVICE, "SERVICE"},
|
||||
{InputAction::UP, "UP"},
|
||||
{InputAction::DOWN, "DOWN"},
|
||||
{InputAction::LEFT, "LEFT"},
|
||||
{InputAction::RIGHT, "RIGHT"},
|
||||
{InputAction::SM_SELECT, "SM_SELECT"},
|
||||
{InputAction::SM_BACK, "SM_BACK"},
|
||||
{InputAction::BACK, "BACK"},
|
||||
{InputAction::EXIT, "EXIT"},
|
||||
{InputAction::PAUSE, "PAUSE"},
|
||||
{InputAction::WINDOW_FULLSCREEN, "WINDOW_FULLSCREEN"},
|
||||
{InputAction::WINDOW_INC_SIZE, "WINDOW_INC_SIZE"},
|
||||
{InputAction::WINDOW_DEC_SIZE, "WINDOW_DEC_SIZE"},
|
||||
{InputAction::TOGGLE_VIDEO_SHADERS, "TOGGLE_VIDEO_SHADERS"},
|
||||
{InputAction::TOGGLE_VIDEO_INTEGER_SCALE, "TOGGLE_VIDEO_INTEGER_SCALE"},
|
||||
{InputAction::TOGGLE_VIDEO_VSYNC, "TOGGLE_VIDEO_VSYNC"},
|
||||
{InputAction::RESET, "RESET"},
|
||||
{InputAction::TOGGLE_AUDIO, "TOGGLE_AUDIO"},
|
||||
{InputAction::CHANGE_LANG, "CHANGE_LANG"},
|
||||
{InputAction::SHOW_INFO, "SHOW_INFO"},
|
||||
{InputAction::CONFIG, "CONFIG"},
|
||||
{InputAction::SWAP_CONTROLLERS, "SWAP_CONTROLLERS"},
|
||||
{InputAction::TOGGLE_AUTO_FIRE, "TOGGLE_AUTO_FIRE"},
|
||||
{InputAction::NONE, "NONE"}
|
||||
};
|
||||
|
||||
const std::unordered_map<std::string, InputAction> stringToAction = {
|
||||
{"FIRE_LEFT", InputAction::FIRE_LEFT},
|
||||
{"FIRE_CENTER", InputAction::FIRE_CENTER},
|
||||
{"FIRE_RIGHT", InputAction::FIRE_RIGHT},
|
||||
{"START", InputAction::START},
|
||||
{"SERVICE", InputAction::SERVICE},
|
||||
{"UP", InputAction::UP},
|
||||
{"DOWN", InputAction::DOWN},
|
||||
{"LEFT", InputAction::LEFT},
|
||||
{"RIGHT", InputAction::RIGHT},
|
||||
{"SM_SELECT", InputAction::SM_SELECT},
|
||||
{"SM_BACK", InputAction::SM_BACK},
|
||||
{"BACK", InputAction::BACK},
|
||||
{"EXIT", InputAction::EXIT},
|
||||
{"PAUSE", InputAction::PAUSE},
|
||||
{"WINDOW_FULLSCREEN", InputAction::WINDOW_FULLSCREEN},
|
||||
{"WINDOW_INC_SIZE", InputAction::WINDOW_INC_SIZE},
|
||||
{"WINDOW_DEC_SIZE", InputAction::WINDOW_DEC_SIZE},
|
||||
{"TOGGLE_VIDEO_SHADERS", InputAction::TOGGLE_VIDEO_SHADERS},
|
||||
{"TOGGLE_VIDEO_INTEGER_SCALE", InputAction::TOGGLE_VIDEO_INTEGER_SCALE},
|
||||
{"TOGGLE_VIDEO_VSYNC", InputAction::TOGGLE_VIDEO_VSYNC},
|
||||
{"RESET", InputAction::RESET},
|
||||
{"TOGGLE_AUDIO", InputAction::TOGGLE_AUDIO},
|
||||
{"CHANGE_LANG", InputAction::CHANGE_LANG},
|
||||
{"SHOW_INFO", InputAction::SHOW_INFO},
|
||||
{"CONFIG", InputAction::CONFIG},
|
||||
{"SWAP_CONTROLLERS", InputAction::SWAP_CONTROLLERS},
|
||||
{"TOGGLE_AUTO_FIRE", InputAction::TOGGLE_AUTO_FIRE},
|
||||
{"NONE", InputAction::NONE}
|
||||
};
|
||||
|
||||
const 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"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_UP, "DPAD_UP"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_DOWN, "DPAD_DOWN"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_LEFT, "DPAD_LEFT"},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_RIGHT, "DPAD_RIGHT"}
|
||||
};
|
||||
|
||||
const 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},
|
||||
{"DPAD_UP", SDL_GAMEPAD_BUTTON_DPAD_UP},
|
||||
{"DPAD_DOWN", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
|
||||
{"DPAD_LEFT", SDL_GAMEPAD_BUTTON_DPAD_LEFT},
|
||||
{"DPAD_RIGHT", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}
|
||||
};
|
||||
53
source/input_types.h
Normal file
53
source/input_types.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
// Acciones de entrada posibles en el juego
|
||||
enum class InputAction : int {
|
||||
// Inputs de movimiento
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
|
||||
// Inputs personalizados
|
||||
FIRE_LEFT,
|
||||
FIRE_CENTER,
|
||||
FIRE_RIGHT,
|
||||
START,
|
||||
|
||||
// Service Menu
|
||||
SM_SELECT,
|
||||
SM_BACK,
|
||||
|
||||
// Inputs de control
|
||||
BACK,
|
||||
EXIT,
|
||||
PAUSE,
|
||||
SERVICE,
|
||||
WINDOW_FULLSCREEN,
|
||||
WINDOW_INC_SIZE,
|
||||
WINDOW_DEC_SIZE,
|
||||
TOGGLE_VIDEO_SHADERS,
|
||||
TOGGLE_VIDEO_INTEGER_SCALE,
|
||||
TOGGLE_VIDEO_VSYNC,
|
||||
RESET,
|
||||
TOGGLE_AUDIO,
|
||||
CHANGE_LANG,
|
||||
SHOW_INFO,
|
||||
CONFIG,
|
||||
SWAP_CONTROLLERS,
|
||||
TOGGLE_AUTO_FIRE,
|
||||
|
||||
// Input obligatorio
|
||||
NONE,
|
||||
SIZE,
|
||||
};
|
||||
|
||||
// Mapas para convertir entre enums y strings
|
||||
extern const std::unordered_map<InputAction, std::string> actionToString;
|
||||
extern const std::unordered_map<std::string, InputAction> stringToAction;
|
||||
extern const std::unordered_map<SDL_GamepadButton, std::string> buttonToString;
|
||||
extern const std::unordered_map<std::string, SDL_GamepadButton> stringToButton;
|
||||
Reference in New Issue
Block a user