From 483a7e020b46bef2d4885cf9d0dd01a2d1b7ded9 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 13 Jun 2025 20:22:26 +0200 Subject: [PATCH] ServiceMenu: treballant en les animacions --- source/service_menu.cpp | 52 ++++++++++++++++++++++++++++++++++++++++- source/service_menu.h | 8 ++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/source/service_menu.cpp b/source/service_menu.cpp index c451e70..9baf5b7 100644 --- a/source/service_menu.cpp +++ b/source/service_menu.cpp @@ -160,8 +160,10 @@ void ServiceMenu::setOptionsPosition() // Cambia el tamaño de la ventana de menu void ServiceMenu::resize() { + // Usa el ancho precalculado para el grupo actual + int menu_width = getMenuWidthForGroup(current_settings_group_); + width_ = menu_width; lower_height_ = ((display_options_.size() - 1) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2); - width_ = 240; height_ = upper_height_ + lower_height_; SDL_FRect new_rect = { (param.game.width - width_) / 2, @@ -379,6 +381,9 @@ void ServiceMenu::initializeOptions() options_.emplace_back(lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::AUDIO); options_.emplace_back(lang::getText("[SERVICE_MENU] GAME"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::GAME); options_.emplace_back(lang::getText("[SERVICE_MENU] SYSTEM"), SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::SYSTEM); + + // Al terminar de inicializar las opciones, recalcula los anchos de menú + precalculateMenuWidths(); } // Devuelve las opciones del grupo como pares (nombre, valor) @@ -435,6 +440,8 @@ void ServiceMenu::updateMenu(SettingsGroup group) AdjustListValues(); option_pairs_ = getOptionPairs(group); display_options_ = getOptionsByGroup(group); + // Recalcula el ancho del menú al cambiar de grupo + resize(); } // Reinicia el menú al estado inicial (grupo principal y opción seleccionada) @@ -508,3 +515,46 @@ void ServiceMenu::AdjustListValues() } } +void ServiceMenu::precalculateMenuWidths() +{ + // Inicializa todos los anchos al mínimo + for (int &w : group_menu_widths_) w = MIN_WIDTH_; + + // Para cada grupo + for (int group = 0; group < 5; ++group) { + SettingsGroup sg = static_cast(group); + int max_width = MIN_WIDTH_; + for (const auto &option : options_) { + if (option.group != sg) continue; + int option_width = element_text_->lenght(option.caption); + int value_width = 0; + // Calcular el valor más ancho posible para esta opción + switch (option.type) { + case ValueType::BOOL: + value_width = std::max(element_text_->lenght(lang::getText("[SERVICE_MENU] ON")), + element_text_->lenght(lang::getText("[SERVICE_MENU] OFF"))); + break; + case ValueType::INT: + // Considera el valor máximo y mínimo como strings + value_width = std::max( + element_text_->lenght(std::to_string(option.min_value)), + element_text_->lenght(std::to_string(option.max_value))); + break; + case ValueType::LIST: + for (const auto &val : option.value_list) + value_width = std::max(value_width, element_text_->lenght(val)); + break; + default: + value_width = 0; + } + int total_width = option_width + MIN_GAP_OPTION_VALUE_ + value_width; + max_width = std::max(max_width, total_width); + } + group_menu_widths_[group] = max_width; + } +} + +int ServiceMenu::getMenuWidthForGroup(SettingsGroup group) const +{ + return group_menu_widths_[static_cast(group)]; +} \ No newline at end of file diff --git a/source/service_menu.h b/source/service_menu.h index d41e11e..886b558 100644 --- a/source/service_menu.h +++ b/source/service_menu.h @@ -215,12 +215,13 @@ private: int resize_anim_steps_ = 8; // Total de pasos de la animación bool resizing_ = false; // Si está animando el resize + int group_menu_widths_[5]; + // --- Métodos internos: Anclaje y aspecto --- void setAnchors(); // Establece el valor de las variables de anclaje Color getSelectedColor() const; // Devuelve el color del elemento seleccionado void setOptionsPosition(); // Establce la posición donde empezar a escribir las opciones del menu void resize(); // Cambia el tamaño de la ventana de menu - size_t getOptionsWidth(); // Obtiene el ancho de las opciones del menu // --- Métodos internos: Gestión de opciones --- void initializeOptions(); // Crea todas las opciones del menú de servicio @@ -242,6 +243,11 @@ private: // --- Métodos internos: Animación de resize --- void updateResizeAnimation(); + // --- Métodos internos: Cálculo de anchos --- +void precalculateMenuWidths(); +int getMenuWidthForGroup(SettingsGroup group) const; + + // --- Patrón Singleton --- ServiceMenu(); // Constructor privado ~ServiceMenu() = default; // Destructor privado