This commit is contained in:
2024-09-16 22:21:57 +02:00
parent 80425a5ed0
commit 159a75a60e
12 changed files with 384 additions and 307 deletions

View File

@@ -65,17 +65,135 @@ void Input::bindKey(inputs_e input, SDL_Scancode code)
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button)
{
if (index >= (int)connectedControllers.size())
if (index < numGamepads)
{
return;
gameControllerBindings[index][input].button = button;
}
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource)
{
if (index < numGamepads)
{
gameControllerBindings[index][inputTarget].button = gameControllerBindings[index][inputSource].button;
}
gameControllerBindings[index][input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
{
if (!enabled)
if (!enabled || index >= numGamepads)
{
return false;
}
bool successKeyboard = false;
bool successGameController = false;
if (device == INPUT_USE_ANY)
{
index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
if (repeat)
{
if (keyStates[keyBindings[input].scancode] != 0)
{
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (!keyBindings[input].active)
{
if (keyStates[keyBindings[input].scancode] != 0)
{
keyBindings[input].active = true;
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (keyStates[keyBindings[input].scancode] == 0)
{
keyBindings[input].active = false;
successKeyboard = false;
}
else
{
successKeyboard = false;
}
}
}
}
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{
successGameController = checkAxisInput(input, index);
if (!successGameController)
{
if (repeat)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
{
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (!gameControllerBindings[index][input].active)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
{
gameControllerBindings[index][input].active = true;
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0)
{
gameControllerBindings[index][input].active = false;
successGameController = false;
}
else
{
successGameController = false;
}
}
}
}
}
return (successKeyboard || successGameController);
}
// Comprueba si un input con modificador esta activo
bool Input::checkModInput(inputs_e inputMod, inputs_e input, bool repeat, int device, int index)
{
if (!enabled || index >= numGamepads || !checkInput(inputMod, INPUT_ALLOW_REPEAT, device, index))
{
return false;
}
@@ -331,27 +449,13 @@ bool Input::discoverGameControllers()
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (numGamepads > 0)
{
return true;
}
else
{
return false;
}
return numGamepads > 0 ? true : false;
}
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int index)
{
if (numGamepads > 0)
{
return controllerNames[index];
}
else
{
return "";
}
return numGamepads > 0 ? controllerNames[index] : "";
}
// Obten el número de mandos conectados
@@ -396,12 +500,7 @@ int Input::getJoyIndex(int id)
// Muestra por consola los controles asignados
void Input::printBindings(int device, int index)
{
if (device == INPUT_USE_ANY)
{
return;
}
if (device == INPUT_USE_KEYBOARD)
if (device == INPUT_USE_ANY || device == INPUT_USE_KEYBOARD)
{
return;
}
@@ -467,6 +566,11 @@ std::string Input::to_string(inputs_e input)
return "input_start";
}
if (input == input_service)
{
return "input_service";
}
return "";
}
@@ -493,6 +597,11 @@ inputs_e Input::to_inputs_e(std::string name)
return input_start;
}
if (name == "input_service")
{
return input_service;
}
return input_null;
}
@@ -509,7 +618,7 @@ void Input::allActive(int index)
bool Input::checkAxisInput(inputs_e input, int index)
{
bool success = false;
switch (input)
{
case input_left: