Input: model upate/poll finalitzat

This commit is contained in:
2025-06-29 11:37:01 +02:00
parent ce1ae74e88
commit 5006289a5d
21 changed files with 183 additions and 155 deletions

View File

@@ -60,27 +60,27 @@ void Input::bindGameControllerButton(int controller_index, InputAction input_tar
}
// Comprueba si un input esta activo
bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device, int controller_index)
bool Input::checkInput(InputAction input, bool repeat, InputDevice device, int controller_index)
{
bool success_keyboard = false;
bool success_controller = false;
const int input_index = static_cast<int>(input);
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
if (device == InputDevice::KEYBOARD || device == InputDevice::ANY)
{
if (repeat)
{ // El usuario quiere saber si está pulsada (estado mantenido)
return key_bindings_[input_index].is_held;
success_keyboard = key_bindings_[input_index].is_held;
}
else
{ // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
return key_bindings_[input_index].just_pressed;
success_keyboard = key_bindings_[input_index].just_pressed;
}
}
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
{
if ((device == InputDeviceToUse::CONTROLLER) || (device == InputDeviceToUse::ANY))
if ((device == InputDevice::CONTROLLER) || (device == InputDevice::ANY))
{
success_controller = checkAxisInput(input, controller_index, repeat);
@@ -88,11 +88,11 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
{
if (repeat)
{ // El usuario quiere saber si está pulsada (estado mantenido)
return controller_bindings_.at(controller_index).at(input_index).is_held;
success_controller = controller_bindings_.at(controller_index).at(input_index).is_held;
}
else
{ // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
return controller_bindings_.at(controller_index).at(input_index).just_pressed;
success_controller = controller_bindings_.at(controller_index).at(input_index).just_pressed;
}
}
}
@@ -102,45 +102,45 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
}
// Comprueba si hay almenos un input activo
bool Input::checkAnyInput(InputDeviceToUse device, int controller_index)
bool Input::checkAnyInput(InputDevice device, int controller_index)
{
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
const int num_actions = static_cast<int>(InputAction::SIZE);
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
const int num_actions = static_cast<int>(InputAction::SIZE);
// --- Comprobación del Teclado ---
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
{
for (int i = 0; i < num_actions; ++i)
{
// Simplemente leemos el estado pre-calculado por Input::update().
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'.
if (key_bindings_.at(i).just_pressed)
{
return true; // Se encontró una acción recién pulsada.
}
}
}
// --- Comprobación del Teclado ---
if (device == InputDevice::KEYBOARD || device == InputDevice::ANY)
{
for (int i = 0; i < num_actions; ++i)
{
// Simplemente leemos el estado pre-calculado por Input::update().
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'.
if (key_bindings_.at(i).just_pressed)
{
return true; // Se encontró una acción recién pulsada.
}
}
}
// --- Comprobación del Mando ---
// Comprobamos si hay mandos y si el índice solicitado es válido.
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
{
if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY)
{
// Bucle CORREGIDO: Iteramos sobre todas las acciones, no sobre el número de mandos.
for (int i = 0; i < num_actions; ++i)
{
// Leemos el estado pre-calculado para el mando y la acción específicos.
if (controller_bindings_.at(controller_index).at(i).just_pressed)
{
return true; // Se encontró una acción recién pulsada en el mando.
}
}
}
}
// --- Comprobación del Mando ---
// Comprobamos si hay mandos y si el índice solicitado es válido.
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
{
if (device == InputDevice::CONTROLLER || device == InputDevice::ANY)
{
// Bucle CORREGIDO: Iteramos sobre todas las acciones, no sobre el número de mandos.
for (int i = 0; i < num_actions; ++i)
{
// Leemos el estado pre-calculado para el mando y la acción específicos.
if (controller_bindings_.at(controller_index).at(i).just_pressed)
{
return true; // Se encontró una acción recién pulsada en el mando.
}
}
}
}
// Si llegamos hasta aquí, no se detectó ninguna nueva pulsación.
return false;
// Si llegamos hasta aquí, no se detectó ninguna nueva pulsación.
return false;
}
// Comprueba si hay algún botón pulsado. Devuelve 0 en caso de no encontrar nada o el indice del dispositivo + 1. Se hace así para poder gastar el valor devuelto como un valor "booleano"
@@ -150,7 +150,7 @@ int Input::checkAnyButton(bool repeat)
for (auto bi : button_inputs_)
{
// Comprueba el teclado
if (checkInput(bi, repeat, InputDeviceToUse::KEYBOARD))
if (checkInput(bi, repeat, InputDevice::KEYBOARD))
{
return 1;
}
@@ -158,7 +158,7 @@ int Input::checkAnyButton(bool repeat)
// Comprueba los mandos
for (int i = 0; i < num_gamepads_; ++i)
{
if (checkInput(bi, repeat, InputDeviceToUse::CONTROLLER, i))
if (checkInput(bi, repeat, InputDevice::CONTROLLER, i))
{
return i + 1;
}
@@ -273,14 +273,14 @@ int Input::getJoyIndex(SDL_JoystickID id) const
}
// Muestra por consola los controles asignados
void Input::printBindings(InputDeviceToUse device, int controller_index) const
void Input::printBindings(InputDevice device, int controller_index) const
{
if (device == InputDeviceToUse::ANY || device == InputDeviceToUse::KEYBOARD)
if (device == InputDevice::ANY || device == InputDevice::KEYBOARD)
{
return;
}
if (device == InputDeviceToUse::CONTROLLER)
if (device == InputDevice::CONTROLLER)
{
if (controller_index >= num_gamepads_)
{
@@ -349,22 +349,21 @@ InputAction Input::to_inputs_e(const std::string &name) const
bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat)
{
// Umbral para considerar el eje como activo
const Sint16 threshold = 30000;
bool axis_active_now = false;
switch (input)
{
case InputAction::LEFT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -AXIS_THRESHOLD_;
break;
case InputAction::RIGHT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > AXIS_THRESHOLD_;
break;
case InputAction::UP:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -AXIS_THRESHOLD_;
break;
case InputAction::DOWN:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > AXIS_THRESHOLD_;
break;
default:
return false;