Files
coffee_crisis_arcade_edition/source/input.h
2025-03-27 09:43:19 +01:00

167 lines
5.2 KiB
C++

#pragma once
#include <SDL3/SDL_gamepad.h> // Para SDL_GamepadButton, SDL_Gamepad
#include <SDL3/SDL_joystick.h> // Para SDL_Joystick, SDL_JoystickID
#include <SDL3/SDL_scancode.h> // Para SDL_Scancode
#include <SDL3/SDL_stdinc.h> // Para Uint8
#include <string> // Para string
#include <vector> // Para vector
/*
connectedControllers es un vector donde estan todos los mandos encontrados [0 .. n]
checkInput requiere de un indice para comprobar las pulsaciónes 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
*/
enum class InputAction : int
{
// Inputs de movimiento
UP,
DOWN,
LEFT,
RIGHT,
// Inputs personalizados
FIRE_LEFT,
FIRE_CENTER,
FIRE_RIGHT,
START,
// Inputs de control
EXIT,
PAUSE,
SERVICE,
WINDOW_FULLSCREEN,
WINDOW_INC_SIZE,
WINDOW_DEC_SIZE,
VIDEO_SHADERS,
VIDEO_INTEGER_SCALE,
RESET,
MUTE,
CHANGE_LANG,
SHOW_INFO,
CONFIG,
SWAP_CONTROLLERS,
AUTO_FIRE,
// Input obligatorio
NONE,
SIZE,
};
constexpr bool INPUT_ALLOW_REPEAT = true;
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
enum class InputDeviceToUse : int
{
KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
class Input
{
private:
// [SINGLETON] Objeto privado
static Input *input_;
struct KeyBindings
{
Uint8 scancode; // Scancode asociado
bool active; // Indica si está activo
// Constructor
explicit KeyBindings(Uint8 sc = 0, bool act = false)
: scancode(sc), active(act) {}
};
struct ControllerBindings
{
SDL_GamepadButton button; // GameControllerButton asociado
bool active; // Indica si está activo
bool axis_active; // Estado del eje
// Constructor
explicit ControllerBindings(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool act = false, bool axis_act = false)
: button(btn), active(act), axis_active(axis_act) {}
};
// Variables
std::vector<SDL_Gamepad *> connected_controllers_; // Vector con todos los mandos conectados
std::vector<SDL_Joystick *> joysticks_; // Vector con todos los joysticks conectados
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 asociadas 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
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
// Comprueba el eje del mando
bool checkAxisInput(InputAction input, int controller_index, bool repeat);
// Constructor
explicit Input(const std::string &game_controller_db_path);
// Destructor
~Input() = default;
public:
// [SINGLETON] Crearemos el objeto con esta función estática
static void init(const std::string &game_controller_db_path);
// [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
static Input *get();
// Asigna inputs a teclas
void bindKey(InputAction input, SDL_Scancode code);
// Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, InputAction input, SDL_GamepadButton button);
void bindGameControllerButton(int controller_index, InputAction inputTarget, InputAction inputSource);
// Comprueba si un input esta activo
bool checkInput(InputAction input, bool repeat = true, InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0);
// Comprueba si hay almenos un input activo
bool checkAnyInput(InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0);
// Comprueba si hay algún botón pulsado
int checkAnyButtonPressed(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT);
// Busca si hay mandos conectados
bool discoverGameControllers();
// Comprueba si hay algun mando conectado
bool gameControllerFound();
// Obten el número de mandos conectados
int getNumControllers() const;
// Obten el nombre de un mando de juego
std::string getControllerName(int controller_index) const;
// Obtiene el indice del controlador a partir de un event.id
int getJoyIndex(SDL_JoystickID id) const;
// Muestra por consola los controles asignados
void printBindings(InputDeviceToUse device = InputDeviceToUse::KEYBOARD, int controller_index = 0) const;
// Obtiene el SDL_GamepadButton asignado a un input
SDL_GamepadButton getControllerBinding(int controller_index, InputAction input) const;
// Convierte un InputAction a std::string
std::string to_string(InputAction input) const;
// Convierte un std::string a InputAction
InputAction to_inputs_e(const std::string &name) const;
// Obtiene el indice a partir del nombre del mando
int getIndexByName(const std::string &name) const;
};