From f5731c818140acec6e828153c274b7192e922cd6 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 21 Jun 2025 14:13:36 +0200 Subject: [PATCH] fix DefineButtons i Title: ja no fa coses rares al definir la ultima tecla --- source/define_buttons.cpp | 53 ++++++++++++++++++++++++++++----------- source/define_buttons.h | 2 ++ source/input.cpp | 13 ++++++++++ source/input.h | 3 +++ source/title.cpp | 39 ++++++---------------------- 5 files changed, 65 insertions(+), 45 deletions(-) diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index 76ed610..9f87f63 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -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_) { - 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()) { 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; } } @@ -140,4 +145,24 @@ void DefineButtons::clearButtons() buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_RIGHT"), InputAction::FIRE_RIGHT, 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); +} + +// 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; + } } \ No newline at end of file diff --git a/source/define_buttons.h b/source/define_buttons.h index 30950d5..8e47b9b 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -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 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 }; diff --git a/source/input.cpp b/source/input.cpp index 2cdab8a..b4a19ba 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -451,4 +451,17 @@ void Input::initSDL() SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "** SDL_GAMEPAD: INITIALIZATION COMPLETE\n"); } } +} + +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; + } + } } \ No newline at end of file diff --git a/source/input.h b/source/input.h index a9df8fc..2bf58d2 100644 --- a/source/input.h +++ b/source/input.h @@ -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_; diff --git a/source/title.cpp b/source/title.cpp index db4cfdc..2ef3ba7 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -143,42 +143,33 @@ 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(); - } + resetCounter(); } GlobalEvents::check(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 - GlobalInputs::check(); + if (!define_buttons_->isEnabled()) + { + GlobalInputs::check(); + } } // Bucle para el titulo del juego