PHASE 2: Refactorización completa del sistema de temas unificado

Arquitectura polimórfica implementada:
- Jerarquía: Theme (base) → StaticTheme / DynamicTheme (derivadas)
- Vector unificado de 10 temas (7 estáticos + 3 dinámicos)
- Eliminada lógica dual (if(dynamic_theme_active_) scattered)

Nuevos archivos:
- source/themes/theme.h: Interfaz base abstracta
- source/themes/static_theme.h/cpp: Temas estáticos (1 keyframe)
- source/themes/dynamic_theme.h/cpp: Temas dinámicos (N keyframes animados)
- source/theme_manager.h/cpp: Gestión unificada de temas

Mejoras de API:
- switchToTheme(0-9): Cambio a cualquier tema (índice 0-9)
- cycleTheme(): Cicla por todos los temas (Tecla B)
- update(delta_time): Actualización simplificada
- getInterpolatedColor(idx): Sin parámetro balls_

Bugs corregidos:
- Tecla B ahora cicla TODOS los 10 temas (antes solo 6)
- DEMO mode elige de TODOS los temas (antes excluía LAVENDER + dinámicos)
- Eliminada duplicación de keyframes en temas dinámicos (loop=true lo maneja)

Código reducido:
- theme_manager.cpp: 558 → 320 líneas (-43%)
- engine.cpp: Eliminados ~470 líneas de lógica de temas
- Complejidad significativamente reducida

Preparado para PHASE 3 (LERP universal entre cualquier par de temas)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-09 13:17:54 +02:00
parent b93028396a
commit a134ae428f
11 changed files with 988 additions and 680 deletions

320
source/theme_manager.cpp Normal file
View File

