fix MenuRenderer: corregida la animació del cuadro del menu de servei

This commit is contained in:
2025-06-21 14:50:33 +02:00
parent f5731c8181
commit b6698043fd
3 changed files with 41 additions and 19 deletions

View File

@@ -1,13 +1,11 @@
#include "menu_renderer.h" #include "menu_renderer.h"
#include "text.h" #include "text.h"
#include "menu_option.h" // Necesario para acceder a las opciones #include "menu_option.h" // Necesario para acceder a las opciones
#include "screen.h" // Para param #include "screen.h" // Para param
#include <array> #include <array>
MenuRenderer::MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text) MenuRenderer::MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> 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) void MenuRenderer::render(const ServiceMenu *menu_state)
{ {
@@ -77,6 +75,14 @@ void MenuRenderer::onLayoutChanged(const ServiceMenu *menu_state)
resize(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) void MenuRenderer::setAnchors(const ServiceMenu *menu_state)
{ {
size_t max_entries = 0; size_t max_entries = 0;
@@ -99,17 +105,21 @@ void MenuRenderer::setAnchors(const ServiceMenu *menu_state)
width_ = ServiceMenu::MIN_WIDTH_; width_ = ServiceMenu::MIN_WIDTH_;
height_ = upper_height_ + lower_height_; 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()); width_ = getMenuWidthForGroup(menu_state->getCurrentGroup());
const auto &display_options = menu_state->getDisplayOptions(); 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); 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_; 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) 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_; 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() void MenuRenderer::updateResizeAnimation()
{ {
if (!resizing_) if (!resizing_)

View File

@@ -5,25 +5,27 @@
#include <vector> #include <vector>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include "ui/service_menu.h" // Necesario para las enums y para acceder al estado del menú #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 // Forward declarations
class Text; class Text;
class MenuOption; class MenuOption;
class MenuRenderer { class MenuRenderer
{
public: public:
MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text); MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text);
// Métodos principales de la vista // Métodos principales de la vista
void render(const ServiceMenu* menu_state); void render(const ServiceMenu *menu_state);
void update(const ServiceMenu* menu_state); void update(const ServiceMenu *menu_state);
// Método para notificar al renderer que el layout puede haber cambiado // 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 // Getters
const SDL_FRect& getRect() const { return rect_; } const SDL_FRect &getRect() const { return rect_; }
private: private:
// --- Referencias a los renderizadores de texto --- // --- Referencias a los renderizadores de texto ---
@@ -54,15 +56,17 @@ private:
int resize_anim_step_ = 0; int resize_anim_step_ = 0;
int resize_anim_steps_ = 8; int resize_anim_steps_ = 8;
bool resizing_ = false; bool resizing_ = false;
// --- Anchos precalculados --- // --- Anchos precalculados ---
int group_menu_widths_[5]{}; int group_menu_widths_[5]{};
// --- Métodos privados de la vista --- // --- Métodos privados de la vista ---
void setAnchors(const ServiceMenu* menu_state); void setAnchors(const ServiceMenu *menu_state);
void resize(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 updateResizeAnimation();
void precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>>& all_options, const ServiceMenu* menu_state); void precalculateMenuWidths(const std::vector<std::unique_ptr<MenuOption>> &all_options, const ServiceMenu *menu_state);
int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const; int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const;
Color getAnimatedSelectedColor(); Color getAnimatedSelectedColor();
void updateColorCounter(); void updateColorCounter();

View File

@@ -65,7 +65,7 @@ void ServiceMenu::reset() {
previous_settings_group_ = SettingsGroup::MAIN; previous_settings_group_ = SettingsGroup::MAIN;
initializeOptions(); initializeOptions();
updateMenu(); 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 --- // --- Lógica de Navegación ---