140 lines
4.2 KiB
C++
140 lines
4.2 KiB
C++
#pragma once
|
|
|
|
#include <SDL2/SDL_gamecontroller.h> // Para SDL_GameControllerButton, SDL_G...
|
|
#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
|
|
|
|
// Definiciones de repetición
|
|
constexpr bool INPUT_ALLOW_REPEAT = true;
|
|
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
|
|
|
|
// Tipos de entrada
|
|
enum class InputDeviceToUse : int
|
|
{
|
|
KEYBOARD = 0,
|
|
CONTROLLER = 1,
|
|
ANY = 2,
|
|
};
|
|
|
|
enum class InputAction
|
|
{
|
|
// Inputs obligatorios
|
|
UP,
|
|
DOWN,
|
|
LEFT,
|
|
RIGHT,
|
|
PAUSE,
|
|
EXIT,
|
|
ACCEPT,
|
|
CANCEL,
|
|
|
|
// Inputs personalizados
|
|
JUMP,
|
|
WINDOW_INC_ZOOM,
|
|
WINDOW_DEC_ZOOM,
|
|
TOGGLE_VIDEOMODE,
|
|
TOGGLE_INTEGER_SCALE,
|
|
TOGGLE_BORDER,
|
|
TOGGLE_MUSIC,
|
|
NEXT_PALETTE,
|
|
PREVIOUS_PALETTE,
|
|
TOGGLE_SHADERS,
|
|
|
|
// Input obligatorio
|
|
NONE,
|
|
SIZE
|
|
};
|
|
|
|
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_GameControllerButton button; // GameControllerButton asociado
|
|
bool active; // Indica si está activo
|
|
bool axis_active; // Estado del eje
|
|
|
|
// Constructor
|
|
explicit ControllerBindings(SDL_GameControllerButton btn = SDL_CONTROLLER_BUTTON_INVALID, bool act = false, bool axis_act = false)
|
|
: button(btn), active(act), axis_active(axis_act) {}
|
|
};
|
|
|
|
// 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<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_GameControllerButton 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);
|
|
|
|
// 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;
|
|
|
|
// Obtiene el SDL_GameControllerButton asignado a un input
|
|
SDL_GameControllerButton getControllerBinding(int controller_index, InputAction input) const;
|
|
|
|
// Obtiene el indice a partir del nombre del mando
|
|
int getIndexByName(const std::string &name) const;
|
|
}; |