GlobalInputs: ja es pot eixir del joc mentre redefinixes botons

This commit is contained in:
2025-08-08 18:55:18 +02:00
parent 3b07b2e130
commit 6f120dd4d2
4 changed files with 95 additions and 184 deletions

View File

@@ -35,14 +35,13 @@ DefineButtons::DefineButtons()
config.text_safety_margin = 15.0F; config.text_safety_margin = 15.0F;
config.min_width = 100; config.min_width = 100;
config.min_height = 32; config.min_height = 32;
config.animation_duration = 0.5F; config.animation_duration = 0.3F;
auto text_renderer = Resource::get()->getText("04b_25_flat"); auto text_renderer = Resource::get()->getText("04b_25_flat");
window_message_ = std::make_unique<WindowMessage>( window_message_ = std::make_unique<WindowMessage>(
text_renderer, text_renderer,
Lang::getText("[DEFINE_BUTTONS] TITLE"), Lang::getText("[DEFINE_BUTTONS] TITLE"),
config config);
);
window_message_->setPosition(param.game.game_area.center_x, param.game.game_area.center_y, WindowMessage::PositionMode::CENTERED); window_message_->setPosition(param.game.game_area.center_x, param.game.game_area.center_y, WindowMessage::PositionMode::CENTERED);
} }
@@ -56,20 +55,20 @@ void DefineButtons::update() {
if (!enabled_) { if (!enabled_) {
return; return;
} }
// Actualizar la ventana siempre // Actualizar la ventana siempre
if (window_message_) { if (window_message_) {
window_message_->update(); window_message_->update();
} }
// Manejar la secuencia de cierre si ya terminamos // Manejar la secuencia de cierre si ya terminamos
if (finished_ && message_shown_) { if (finished_ && message_shown_) {
message_timer_++; message_timer_++;
// Después del delay, iniciar animación de cierre (solo una vez) // Después del delay, iniciar animación de cierre (solo una vez)
if (message_timer_ > MESSAGE_DISPLAY_FRAMES && !closing_) { if (message_timer_ > MESSAGE_DISPLAY_FRAMES && !closing_) {
if (window_message_) { if (window_message_) {
window_message_->hide(); // Iniciar animación de cierre window_message_->hide(); // Iniciar animación de cierre
} }
closing_ = true; closing_ = true;
} }
@@ -97,6 +96,8 @@ auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool {
enabled_ = true; enabled_ = true;
finished_ = false; finished_ = false;
index_button_ = 0; index_button_ = 0;
message_shown_ = false;
closing_ = false;
clearButtons(); clearButtons();
updateWindowMessage(); updateWindowMessage();
@@ -113,6 +114,8 @@ auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool {
void DefineButtons::disable() { void DefineButtons::disable() {
enabled_ = false; enabled_ = false;
finished_ = false; finished_ = false;
message_shown_ = false;
closing_ = false;
if (window_message_) { if (window_message_) {
window_message_->hide(); window_message_->hide();
@@ -187,11 +190,11 @@ void DefineButtons::checkEnd() {
bool DefineButtons::isReadyToClose() const { bool DefineButtons::isReadyToClose() const {
// Solo está listo para cerrar si: // Solo está listo para cerrar si:
// 1. Terminó // 1. Terminó
// 2. Ya mostró el mensaje // 2. Ya mostró el mensaje
// 3. Está cerrando // 3. Está cerrando
// 4. La ventana ya no está visible (animación terminada) // 4. La ventana ya no está visible (animación terminada)
return finished_ && message_shown_ && closing_ && return finished_ && message_shown_ && closing_ &&
(!window_message_ || !window_message_->isVisible()); (!window_message_ || !window_message_->isVisible());
} }
void DefineButtons::updateWindowMessage() { void DefineButtons::updateWindowMessage() {

View File

@@ -40,7 +40,7 @@ class DefineButtons {
private: private:
// Constante para cuánto tiempo mostrar el mensaje (en frames) // Constante para cuánto tiempo mostrar el mensaje (en frames)
static constexpr size_t MESSAGE_DISPLAY_FRAMES = 180; // ~3 segundos a 60fps static constexpr size_t MESSAGE_DISPLAY_FRAMES = 120; // ~2 segundos a 60fps
// Punteros // Punteros
Input *input_ = nullptr; // Entrada del usuario Input *input_ = nullptr; // Entrada del usuario

View File

@@ -171,109 +171,60 @@ auto checkServiceButton() -> bool {
} }
// Mandos // Mandos
{ for (auto gamepad : Input::get()->getGamepads()) {
auto gamepads = Input::get()->getGamepads(); if (Input::get()->checkAction(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
for (auto gamepad : gamepads) { toggleServiceMenu();
if (Input::get()->checkAction(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) { return true;
toggleServiceMenu();
return true;
}
} }
} }
return false;
}
// Comprueba las entradas para elementos del sistema
auto checkSystemInputs() -> bool {
using Action = Input::Action;
static const std::vector<std::pair<Action, std::function<void()>>> actions = {
{Action::WINDOW_FULLSCREEN, toggleFullscreen},
{Action::WINDOW_DEC_SIZE, decWindowSize},
{Action::WINDOW_INC_SIZE, incWindowSize},
{Action::EXIT, quit},
{Action::RESET, reset},
{Action::TOGGLE_AUDIO, toggleAudio},
{Action::TOGGLE_AUTO_FIRE, toggleFireMode},
{Action::CHANGE_LANG, changeLang},
{Action::TOGGLE_VIDEO_SHADERS, toggleShaders},
{Action::TOGGLE_VIDEO_INTEGER_SCALE, toggleIntegerScale},
{Action::TOGGLE_VIDEO_VSYNC, toggleVSync},
#ifdef _DEBUG
{Action::SHOW_INFO, [] { Screen::get()->toggleDebugInfo(); }},
#endif
};
for (const auto& [action, func] : actions) {
if (Input::get()->checkAction(action, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
func();
return true;
}
}
return false;
}
// Comprueba el resto de entradas
auto checkOtherInputs() -> bool {
// Saltar sección
if ((Input::get()->checkAnyButton()) && !ServiceMenu::get()->isEnabled()) {
skipSection();
return true;
}
return false; return false;
} }
// Comprueba las entradas fuera del menú de servicio // Comprueba las entradas del Menu de Servicio
auto checkInputs() -> bool { inline auto checkServiceMenuInputs() -> bool {
// Teclado return ServiceMenu::get()->checkInput();
{
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (Input::get()->checkAction(Input::Action::WINDOW_FULLSCREEN, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
Screen::get()->toggleFullscreen();
const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE});
return true;
}
// Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkAction(Input::Action::WINDOW_DEC_SIZE, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
if (Screen::get()->decWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
return true;
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkAction(Input::Action::WINDOW_INC_SIZE, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
if (Screen::get()->incWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
return true;
}
// Salir
if (Input::get()->checkAction(Input::Action::EXIT, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
quit();
return true;
}
// Saltar sección
if ((Input::get()->checkAnyButton()) && !ServiceMenu::get()->isEnabled()) {
skipSection();
return true;
}
// Reset
if (Input::get()->checkAction(Input::Action::RESET, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
reset();
return true;
}
// Audio
if (Input::get()->checkAction(Input::Action::TOGGLE_AUDIO, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleAudio();
return true;
}
// Autofire
if (Input::get()->checkAction(Input::Action::TOGGLE_AUTO_FIRE, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleFireMode();
return true;
}
// Idioma
if (Input::get()->checkAction(Input::Action::CHANGE_LANG, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
changeLang();
return true;
}
// Shaders
if (Input::get()->checkAction(Input::Action::TOGGLE_VIDEO_SHADERS, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleShaders();
return true;
}
// Integer Scale
if (Input::get()->checkAction(Input::Action::TOGGLE_VIDEO_INTEGER_SCALE, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleIntegerScale();
return true;
}
// VSync
if (Input::get()->checkAction(Input::Action::TOGGLE_VIDEO_VSYNC, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
toggleVSync();
return true;
}
#ifdef _DEBUG
// Debug info
if (Input::get()->checkAction(Input::Action::SHOW_INFO, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
Screen::get()->toggleDebugInfo();
return true;
}
#endif
}
return false;
} }
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
@@ -281,12 +232,15 @@ auto check() -> bool {
if (checkServiceButton()) { if (checkServiceButton()) {
return true; return true;
} }
if (ServiceMenu::get()->checkInput()) { if (checkServiceMenuInputs()) {
return true; return true;
} }
if (checkInputs()) { if (checkSystemInputs()) {
return true;
}
if (checkOtherInputs()) {
return true; return true;
} }
return false; return false;
} }
} // namespace GlobalInputs } // namespace GlobalInputs

View File

@@ -547,7 +547,7 @@ void ServiceMenu::handleEvent(const SDL_Event &event) {
// Si DefineButtons está activo, que maneje todos los eventos // Si DefineButtons está activo, que maneje todos los eventos
if (define_buttons_ && define_buttons_->isEnabled()) { if (define_buttons_ && define_buttons_->isEnabled()) {
define_buttons_->checkEvents(event); define_buttons_->checkEvents(event);
return; // No procesar otros eventos mientras DefineButtons está activo //return; // No procesar otros eventos mientras DefineButtons está activo
} }
} }
@@ -557,85 +557,39 @@ bool ServiceMenu::checkInput() {
} }
if (define_buttons_ && define_buttons_->isEnabled()) { if (define_buttons_ && define_buttons_->isEnabled()) {
return true; return false;
} }
static auto input = Input::get(); static auto input = Input::get();
// --- Teclado ---
// Arriba
if (input->checkAction(Input::Action::UP, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
setSelectorUp();
return true;
}
// Abajo using Action = Input::Action;
if (input->checkAction(Input::Action::DOWN, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
setSelectorDown();
return true;
}
// Derecha const std::vector<std::pair<Action, std::function<void()>>> actions = {
if (input->checkAction(Input::Action::RIGHT, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) { { Action::UP, [this]() { setSelectorUp(); } },
adjustOption(true); { Action::DOWN, [this]() { setSelectorDown(); } },
return true; { Action::RIGHT, [this]() { adjustOption(true); } },
} { Action::LEFT, [this]() { adjustOption(false); } },
{ Action::SM_SELECT, [this]() { selectOption(); } },
{ Action::SM_BACK, [this]() { moveBack(); } },
};
// Izquierda // Teclado
if (input->checkAction(Input::Action::LEFT, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) { for (const auto& [action, func] : actions) {
adjustOption(false); if (input->checkAction(action, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
return true; func();
}
// Aceptar
if (input->checkAction(Input::Action::SM_SELECT, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
selectOption();
return true;
}
// Atras
if (input->checkAction(Input::Action::SM_BACK, Input::DO_NOT_ALLOW_REPEAT, Input::CHECK_KEYBOARD)) {
moveBack();
return true;
}
// --- Mandos ---
auto gamepads = input->getGamepads();
for (auto gamepad : gamepads) {
// Arriba
if (input->checkAction(Input::Action::UP, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
setSelectorUp();
return true;
}
// Abajo
if (input->checkAction(Input::Action::DOWN, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
setSelectorDown();
return true;
}
// Derecha
if (input->checkAction(Input::Action::RIGHT, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
adjustOption(true);
return true;
}
// Izquierda
if (input->checkAction(Input::Action::LEFT, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
adjustOption(false);
return true;
}
// Aceptar
if (input->checkAction(Input::Action::SM_SELECT, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
selectOption();
return true;
}
// Atras
if (input->checkAction(Input::Action::SM_BACK, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
moveBack();
return true; return true;
} }
} }
// Mandos
for (auto gamepad : input->getGamepads()) {
for (const auto& [action, func] : actions) {
if (input->checkAction(action, Input::DO_NOT_ALLOW_REPEAT, Input::DO_NOT_CHECK_KEYBOARD, gamepad)) {
func();
return true;
}
}
}
return false; return false;
} }