From cd68c5ffeab5d4ddb38a9b78b3efab43e7c4761d Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 1 Nov 2024 14:32:27 +0100 Subject: [PATCH] =?UTF-8?q?Al=20redefinir=20botons,=20ja=20no=20pots=20rep?= =?UTF-8?q?etir=20bot=C3=B3.=20Util=20per=20als=20qui=20tenim=20la=20ma=20?= =?UTF-8?q?tremolosa=20i=20apretem=20dos=20voltes=20sense=20voler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/define_buttons.cpp | 47 ++++++++++++++++++++++++++------------- source/define_buttons.h | 9 +++++--- source/title.cpp | 25 ++++++++++++--------- source/title.h | 3 +++ 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index fa37ac6..03de0eb 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -16,11 +16,11 @@ DefineButtons::DefineButtons(std::unique_ptr text_) x_ = param.game.width / 2; y_ = param.title.press_start_position; - buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_X); - buttons_.emplace_back(lang::getText(96), InputType::FIRE_CENTER, SDL_CONTROLLER_BUTTON_Y); - buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); - buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_START); - buttons_.emplace_back(lang::getText(99), InputType::EXIT, SDL_CONTROLLER_BUTTON_BACK); + buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_INVALID); + buttons_.emplace_back(lang::getText(96), InputType::FIRE_CENTER, SDL_CONTROLLER_BUTTON_INVALID); + buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_INVALID); + buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_INVALID); + buttons_.emplace_back(lang::getText(99), InputType::EXIT, SDL_CONTROLLER_BUTTON_INVALID); for (int i = 0; i < input_->getNumControllers(); ++i) { @@ -40,9 +40,9 @@ void DefineButtons::render() } // Comprueba el botón que se ha pulsado -void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent *event) +void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent &event) { - int i = input_->getJoyIndex(event->which); + size_t i = input_->getJoyIndex(event.which); // Solo pillamos botones del mando que toca if (i != index_controller_) @@ -50,14 +50,18 @@ void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent *event) return; } - buttons_[index_button_].button = (SDL_GameControllerButton)event->button; - incIndexButton(); + const auto button = static_cast(event.button); + if (checkButtonNotInUse(button)) + { + buttons_[index_button_].button = button; + incIndexButton(); + } } // Asigna los botones definidos al input_ void DefineButtons::bindButtons() { - for (int i = 0; i < (int)buttons_.size(); ++i) + for (size_t i = 0; i < buttons_.size(); ++i) { input_->bindGameControllerButton(index_controller_, buttons_[i].input, buttons_[i].button); } @@ -84,7 +88,7 @@ void DefineButtons::checkInput() case SDL_CONTROLLERBUTTONDOWN: { - doControllerButtonDown(&event.cbutton); + doControllerButtonDown(event.cbutton); break; } @@ -121,7 +125,7 @@ void DefineButtons::incIndexButton() index_button_++; // Comprueba si ha finalizado - if (index_button_ == (int)buttons_.size()) + if (index_button_ == buttons_.size()) { // Asigna los botones definidos al input_ bindButtons(); @@ -152,7 +156,18 @@ void DefineButtons::saveBindingsToOptions() // Intercambia los jugadores asignados a los dos primeros mandos void DefineButtons::swapControllers() { - const int temp = options.controller[0].player_id; - options.controller[0].player_id = options.controller[1].player_id; - options.controller[1].player_id = temp; -} \ No newline at end of file + std::swap(options.controller[0].player_id, options.controller[1].player_id); +} + +// Comprueba que un botón no esté ya asignado +bool DefineButtons::checkButtonNotInUse(SDL_GameControllerButton button) +{ + for (const auto &b : buttons_) + { + if (b.button == button) + { + return false; + } + } + return true; +} diff --git a/source/define_buttons.h b/source/define_buttons.h index 7ad27f6..fa438f2 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -33,15 +33,15 @@ private: int x_; // Posición donde dibujar el texto int y_; // Posición donde dibujar el texto std::vector buttons_; // Vector con las nuevas definiciones de botones/acciones - int index_controller_ = 0; // Indice del controlador a reasignar - int index_button_ = 0; // Indice para saber qué bot´çon se está definiendo + size_t index_controller_ = 0; // Indice del controlador a reasignar + size_t index_button_ = 0; // Indice para saber qué botón se está definiendo std::vector controller_names_; // Nombres de los mandos // Incrementa el indice de los botones void incIndexButton(); // Comprueba el botón que se ha pulsado - void doControllerButtonDown(SDL_ControllerButtonEvent *event); + void doControllerButtonDown(SDL_ControllerButtonEvent &event); // Asigna los botones definidos al input void bindButtons(); @@ -49,6 +49,9 @@ private: // Guarda los cambios en las opciones void saveBindingsToOptions(); + // Comprueba que un botón no esté ya asignado + bool checkButtonNotInUse(SDL_GameControllerButton button); + public: // Constructor explicit DefineButtons(std::unique_ptr text); diff --git a/source/title.cpp b/source/title.cpp index 80bad4c..5f67e12 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -216,6 +216,12 @@ void Title::checkEvents() break; } + case SDLK_4: + { + showControllers(); + break; + } + default: break; } @@ -302,23 +308,21 @@ void Title::reLoadTextures() } // Reinicia el contador interno -void Title::resetCounter() -{ - counter_ = 0; -} +void Title::resetCounter() { counter_ = 0; } // Intercambia la asignación de mandos a los jugadores void Title::swapControllers() { - const auto num_controllers = Input::get()->getNumControllers(); - - if (num_controllers == 0) - { + if (Input::get()->getNumControllers() == 0) return; - } define_buttons_->swapControllers(); + showControllers(); +} +// Muestra información sobre los controles y los jugadores +void Title::showControllers() +{ // Crea cadenas de texto vacias para un numero máximo de mandos constexpr int MAX_CONTROLLERS = 2; std::string text[MAX_CONTROLLERS]; @@ -341,11 +345,10 @@ void Title::swapControllers() const int index = playerControllerIndex[i]; if (options.controller[index].plugged) { - text[i] = lang::getText(101) + std::to_string(i + 1) + ": " + options.controller[index].name; + text[i] = lang::getText(100) + std::to_string(i + 1) + ": " + options.controller[index].name; } } Notifier::get()->showText(text[0], text[1]); - resetCounter(); } \ No newline at end of file diff --git a/source/title.h b/source/title.h index d8f887d..19ff753 100644 --- a/source/title.h +++ b/source/title.h @@ -74,6 +74,9 @@ private: // Intercambia la asignación de mandos a los jugadores void swapControllers(); + // Muestra información sobre los controles y los jugadores + void showControllers(); + public: // Constructor Title();