Variables renombrades en input.cpp

This commit is contained in:
2024-10-12 11:01:42 +02:00
parent 4ef759772a
commit 101e375fd3
16 changed files with 467 additions and 523 deletions

View File

@@ -1,11 +1,11 @@
#pragma once
#include <SDL2/SDL_gamecontroller.h> // for SDL_GameControllerButton, SDL_G...
#include <SDL2/SDL_joystick.h> // for SDL_Joystick
#include <SDL2/SDL_scancode.h> // for SDL_Scancode
#include <SDL2/SDL_stdinc.h> // for Uint8
#include <string> // for string, basic_string
#include <vector> // for vector
#include <SDL2/SDL_gamecontroller.h> // for SDL_GameControllerButton, SDL_G...
#include <SDL2/SDL_joystick.h> // for SDL_Joystick
#include <SDL2/SDL_scancode.h> // for SDL_Scancode
#include <SDL2/SDL_stdinc.h> // for Uint8
#include <string> // for string, basic_string
#include <vector> // for vector
/*
connectedControllers es un vector donde estan todos los mandos encontrados [0 .. n]
@@ -16,98 +16,89 @@ device contiene el tipo de dispositivo a comprobar:
INPUT_USE_ANY mirará tanto el teclado como el PRIMER controlador
*/
enum inputs_e
enum class InputType : int
{
// Inputs de movimiento
input_up,
input_down,
input_left,
input_right,
UP,
DOWN,
LEFT,
RIGHT,
// Inputs personalizados
input_fire_left,
input_fire_center,
input_fire_right,
input_start,
FIRE_LEFT,
FIRE_CENTER,
FIRE_RIGHT,
START,
// Inputs de control
input_exit,
input_pause,
input_service,
input_window_fullscreen,
input_window_inc_size,
input_window_dec_size,
input_video_shaders,
input_reset,
input_mute,
input_showinfo,
input_config,
input_swap_controllers,
EXIT,
PAUSE,
SERVICE,
WINDOW_FULLSCREEN,
WINDOW_INC_SIZE,
WINDOW_DEC_SIZE,
VIDEO_SHADERS,
RESET,
MUTE,
SHOWINFO,
CONFIG,
SWAP_CONTROLLERS,
// Input obligatorio
input_null,
input_number_of_inputs,
NONE,
NUMBER_OF_INPUTS,
};
#define INPUT_ALLOW_REPEAT true
#define INPUT_DO_NOT_ALLOW_REPEAT false
constexpr bool INPUT_ALLOW_REPEAT = true;
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
#define INPUT_USE_KEYBOARD 0
#define INPUT_USE_GAMECONTROLLER 1
#define INPUT_USE_ANY 2
enum i_disable_e
{
d_notDisabled,
d_forever,
d_keyPressed
};
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;
static Input *input_;
struct keyBindings_t
struct KeyBindings
{
Uint8 scancode; // Scancode asociado
bool active; // Indica si está activo
};
struct GameControllerBindings_t
struct ControllerBindings
{
SDL_GameControllerButton button; // GameControllerButton asociado
bool active; // Indica si está activo
};
// Variables
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
std::vector<SDL_Joystick *> joysticks; // Vector con todos los joysticks conectados
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
std::vector<std::vector<GameControllerBindings_t>> gameControllerBindings; // Vector con los botones asociadas a los inputs predefinidos para cada mando
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
std::vector<inputs_e> gameInputs; // Inputs usados para jugar, normalmente direcciones y botones
std::vector<inputs_e> buttonInputs; // Inputs asignados al jugador y a botones, excluyendo direcciones
int numJoysticks; // Número de joysticks conectados
int numGamepads; // Número de mandos conectados
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
bool verbose; // Indica si ha de mostrar mensajes
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
bool enabled; // Indica si está habilitado
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(inputs_e input, int index = 0) const;
bool checkAxisInput(InputType input, int controller_index = 0) const;
// Constructor
Input(std::string dbPath);
Input(std::string game_controller_db_path);
// Destructor
~Input();
~Input() = default;
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init(std::string dbPath);
static void init(std::string game_controller_db_path);
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void destroy();
@@ -115,24 +106,21 @@ public:
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static Input *get();
// Actualiza el estado del objeto
void update();
// Asigna inputs a teclas
void bindKey(inputs_e input, SDL_Scancode code);
void bindKey(InputType input, SDL_Scancode code);
// Asigna inputs a botones del mando
void bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button);
void bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource);
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(inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
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(inputs_e inputMod, inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
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 index = 0);
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);
@@ -147,35 +135,26 @@ public:
int getNumControllers() const;
// Obten el nombre de un mando de juego
std::string getControllerName(int index) const;
// Establece si ha de mostrar mensajes
void setVerbose(bool value);
// Deshabilita las entradas durante un periodo de tiempo
void disableUntil(i_disable_e value);
// Hablita las entradas
void enable();
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 index = 0) const;
void printBindings(int device = INPUT_USE_KEYBOARD, int controller_index = 0) const;
// Obtiene el SDL_GameControllerButton asignado a un input
SDL_GameControllerButton getControllerBinding(int index, inputs_e input) const;
SDL_GameControllerButton getControllerBinding(int controller_index, InputType input) const;
// Convierte un inputs_e a std::string
std::string to_string(inputs_e input) const;
// Convierte un InputType a std::string
std::string to_string(InputType input) const;
// Convierte un std::string a inputs_e
inputs_e to_inputs_e(std::string name) const;
// Convierte un std::string a InputType
InputType to_inputs_e(std::string name) const;
// Obtiene el indice a partir del nombre del mando
int getIndexByName(std::string name) const;
// Activa todos los inputs. Sirve para evitar inputs sin repeticiones pero que ya vienen pulsados cuando checkInput no estaba monitorizando
void allActive(int index);
//void allActive(int index);
};