Refactor fase 3: Extraer UIManager de Engine

Migra toda la lógica de interfaz de usuario (HUD, FPS, debug, notificaciones)
a UIManager siguiendo el principio de Single Responsibility (SRP).

## Archivos Nuevos

**source/ui/ui_manager.h:**
- Declaración de clase UIManager
- Gestión de HUD debug, FPS counter, notificaciones, texto obsoleto
- Constructor/destructor con gestión de TextRenderers y Notifier
- Métodos públicos: initialize(), update(), render(), toggleDebug()
- Getters: isDebugActive(), getCurrentFPS(), isTextObsoleteVisible()

**source/ui/ui_manager.cpp:**
- Implementación completa de UI (~250 líneas)
- renderDebugHUD(): Renderiza toda la información de debug
- renderObsoleteText(): Sistema antiguo de texto (DEPRECATED)
- update(): Calcula FPS y actualiza notificaciones
- Gestión de 3 TextRenderers (display, debug, notifier)
- Integración con Notifier para mensajes tipo iOS/Android

## Archivos Modificados

**source/defines.h:**
- Movido: enum class AppMode (antes estaba en engine.h)
- Ahora AppMode es global y accesible para todos los componentes

**source/engine.h:**
- Agregado: #include "ui/ui_manager.h"
- Agregado: std::unique_ptr<UIManager> ui_manager_
- Removido: enum class AppMode (movido a defines.h)
- Removido: bool show_debug_, bool show_text_
- Removido: TextRenderer text_renderer_, text_renderer_debug_, text_renderer_notifier_
- Removido: Notifier notifier_
- Removido: std::string text_, int text_pos_, Uint64 text_init_time_
- Removido: Uint64 fps_last_time_, int fps_frame_count_, int fps_current_
- Removido: std::string fps_text_, vsync_text_
- Removidos métodos privados: setText(), gravityDirectionToString()

**source/engine.cpp:**
- initialize(): Crea ui_manager_ con renderer y theme_manager
- update(): Delega a ui_manager_->update()
- render(): Reemplaza 90+ líneas de debug HUD con ui_manager_->render()
- toggleDebug(): Delega a ui_manager_->toggleDebug()
- toggleVSync(): Actualiza texto con ui_manager_->updateVSyncText()
- showNotificationForAction(): Delega a ui_manager_->showNotification()
- updatePhysicalWindowSize(): Simplificado, delega a ui_manager_
- toggleIntegerScaling(): Usa ui_manager_ en lugar de texto obsoleto
- toggleShapeModeInternal(): Usa ui_manager_->showNotification()
- activateShapeInternal(): Usa ui_manager_->showNotification()
- Removidos métodos completos: setText() (~27 líneas), gravityDirectionToString()
- Removidas ~90 líneas de renderizado debug manual
- Removidas ~65 líneas de gestión de TextRenderers/Notifier

## Resultado

- Engine.cpp reducido de ~1950 → ~1700 líneas (-250 líneas, -12.8%)
- UIManager: 250 líneas de lógica UI separada
- Separación clara: Engine coordina, UIManager renderiza UI
- AppMode ahora es enum global en defines.h
- 100% funcional: Compila sin errores ni warnings
- Preparado para Fase 4 (StateManager)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-10 12:15:54 +02:00
parent f93879b803
commit e655c643a5
4 changed files with 316 additions and 248 deletions

View File

