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

@@ -5,9 +5,7 @@
#include <array>
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)
{
@@ -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_)

View File

@@ -11,7 +11,8 @@
class Text;
class MenuOption;
class MenuRenderer {
class MenuRenderer
{
public:
MenuRenderer(std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text);
@@ -21,6 +22,7 @@ public:
// Método para notificar al renderer que el layout puede haber cambiado
void onLayoutChanged(const ServiceMenu *menu_state);
void setLayout(const ServiceMenu *menu_state);
// Getters
const SDL_FRect &getRect() const { return rect_; }
@@ -60,7 +62,9 @@ private:
// --- Métodos privados de la vista ---
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<std::unique_ptr<MenuOption>> &all_options, const ServiceMenu *menu_state);
int getMenuWidthForGroup(ServiceMenu::SettingsGroup group) const;

View File

@@ -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 ---