Input: canviant a nou model update/poll
This commit is contained in:
181
source/input.cpp
181
source/input.cpp
@@ -68,34 +68,13 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
|
||||
|
||||
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
|
||||
{
|
||||
const bool *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
success_keyboard = keyStates[key_bindings_[input_index].scancode] != 0;
|
||||
{ // El usuario quiere saber si está pulsada (estado mantenido)
|
||||
return key_bindings_[input_index].is_held;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!key_bindings_[input_index].active)
|
||||
{
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
key_bindings_[input_index].active = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[key_bindings_[input_index].scancode] == 0)
|
||||
{
|
||||
key_bindings_[input_index].active = false;
|
||||
}
|
||||
success_keyboard = false;
|
||||
}
|
||||
{ // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
|
||||
return key_bindings_[input_index].just_pressed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,31 +87,12 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
|
||||
if (!success_controller)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
success_controller = SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0;
|
||||
{ // El usuario quiere saber si está pulsada (estado mantenido)
|
||||
return controller_bindings_.at(controller_index).at(input_index).is_held;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!controller_bindings_.at(controller_index).at(input_index).active)
|
||||
{
|
||||
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0)
|
||||
{
|
||||
controller_bindings_.at(controller_index).at(input_index).active = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) == 0)
|
||||
{
|
||||
controller_bindings_.at(controller_index).at(input_index).active = false;
|
||||
}
|
||||
success_controller = false;
|
||||
}
|
||||
{ // 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,47 +104,48 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool Input::checkAnyInput(InputDeviceToUse device, int controller_index)
|
||||
{
|
||||
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
|
||||
{
|
||||
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
|
||||
const int num_actions = static_cast<int>(InputAction::SIZE);
|
||||
|
||||
for (int i = 0; i < (int)key_bindings_.size(); ++i)
|
||||
{
|
||||
if (mKeystates[key_bindings_[i].scancode] != 0 && !key_bindings_[i].active)
|
||||
{
|
||||
key_bindings_[i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- 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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound())
|
||||
{
|
||||
if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY)
|
||||
{
|
||||
for (int i = 0; i < (int)controller_bindings_.size(); ++i)
|
||||
{
|
||||
if (SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button) != 0 && !controller_bindings_[controller_index][i].active)
|
||||
{
|
||||
controller_bindings_[controller_index][i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- 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.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
int Input::checkAnyButtonPressed(bool repeat)
|
||||
int Input::checkAnyButton(bool repeat)
|
||||
{
|
||||
// Si está pulsado el botón de servicio, ningún botón se puede considerar pulsado
|
||||
if (checkInput(InputAction::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::ANY))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Solo comprueba los botones definidos previamente
|
||||
for (auto bi : button_inputs_)
|
||||
{
|
||||
@@ -218,7 +179,7 @@ bool Input::discoverGameControllers()
|
||||
}
|
||||
|
||||
// En SDL3, SDL_GetJoysticks devuelve un array de IDs, no un contador
|
||||
SDL_JoystickID* joystick_ids = SDL_GetJoysticks(&num_joysticks_);
|
||||
SDL_JoystickID *joystick_ids = SDL_GetJoysticks(&num_joysticks_);
|
||||
num_gamepads_ = 0;
|
||||
|
||||
// Cuenta el número de mandos
|
||||
@@ -228,7 +189,7 @@ bool Input::discoverGameControllers()
|
||||
// Usar el ID del joystick, no el índice
|
||||
auto joy = SDL_OpenJoystick(joystick_ids[i]);
|
||||
joysticks_.push_back(joy);
|
||||
|
||||
|
||||
// En SDL3, SDL_IsGamepad toma un SDL_JoystickID, no un índice
|
||||
if (SDL_IsGamepad(joystick_ids[i]))
|
||||
{
|
||||
@@ -261,11 +222,11 @@ bool Input::discoverGameControllers()
|
||||
if (pad != nullptr)
|
||||
{
|
||||
connected_controllers_.push_back(pad);
|
||||
|
||||
|
||||
// Obtener el nombre usando el ID del joystick
|
||||
const char* name_cstr = SDL_GetGamepadNameForID(joystick_ids[i]);
|
||||
const char *name_cstr = SDL_GetGamepadNameForID(joystick_ids[i]);
|
||||
std::string name = name_cstr ? name_cstr : "Unknown Gamepad";
|
||||
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "#%d: %s", i, name.c_str());
|
||||
controller_names_.push_back(name);
|
||||
}
|
||||
@@ -453,15 +414,49 @@ void Input::initSDLGamePad()
|
||||
}
|
||||
}
|
||||
|
||||
void Input::resetInputStates() {
|
||||
void Input::resetInputStates()
|
||||
{
|
||||
// Resetear todos los KeyBindings.active a false
|
||||
for (auto &key : key_bindings_) {
|
||||
key.active = false;
|
||||
for (auto &key : key_bindings_)
|
||||
{
|
||||
key.is_held = false;
|
||||
key.just_pressed = false;
|
||||
}
|
||||
// Resetear todos los ControllerBindings.active a false
|
||||
for (auto &controller_vec : controller_bindings_) {
|
||||
for (auto &binding : controller_vec) {
|
||||
binding.active = false;
|
||||
for (auto &controller_vec : controller_bindings_)
|
||||
{
|
||||
for (auto &binding : controller_vec)
|
||||
{
|
||||
binding.is_held = false;
|
||||
binding.just_pressed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Input::update()
|
||||
{
|
||||
// --- TECLADO ---
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < key_bindings_.size(); ++i)
|
||||
{
|
||||
bool key_is_down_now = key_states[key_bindings_[i].scancode];
|
||||
|
||||
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
|
||||
key_bindings_[i].just_pressed = key_is_down_now && !key_bindings_[i].is_held;
|
||||
key_bindings_[i].is_held = key_is_down_now;
|
||||
}
|
||||
|
||||
// --- MANDOS ---
|
||||
for (int c = 0; c < num_gamepads_; ++c)
|
||||
{
|
||||
for (int i = 0; i < controller_bindings_[c].size(); ++i)
|
||||
{
|
||||
bool button_is_down_now = SDL_GetGamepadButton(connected_controllers_.at(c), controller_bindings_.at(c).at(i).button) != 0;
|
||||
|
||||
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
|
||||
controller_bindings_[c][i].just_pressed = button_is_down_now && !controller_bindings_[c][i].is_held;
|
||||
controller_bindings_[c][i].is_held = button_is_down_now;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user