@@ -224,9 +224,13 @@ bool Engine::initialize(int width, int height, int zoom, bool fullscreen) {
scene_manager_ = std::make_unique<SceneManager>(current_screen_width_, current_screen_height_);
scene_manager_->initialize(0, texture_, theme_manager_.get()); // Escenario 0 (10 bolas) por defecto
// Calcular tamaño físico de ventana y tamaño de fuente absoluto
// NOTA: Debe llamarse DESPUÉS de inicializar ThemeManager porque notifier_.init() lo necesita
// Calcular tamaño físico de ventana ANTES de inicializar UIManager
updatePhysicalWindowSize();
// Inicializar UIManager (HUD, FPS, notificaciones)
// NOTA: Debe llamarse DESPUÉS de updatePhysicalWindowSize() y ThemeManager
ui_manager_ = std::make_unique<UIManager>();
ui_manager_->initialize(renderer_, theme_manager_.get(), physical_window_width_, physical_window_height_);
}
return success;
@@ -284,16 +288,11 @@ void Engine::update() {
// Actualizar visibilidad del cursor (auto-ocultar tras inactividad)
Mouse::updateCursorVisibility();
// Calcular FPS
fps_frame_count_++;
// Obtener tiempo actual
Uint64 current_time = SDL_GetTicks();
if (current_time - fps_last_time_ >= 1000) // Actualizar cada segundo
{
fps_current_ = fps_frame_count_;
fps_frame_count_ = 0;
fps_last_time_ = current_time;
fps_text_ = "fps: " + std::to_string(fps_current_);
}
// Actualizar UI (FPS, notificaciones, texto obsoleto) - delegado a UIManager
ui_manager_->update(current_time, delta_time_);
// Bifurcar actualización según modo activo
if (current_mode_ == SimulationMode::PHYSICS) {
@@ -304,14 +303,6 @@ void Engine::update() {
updateShape();
}
// Actualizar texto (OBSOLETO: sistema antiguo, se mantiene por compatibilidad temporal)
if (show_text_) {
show_text_ = !(SDL_GetTicks() - text_init_time_ > TEXT_DURATION);
}
// Actualizar sistema de notificaciones
notifier_.update(current_time);
// Actualizar Modo DEMO (auto-play)
updateDemoMode();
@@ -349,7 +340,7 @@ void Engine::handleGravityDirectionChange(GravityDirection direction, const char
// Display y depuración
void Engine::toggleDebug() {
show_debug_ = !show_debug_;
ui_manager_->toggleDebug();
}
// Figuras 3D
@@ -634,142 +625,20 @@ void Engine::render() {
}
*/
// Debug display (solo si está activado con tecla H)
if (show_debug_) {
// Obtener altura de línea para espaciado dinámico (usando fuente debug)
int line_height = text_renderer_debug_.getTextHeight();
int margin = 8; // Margen constante en píxeles físicos
int current_y = margin; // Y inicial en píxeles físicos
// Mostrar contador de FPS en esquina superior derecha
int fps_text_width = text_renderer_debug_.getTextWidthPhysical(fps_text_.c_str());
int fps_x = physical_window_width_ - fps_text_width - margin;
text_renderer_debug_.printAbsolute(fps_x, current_y, fps_text_.c_str(), {255, 255, 0, 255}); // Amarillo
// Mostrar estado V-Sync en esquina superior izquierda
text_renderer_debug_.printAbsolute(margin, current_y, vsync_text_.c_str(), {0, 255, 255, 255}); // Cian
current_y += line_height;
// Debug: Mostrar valores de la primera pelota (si existe)
const Ball* first_ball = scene_manager_->getFirstBall();
if (first_ball != nullptr) {
// Línea 1: Gravedad
int grav_int = static_cast<int>(first_ball->getGravityForce());
std::string grav_text = "Gravedad: " + std::to_string(grav_int);
text_renderer_debug_.printAbsolute(margin, current_y, grav_text.c_str(), {255, 0, 255, 255}); // Magenta
current_y += line_height;
// Línea 2: Velocidad Y
int vy_int = static_cast<int>(first_ball->getVelocityY());
std::string vy_text = "Velocidad Y: " + std::to_string(vy_int);
text_renderer_debug_.printAbsolute(margin, current_y, vy_text.c_str(), {255, 0, 255, 255}); // Magenta
current_y += line_height;
// Línea 3: Estado superficie
std::string surface_text = first_ball->isOnSurface() ? "Superficie: Sí" : "Superficie: No";
text_renderer_debug_.printAbsolute(margin, current_y, surface_text.c_str(), {255, 0, 255, 255}); // Magenta
current_y += line_height;
// Línea 4: Coeficiente de rebote (loss)
float loss_val = first_ball->getLossCoefficient();
std::string loss_text = "Rebote: " + std::to_string(loss_val).substr(0, 4);
text_renderer_debug_.printAbsolute(margin, current_y, loss_text.c_str(), {255, 0, 255, 255}); // Magenta
current_y += line_height;
// Línea 5: Dirección de gravedad
std::string gravity_dir_text = "Dirección: " + gravityDirectionToString(scene_manager_->getCurrentGravity());
text_renderer_debug_.printAbsolute(margin, current_y, gravity_dir_text.c_str(), {255, 255, 0, 255}); // Amarillo
current_y += line_height;
}
// Debug: Mostrar tema actual (delegado a ThemeManager)
std::string theme_text = std::string("Tema: ") + theme_manager_->getCurrentThemeNameEN();
text_renderer_debug_.printAbsolute(margin, current_y, theme_text.c_str(), {255, 255, 128, 255}); // Amarillo claro
current_y += line_height;
// Debug: Mostrar modo de simulación actual
std::string mode_text;
if (current_mode_ == SimulationMode::PHYSICS) {
mode_text = "Modo: Física";
} else if (active_shape_) {
mode_text = std::string("Modo: ") + active_shape_->getName();
} else {
mode_text = "Modo: Forma";
}
text_renderer_debug_.printAbsolute(margin, current_y, mode_text.c_str(), {0, 255, 128, 255}); // Verde claro
current_y += line_height;
// Debug: Mostrar convergencia en modo LOGO (solo cuando está activo)
if (current_app_mode_ == AppMode::LOGO && current_mode_ == SimulationMode::SHAPE) {
int convergence_percent = static_cast<int>(shape_convergence_ * 100.0f);
std::string convergence_text = "Convergencia: " + std::to_string(convergence_percent) + "%";
text_renderer_debug_.printAbsolute(margin, current_y, convergence_text.c_str(), {255, 128, 0, 255}); // Naranja
current_y += line_height;
}
// Debug: Mostrar modo DEMO/LOGO activo (siempre visible cuando debug está ON)
// FIJO en tercera fila (no se mueve con otros elementos del HUD)
int fixed_y = margin + (line_height * 2); // Tercera fila fija
if (current_app_mode_ == AppMode::LOGO) {
const char* logo_text = "Modo Logo";
int logo_text_width = text_renderer_debug_.getTextWidthPhysical(logo_text);
int logo_x = (physical_window_width_ - logo_text_width) / 2;
text_renderer_debug_.printAbsolute(logo_x, fixed_y, logo_text, {255, 128, 0, 255}); // Naranja
} else if (current_app_mode_ == AppMode::DEMO) {
const char* demo_text = "Modo Demo";
int demo_text_width = text_renderer_debug_.getTextWidthPhysical(demo_text);
int demo_x = (physical_window_width_ - demo_text_width) / 2;
text_renderer_debug_.printAbsolute(demo_x, fixed_y, demo_text, {255, 165, 0, 255}); // Naranja
} else if (current_app_mode_ == AppMode::DEMO_LITE) {
const char* lite_text = "Modo Demo Lite";
int lite_text_width = text_renderer_debug_.getTextWidthPhysical(lite_text);
int lite_x = (physical_window_width_ - lite_text_width) / 2;
text_renderer_debug_.printAbsolute(lite_x, fixed_y, lite_text, {255, 200, 0, 255}); // Amarillo-naranja
}
}
// Renderizar notificaciones (siempre al final, sobre todo lo demás)
notifier_.render();
// Renderizar UI (debug HUD, texto obsoleto, notificaciones) - delegado a UIManager
ui_manager_->render(renderer_, scene_manager_.get(), current_mode_, current_app_mode_,
active_shape_.get(), shape_convergence_,
physical_window_width_, physical_window_height_, current_screen_width_);
SDL_RenderPresent(renderer_);
}
void Engine::setText() {
// Suprimir textos durante modos demo
if (current_app_mode_ != AppMode::SANDBOX) return;
// Generar texto de número de pelotas
int num_balls = BALL_COUNT_SCENARIOS[scene_manager_->getCurrentScenario()];
std::string notification_text;
if (num_balls == 1) {
notification_text = "1 Pelota";
} else if (num_balls < 1000) {
notification_text = std::to_string(num_balls) + " Pelotas";
} else {
// Formato con separador de miles para números grandes
notification_text = std::to_string(num_balls / 1000) + "," +
(num_balls % 1000 < 100 ? "0" : "") +
(num_balls % 1000 < 10 ? "0" : "") +
std::to_string(num_balls % 1000) + " Pelotas";
}
// Mostrar notificación (colores se obtienen dinámicamente desde ThemeManager)
notifier_.show(notification_text, NOTIFICATION_DURATION);
// Sistema antiguo (mantener temporalmente para compatibilidad)
text_ = notification_text;
text_pos_ = (current_screen_width_ - text_renderer_.getTextWidth(text_.c_str())) / 2;
show_text_ = true;
text_init_time_ = SDL_GetTicks();
}
void Engine::showNotificationForAction(const std::string& text) {
// IMPORTANTE: Esta función solo se llama desde handlers de teclado (acciones manuales)
// NUNCA se llama desde código automático (DEMO/LOGO), por lo tanto siempre mostramos notificación
// Los colores se obtienen dinámicamente cada frame desde ThemeManager en render()
// Esto permite transiciones LERP suaves y siempre usar el color del tema actual
notifier_.show(text, NOTIFICATION_DURATION);
// Delegar a UIManager
ui_manager_->showNotification(text, NOTIFICATION_DURATION);
}
void Engine::pushBallsAwayFromGravity() {
@@ -778,7 +647,9 @@ void Engine::pushBallsAwayFromGravity() {
void Engine::toggleVSync() {
vsync_enabled_ = !vsync_enabled_;
vsync_text_ = vsync_enabled_ ? "V-Sync: On" : "V-Sync: Off";
// Actualizar texto en UIManager
ui_manager_->updateVSyncText(vsync_enabled_);
// Aplicar el cambio de V-Sync al renderizador
SDL_SetRenderVSync(renderer_, vsync_enabled_ ? 1 : 0);
@@ -895,27 +766,9 @@ void Engine::toggleIntegerScaling() {
SDL_SetRenderLogicalPresentation(renderer_, current_screen_width_, current_screen_height_, presentation);
// Mostrar texto informativo
text_ = "Escalado: ";
text_ += mode_name;
text_pos_ = (current_screen_width_ - text_renderer_.getTextWidth(text_.c_str())) / 2;
show_text_ = true;
text_init_time_ = SDL_GetTicks();
}
std::string Engine::gravityDirectionToString(GravityDirection direction) const {
switch (direction) {
case GravityDirection::DOWN:
return "DOWN";
case GravityDirection::UP:
return "UP";
case GravityDirection::LEFT:
return "LEFT";
case GravityDirection::RIGHT:
return "RIGHT";
default:
return "UNKNOWN";
}
// Mostrar notificación del cambio
std::string notification = std::string("Escalado: ") + mode_name;
ui_manager_->showNotification(notification);
}
void Engine::addSpriteToBatch(float x, float y, float w, float h, int r, int g, int b, float scale) {
@@ -1055,8 +908,6 @@ void Engine::updatePhysicalWindowSize() {
physical_window_height_ = current_screen_height_;
} else if (fullscreen_enabled_) {
// En fullscreen F3, obtener tamaño REAL del display (no del framebuffer lógico)
// SDL_GetRenderOutputSize() falla en F3 (devuelve tamaño lógico 960x720)
// Necesitamos el tamaño FÍSICO real de la pantalla
int num_displays = 0;
SDL_DisplayID* displays = SDL_GetDisplays(&num_displays);
if (displays != nullptr && num_displays > 0) {
@@ -1075,43 +926,8 @@ void Engine::updatePhysicalWindowSize() {
physical_window_height_ = window_h;
}
// Recalcular tamaño de fuente basado en altura física
// Referencia: 8px a 1440p (monitor del usuario)
int font_size = (physical_window_height_ * TEXT_BASE_SIZE) / 1440;
if (font_size < 6) font_size = 6; // Tamaño mínimo legible
// Reinicializar TextRenderers con nuevo tamaño de fuente
text_renderer_.cleanup();
text_renderer_debug_.cleanup();
text_renderer_notifier_.cleanup();
text_renderer_.init(renderer_, TEXT_FONT_PATH, font_size, TEXT_ANTIALIASING);
text_renderer_debug_.init(renderer_, TEXT_FONT_PATH, font_size, TEXT_ANTIALIASING);
// TextRenderer para notificaciones: Detectar DPI y ajustar tamaño
// En pantallas Retina/HiDPI, el texto necesita ser más grande para ser legible
int logical_w = 0, logical_h = 0;
SDL_GetWindowSize(window_, &logical_w, &logical_h);
// Usar physical_window_width_ que ya contiene el tamaño real del framebuffer
// (calculado arriba con SDL_GetRenderOutputSize o current_screen_width_)
int pixels_w = physical_window_width_;
// Calcular escala DPI (1.0 normal, 2.0 Retina, 3.0 en algunos displays)
float dpi_scale = (logical_w > 0) ? static_cast<float>(pixels_w) / static_cast<float>(logical_w) : 1.0f;
// Ajustar tamaño de fuente base (16px) por escala DPI
// Retina macOS: 16px * 2.0 = 32px (legible)
// Normal: 16px * 1.0 = 16px
int notification_font_size = static_cast<int>(TEXT_ABSOLUTE_SIZE * dpi_scale);
if (notification_font_size < 12) notification_font_size = 12; // Mínimo legible
text_renderer_notifier_.init(renderer_, TEXT_FONT_PATH, notification_font_size, TEXT_ANTIALIASING);
// Inicializar/actualizar Notifier con nuevas dimensiones y ThemeManager
// NOTA: init() es seguro de llamar múltiples veces, solo actualiza punteros y dimensiones
// Esto asegura que el notifier tenga las referencias correctas tras resize/fullscreen
notifier_.init(renderer_, &text_renderer_notifier_, theme_manager_.get(), physical_window_width_, physical_window_height_);
// Notificar a UIManager del cambio de tamaño (delegado)
ui_manager_->updatePhysicalWindowSize(physical_window_width_, physical_window_height_);
}
// ============================================================================
@@ -1783,12 +1599,9 @@ void Engine::toggleShapeModeInternal(bool force_gravity_on_exit) {
scene_manager_->forceBallsGravityOn();
}
// Mostrar texto informativo (solo si NO estamos en modo demo o logo)
// Mostrar notificación (solo si NO estamos en modo demo o logo)
if (current_app_mode_ == AppMode::SANDBOX) {
text_ = "Modo Física";
text_pos_ = (current_screen_width_ - text_renderer_.getTextWidth(text_.c_str())) / 2;
text_init_time_ = SDL_GetTicks();
show_text_ = true;
ui_manager_->showNotification("Modo Física");
}
}
}
@@ -1848,12 +1661,10 @@ void Engine::activateShapeInternal(ShapeType type) {
ball->enableShapeAttraction(true);
}
// Mostrar texto informativo con nombre de figura (solo si NO estamos en modo demo o logo)
// Mostrar notificación con nombre de figura (solo si NO estamos en modo demo o logo)
if (active_shape_ && current_app_mode_ == AppMode::SANDBOX) {
text_ = std::string("Modo ") + active_shape_->getName();
text_pos_ = (current_screen_width_ - text_renderer_.getTextWidth(text_.c_str())) / 2;
text_init_time_ = SDL_GetTicks();
show_text_ = true;
std::string notification = std::string("Modo ") + active_shape_->getName();
ui_manager_->showNotification(notification);
}
}