Actualizado input.cpp

This commit is contained in:
2022-12-07 07:58:04 +01:00
parent fbeea9f424
commit 90785d2506
3 changed files with 106 additions and 39 deletions

View File

@@ -18,35 +18,48 @@ Input::Input(std::string file)
gcb.active = false; gcb.active = false;
gameControllerBindings.resize(17, gcb); gameControllerBindings.resize(17, gcb);
// Comprueba si hay un mando conectado verbose = true;
discoverGameController(); enabled = true;
numGamepads = 0;
} }
// Destructor // Actualiza el estado del objeto
Input::~Input() void Input::update()
{ {
if (disabledUntil == d_keyPressed && !checkAnyInput())
{
enable();
}
} }
// Asigna uno de los posibles inputs a una tecla del teclado // Asigna inputs a teclas
void Input::bindKey(Uint8 input, SDL_Scancode code) void Input::bindKey(Uint8 input, SDL_Scancode code)
{ {
keyBindings.at(input).scancode = code; keyBindings[input].scancode = code;
} }
// Asigna uno de los posibles inputs a un botón del mando // Asigna inputs a botones del mando
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button) void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
{ {
gameControllerBindings.at(input).button = button; gameControllerBindings[input].button = button;
} }
// Comprueba si un input esta activo // Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat, int device, int index) bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{ {
if (!enabled)
{
return false;
}
bool successKeyboard = false; bool successKeyboard = false;
bool successGameController = false; bool successGameController = false;
if (device == INPUT_USE_ANY) if (device == INPUT_USE_ANY)
{
index = 0; index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{ {
@@ -54,7 +67,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
if (repeat) if (repeat)
{ {
if (keyStates[keyBindings.at(input).scancode] != 0) if (keyStates[keyBindings[input].scancode] != 0)
{ {
successKeyboard = true; successKeyboard = true;
} }
@@ -65,11 +78,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
} }
else else
{ {
if (!keyBindings.at(input).active) if (!keyBindings[input].active)
{ {
if (keyStates[keyBindings.at(input).scancode] != 0) if (keyStates[keyBindings[input].scancode] != 0)
{ {
keyBindings.at(input).active = true; keyBindings[input].active = true;
successKeyboard = true; successKeyboard = true;
} }
else else
@@ -79,9 +92,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
} }
else else
{ {
if (keyStates[keyBindings.at(input).scancode] == 0) if (keyStates[keyBindings[input].scancode] == 0)
{ {
keyBindings.at(input).active = false; keyBindings[input].active = false;
successKeyboard = false; successKeyboard = false;
} }
else else
@@ -97,7 +110,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{ {
if (repeat) if (repeat)
{ {
if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
{ {
successGameController = true; successGameController = true;
} }
@@ -108,11 +121,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
} }
else else
{ {
if (!gameControllerBindings.at(input).active) if (!gameControllerBindings[input].active)
{ {
if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
{ {
gameControllerBindings.at(input).active = true; gameControllerBindings[input].active = true;
successGameController = true; successGameController = true;
} }
else else
@@ -122,9 +135,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
} }
else else
{ {
if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) == 0) if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
{ {
gameControllerBindings.at(input).active = false; gameControllerBindings[input].active = false;
successGameController = false; successGameController = false;
} }
else else
@@ -152,7 +165,7 @@ bool Input::checkAnyInput(int device, int index)
for (int i = 0; i < (int)keyBindings.size(); ++i) for (int i = 0; i < (int)keyBindings.size(); ++i)
{ {
if (mKeystates[keyBindings.at(i).scancode] != 0) if (mKeystates[keyBindings[i].scancode] != 0)
{ {
return true; return true;
} }
@@ -165,7 +178,7 @@ bool Input::checkAnyInput(int device, int index)
{ {
for (int i = 0; i < (int)gameControllerBindings.size(); ++i) for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
{ {
if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(i).button) != 0) if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
{ {
return true; return true;
} }
@@ -176,7 +189,7 @@ bool Input::checkAnyInput(int device, int index)
return false; return false;
} }
// Comprueba si hay un mando conectado // Busca si hay un mando conectado
bool Input::discoverGameController() bool Input::discoverGameController()
{ {
bool found = false; bool found = false;
@@ -188,7 +201,10 @@ bool Input::discoverGameController()
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0) if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
{ {
printf("Error, could not load %s file: %s\n", dbPath.c_str(), SDL_GetError()); if (verbose)
{
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
}
} }
const int nJoysticks = SDL_NumJoysticks(); const int nJoysticks = SDL_NumJoysticks();
@@ -203,8 +219,11 @@ bool Input::discoverGameController()
} }
} }
printf("\nChecking for game controllers...\n"); if (verbose)
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads); {
std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
}
if (numGamepads > 0) if (numGamepads > 0)
{ {
@@ -221,12 +240,18 @@ bool Input::discoverGameController()
std::string name = SDL_GameControllerNameForIndex(i); std::string name = SDL_GameControllerNameForIndex(i);
name.resize(25); name.resize(25);
name = name + separator + std::to_string(i); name = name + separator + std::to_string(i);
std::cout << name << std::endl; if (verbose)
{
std::cout << name << std::endl;
}
controllerNames.push_back(name); controllerNames.push_back(name);
} }
else else
{ {
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; if (verbose)
{
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
} }
} }
@@ -254,7 +279,7 @@ std::string Input::getControllerName(int index)
{ {
if (numGamepads > 0) if (numGamepads > 0)
{ {
return controllerNames.at(index); return controllerNames[index];
} }
else else
{ {
@@ -267,3 +292,23 @@ int Input::getNumControllers()
{ {
return numGamepads; return numGamepads;
} }
// Establece si ha de mostrar mensajes
void Input::setVerbose(bool value)
{
verbose = value;
}
// Deshabilita las entradas durante un periodo de tiempo
void Input::disableUntil(i_disable_e value)
{
disabledUntil = value;
enabled = false;
}
// Hablita las entradas
void Input::enable()
{
enabled = true;
disabledUntil = d_notDisabled;
}

View File

@@ -32,7 +32,13 @@
#define INPUT_USE_GAMECONTROLLER 1 #define INPUT_USE_GAMECONTROLLER 1
#define INPUT_USE_ANY 2 #define INPUT_USE_ANY 2
// Clase Input enum i_disable_e
{
d_notDisabled,
d_forever,
d_keyPressed
};
class Input class Input
{ {
private: private:
@@ -48,34 +54,40 @@ private:
bool active; // Indica si está activo bool active; // Indica si está activo
}; };
// Objetos y punteros
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
// Variables
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
int numGamepads; // Numero de mandos conectados int numGamepads; // Numero de mandos conectados
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
bool verbose; // Indica si ha de mostrar mensajes
// Comprueba si hay un mando conectado i_disable_e disabledUntil; // Tiempo que esta deshabilitado
bool discoverGameController(); bool enabled; // Indica si está habilitado
public: public:
// Constructor // Constructor
Input(std::string file); Input(std::string file);
// Destructor // Actualiza el estado del objeto
~Input(); void update();
// Asigna uno de los posibles inputs a una tecla del teclado // Asigna inputs a teclas
void bindKey(Uint8 input, SDL_Scancode code); void bindKey(Uint8 input, SDL_Scancode code);
// Asigna uno de los posibles inputs a un botón del mando // Asigna inputs a botones del mando
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button); void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
// Comprueba si un input esta activo // Comprueba si un input esta activo
bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0); bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0);
// Comprueba si hay almenos un input activo // Comprueba si hay almenos un input activo
bool checkAnyInput(int device, int index); bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
// Busca si hay un mando conectado
bool discoverGameController();
// Comprueba si hay algun mando conectado // Comprueba si hay algun mando conectado
bool gameControllerFound(); bool gameControllerFound();
@@ -85,6 +97,15 @@ public:
// Obten el nombre de un mando de juego // Obten el nombre de un mando de juego
std::string getControllerName(int index); std::string getControllerName(int index);
// 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();
}; };
#endif #endif

View File

@@ -1102,6 +1102,7 @@ void Title::createTiledBackground()
void Title::checkInputDevices() void Title::checkInputDevices()
{ {
printf("Filling devices for options menu...\n"); printf("Filling devices for options menu...\n");
input->discoverGameController();
const int numControllers = input->getNumControllers(); const int numControllers = input->getNumControllers();
availableInputDevices.clear(); availableInputDevices.clear();
input_t temp; input_t temp;