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
void DefineButtons::checkEvents(const SDL_Event &event)
{
if (enabled_ && event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN)
if (enabled_)
{
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())
{
enabled_ = true;
finished_ = false;
index_controller_ = index;
index_button_ = 0;
clearButtons();
@@ -90,19 +101,13 @@ bool DefineButtons::isEnabled() const { return enabled_; }
// Incrementa el indice de los botones
void DefineButtons::incIndexButton()
{
++index_button_;
// Comprueba si ha finalizado
if (index_button_ == buttons_.size())
if (index_button_ < buttons_.size() - 1)
{
// Asigna los botones definidos al input_
bindButtons();
// Guarda los cambios en las opciones
saveBindingsToOptions();
// Deshabilita
enabled_ = false;
++index_button_;
}
else
{
finished_ = true;
}
}
@@ -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] 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_button_ = 0; // Índice del botón en proceso
std::vector<std::string> controller_names_; // Nombres de los mandos
bool finished_ = false;
// Métodos internos
void incIndexButton(); // Incrementa el índice de botones
@@ -55,4 +56,5 @@ private:
void saveBindingsToOptions(); // Guarda configuraciones
bool checkButtonNotInUse(SDL_GamepadButton button); // Verifica uso de botones
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
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:
// --- Singleton ---
static Input *instance_;

View File

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