Al redefinir botons, ja no pots repetir botó. Util per als qui tenim la ma tremolosa i apretem dos voltes sense voler

This commit is contained in:
2024-11-01 14:32:27 +01:00
parent f786cb7776
commit cd68c5ffea
4 changed files with 54 additions and 30 deletions

View File

@@ -16,11 +16,11 @@ DefineButtons::DefineButtons(std::unique_ptr<Text> text_)
x_ = param.game.width / 2; x_ = param.game.width / 2;
y_ = param.title.press_start_position; y_ = param.title.press_start_position;
buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_X); 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_Y); 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_RIGHTSHOULDER); buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_START); buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(99), InputType::EXIT, SDL_CONTROLLER_BUTTON_BACK); buttons_.emplace_back(lang::getText(99), InputType::EXIT, SDL_CONTROLLER_BUTTON_INVALID);
for (int i = 0; i < input_->getNumControllers(); ++i) for (int i = 0; i < input_->getNumControllers(); ++i)
{ {
@@ -40,9 +40,9 @@ void DefineButtons::render()
} }
// Comprueba el botón que se ha pulsado // 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 // Solo pillamos botones del mando que toca
if (i != index_controller_) if (i != index_controller_)
@@ -50,14 +50,18 @@ void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent *event)
return; return;
} }
buttons_[index_button_].button = (SDL_GameControllerButton)event->button; const auto button = static_cast<SDL_GameControllerButton>(event.button);
if (checkButtonNotInUse(button))
{
buttons_[index_button_].button = button;
incIndexButton(); incIndexButton();
} }
}
// Asigna los botones definidos al input_ // Asigna los botones definidos al input_
void DefineButtons::bindButtons() 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); input_->bindGameControllerButton(index_controller_, buttons_[i].input, buttons_[i].button);
} }
@@ -84,7 +88,7 @@ void DefineButtons::checkInput()
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN:
{ {
doControllerButtonDown(&event.cbutton); doControllerButtonDown(event.cbutton);
break; break;
} }
@@ -121,7 +125,7 @@ void DefineButtons::incIndexButton()
index_button_++; index_button_++;
// Comprueba si ha finalizado // Comprueba si ha finalizado
if (index_button_ == (int)buttons_.size()) if (index_button_ == buttons_.size())
{ {
// Asigna los botones definidos al input_ // Asigna los botones definidos al input_
bindButtons(); bindButtons();
@@ -152,7 +156,18 @@ void DefineButtons::saveBindingsToOptions()
// Intercambia los jugadores asignados a los dos primeros mandos // Intercambia los jugadores asignados a los dos primeros mandos
void DefineButtons::swapControllers() void DefineButtons::swapControllers()
{ {
const int temp = options.controller[0].player_id; std::swap(options.controller[0].player_id, options.controller[1].player_id);
options.controller[0].player_id = options.controller[1].player_id; }
options.controller[1].player_id = temp;
// 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;
} }

View File

@@ -33,15 +33,15 @@ private:
int x_; // Posición donde dibujar el texto int x_; // Posición donde dibujar el texto
int y_; // Posición donde dibujar el texto int y_; // Posición donde dibujar el texto
std::vector<DefineButtonsButton> buttons_; // Vector con las nuevas definiciones de botones/acciones std::vector<DefineButtonsButton> buttons_; // Vector con las nuevas definiciones de botones/acciones
int index_controller_ = 0; // Indice del controlador a reasignar size_t index_controller_ = 0; // Indice del controlador a reasignar
int index_button_ = 0; // Indice para saber qué bot´çon se está definiendo size_t index_button_ = 0; // Indice para saber qué botón se está definiendo
std::vector<std::string> controller_names_; // Nombres de los mandos std::vector<std::string> controller_names_; // Nombres de los mandos
// Incrementa el indice de los botones // Incrementa el indice de los botones
void incIndexButton(); void incIndexButton();
// Comprueba el botón que se ha pulsado // Comprueba el botón que se ha pulsado
void doControllerButtonDown(SDL_ControllerButtonEvent *event); void doControllerButtonDown(SDL_ControllerButtonEvent &event);
// Asigna los botones definidos al input // Asigna los botones definidos al input
void bindButtons(); void bindButtons();
@@ -49,6 +49,9 @@ private:
// Guarda los cambios en las opciones // Guarda los cambios en las opciones
void saveBindingsToOptions(); void saveBindingsToOptions();
// Comprueba que un botón no esté ya asignado
bool checkButtonNotInUse(SDL_GameControllerButton button);
public: public:
// Constructor // Constructor
explicit DefineButtons(std::unique_ptr<Text> text); explicit DefineButtons(std::unique_ptr<Text> text);

View File

@@ -216,6 +216,12 @@ void Title::checkEvents()
break; break;
} }
case SDLK_4:
{
showControllers();
break;
}
default: default:
break; break;
} }
@@ -302,23 +308,21 @@ void Title::reLoadTextures()
} }
// Reinicia el contador interno // Reinicia el contador interno
void Title::resetCounter() void Title::resetCounter() { counter_ = 0; }
{
counter_ = 0;
}
// Intercambia la asignación de mandos a los jugadores // Intercambia la asignación de mandos a los jugadores
void Title::swapControllers() void Title::swapControllers()
{ {
const auto num_controllers = Input::get()->getNumControllers(); if (Input::get()->getNumControllers() == 0)
if (num_controllers == 0)
{
return; return;
}
define_buttons_->swapControllers(); 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 // Crea cadenas de texto vacias para un numero máximo de mandos
constexpr int MAX_CONTROLLERS = 2; constexpr int MAX_CONTROLLERS = 2;
std::string text[MAX_CONTROLLERS]; std::string text[MAX_CONTROLLERS];
@@ -341,11 +345,10 @@ void Title::swapControllers()
const int index = playerControllerIndex[i]; const int index = playerControllerIndex[i];
if (options.controller[index].plugged) 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]); Notifier::get()->showText(text[0], text[1]);
resetCounter(); resetCounter();
} }

View File

@@ -74,6 +74,9 @@ private:
// Intercambia la asignación de mandos a los jugadores // Intercambia la asignación de mandos a los jugadores
void swapControllers(); void swapControllers();
// Muestra información sobre los controles y los jugadores
void showControllers();
public: public:
// Constructor // Constructor
Title(); Title();