diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index bd1c732..a77bba3 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -30,18 +30,20 @@ DefineButtons::DefineButtons() void DefineButtons::render() { static auto text = Resource::get()->getText("8bithud"); if (enabled_) { - text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast(gamepad_->player_id))); - text->writeCentered(x_, y_, gamepad_->name); + text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast(options_gamepad_->player_id))); + text->writeCentered(x_, y_, options_gamepad_->name); text->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label); } } // Comprueba el botón que se ha pulsado void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) { - // Solo pilla botones del mando que toca - // if (input_->getJoyIndex(event.which) != static_cast(index_controller_)) { - // return; - //} + auto gamepad = input_->getGamepad(event.which); + + // Asegúrate de que el gamepad sea válido y sea el que corresponde + if (!gamepad) { + return; + } const auto BUTTON = static_cast(event.button); if (checkButtonNotInUse(BUTTON)) { @@ -51,14 +53,14 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) } // Asigna los botones definidos al input_ -void DefineButtons::bindButtons(std::shared_ptr gamepad) { +void DefineButtons::bindButtons(Options::Gamepad *options_gamepad) { for (const auto &button : buttons_) { - input_->bindGameControllerButton(gamepad, button.action, button.button); + input_->bindGameControllerButton(options_gamepad->instance, button.action, button.button); } // Remapea los inputs a inputs - input_->bindGameControllerButton(gamepad, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT); - input_->bindGameControllerButton(gamepad, Input::Action::SM_BACK, Input::Action::FIRE_CENTER); + input_->bindGameControllerButton(options_gamepad->instance, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT); + input_->bindGameControllerButton(options_gamepad->instance, Input::Action::SM_BACK, Input::Action::FIRE_CENTER); } // Comprueba los eventos @@ -78,9 +80,9 @@ void DefineButtons::checkEvents(const SDL_Event &event) { } // Habilita el objeto -auto DefineButtons::enable(Options::Gamepad *gamepad) -> bool { - if (gamepad != nullptr) { - gamepad_ = gamepad; +auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool { + if (options_gamepad != nullptr) { + options_gamepad_ = options_gamepad; enabled_ = true; finished_ = false; index_button_ = 0; @@ -99,15 +101,6 @@ void DefineButtons::incIndexButton() { } } -// Guarda los cambios en las opciones -void DefineButtons::saveBindingsToOptions() { - // auto &controller = Options::controllers.at(index_controller_); - // controller.name = input_->getControllerName(index_controller_); - // for (size_t j = 0; j < controller.inputs.size(); ++j) { - // controller.buttons.at(j) = input_->getControllerBinding(index_controller_, controller.inputs.at(j)); - // } -} - // Comprueba que un botón no esté ya asignado auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool { return std::ranges::all_of(buttons_, [button](const auto &b) { @@ -128,8 +121,8 @@ void DefineButtons::clearButtons() { // Comprueba si ha finalizado void DefineButtons::checkEnd() { if (finished_) { - // bindButtons(); // Asigna los botones definidos al input_ - saveBindingsToOptions(); // Guarda los cambios en las opciones + bindButtons(options_gamepad_); // Asigna los botones definidos al input_ + input_->saveGamepadConfigFromGamepad(options_gamepad_->instance); // Guarda los cambios input_->resetInputStates(); // Reinicia los estados de las pulsaciones de los botones enabled_ = false; // Deshabilita } diff --git a/source/define_buttons.h b/source/define_buttons.h index fd4fc77..74c6667 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -29,7 +29,7 @@ class DefineButtons { void render(); // Dibuja el objeto en pantalla void checkEvents(const SDL_Event &event); // Procesa los eventos - auto enable(Options::Gamepad *gamepad) -> bool; // Habilita la redefinición de botones + auto enable(Options::Gamepad *options_gamepad) -> bool; // Habilita la redefinición de botones [[nodiscard]] auto isEnabled() const -> bool { return enabled_; }; // Comprueba si está habilitado private: @@ -44,13 +44,12 @@ class DefineButtons { std::vector