fix DefineButtons i Title: ja no fa coses rares al definir la ultima tecla

This commit is contained in:
2025-06-21 14:13:36 +02:00
parent e8b67d3be1
commit f5731c8181
5 changed files with 65 additions and 45 deletions

View File

@@ -63,9 +63,19 @@ void DefineButtons::bindButtons()
// Comprueba los eventos // Comprueba los eventos
void DefineButtons::checkEvents(const SDL_Event &event) void DefineButtons::checkEvents(const SDL_Event &event)
{ {
if (enabled_ && event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) if (enabled_)
{ {
doControllerButtonDown(event.gbutton); switch (event.type)
{
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
doControllerButtonDown(event.gbutton);
break;
case SDL_EVENT_GAMEPAD_BUTTON_UP:
checkEnd();
break;
default:
break;
}
} }
} }
@@ -75,6 +85,7 @@ bool DefineButtons::enable(int index)
if (index < input_->getNumControllers()) if (index < input_->getNumControllers())
{ {
enabled_ = true; enabled_ = true;
finished_ = false;
index_controller_ = index; index_controller_ = index;
index_button_ = 0; index_button_ = 0;
clearButtons(); clearButtons();
@@ -90,19 +101,13 @@ bool DefineButtons::isEnabled() const { return enabled_; }
// Incrementa el indice de los botones // Incrementa el indice de los botones
void DefineButtons::incIndexButton() void DefineButtons::incIndexButton()
{ {
++index_button_; if (index_button_ < buttons_.size() - 1)
// Comprueba si ha finalizado
if (index_button_ == buttons_.size())
{ {
// Asigna los botones definidos al input_ ++index_button_;
bindButtons(); }
else
// Guarda los cambios en las opciones {
saveBindingsToOptions(); finished_ = true;
// Deshabilita
enabled_ = false;
} }
} }
@@ -141,3 +146,23 @@ void DefineButtons::clearButtons()
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] START"), InputAction::START, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] START"), InputAction::START, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] SERVICE_MENU"), InputAction::SERVICE, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] SERVICE_MENU"), InputAction::SERVICE, SDL_GAMEPAD_BUTTON_INVALID);
} }
// Comprueba si ha finalizado
void DefineButtons::checkEnd()
{
// Comprueba si ha finalizado
if (finished_)
{
// Asigna los botones definidos al input_
bindButtons();
// Guarda los cambios en las opciones
saveBindingsToOptions();
// Reinicia los estados de las pulsaciones de los botones
input_->resetInputStates();
// Deshabilita
enabled_ = false;
}
}

View File

@@ -47,6 +47,7 @@ private:
size_t index_controller_ = 0; // Índice del controlador asignado size_t index_controller_ = 0; // Índice del controlador asignado
size_t index_button_ = 0; // Índice del botón en proceso size_t index_button_ = 0; // Índice del botón en proceso
std::vector<std::string> controller_names_; // Nombres de los mandos std::vector<std::string> controller_names_; // Nombres de los mandos
bool finished_ = false;
// Métodos internos // Métodos internos
void incIndexButton(); // Incrementa el índice de botones void incIndexButton(); // Incrementa el índice de botones
@@ -55,4 +56,5 @@ private:
void saveBindingsToOptions(); // Guarda configuraciones void saveBindingsToOptions(); // Guarda configuraciones
bool checkButtonNotInUse(SDL_GamepadButton button); // Verifica uso de botones bool checkButtonNotInUse(SDL_GamepadButton button); // Verifica uso de botones
void clearButtons(); // Limpia asignaciones actuales void clearButtons(); // Limpia asignaciones actuales
void checkEnd(); // Comprueba si ha finalizado
}; };

View File

@@ -452,3 +452,16 @@ void Input::initSDL()
} }
} }
} }
void Input::resetInputStates() {
// Resetear todos los KeyBindings.active a false
for (auto &key : key_bindings_) {
key.active = false;
}
// Resetear todos los ControllerBindings.active a false
for (auto &controller_vec : controller_bindings_) {
for (auto &binding : controller_vec) {
binding.active = false;
}
}
}

View File

@@ -103,6 +103,9 @@ public:
InputAction to_inputs_e(const std::string &name) const; // Convierte un std::string a InputAction InputAction to_inputs_e(const std::string &name) const; // Convierte un std::string a InputAction
int getIndexByName(const std::string &name) const; // Obtiene el índice a partir del nombre del mando int getIndexByName(const std::string &name) const; // Obtiene el índice a partir del nombre del mando
// --- Métodos de reseteo de estado de entrada ---
void resetInputStates(); // Pone todos los KeyBindings.active y ControllerBindings.active a false
private: private:
// --- Singleton --- // --- Singleton ---
static Input *instance_; static Input *instance_;

View File

@@ -143,42 +143,33 @@ void Title::checkEvents()
{ {
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0) if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
{ {
bool should_reset = false;
switch (event.key.key) switch (event.key.key)
{ {
case SDLK_1: // Redefine los botones del mando #0 case SDLK_1: // Redefine los botones del mando #0
should_reset = define_buttons_->enable(0); define_buttons_->enable(0);
break; break;
case SDLK_2: // Redefine los botones del mando #1 case SDLK_2: // Redefine los botones del mando #1
should_reset = define_buttons_->enable(1); define_buttons_->enable(1);
break; break;
case SDLK_3: // Intercambia los mandos entre los dos jugadores case SDLK_3: // Intercambia los mandos entre los dos jugadores
swapControllers(); swapControllers();
should_reset = true;
break; break;
case SDLK_4: // Intercambia la asignación del teclado case SDLK_4: // Intercambia la asignación del teclado
swapKeyboard(); swapKeyboard();
should_reset = true;
break; break;
case SDLK_5: // Muestra la asignación de mandos y teclado case SDLK_5: // Muestra la asignación de mandos y teclado
showControllers(); showControllers();
should_reset = true;
break; break;
default: default:
break; break;
} }
// Resetear el contador si es necesario resetCounter();
if (should_reset)
{
resetCounter();
}
} }
GlobalEvents::check(event); GlobalEvents::check(event);
@@ -198,8 +189,7 @@ void Title::checkInput()
for (const auto &CONTROLLER : Options::controllers) for (const auto &CONTROLLER : Options::controllers)
{ {
// START // START
if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) && if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
!Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
{ {
if ((state_ == TitleState::LOGO_FINISHED || ALLOW_TITLE_ANIMATION_SKIP) && !fade_->isEnabled()) if ((state_ == TitleState::LOGO_FINISHED || ALLOW_TITLE_ANIMATION_SKIP) && !fade_->isEnabled())
{ {
@@ -222,28 +212,15 @@ void Title::checkInput()
return; return;
} }
} }
// SWAP_CONTROLLERS
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) &&
Input::get()->checkInput(InputAction::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
{
swapControllers();
return;
}
// CONFIG
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) &&
Input::get()->checkInput(InputAction::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
{
define_buttons_->enable(CONTROLLER.index);
return;
}
} }
} }
} }
// 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
GlobalInputs::check(); if (!define_buttons_->isEnabled())
{
GlobalInputs::check();
}
} }
// Bucle para el titulo del juego // Bucle para el titulo del juego