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;
gameControllerBindings.resize(17, gcb);
// Comprueba si hay un mando conectado
discoverGameController();
verbose = true;
enabled = true;
numGamepads = 0;
}
// Destructor
Input::~Input()
// Actualiza el estado del objeto
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)
{
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)
{
gameControllerBindings.at(input).button = button;
gameControllerBindings[input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
if (!enabled)
{
return false;
}
bool successKeyboard = false;
bool successGameController = false;
if (device == INPUT_USE_ANY)
{
index = 0;
}
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 (keyStates[keyBindings.at(input).scancode] != 0)
if (keyStates[keyBindings[input].scancode] != 0)
{
successKeyboard = true;
}
@@ -65,11 +78,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
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;
}
else
@@ -79,9 +92,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
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;
}
else
@@ -97,7 +110,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
if (repeat)
{
if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0)
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
{
successGameController = true;
}
@@ -108,11 +121,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
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;
}
else
@@ -122,9 +135,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
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;
}
else
@@ -152,7 +165,7 @@ bool Input::checkAnyInput(int device, int index)
for (int i = 0; i < (int)keyBindings.size(); ++i)
{
if (mKeystates[keyBindings.at(i).scancode] != 0)
if (mKeystates[keyBindings[i].scancode] != 0)
{
return true;
}
@@ -165,7 +178,7 @@ bool Input::checkAnyInput(int device, int index)
{
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;
}
@@ -176,7 +189,7 @@ bool Input::checkAnyInput(int device, int index)
return false;
}
// Comprueba si hay un mando conectado
// Busca si hay un mando conectado
bool Input::discoverGameController()
{
bool found = false;
@@ -188,7 +201,10 @@ bool Input::discoverGameController()
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();
@@ -203,8 +219,11 @@ bool Input::discoverGameController()
}
}
printf("\nChecking for game controllers...\n");
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads);
if (verbose)
{
std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
}
if (numGamepads > 0)
{
@@ -221,12 +240,18 @@ bool Input::discoverGameController()
std::string name = SDL_GameControllerNameForIndex(i);
name.resize(25);
name = name + separator + std::to_string(i);
std::cout << name << std::endl;
if (verbose)
{
std::cout << name << std::endl;
}
controllerNames.push_back(name);
}
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)
{
return controllerNames.at(index);
return controllerNames[index];
}
else
{
@@ -266,4 +291,24 @@ std::string Input::getControllerName(int index)
int Input::getNumControllers()
{
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;
}