diff --git a/source/ui/menu_renderer.cpp b/source/ui/menu_renderer.cpp index 7d782b9..1610f67 100644 --- a/source/ui/menu_renderer.cpp +++ b/source/ui/menu_renderer.cpp @@ -1,13 +1,11 @@ #include "menu_renderer.h" #include "text.h" #include "menu_option.h" // Necesario para acceder a las opciones -#include "screen.h" // Para param +#include "screen.h" // Para param #include MenuRenderer::MenuRenderer(std::shared_ptr element_text, std::shared_ptr title_text) - : element_text_(std::move(element_text)), title_text_(std::move(title_text)) -{ -} + : element_text_(std::move(element_text)), title_text_(std::move(title_text)) {} void MenuRenderer::render(const ServiceMenu *menu_state) { @@ -77,6 +75,14 @@ void MenuRenderer::onLayoutChanged(const ServiceMenu *menu_state) resize(menu_state); } +void MenuRenderer::setLayout(const ServiceMenu *menu_state) +{ + // Cuando la lógica del menú notifica un cambio, el renderer recalcula su layout + precalculateMenuWidths(menu_state->getAllOptions(), menu_state); + setAnchors(menu_state); + setSize(menu_state); +} + void MenuRenderer::setAnchors(const ServiceMenu *menu_state) { size_t max_entries = 0; @@ -99,17 +105,21 @@ void MenuRenderer::setAnchors(const ServiceMenu *menu_state) width_ = ServiceMenu::MIN_WIDTH_; height_ = upper_height_ + lower_height_; - - rect_ = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_}; } -void MenuRenderer::resize(const ServiceMenu *menu_state) +SDL_FRect MenuRenderer::calculateNewRect(const ServiceMenu *menu_state) { width_ = getMenuWidthForGroup(menu_state->getCurrentGroup()); const auto &display_options = menu_state->getDisplayOptions(); lower_height_ = ((display_options.size() > 0 ? display_options.size() - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2); height_ = upper_height_ + lower_height_; - SDL_FRect new_rect = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_}; + + return {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, (float)width_, (float)height_}; +} + +void MenuRenderer::resize(const ServiceMenu *menu_state) +{ + SDL_FRect new_rect = calculateNewRect(menu_state); if (rect_.x != new_rect.x || rect_.y != new_rect.y || rect_.w != new_rect.w || rect_.h != new_rect.h) { @@ -126,6 +136,14 @@ void MenuRenderer::resize(const ServiceMenu *menu_state) options_y_ = new_rect.y + upper_height_ + lower_padding_; } +void MenuRenderer::setSize(const ServiceMenu *menu_state) +{ + SDL_FRect new_rect = calculateNewRect(menu_state); + rect_ = new_rect; + resizing_ = false; + options_y_ = rect_.y + upper_height_ + lower_padding_; +} + void MenuRenderer::updateResizeAnimation() { if (!resizing_) diff --git a/source/ui/menu_renderer.h b/source/ui/menu_renderer.h index e5331b7..b0adfd7 100644 --- a/source/ui/menu_renderer.h +++ b/source/ui/menu_renderer.h @@ -5,25 +5,27 @@ #include #include #include "ui/service_menu.h" // Necesario para las enums y para acceder al estado del menú -#include "utils.h" // Para Color +#include "utils.h" // Para Color // Forward declarations class Text; class MenuOption; -class MenuRenderer { +class MenuRenderer +{ public: MenuRenderer(std::shared_ptr element_text, std::shared_ptr title_text); // Métodos principales de la vista - void render(const ServiceMenu* menu_state); - void update(const ServiceMenu* menu_state); + void render(const ServiceMenu *menu_state); + void update(const ServiceMenu *menu_state); // Método para notificar al renderer que el layout puede haber cambiado - void onLayoutChanged(const ServiceMenu* menu_state); + void onLayoutChanged(const ServiceMenu *menu_state); + void setLayout(const ServiceMenu *menu_state); // Getters - const SDL_FRect& getRect() const { return rect_; } + const SDL_FRect &getRect() const { return rect_; } private: // --- Referencias a los renderizadores de texto --- @@ -54,15 +56,17 @@ private: int resize_anim_step_ = 0; int resize_anim_steps_ = 8; bool resizing_ = false; - + // --- Anchos precalculados --- int group_menu_widths_[5]{}; // --- Métodos privados de la vista --- - void setAnchors(const ServiceMenu* menu_state); - void resize(const ServiceMenu* menu_state); + void setAnchors(const ServiceMenu *menu_state); + SDL_FRect calculateNewRect(const ServiceMenu *menu_state); + void resize(const ServiceMenu *menu_state); + void setSize(const ServiceMenu *menu_state); void updateResizeAnimation(); - void precalculateMenuWidths(const std::vector>& all_options, const ServiceMenu* menu_state); + void precalculateMenuWidths(const std::vector> &all_options, const ServiceMenu *menu_state); int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const; Color getAnimatedSelectedColor(); void updateColorCounter(); diff --git a/source/ui/service_menu.cpp b/source/ui/service_menu.cpp index eb0abac..28d5000 100644 --- a/source/ui/service_menu.cpp +++ b/source/ui/service_menu.cpp @@ -65,7 +65,7 @@ void ServiceMenu::reset() { previous_settings_group_ = SettingsGroup::MAIN; initializeOptions(); updateMenu(); - renderer_->onLayoutChanged(this); // Notifica al renderer para que calcule el layout inicial + renderer_->setLayout(this); // Notifica al renderer para que calcule el layout inicial } // --- Lógica de Navegación ---