forked from jaildesigner-jailgames/jaildoctors_dilemma
117 lines
3.1 KiB
C++
117 lines
3.1 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 REPEAT_TRUE = true;
|
|
constexpr bool REPEAT_FALSE = false;
|
|
|
|
// Tipos de entrada
|
|
constexpr int INPUT_USE_KEYBOARD = 0;
|
|
constexpr int INPUT_USE_GAMECONTROLLER = 1;
|
|
constexpr int INPUT_USE_ANY = 2;
|
|
|
|
enum class InputAction
|
|
{
|
|
// Inputs obligatorios
|
|
NONE,
|
|
UP,
|
|
DOWN,
|
|
LEFT,
|
|
RIGHT,
|
|
PAUSE,
|
|
EXIT,
|
|
ACCEPT,
|
|
CANCEL,
|
|
|
|
// Inputs personalizados
|
|
JUMP,
|
|
WINDOW_INC_ZOOM,
|
|
WINDOW_DEC_ZOOM,
|
|
TOGGLE_VIDEOMODE,
|
|
TOGGLE_BORDER,
|
|
TOGGLE_MUSIC,
|
|
TOGGLE_PALETTE,
|
|
TOGGLE_SHADERS,
|
|
|
|
// Input obligatorio
|
|
SIZE
|
|
};
|
|
|
|
class Input
|
|
{
|
|
private:
|
|
// [SINGLETON] Objeto privado
|
|
static Input *input_;
|
|
|
|
struct KeyBindings
|
|
{
|
|
Uint8 scancode; // Scancode asociado
|
|
bool active; // Indica si está activo
|
|
};
|
|
|
|
struct GameControllerBindings
|
|
{
|
|
SDL_GameControllerButton button; // GameControllerButton asociado
|
|
bool active; // Indica si está activo
|
|
};
|
|
|
|
// Objetos y punteros
|
|
std::vector<SDL_GameController *> connected_controllers_; // Vector con todos los mandos conectados
|
|
|
|
// Variables
|
|
std::vector<KeyBindings> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
|
std::vector<GameControllerBindings> game_controller_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
|
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
|
|
int num_gamepads_ = 0; // Numero de mandos conectados
|
|
std::string db_path_; // Ruta al archivo gamecontrollerdb.txt
|
|
bool verbose_ = true; // Indica si ha de mostrar mensajes
|
|
bool enabled_ = true; // Indica si está habilitado
|
|
|
|
// 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(InputAction input, SDL_GameControllerButton button);
|
|
|
|
// Comprueba si un input esta activo
|
|
bool checkInput(InputAction input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
|
|
|
|
// Comprueba si hay almenos un input activo
|
|
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
|
|
|
|
// Busca si hay un mando conectado
|
|
bool discoverGameController();
|
|
|
|
// Comprueba si hay algun mando conectado
|
|
bool gameControllerFound();
|
|
|
|
// Obten el numero de mandos conectados
|
|
int getNumControllers();
|
|
|
|
// Obten el nombre de un mando de juego
|
|
std::string getControllerName(int index);
|
|
|
|
// Establece si ha de mostrar mensajes
|
|
void setVerbose(bool value);
|
|
}; |