#include "define_buttons.h" #include #include #include #include "input.h" #include "input_types.h" #include "lang.h" #include "options.h" #include "param.h" #include "resource.h" #include "text.h" #include "ui/window_message.h" DefineButtons::DefineButtons() : input_(Input::get()) { clearButtons(); auto gamepads = input_->getGamepads(); for (auto gamepad : gamepads) { controller_names_.emplace_back(Input::getControllerName(gamepad)); } // Crear la ventana de mensaje auto text_renderer = Resource::get()->getText("04b_25_flat"); window_message_ = std::make_unique( text_renderer, Lang::getText("[DEFINE_BUTTONS] TITLE"), Color{20, 30, 50, 208}, // Fondo azul oscuro semi-transparente Color{100, 150, 200, 255}, // Borde azul claro Color{100, 150, 200, 255}, // Titulo azul claro Color{220, 220, 220, 255} // Texto gris claro ); window_message_->setPadding(0.0f); window_message_->setLineSpacing(0.0f); } void DefineButtons::render() { if (enabled_ && window_message_) { window_message_->render(); } } void DefineButtons::update() { if (enabled_ && window_message_) { window_message_->update(); } } void DefineButtons::checkEvents(const SDL_Event &event) { if (enabled_) { switch (event.type) { case SDL_EVENT_GAMEPAD_BUTTON_DOWN: doControllerButtonDown(event.gbutton); break; case SDL_EVENT_GAMEPAD_BUTTON_UP: checkEnd(); break; default: break; } } } auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool { if (options_gamepad != nullptr) { options_gamepad_ = options_gamepad; enabled_ = true; finished_ = false; index_button_ = 0; clearButtons(); updateWindowMessage(); if (window_message_) { window_message_->autoSize(); window_message_->centerOnScreen(); window_message_->show(); } return true; } return false; } void DefineButtons::disable() { enabled_ = false; finished_ = false; if (window_message_) { window_message_->hide(); } } void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) { auto gamepad = input_->getGamepad(event.which); if (!gamepad || gamepad != options_gamepad_->instance) { return; } const auto BUTTON = static_cast(event.button); if (checkButtonNotInUse(BUTTON)) { buttons_.at(index_button_).button = BUTTON; incIndexButton(); updateWindowMessage(); } } void DefineButtons::bindButtons(Options::Gamepad *options_gamepad) { for (const auto &button : buttons_) { Input::bindGameControllerButton(options_gamepad->instance, button.action, button.button); } 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); } void DefineButtons::incIndexButton() { if (index_button_ < buttons_.size() - 1) { ++index_button_; } else { finished_ = true; } } auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool { return std::ranges::all_of(buttons_, [button](const auto &b) { return b.button != button; }); } void DefineButtons::clearButtons() { buttons_.clear(); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_LEFT"), Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_UP"), Input::Action::FIRE_CENTER, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_RIGHT"), Input::Action::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] START"), Input::Action::START, SDL_GAMEPAD_BUTTON_INVALID); buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] SERVICE_MENU"), Input::Action::SERVICE, SDL_GAMEPAD_BUTTON_INVALID); } void DefineButtons::checkEnd() { if (finished_) { bindButtons(options_gamepad_); input_->saveGamepadConfigFromGamepad(options_gamepad_->instance); input_->resetInputStates(); // Mostrar mensaje de finalización brevemente if (window_message_) { window_message_->clearTexts(); window_message_->addText(Lang::getText("[DEFINE_BUTTONS] CONFIGURATION_COMPLETE")); window_message_->autoSize(); window_message_->centerOnScreen(); } // Se deshabilitará desde el ServiceMenu después de un breve delay } } void DefineButtons::updateWindowMessage() { if (!window_message_ || !options_gamepad_) { return; } // Configurar título std::string title = Lang::getText("[DEFINE_BUTTONS] CONFIGURING") + ": " + options_gamepad_->name; window_message_->setTitle(title); // Limpiar textos anteriores window_message_->clearTexts(); if (index_button_ < buttons_.size()) { // Mostrar progreso /*std::string progress = "(" + std::to_string(index_button_ + 1) + "/" + std::to_string(buttons_.size()) + ")"; window_message_->addText(progress); window_message_->addText("");*/ // Instrucción actual std::string instruction = Lang::getText("[DEFINE_BUTTONS] PRESS_BUTTON_FOR") + ":"; window_message_->addText(instruction); window_message_->addText(buttons_.at(index_button_).label); // Botones ya configurados /*if (index_button_ > 0) { window_message_->addText(""); window_message_->addText(Lang::getText("[DEFINE_BUTTONS] CONFIGURED") + ":"); for (size_t i = 0; i < index_button_; ++i) { std::string configured = "✓ " + buttons_[i].label; window_message_->addText(configured); } }*/ } }