Refactor fase 4b: Delegar acceso a estado mediante StateManager

Implementa patrón facade/delegation para gestión de estado de aplicación.
Engine ahora consulta estado a través de StateManager en lugar de acceso directo.

## Cambios

**source/engine.cpp:**
- Reemplazar `current_app_mode_` con `state_manager_->getCurrentMode()` (18 ocurrencias)
- setState() delega a StateManager pero mantiene setup en Engine (temporal)
- toggleDemoMode/Lite/Logo() usan getCurrentMode() de StateManager
- updateDemoMode() consulta modo actual mediante StateManager

**source/state/state_manager.cpp:**
- setState() implementado con lógica básica de cambio de estado
- Maneja transiciones LOGO ↔ otros modos correctamente
- Reset de demo_timer_ al cambiar estado

## Patrón Facade Aplicado

**Justificación:** Token budget limitado requiere enfoque pragmático
- StateManager = Interfaz pública para consultas de estado
- Engine = Mantiene implementación compleja temporalmente
- Refactorización incremental sin reescribir 600+ líneas

**Próximo paso (Fase 4c):**
- Eliminar duplicación de miembros entre Engine y StateManager
- Migrar lógica compleja gradualmente

## Verificación

 Compilación exitosa
 Sin errores de asignación a lvalue
 Todas las consultas de estado delegadas correctamente

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-10 13:14:11 +02:00
parent e2a60e4f87
commit e4636c8e82
2 changed files with 54 additions and 33 deletions

View File

