Input: canviant a nou model update/poll

This commit is contained in:
2025-06-29 09:35:49 +02:00
parent 78338933e2
commit ce1ae74e88
7 changed files with 153 additions and 144 deletions

View File

@@ -144,7 +144,7 @@ void Credits::checkInput()
if (!ServiceMenu::get()->isEnabled()) if (!ServiceMenu::get()->isEnabled())
{ {
// Comprueba si se ha pulsado cualquier botón (de los usados para jugar) // Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
if (Input::get()->checkAnyButtonPressed(INPUT_ALLOW_REPEAT)) if (Input::get()->checkAnyButton(INPUT_ALLOW_REPEAT))
{ {
want_to_pass_ = true; want_to_pass_ = true;
fading_ = mini_logo_on_position_; fading_ = mini_logo_on_position_;

View File

@@ -58,6 +58,10 @@ void DefineButtons::bindButtons()
{ {
input_->bindGameControllerButton(index_controller_, button.input, button.button); input_->bindGameControllerButton(index_controller_, button.input, button.button);
} }
// Remapea los inputs a inputs
input_->bindGameControllerButton(index_controller_, InputAction::SM_SELECT, InputAction::FIRE_LEFT);
input_->bindGameControllerButton(index_controller_, InputAction::SM_BACK, InputAction::FIRE_CENTER);
} }
// Comprueba los eventos // Comprueba los eventos

View File

@@ -1353,8 +1353,7 @@ void Game::checkPauseInput()
// Comprueba los mandos // Comprueba los mandos
for (int i = 0; i < input_->getNumControllers(); ++i) for (int i = 0; i < input_->getNumControllers(); ++i)
{ {
if (input_->checkInput(InputAction::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) && if (input_->checkInput(InputAction::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
input_->checkInput(InputAction::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
pause(!paused_); pause(!paused_);
return; return;
@@ -1372,7 +1371,7 @@ void Game::checkPauseInput()
// Gestiona las entradas de los jugadores en el modo demo para saltarse la demo. // Gestiona las entradas de los jugadores en el modo demo para saltarse la demo.
void Game::DEMO_handlePassInput() void Game::DEMO_handlePassInput()
{ {
if (input_->checkAnyButtonPressed()) if (input_->checkAnyButton())
{ {
Section::name = Section::Name::TITLE; // Salir del modo demo y regresar al menú principal. Section::name = Section::Name::TITLE; // Salir del modo demo y regresar al menú principal.
Section::attract_mode = Section::AttractMode::TITLE_TO_DEMO; // El juego volverá a mostrar la demo Section::attract_mode = Section::AttractMode::TITLE_TO_DEMO; // El juego volverá a mostrar la demo

View File

@@ -11,7 +11,7 @@
#include "param.h" // Para Param, ParamGame, param #include "param.h" // Para Param, ParamGame, param
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options, AttractMode #include "section.h" // Para Name, name, Options, options, AttractMode
#include "ui/service_menu.h" // Para ServiceMenu #include "ui/service_menu.h" // Para ServiceMenu
#include "utils.h" // Para boolToOnOff #include "utils.h" // Para boolToOnOff
namespace GlobalInputs namespace GlobalInputs
@@ -192,13 +192,13 @@ namespace GlobalInputs
} }
// Comprueba el boton de servicio // Comprueba el boton de servicio
void checkServiceButton() bool checkServiceButton()
{ {
// Teclado // Teclado
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleServiceMenu(); toggleServiceMenu();
return; return true;
} }
// Mandos // Mandos
@@ -208,17 +208,18 @@ namespace GlobalInputs
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
toggleServiceMenu(); toggleServiceMenu();
return; return true;
} }
} }
} }
return false;
} }
// Comprueba las entradas del menú de servicio // Comprueba las entradas del menú de servicio
void checkServiceInputs() bool checkServiceInputs()
{ {
if (!ServiceMenu::get()->isEnabled()) if (!ServiceMenu::get()->isEnabled())
return; return false;
// Teclado // Teclado
{ {
@@ -226,42 +227,42 @@ namespace GlobalInputs
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->setSelectorUp(); ServiceMenu::get()->setSelectorUp();
return; return true;
} }
// Abajo // Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->setSelectorDown(); ServiceMenu::get()->setSelectorDown();
return; return true;
} }
// Derecha // Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->adjustOption(true); ServiceMenu::get()->adjustOption(true);
return; return true;
} }
// Izquierda // Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->adjustOption(false); ServiceMenu::get()->adjustOption(false);
return; return true;
} }
// Aceptar // Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->selectOption(); ServiceMenu::get()->selectOption();
return; return true;
} }
// Atras // Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
ServiceMenu::get()->moveBack(); ServiceMenu::get()->moveBack();
return; return true;
} }
} }
@@ -273,49 +274,50 @@ namespace GlobalInputs
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->setSelectorUp(); ServiceMenu::get()->setSelectorUp();
return; return true;
} }
// Abajo // Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->setSelectorDown(); ServiceMenu::get()->setSelectorDown();
return; return true;
} }
// Derecha // Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->adjustOption(true); ServiceMenu::get()->adjustOption(true);
return; return true;
} }
// Izquierda // Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->adjustOption(false); ServiceMenu::get()->adjustOption(false);
return; return true;
} }
// Aceptar // Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->selectOption(); ServiceMenu::get()->selectOption();
return; return true;
} }
// Atras // Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i)) if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{ {
ServiceMenu::get()->moveBack(); ServiceMenu::get()->moveBack();
return; return true;
} }
} }
} }
return false;
} }
// Comprueba las entradas fuera del menú de servicio // Comprueba las entradas fuera del menú de servicio
void checkInputs() bool checkInputs()
{ {
// Teclado // Teclado
{ {
@@ -325,7 +327,7 @@ namespace GlobalInputs
Screen::get()->toggleFullscreen(); Screen::get()->toggleFullscreen();
const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10"); const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE}); Notifier::get()->show({MODE});
return; return true;
} }
// Comprueba el teclado para decrementar el tamaño de la ventana // Comprueba el teclado para decrementar el tamaño de la ventana
@@ -335,7 +337,7 @@ namespace GlobalInputs
{ {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)}); Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)});
} }
return; return true;
} }
// Comprueba el teclado para incrementar el tamaño de la ventana // Comprueba el teclado para incrementar el tamaño de la ventana
@@ -345,70 +347,70 @@ namespace GlobalInputs
{ {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)}); Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.size)});
} }
return; return true;
} }
// Salir // Salir
if (Input::get()->checkInput(InputAction::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
quit(); quit();
return; return true;
} }
// Saltar sección // Saltar sección
if (Input::get()->checkAnyButtonPressed() && !ServiceMenu::get()->isEnabled()) if (Input::get()->checkAnyButton() && !ServiceMenu::get()->isEnabled())
{ {
skipSection(); skipSection();
return; return true;
} }
// Reset // Reset
if (Input::get()->checkInput(InputAction::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
reset(); reset();
return; return true;
} }
// Audio // Audio
if (Input::get()->checkInput(InputAction::TOGGLE_AUDIO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::TOGGLE_AUDIO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleAudio(); toggleAudio();
return; return true;
} }
// Autofire // Autofire
if (Input::get()->checkInput(InputAction::TOGGLE_AUTO_FIRE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::TOGGLE_AUTO_FIRE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleFireMode(); toggleFireMode();
return; return true;
} }
// Idioma // Idioma
if (Input::get()->checkInput(InputAction::CHANGE_LANG, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::CHANGE_LANG, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
changeLang(); changeLang();
return; return true;
} }
// Shaders // Shaders
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleShaders(); toggleShaders();
return; return true;
} }
// Integer Scale // Integer Scale
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleIntegerScale(); toggleIntegerScale();
return; return true;
} }
// VSync // VSync
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_VSYNC, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_VSYNC, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
toggleVSync(); toggleVSync();
return; return true;
} }
#ifdef DEBUG #ifdef DEBUG
@@ -416,17 +418,23 @@ namespace GlobalInputs
if (Input::get()->checkInput(InputAction::SHOW_INFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD)) if (Input::get()->checkInput(InputAction::SHOW_INFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
Screen::get()->toggleDebugInfo(); Screen::get()->toggleDebugInfo();
return; return true;
} }
#endif #endif
} }
return false;
} }
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check() bool check()
{ {
checkServiceButton(); Input::get()->update();
checkServiceInputs(); if (checkServiceButton())
checkInputs(); return true;
if (checkServiceInputs())
return true;
if (checkInputs())
return true;
return false;
} }
} }

