migrant input: ja es poden redefinir els botons. Falta rebindar les accions a accions

This commit is contained in:
2025-08-04 13:55:58 +02:00
parent cde6ad4b71
commit adf21086e5
5 changed files with 41 additions and 47 deletions

View File

@@ -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<int>(gamepad_->player_id)));
text->writeCentered(x_, y_, gamepad_->name);
text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast<int>(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<int>(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<SDL_GamepadButton>(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<Input::Gamepad> 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
}