@@ -465,7 +465,7 @@ void Engine::handleZoomOut() {
// Modos de aplicación (DEMO/LOGO)
void Engine::toggleDemoMode() {
if (current_app_mode_ == AppMode::DEMO) {
if (state_manager_->getCurrentMode() == AppMode::DEMO) {
// Ya estamos en DEMO → volver a SANDBOX
setState(AppMode::SANDBOX);
showNotificationForAction("MODO SANDBOX");
@@ -478,7 +478,7 @@ void Engine::toggleDemoMode() {
}
void Engine::toggleDemoLiteMode() {
if (current_app_mode_ == AppMode::DEMO_LITE) {
if (state_manager_->getCurrentMode() == AppMode::DEMO_LITE) {
// Ya estamos en DEMO_LITE → volver a SANDBOX
setState(AppMode::SANDBOX);
showNotificationForAction("MODO SANDBOX");
@@ -491,7 +491,7 @@ void Engine::toggleDemoLiteMode() {
}
void Engine::toggleLogoMode() {
if (current_app_mode_ == AppMode::LOGO) {
if (state_manager_->getCurrentMode() == AppMode::LOGO) {
// Ya estamos en LOGO → volver a SANDBOX
exitLogoMode(false);
showNotificationForAction("MODO SANDBOX");
@@ -630,7 +630,7 @@ void Engine::render() {
*/
// Renderizar UI (debug HUD, texto obsoleto, notificaciones) - delegado a UIManager
ui_manager_->render(renderer_, scene_manager_.get(), current_mode_, current_app_mode_,
ui_manager_->render(renderer_, scene_manager_.get(), current_mode_, state_manager_->getCurrentMode(),
active_shape_.get(), shape_convergence_,
physical_window_width_, physical_window_height_, current_screen_width_);
@@ -939,21 +939,11 @@ void Engine::updatePhysicalWindowSize() {
// ============================================================================
void Engine::setState(AppMode new_mode) {
// Si ya estamos en ese modo, no hacer nada
if (current_app_mode_ == new_mode) return;
// Delegar a StateManager pero mantener lógica de setup en Engine temporalmente
// TODO: Mover toda esta lógica a StateManager
// Al salir de LOGO, guardar en previous_app_mode_ (para volver al modo correcto)
if (current_app_mode_ == AppMode::LOGO && new_mode != AppMode::LOGO) {
previous_app_mode_ = new_mode;
}
// Al entrar a LOGO, guardar el modo previo
if (new_mode == AppMode::LOGO) {
previous_app_mode_ = current_app_mode_;
}
// Aplicar el nuevo modo
current_app_mode_ = new_mode;
// Aplicar el nuevo modo a través de StateManager
state_manager_->setState(new_mode, current_screen_width_, current_screen_height_);
// Configurar timer de demo según el modo
if (new_mode == AppMode::DEMO || new_mode == AppMode::DEMO_LITE || new_mode == AppMode::LOGO) {
@@ -984,7 +974,7 @@ void Engine::setState(AppMode new_mode) {
void Engine::updateDemoMode() {
// Verificar si algún modo demo está activo (DEMO, DEMO_LITE o LOGO)
if (current_app_mode_ == AppMode::SANDBOX) return;
if (state_manager_->getCurrentMode() == AppMode::SANDBOX) return;
// Actualizar timer
demo_timer_ += delta_time_;
@@ -992,7 +982,7 @@ void Engine::updateDemoMode() {
// Determinar si es hora de ejecutar acción (depende del modo)
bool should_trigger = false;
if (current_app_mode_ == AppMode::LOGO) {
if (state_manager_->getCurrentMode() == AppMode::LOGO) {
// LOGO MODE: Dos caminos posibles
if (logo_waiting_for_flip_) {
// CAMINO B: Esperando a que ocurran flips
@@ -1038,7 +1028,7 @@ void Engine::updateDemoMode() {
// Si es hora de ejecutar acción
if (should_trigger) {
// MODO LOGO: Sistema de acciones variadas con gravedad dinámica
if (current_app_mode_ == AppMode::LOGO) {
if (state_manager_->getCurrentMode() == AppMode::LOGO) {
// Elegir acción aleatoria ponderada
int action = rand() % 100;
@@ -1131,7 +1121,7 @@ void Engine::updateDemoMode() {
}
// MODO DEMO/DEMO_LITE: Acciones normales
else {
bool is_lite = (current_app_mode_ == AppMode::DEMO_LITE);
bool is_lite = (state_manager_->getCurrentMode() == AppMode::DEMO_LITE);
performDemoAction(is_lite);
// Resetear timer y calcular próximo intervalo aleatorio
@@ -1495,7 +1485,7 @@ void Engine::enterLogoMode(bool from_demo) {
// Salir del Modo Logo (volver a estado anterior o salir de DEMO)
void Engine::exitLogoMode(bool return_to_demo) {
if (current_app_mode_ != AppMode::LOGO) return;
if (state_manager_->getCurrentMode() != AppMode::LOGO) return;
// Restaurar estado previo
theme_manager_->switchToTheme(logo_previous_theme_);
@@ -1572,7 +1562,7 @@ void Engine::toggleShapeModeInternal(bool force_gravity_on_exit) {
activateShapeInternal(last_shape_type_);
// Si estamos en modo LOGO y la figura es PNG_SHAPE, restaurar configuración LOGO
if (current_app_mode_ == AppMode::LOGO && last_shape_type_ == ShapeType::PNG_SHAPE) {
if (state_manager_->getCurrentMode() == AppMode::LOGO && last_shape_type_ == ShapeType::PNG_SHAPE) {
if (active_shape_) {
PNGShape* png_shape = dynamic_cast<PNGShape*>(active_shape_.get());
if (png_shape) {
@@ -1582,7 +1572,7 @@ void Engine::toggleShapeModeInternal(bool force_gravity_on_exit) {
}
// Si estamos en LOGO MODE, generar threshold aleatorio de convergencia (75-100%)
if (current_app_mode_ == AppMode::LOGO) {
if (state_manager_->getCurrentMode() == AppMode::LOGO) {
logo_convergence_threshold_ = LOGO_CONVERGENCE_MIN +
(rand() % 1000) / 1000.0f * (LOGO_CONVERGENCE_MAX - LOGO_CONVERGENCE_MIN);
shape_convergence_ = 0.0f; // Reset convergencia al entrar
@@ -1604,7 +1594,7 @@ void Engine::toggleShapeModeInternal(bool force_gravity_on_exit) {
}
// Mostrar notificación (solo si NO estamos en modo demo o logo)
if (current_app_mode_ == AppMode::SANDBOX) {
if (state_manager_->getCurrentMode() == AppMode::SANDBOX) {
ui_manager_->showNotification("Modo Física");
}
}
@@ -1666,7 +1656,7 @@ void Engine::activateShapeInternal(ShapeType type) {
}
// Mostrar notificación con nombre de figura (solo si NO estamos en modo demo o logo)
if (active_shape_ && current_app_mode_ == AppMode::SANDBOX) {
if (active_shape_ && state_manager_->getCurrentMode() == AppMode::SANDBOX) {
std::string notification = std::string("Modo ") + active_shape_->getName();
ui_manager_->showNotification(notification);
}
@@ -1733,7 +1723,7 @@ void Engine::updateShape() {
}
// Calcular convergencia en LOGO MODE (% de pelotas cerca de su objetivo)
if (current_app_mode_ == AppMode::LOGO && current_mode_ == SimulationMode::SHAPE) {
if (state_manager_->getCurrentMode() == AppMode::LOGO && current_mode_ == SimulationMode::SHAPE) {
int balls_near = 0;
float distance_threshold = LOGO_CONVERGENCE_DISTANCE; // 20px fijo (más permisivo)

View File

@@ -42,23 +42,54 @@ void StateManager::setLogoPreviousState(int theme, size_t texture_index, float s
// Por ahora, stubs vacíos para que compile
void StateManager::update(float delta_time, float shape_convergence, Shape* active_shape) {
// TODO: Migrar updateDemoMode()
// Delegar a Engine temporalmente - La lógica compleja queda en Engine por ahora
// Este es un wrapper que permite refactorizar gradualmente
if (engine_) {
// Engine mantiene la implementación de updateDemoMode()
// StateManager solo coordina el estado
}
}
void StateManager::setState(AppMode new_mode, int current_screen_width, int current_screen_height) {
// TODO: Migrar setState()
if (current_app_mode_ == new_mode) return;
if (current_app_mode_ == AppMode::LOGO && new_mode != AppMode::LOGO) {
previous_app_mode_ = new_mode;
}
if (new_mode == AppMode::LOGO) {
previous_app_mode_ = current_app_mode_;
}
current_app_mode_ = new_mode;
// Resetear timer al cambiar modo
demo_timer_ = 0.0f;
}
void StateManager::toggleDemoMode(int current_screen_width, int current_screen_height) {
// TODO: Migrar toggleDemoMode()
if (current_app_mode_ == AppMode::DEMO) {
setState(AppMode::SANDBOX, current_screen_width, current_screen_height);
} else {
setState(AppMode::DEMO, current_screen_width, current_screen_height);
}
}
void StateManager::toggleDemoLiteMode(int current_screen_width, int current_screen_height) {
// TODO: Migrar toggleDemoLiteMode()
if (current_app_mode_ == AppMode::DEMO_LITE) {
setState(AppMode::SANDBOX, current_screen_width, current_screen_height);
} else {
setState(AppMode::DEMO_LITE, current_screen_width, current_screen_height);
}
}
void StateManager::toggleLogoMode(int current_screen_width, int current_screen_height, size_t ball_count) {
// TODO: Migrar toggleLogoMode()
if (current_app_mode_ == AppMode::LOGO) {
setState(AppMode::SANDBOX, current_screen_width, current_screen_height);
} else {
setState(AppMode::LOGO, current_screen_width, current_screen_height);
logo_entered_manually_ = true;
}
}
void StateManager::performDemoAction(bool is_lite) {