157 lines
5.2 KiB
C++
157 lines
5.2 KiB
C++
#pragma once
|
|
|
|
#include <SDL2/SDL_gamecontroller.h> // para SDL_GameControllerButton, SDL_G...
|
|
#include <SDL2/SDL_joystick.h> // para SDL_Joystick
|
|
#include <SDL2/SDL_scancode.h> // para SDL_Scancode
|
|
#include <SDL2/SDL_stdinc.h> // para Uint8
|
|
#include <string> // para string, basic_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:
|
|
INPUT_USE_KEYBOARD solo mirará el teclado
|
|
INPUT_USE_GAMECONTROLLER solo mirará el controlador especificado (Si no se especifica, el primero)
|
|
INPUT_USE_ANY mirará tanto el teclado como el PRIMER controlador
|
|
*/
|
|
|
|
enum class InputType : 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,
|
|
RESET,
|
|
MUTE,
|
|
SHOWINFO,
|
|
CONFIG,
|
|
SWAP_CONTROLLERS,
|
|
|
|
// Input obligatorio
|
|
NONE,
|
|
NUMBER_OF_INPUTS,
|
|
};
|
|
|
|
constexpr bool INPUT_ALLOW_REPEAT = true;
|
|
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
|
|
|
|
constexpr int INPUT_USE_KEYBOARD = 0;
|
|
constexpr int INPUT_USE_GAMECONTROLLER = 1;
|
|
constexpr int INPUT_USE_ANY = 2;
|
|
|
|
class Input
|
|
{
|
|
private:
|
|
// [SINGLETON] Objeto screen privado para Don Melitón
|
|
static Input *input_;
|
|
|
|
struct KeyBindings
|
|
{
|
|
Uint8 scancode; // Scancode asociado
|
|
bool active; // Indica si está activo
|
|
};
|
|
|
|
struct ControllerBindings
|
|
{
|
|
SDL_GameControllerButton button; // GameControllerButton asociado
|
|
bool active; // Indica si está activo
|
|
};
|
|
|
|
// Variables
|
|
std::vector<SDL_GameController *> 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<InputType> game_inputs_; // Inputs usados para jugar, normalmente direcciones y botones
|
|
std::vector<InputType> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
|
|
int num_joysticks_; // Número de joysticks conectados
|
|
int num_gamepads_; // Número de mandos conectados
|
|
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
|
|
bool enabled_; // Indica si está habilitado
|
|
|
|
// Comprueba el eje del mando
|
|
bool checkAxisInput(InputType input, int controller_index = 0) const;
|
|
|
|
// Constructor
|
|
explicit Input(const std::string &game_controller_db_path);
|
|
|
|
// Destructor
|
|
~Input() = default;
|
|
|
|
public:
|
|
// [SINGLETON] Crearemos el objeto screen con esta función estática
|
|
static void init(const std::string &game_controller_db_path);
|
|
|
|
// [SINGLETON] Destruiremos el objeto screen con esta función estática
|
|
static void destroy();
|
|
|
|
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
|
|
static Input *get();
|
|
|
|
// Asigna inputs a teclas
|
|
void bindKey(InputType input, SDL_Scancode code);
|
|
|
|
// Asigna inputs a botones del mando
|
|
void bindGameControllerButton(int controller_index, InputType input, SDL_GameControllerButton button);
|
|
void bindGameControllerButton(int controller_index, InputType inputTarget, InputType inputSource);
|
|
|
|
// Comprueba si un input esta activo
|
|
bool checkInput(InputType input, bool repeat = true, int device = INPUT_USE_ANY, int controller_index = 0);
|
|
|
|
// Comprueba si un input con modificador esta activo
|
|
bool checkModInput(InputType input_mod, InputType input, bool repeat = true, int device = INPUT_USE_ANY, int controller_index = 0);
|
|
|
|
// Comprueba si hay almenos un input activo
|
|
bool checkAnyInput(int device = INPUT_USE_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(int id) const;
|
|
|
|
// Muestra por consola los controles asignados
|
|
void printBindings(int device = INPUT_USE_KEYBOARD, int controller_index = 0) const;
|
|
|
|
// Obtiene el SDL_GameControllerButton asignado a un input
|
|
SDL_GameControllerButton getControllerBinding(int controller_index, InputType input) const;
|
|
|
|
// Convierte un InputType a std::string
|
|
std::string to_string(InputType input) const;
|
|
|
|
// Convierte un std::string a InputType
|
|
InputType to_inputs_e(const std::string &name) const;
|
|
|
|
// Obtiene el indice a partir del nombre del mando
|
|
int getIndexByName(const std::string &name) const;
|
|
}; |