Input: mogudes structs, enums i consts a la part publica

This commit is contained in:
2025-07-30 08:53:57 +02:00
parent 677feb3afe
commit 989f081a25
14 changed files with 302 additions and 275 deletions

View File

@@ -14,76 +14,77 @@ device contiene el tipo de dispositivo a comprobar:
InputDeviceToUse::ANY mirará tanto el teclado como el PRIMER controlador
*/
// 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,
};
constexpr bool INPUT_ALLOW_REPEAT = true;
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
// Tipos de dispositivos de entrada
enum class InputDevice : int {
KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
// Clase Input: gestiona la entrada de teclado y mandos (singleton)
class Input {
public:
// --- Constantes ---
static constexpr bool ALLOW_REPEAT = true;
static constexpr bool DO_NOT_ALLOW_REPEAT = 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,
};
// Tipos de dispositivos de entrada
enum class Device : int {
KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
// --- 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
// --- Métodos de configuración de controles ---
void bindKey(InputAction input, SDL_Scancode code); // Asigna inputs a teclas
void bindGameControllerButton(int controller_index, InputAction input, SDL_GamepadButton button); // Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, InputAction input_target, InputAction input_source); // Asigna inputs a otros inputs del mando
void bindKey(Action input, SDL_Scancode code); // Asigna inputs a teclas
void bindGameControllerButton(int controller_index, Action input, SDL_GamepadButton button); // Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, Action input_target, Action input_source); // Asigna inputs a otros inputs del mando
// --- Métodos de consulta de entrada ---
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkInput(InputAction input, bool repeat = true, InputDevice device = InputDevice::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(InputDevice device = InputDevice::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkInput(Action input, bool repeat = true, Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
// --- Métodos de gestión de mandos ---
auto discoverGameControllers() -> bool; // Busca si hay mandos conectados
@@ -93,15 +94,18 @@ class Input {
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int; // Obtiene el índice del controlador a partir de un event.id
// --- Métodos de consulta y utilidades ---
void printBindings(InputDevice device = InputDevice::KEYBOARD, int controller_index = 0) const; // Muestra por consola los controles asignados
[[nodiscard]] auto getControllerBinding(int controller_index, InputAction input) const -> SDL_GamepadButton; // Obtiene el SDL_GamepadButton asignado a un input
[[nodiscard]] static auto inputToString(InputAction input) -> std::string; // Convierte un InputAction a std::string
[[nodiscard]] static auto stringToInput(const std::string &name) -> InputAction; // Convierte un std::string a InputAction
[[nodiscard]] auto getIndexByName(const std::string &name) const -> int; // Obtiene el índice a partir del nombre del mando
void printBindings(Device device = Device::KEYBOARD, int controller_index = 0) const; // Muestra por consola los controles asignados
[[nodiscard]] auto getControllerBinding(int controller_index, 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 getIndexByName(const std::string &name) const -> int; // Obtiene el índice a partir del nombre del mando
// --- Métodos de reseteo de estado de entrada ---
void resetInputStates(); // Pone todos los KeyBindings.active y ControllerBindings.active a false
// --- Eventos ---
void handleEvent(const SDL_Event &event); // Comprueba si se conecta algun mando
private:
// --- Estructuras internas ---
struct KeyBindings {
@@ -132,14 +136,14 @@ class Input {
std::vector<KeyBindings> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
std::vector<std::vector<ControllerBindings>> controller_bindings_; // Vector con los botones asociados a los inputs predefinidos para cada mando
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
std::vector<InputAction> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
std::vector<Action> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
int num_joysticks_ = 0; // Número de joysticks conectados
int num_gamepads_ = 0; // Número de mandos conectados
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
// --- Métodos internos ---
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
auto checkAxisInput(InputAction input, int controller_index, bool repeat) -> bool; // Comprueba el eje del mando
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
auto checkAxisInput(Action input, int controller_index, bool repeat) -> bool; // Comprueba el eje del mando
// --- Constructor y destructor ---
explicit Input(std::string game_controller_db_path); // Constructor privado