@@ -0,0 +1,320 @@
#include "theme_manager.h"
#include "themes/static_theme.h"
#include "themes/dynamic_theme.h"
// ============================================================================
// INICIALIZACIÓN
// ============================================================================
void ThemeManager::initialize() {
themes_.clear();
themes_.reserve(10); // 7 estáticos + 3 dinámicos
// ========================================
// TEMAS ESTÁTICOS (índices 0-6)
// ========================================
// 0: SUNSET (Atardecer) - Naranjas, rojos, amarillos, rosas
themes_.push_back(std::make_unique<StaticTheme>(
"SUNSET",
"ATARDECER",
255, 140, 60, // Color texto: naranja cálido
180.0f / 255.0f, 140.0f / 255.0f, 100.0f / 255.0f, // Fondo superior: naranja suave
40.0f / 255.0f, 20.0f / 255.0f, 60.0f / 255.0f, // Fondo inferior: púrpura oscuro
std::vector<Color>{
{255, 140, 0}, {255, 69, 0}, {255, 215, 0}, {255, 20, 147},
{255, 99, 71}, {255, 165, 0}, {255, 192, 203}, {220, 20, 60}
}
));
// 1: OCEAN (Océano) - Azules, turquesas, blancos
themes_.push_back(std::make_unique<StaticTheme>(
"OCEAN",
"OCEANO",
80, 200, 255, // Color texto: azul océano
100.0f / 255.0f, 150.0f / 255.0f, 200.0f / 255.0f, // Fondo superior: azul cielo
20.0f / 255.0f, 40.0f / 255.0f, 80.0f / 255.0f, // Fondo inferior: azul marino
std::vector<Color>{
{0, 191, 255}, {0, 255, 255}, {32, 178, 170}, {176, 224, 230},
{70, 130, 180}, {0, 206, 209}, {240, 248, 255}, {64, 224, 208}
}
));
// 2: NEON - Cian, magenta, verde lima, amarillo vibrante
themes_.push_back(std::make_unique<StaticTheme>(
"NEON",
"NEON",
255, 60, 255, // Color texto: magenta brillante
20.0f / 255.0f, 20.0f / 255.0f, 40.0f / 255.0f, // Fondo superior: negro azulado
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo inferior: negro
std::vector<Color>{
{0, 255, 255}, {255, 0, 255}, {50, 205, 50}, {255, 255, 0},
{255, 20, 147}, {0, 255, 127}, {138, 43, 226}, {255, 69, 0}
}
));
// 3: FOREST (Bosque) - Verdes, marrones, amarillos otoño
themes_.push_back(std::make_unique<StaticTheme>(
"FOREST",
"BOSQUE",
100, 255, 100, // Color texto: verde natural
144.0f / 255.0f, 238.0f / 255.0f, 144.0f / 255.0f, // Fondo superior: verde claro
101.0f / 255.0f, 67.0f / 255.0f, 33.0f / 255.0f, // Fondo inferior: marrón tierra
std::vector<Color>{
{34, 139, 34}, {107, 142, 35}, {154, 205, 50}, {255, 215, 0},
{210, 180, 140}, {160, 82, 45}, {218, 165, 32}, {50, 205, 50}
}
));
// 4: RGB - Círculo cromático con 24 puntos (cada 15°)
themes_.push_back(std::make_unique<StaticTheme>(
"RGB",
"RGB",
100, 100, 100, // Color texto: gris oscuro
1.0f, 1.0f, 1.0f, // Fondo superior: blanco puro
1.0f, 1.0f, 1.0f, // Fondo inferior: blanco puro (sin degradado)
std::vector<Color>{
{255, 0, 0}, // 0° - Rojo puro
{255, 64, 0}, // 15° - Rojo-Naranja
{255, 128, 0}, // 30° - Naranja
{255, 191, 0}, // 45° - Naranja-Amarillo
{255, 255, 0}, // 60° - Amarillo puro
{191, 255, 0}, // 75° - Amarillo-Verde claro
{128, 255, 0}, // 90° - Verde-Amarillo
{64, 255, 0}, // 105° - Verde claro-Amarillo
{0, 255, 0}, // 120° - Verde puro
{0, 255, 64}, // 135° - Verde-Cian claro
{0, 255, 128}, // 150° - Verde-Cian
{0, 255, 191}, // 165° - Verde claro-Cian
{0, 255, 255}, // 180° - Cian puro
{0, 191, 255}, // 195° - Cian-Azul claro
{0, 128, 255}, // 210° - Azul-Cian
{0, 64, 255}, // 225° - Azul claro-Cian
{0, 0, 255}, // 240° - Azul puro
{64, 0, 255}, // 255° - Azul-Magenta claro
{128, 0, 255}, // 270° - Azul-Magenta
{191, 0, 255}, // 285° - Azul claro-Magenta
{255, 0, 255}, // 300° - Magenta puro
{255, 0, 191}, // 315° - Magenta-Rojo claro
{255, 0, 128}, // 330° - Magenta-Rojo
{255, 0, 64} // 345° - Magenta claro-Rojo
}
));
// 5: MONOCHROME (Monocromo) - Fondo negro degradado, sprites blancos
themes_.push_back(std::make_unique<StaticTheme>(
"MONOCHROME",
"MONOCROMO",
200, 200, 200, // Color texto: gris claro
20.0f / 255.0f, 20.0f / 255.0f, 20.0f / 255.0f, // Fondo superior: gris muy oscuro
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo inferior: negro
std::vector<Color>{
{255, 255, 255}, {255, 255, 255}, {255, 255, 255}, {255, 255, 255},
{255, 255, 255}, {255, 255, 255}, {255, 255, 255}, {255, 255, 255}
}
));
// 6: LAVENDER (Lavanda) - Degradado violeta oscuro → azul medianoche, pelotas amarillo dorado
themes_.push_back(std::make_unique<StaticTheme>(
"LAVENDER",
"LAVANDA",
255, 200, 100, // Color texto: amarillo cálido
120.0f / 255.0f, 80.0f / 255.0f, 140.0f / 255.0f, // Fondo superior: violeta oscuro
25.0f / 255.0f, 30.0f / 255.0f, 60.0f / 255.0f, // Fondo inferior: azul medianoche
std::vector<Color>{
{255, 215, 0}, {255, 215, 0}, {255, 215, 0}, {255, 215, 0},
{255, 215, 0}, {255, 215, 0}, {255, 215, 0}, {255, 215, 0}
}
));
// ========================================
// TEMAS DINÁMICOS (índices 7-9)
// ========================================
// 7: SUNRISE (Amanecer) - Noche → Alba → Día (loop)
themes_.push_back(std::make_unique<DynamicTheme>(
"SUNRISE",
"AMANECER",
255, 200, 100, // Color texto: amarillo cálido
std::vector<DynamicThemeKeyframe>{
// Keyframe 0: Noche oscura (estado inicial)
{
20.0f / 255.0f, 25.0f / 255.0f, 60.0f / 255.0f, // Fondo superior: azul medianoche
10.0f / 255.0f, 10.0f / 255.0f, 30.0f / 255.0f, // Fondo inferior: azul muy oscuro
std::vector<Color>{
{100, 100, 150}, {120, 120, 170}, {90, 90, 140}, {110, 110, 160},
{95, 95, 145}, {105, 105, 155}, {100, 100, 150}, {115, 115, 165}
},
0.0f // Sin transición (estado inicial)
},
// Keyframe 1: Alba naranja-rosa
{
180.0f / 255.0f, 100.0f / 255.0f, 120.0f / 255.0f, // Fondo superior: naranja-rosa
255.0f / 255.0f, 140.0f / 255.0f, 100.0f / 255.0f, // Fondo inferior: naranja cálido
std::vector<Color>{
{255, 180, 100}, {255, 160, 80}, {255, 200, 120}, {255, 150, 90},
{255, 190, 110}, {255, 170, 95}, {255, 185, 105}, {255, 165, 88}
},
4.0f // 4 segundos para llegar aquí
},
// Keyframe 2: Día brillante amarillo
{
255.0f / 255.0f, 240.0f / 255.0f, 180.0f / 255.0f, // Fondo superior: amarillo claro
255.0f / 255.0f, 255.0f / 255.0f, 220.0f / 255.0f, // Fondo inferior: amarillo muy claro
std::vector<Color>{
{255, 255, 200}, {255, 255, 180}, {255, 255, 220}, {255, 255, 190},
{255, 255, 210}, {255, 255, 185}, {255, 255, 205}, {255, 255, 195}
},
3.0f // 3 segundos para llegar aquí
}
// NOTA: Keyframe 3 (vuelta a noche) eliminado - loop=true lo maneja automáticamente
},
true // Loop = true
));
// 8: OCEAN WAVES (Olas Oceánicas) - Azul oscuro ↔ Turquesa (loop)
themes_.push_back(std::make_unique<DynamicTheme>(
"OCEAN WAVES",
"OLAS OCEANICAS",
100, 220, 255, // Color texto: cian claro
std::vector<DynamicThemeKeyframe>{
// Keyframe 0: Profundidad oceánica (azul oscuro)
{
20.0f / 255.0f, 50.0f / 255.0f, 100.0f / 255.0f, // Fondo superior: azul marino
10.0f / 255.0f, 30.0f / 255.0f, 60.0f / 255.0f, // Fondo inferior: azul muy oscuro
std::vector<Color>{
{60, 100, 180}, {50, 90, 170}, {70, 110, 190}, {55, 95, 175},
{65, 105, 185}, {58, 98, 172}, {62, 102, 182}, {52, 92, 168}
},
0.0f // Estado inicial
},
// Keyframe 1: Aguas poco profundas (turquesa brillante)
{
100.0f / 255.0f, 200.0f / 255.0f, 230.0f / 255.0f, // Fondo superior: turquesa claro
50.0f / 255.0f, 150.0f / 255.0f, 200.0f / 255.0f, // Fondo inferior: turquesa medio
std::vector<Color>{
{100, 220, 255}, {90, 210, 245}, {110, 230, 255}, {95, 215, 250},
{105, 225, 255}, {98, 218, 248}, {102, 222, 252}, {92, 212, 242}
},
4.0f // 4 segundos para llegar
}
// NOTA: Keyframe 2 (vuelta a profundidad) eliminado - loop=true lo maneja automáticamente
},
true // Loop = true
));
// 9: NEON PULSE (Pulso Neón) - Negro → Neón brillante (rápido ping-pong)
themes_.push_back(std::make_unique<DynamicTheme>(
"NEON PULSE",
"PULSO NEON",
255, 60, 255, // Color texto: magenta brillante
std::vector<DynamicThemeKeyframe>{
// Keyframe 0: Apagado (negro)
{
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo superior: negro
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo inferior: negro
std::vector<Color>{
{40, 40, 40}, {50, 50, 50}, {45, 45, 45}, {48, 48, 48},
{42, 42, 42}, {47, 47, 47}, {44, 44, 44}, {46, 46, 46}
},
0.0f // Estado inicial
},
// Keyframe 1: Encendido (neón cian-magenta)
{
20.0f / 255.0f, 20.0f / 255.0f, 40.0f / 255.0f, // Fondo superior: azul oscuro
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo inferior: negro
std::vector<Color>{
{0, 255, 255}, {255, 0, 255}, {0, 255, 200}, {255, 50, 255},
{50, 255, 255}, {255, 0, 200}, {0, 255, 230}, {255, 80, 255}
},
1.5f // 1.5 segundos para encender (rápido)
}
// NOTA: Keyframe 2 (vuelta a apagado) eliminado - loop=true crea ping-pong automáticamente
},
true // Loop = true
));
}
// ============================================================================
// INTERFAZ UNIFICADA (PHASE 2)
// ============================================================================
void ThemeManager::switchToTheme(int theme_index) {
// Validar índice
if (theme_index < 0 || theme_index >= static_cast<int>(themes_.size())) {
return; // Índice inválido, no hacer nada
}
// Cambiar tema activo
current_theme_index_ = theme_index;
// Si es tema dinámico, reiniciar progreso
if (themes_[current_theme_index_]->needsUpdate()) {
themes_[current_theme_index_]->resetProgress();
}
}
void ThemeManager::update(float delta_time) {
// Solo actualizar si el tema actual necesita update (dinámicos)
if (themes_[current_theme_index_]->needsUpdate()) {
themes_[current_theme_index_]->update(delta_time);
}
}
void ThemeManager::cycleTheme() {
// Ciclar al siguiente tema con wraparound
current_theme_index_ = (current_theme_index_ + 1) % static_cast<int>(themes_.size());
// Si es tema dinámico, reiniciar progreso
if (themes_[current_theme_index_]->needsUpdate()) {
themes_[current_theme_index_]->resetProgress();
}
}
void ThemeManager::pauseDynamic() {
// Solo funciona si el tema actual es dinámico
if (themes_[current_theme_index_]->needsUpdate()) {
themes_[current_theme_index_]->togglePause();
}
}
// ============================================================================
// QUERIES DE COLORES
// ============================================================================
Color ThemeManager::getInterpolatedColor(size_t ball_index) const {
// Delegar al tema activo (progress = 0.0f por ahora, PHASE 3 usará LERP externo)
return themes_[current_theme_index_]->getBallColor(ball_index, 0.0f);
}
void ThemeManager::getBackgroundColors(float& top_r, float& top_g, float& top_b,
float& bottom_r, float& bottom_g, float& bottom_b) const {
// Delegar al tema activo (progress = 0.0f por ahora, PHASE 3 usará LERP externo)
themes_[current_theme_index_]->getBackgroundColors(0.0f, top_r, top_g, top_b, bottom_r, bottom_g, bottom_b);
}
// ============================================================================
// QUERIES DE ESTADO
// ============================================================================
bool ThemeManager::isCurrentThemeDynamic() const {
return themes_[current_theme_index_]->needsUpdate();
}
const char* ThemeManager::getCurrentThemeNameEN() const {
return themes_[current_theme_index_]->getNameEN();
}
const char* ThemeManager::getCurrentThemeNameES() const {
return themes_[current_theme_index_]->getNameES();
}
void ThemeManager::getCurrentThemeTextColor(int& r, int& g, int& b) const {
themes_[current_theme_index_]->getTextColor(r, g, b);
}
Color ThemeManager::getInitialBallColor(int random_index) const {
// Obtener color inicial del tema activo (progress = 0.0f)
return themes_[current_theme_index_]->getBallColor(random_index, 0.0f);
}