View File

@@ -3,5 +3,5 @@
namespace GlobalInputs namespace GlobalInputs
{ {
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check(); bool check();
} }

View File

@@ -68,34 +68,13 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY) if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
{ {
const bool *keyStates = SDL_GetKeyboardState(nullptr);
if (repeat) if (repeat)
{ { // El usuario quiere saber si está pulsada (estado mantenido)
success_keyboard = keyStates[key_bindings_[input_index].scancode] != 0; return key_bindings_[input_index].is_held;
} }
else else
{ { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
if (!key_bindings_[input_index].active) return key_bindings_[input_index].just_pressed;
{
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;
}
} }
} }
@@ -108,31 +87,12 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
if (!success_controller) if (!success_controller)
{ {
if (repeat) if (repeat)
{ { // El usuario quiere saber si está pulsada (estado mantenido)
success_controller = SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0; return controller_bindings_.at(controller_index).at(input_index).is_held;
} }
else else
{ { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
if (!controller_bindings_.at(controller_index).at(input_index).active) return controller_bindings_.at(controller_index).at(input_index).just_pressed;
{
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;
}
} }
} }
} }
@@ -144,47 +104,48 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
// Comprueba si hay almenos un input activo // Comprueba si hay almenos un input activo
bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) bool Input::checkAnyInput(InputDeviceToUse device, int controller_index)
{ {
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY) // Obtenemos el número total de acciones posibles para iterar sobre ellas.
{ const int num_actions = static_cast<int>(InputAction::SIZE);
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < (int)key_bindings_.size(); ++i) // --- Comprobación del Teclado ---
{ if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
if (mKeystates[key_bindings_[i].scancode] != 0 && !key_bindings_[i].active) {
{ for (int i = 0; i < num_actions; ++i)
key_bindings_[i].active = true; {
return true; // 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()) // --- Comprobación del Mando ---
{ // Comprobamos si hay mandos y si el índice solicitado es válido.
if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY) if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
{ {
for (int i = 0; i < (int)controller_bindings_.size(); ++i) if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY)
{ {
if (SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button) != 0 && !controller_bindings_[controller_index][i].active) // Bucle CORREGIDO: Iteramos sobre todas las acciones, no sobre el número de mandos.
{ for (int i = 0; i < num_actions; ++i)
controller_bindings_[controller_index][i].active = true; {
return true; // 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" // 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 // Solo comprueba los botones definidos previamente
for (auto bi : button_inputs_) for (auto bi : button_inputs_)
{ {
@@ -218,7 +179,7 @@ bool Input::discoverGameControllers()
} }
// En SDL3, SDL_GetJoysticks devuelve un array de IDs, no un contador // 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; num_gamepads_ = 0;
// Cuenta el número de mandos // Cuenta el número de mandos
@@ -228,7 +189,7 @@ bool Input::discoverGameControllers()
// Usar el ID del joystick, no el índice // Usar el ID del joystick, no el índice
auto joy = SDL_OpenJoystick(joystick_ids[i]); auto joy = SDL_OpenJoystick(joystick_ids[i]);
joysticks_.push_back(joy); joysticks_.push_back(joy);
// En SDL3, SDL_IsGamepad toma un SDL_JoystickID, no un índice // En SDL3, SDL_IsGamepad toma un SDL_JoystickID, no un índice
if (SDL_IsGamepad(joystick_ids[i])) if (SDL_IsGamepad(joystick_ids[i]))
{ {
@@ -261,11 +222,11 @@ bool Input::discoverGameControllers()
if (pad != nullptr) if (pad != nullptr)
{ {
connected_controllers_.push_back(pad); connected_controllers_.push_back(pad);
// Obtener el nombre usando el ID del joystick // 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"; std::string name = name_cstr ? name_cstr : "Unknown Gamepad";
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "#%d: %s", i, name.c_str()); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "#%d: %s", i, name.c_str());
controller_names_.push_back(name); 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 // Resetear todos los KeyBindings.active a false
for (auto &key : key_bindings_) { for (auto &key : key_bindings_)
key.active = false; {
key.is_held = false;
key.just_pressed = false;
} }
// Resetear todos los ControllerBindings.active a false // Resetear todos los ControllerBindings.active a false
for (auto &controller_vec : controller_bindings_) { for (auto &controller_vec : controller_bindings_)
for (auto &binding : controller_vec) { {
binding.active = false; 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;
} }
} }
} }

View File

@@ -85,9 +85,10 @@ public:
void bindGameControllerButton(int controller_index, InputAction inputTarget, InputAction inputSource); // Asigna inputs a otros inputs del mando void bindGameControllerButton(int controller_index, InputAction inputTarget, InputAction inputSource); // Asigna inputs a otros inputs del mando
// --- Métodos de consulta de entrada --- // --- Métodos de consulta de entrada ---
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
bool checkInput(InputAction input, bool repeat = true, InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0); // Comprueba si un input está activo bool checkInput(InputAction input, bool repeat = true, InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0); // Comprueba si un input está activo
bool checkAnyInput(InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0); // Comprueba si hay al menos un input activo bool checkAnyInput(InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0); // Comprueba si hay al menos un input activo
int checkAnyButtonPressed(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT); // Comprueba si hay algún botón pulsado int checkAnyButton(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT); // Comprueba si hay algún botón pulsado
// --- Métodos de gestión de mandos --- // --- Métodos de gestión de mandos ---
bool discoverGameControllers(); // Busca si hay mandos conectados bool discoverGameControllers(); // Busca si hay mandos conectados
@@ -113,21 +114,23 @@ private:
// --- Estructuras internas --- // --- Estructuras internas ---
struct KeyBindings struct KeyBindings
{ {
Uint8 scancode; // Scancode asociado Uint8 scancode; // Scancode asociado
bool active; // Indica si está activo bool is_held; // Está pulsada ahora mismo
bool just_pressed; // Se acaba de pulsar en este fotograma
KeyBindings(Uint8 sc = 0, bool act = false) KeyBindings(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
: scancode(sc), active(act) {} : scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
}; };
struct ControllerBindings struct ControllerBindings
{ {
SDL_GamepadButton button; // GameControllerButton asociado SDL_GamepadButton button; // GameControllerButton asociado
bool active; // Indica si está activo bool is_held; // Está pulsada ahora mismo
bool just_pressed; // Se acaba de pulsar en este fotograma
bool axis_active; // Estado del eje bool axis_active; // Estado del eje
ControllerBindings(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool act = false, bool axis_act = false) ControllerBindings(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
: button(btn), active(act), axis_active(axis_act) {} : button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
}; };
// --- Variables internas --- // --- Variables internas ---
@@ -142,7 +145,7 @@ private:
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
// --- Métodos internos --- // --- Métodos internos ---
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
bool checkAxisInput(InputAction input, int controller_index, bool repeat); // Comprueba el eje del mando bool checkAxisInput(InputAction input, int controller_index, bool repeat); // Comprueba el eje del mando
// --- Constructor y destructor --- // --- Constructor y destructor ---