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

View File

@@ -0,0 +1,122 @@
#include "dynamic_theme.h"
#include <algorithm> // for std::min
DynamicTheme::DynamicTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
std::vector<DynamicThemeKeyframe> keyframes,
bool loop)
: name_en_(name_en),
name_es_(name_es),
text_r_(text_r), text_g_(text_g), text_b_(text_b),
keyframes_(std::move(keyframes)),
loop_(loop),
current_keyframe_index_(0),
target_keyframe_index_(1),
transition_progress_(0.0f),
paused_(false) {
// Validación: mínimo 2 keyframes
if (keyframes_.size() < 2) {
// Fallback: duplicar primer keyframe si solo hay 1
if (keyframes_.size() == 1) {
keyframes_.push_back(keyframes_[0]);
}
}
// Asegurar que target_keyframe_index es válido
if (target_keyframe_index_ >= keyframes_.size()) {
target_keyframe_index_ = 0;
}
}
void DynamicTheme::update(float delta_time) {
if (paused_) return; // No actualizar si está pausado
// Obtener duración del keyframe objetivo
float duration = keyframes_[target_keyframe_index_].duration;
if (duration <= 0.0f) {
duration = 1.0f; // Fallback si duración inválida
}
// Avanzar progreso
transition_progress_ += delta_time / duration;
// Si completamos la transición, avanzar al siguiente keyframe
if (transition_progress_ >= 1.0f) {
advanceToNextKeyframe();
}
}
void DynamicTheme::resetProgress() {
current_keyframe_index_ = 0;
target_keyframe_index_ = 1;
if (target_keyframe_index_ >= keyframes_.size()) {
target_keyframe_index_ = 0;
}
transition_progress_ = 0.0f;
}
void DynamicTheme::advanceToNextKeyframe() {
// Mover al siguiente keyframe
current_keyframe_index_ = target_keyframe_index_;
target_keyframe_index_++;
// Loop: volver al inicio si llegamos al final
if (target_keyframe_index_ >= keyframes_.size()) {
if (loop_) {
target_keyframe_index_ = 0;
} else {
// Si no hay loop, quedarse en el último keyframe
target_keyframe_index_ = keyframes_.size() - 1;
}
}
// Reiniciar progreso
transition_progress_ = 0.0f;
}
Color DynamicTheme::getBallColor(size_t ball_index, float progress) const {
// Obtener keyframes actual y objetivo
const auto& current_kf = keyframes_[current_keyframe_index_];
const auto& target_kf = keyframes_[target_keyframe_index_];
// Si paletas vacías, retornar blanco
if (current_kf.ball_colors.empty() || target_kf.ball_colors.empty()) {
return {255, 255, 255};
}
// Obtener colores de ambos keyframes (con wrap)
size_t current_palette_size = current_kf.ball_colors.size();
size_t target_palette_size = target_kf.ball_colors.size();
Color c1 = current_kf.ball_colors[ball_index % current_palette_size];
Color c2 = target_kf.ball_colors[ball_index % target_palette_size];
// Interpolar entre ambos colores usando progreso interno
// (progress parámetro será usado en PHASE 3 para LERP externo)
float t = transition_progress_;
return {
static_cast<int>(lerp(c1.r, c2.r, t)),
static_cast<int>(lerp(c1.g, c2.g, t)),
static_cast<int>(lerp(c1.b, c2.b, t))
};
}
void DynamicTheme::getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const {
// Obtener keyframes actual y objetivo
const auto& current_kf = keyframes_[current_keyframe_index_];
const auto& target_kf = keyframes_[target_keyframe_index_];
// Interpolar colores de fondo usando progreso interno
// (progress parámetro será usado en PHASE 3 para LERP externo)
float t = transition_progress_;
tr = lerp(current_kf.bg_top_r, target_kf.bg_top_r, t);
tg = lerp(current_kf.bg_top_g, target_kf.bg_top_g, t);
tb = lerp(current_kf.bg_top_b, target_kf.bg_top_b, t);
br = lerp(current_kf.bg_bottom_r, target_kf.bg_bottom_r, t);
bg = lerp(current_kf.bg_bottom_g, target_kf.bg_bottom_g, t);
bb = lerp(current_kf.bg_bottom_b, target_kf.bg_bottom_b, t);
}

View File

@@ -0,0 +1,103 @@
#pragma once
#include "theme.h"
#include <string>
// Forward declaration (estructura definida en defines.h)
struct DynamicThemeKeyframe;
/**
* DynamicTheme: Tema animado con N keyframes (2+)
*
* Características:
* - Animación continua entre keyframes
* - Progreso interno 0.0-1.0 entre keyframe actual y siguiente
* - Loop automático (vuelve al primer keyframe al terminar)
* - Pausable con Shift+D
* - Compatible con LERP externo (PHASE 3) vía parámetro progress
*
* Uso:
* - 3 temas dinámicos: SUNRISE, OCEAN_WAVES, NEON_PULSE
* - Indices 7-9 en el array unificado de ThemeManager
*/
class DynamicTheme : public Theme {
public:
/**
* Constructor
* @param name_en: Nombre en inglés
* @param name_es: Nombre en español
* @param text_r, text_g, text_b: Color de texto UI
* @param keyframes: Vector de keyframes (mínimo 2)
* @param loop: ¿Volver al inicio al terminar? (siempre true en esta app)
*/
DynamicTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
std::vector<DynamicThemeKeyframe> keyframes,
bool loop = true);
~DynamicTheme() override = default;
// ========================================
// QUERIES BÁSICAS
// ========================================
const char* getNameEN() const override { return name_en_.c_str(); }
const char* getNameES() const override { return name_es_.c_str(); }
void getTextColor(int& r, int& g, int& b) const override {
r = text_r_;
g = text_g_;
b = text_b_;
}
// ========================================
// CORE: OBTENER COLORES (interpolados)
// ========================================
Color getBallColor(size_t ball_index, float progress) const override;
void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const override;
// ========================================
// ANIMACIÓN (soporte completo)
// ========================================
void update(float delta_time) override;
bool needsUpdate() const override { return true; }
float getProgress() const override { return transition_progress_; }
void resetProgress() override;
// ========================================
// PAUSA (tecla Shift+D)
// ========================================
bool isPaused() const override { return paused_; }
void togglePause() override { paused_ = !paused_; }
private:
// ========================================
// DATOS DEL TEMA
// ========================================
std::string name_en_;
std::string name_es_;
int text_r_, text_g_, text_b_;
std::vector<DynamicThemeKeyframe> keyframes_;
bool loop_;
// ========================================
// ESTADO DE ANIMACIÓN
// ========================================
size_t current_keyframe_index_ = 0; // Keyframe actual
size_t target_keyframe_index_ = 1; // Próximo keyframe
float transition_progress_ = 0.0f; // Progreso 0.0-1.0 hacia target
bool paused_ = false; // Pausa manual con Shift+D
// ========================================
// UTILIDADES PRIVADAS
// ========================================
float lerp(float a, float b, float t) const { return a + (b - a) * t; }
void advanceToNextKeyframe(); // Avanza al siguiente keyframe (con loop)
};

View File

@@ -0,0 +1,36 @@
#include "static_theme.h"
StaticTheme::StaticTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
float bg_top_r, float bg_top_g, float bg_top_b,
float bg_bottom_r, float bg_bottom_g, float bg_bottom_b,
std::vector<Color> ball_colors)
: name_en_(name_en),
name_es_(name_es),
text_r_(text_r), text_g_(text_g), text_b_(text_b),
bg_top_r_(bg_top_r), bg_top_g_(bg_top_g), bg_top_b_(bg_top_b),
bg_bottom_r_(bg_bottom_r), bg_bottom_g_(bg_bottom_g), bg_bottom_b_(bg_bottom_b),
ball_colors_(std::move(ball_colors)) {
}
Color StaticTheme::getBallColor(size_t ball_index, float progress) const {
// Tema estático: siempre retorna color de paleta según índice
// (progress se ignora aquí, pero será usado en PHASE 3 para LERP externo)
if (ball_colors_.empty()) {
return {255, 255, 255}; // Blanco por defecto si paleta vacía
}
return ball_colors_[ball_index % ball_colors_.size()];
}
void StaticTheme::getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const {
// Tema estático: siempre retorna colores de fondo fijos
// (progress se ignora aquí, pero será usado en PHASE 3 para LERP externo)
tr = bg_top_r_;
tg = bg_top_g_;
tb = bg_top_b_;
br = bg_bottom_r_;
bg = bg_bottom_g_;
bb = bg_bottom_b_;
}

View File

@@ -0,0 +1,72 @@
#pragma once
#include "theme.h"
#include <string>
/**
* StaticTheme: Tema estático con 1 keyframe (sin animación)
*
* Características:
* - Colores fijos (no cambian con el tiempo)
* - Sin update() necesario (needsUpdate() retorna false)
* - Progress siempre 0.0 (no hay animación interna)
* - Compatible con LERP externo (PHASE 3) vía parámetro progress
*
* Uso:
* - 7 temas estáticos: SUNSET, OCEAN, NEON, FOREST, RGB, MONOCHROME, LAVENDER
* - Indices 0-6 en el array unificado de ThemeManager
*/
class StaticTheme : public Theme {
public:
/**
* Constructor
* @param name_en: Nombre en inglés
* @param name_es: Nombre en español
* @param text_r, text_g, text_b: Color de texto UI
* @param bg_top_r, bg_top_g, bg_top_b: Color superior de fondo
* @param bg_bottom_r, bg_bottom_g, bg_bottom_b: Color inferior de fondo
* @param ball_colors: Paleta de colores para pelotas
*/
StaticTheme(const char* name_en, const char* name_es,
int text_r, int text_g, int text_b,
float bg_top_r, float bg_top_g, float bg_top_b,
float bg_bottom_r, float bg_bottom_g, float bg_bottom_b,
std::vector<Color> ball_colors);
~StaticTheme() override = default;
// ========================================
// QUERIES BÁSICAS
// ========================================
const char* getNameEN() const override { return name_en_.c_str(); }
const char* getNameES() const override { return name_es_.c_str(); }
void getTextColor(int& r, int& g, int& b) const override {
r = text_r_;
g = text_g_;
b = text_b_;
}
// ========================================
// CORE: OBTENER COLORES
// ========================================
Color getBallColor(size_t ball_index, float progress) const override;
void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const override;
// ========================================
// ANIMACIÓN (sin soporte - tema estático)
// ========================================
// update(), needsUpdate(), getProgress(), resetProgress() usan defaults de Theme
private:
std::string name_en_;
std::string name_es_;
int text_r_, text_g_, text_b_;
float bg_top_r_, bg_top_g_, bg_top_b_;
float bg_bottom_r_, bg_bottom_g_, bg_bottom_b_;
std::vector<Color> ball_colors_;
};

93
source/themes/theme.h Normal file
View File

@@ -0,0 +1,93 @@
#pragma once
#include <vector>
#include "../defines.h" // for Color, ThemeKeyframe
/**
* Theme: Interfaz polimórfica para todos los temas (estáticos y dinámicos)
*
* Responsabilidades:
* - Proporcionar información básica del tema (nombre, color de texto)
* - Generar colores interpolados para pelotas y fondo
* - Actualizar estado interno si es animado (solo dinámicos)
*
* Implementaciones:
* - StaticTheme: 1 keyframe, sin animación, colores fijos
* - DynamicTheme: N keyframes, animación continua con loop
*/
class Theme {
public:
virtual ~Theme() = default;
// ========================================
// QUERIES BÁSICAS (implementar en derivadas)
// ========================================
virtual const char* getNameEN() const = 0;
virtual const char* getNameES() const = 0;
virtual void getTextColor(int& r, int& g, int& b) const = 0;
// ========================================
// CORE: OBTENER COLORES (polimórfico)
// ========================================
/**
* Obtiene color de pelota según índice y progreso de animación
* @param ball_index: Índice de pelota (para seleccionar color de paleta)
* @param progress: Progreso 0.0-1.0 si hay transición LERP externa (PHASE 3)
* @return Color interpolado para esta pelota
*/
virtual Color getBallColor(size_t ball_index, float progress) const = 0;
/**
* Obtiene colores de fondo degradado (top/bottom)
* @param progress: Progreso 0.0-1.0 si hay transición LERP externa (PHASE 3)
* @param tr, tg, tb: Color superior (out)
* @param br, bg, bb: Color inferior (out)
*/
virtual void getBackgroundColors(float progress,
float& tr, float& tg, float& tb,
float& br, float& bg, float& bb) const = 0;
// ========================================
// ANIMACIÓN (solo temas dinámicos)
// ========================================
/**
* Actualiza progreso de animación interna (solo dinámicos)
* @param delta_time: Tiempo transcurrido desde último frame
*/
virtual void update(float delta_time) { }
/**
* ¿Este tema necesita update() cada frame?
* @return false para estáticos, true para dinámicos
*/
virtual bool needsUpdate() const { return false; }
/**
* Obtiene progreso actual de animación interna
* @return 0.0 para estáticos, 0.0-1.0 para dinámicos
*/
virtual float getProgress() const { return 0.0f; }
/**
* Reinicia progreso de animación a 0.0 (usado al activar tema)
*/
virtual void resetProgress() { }
// ========================================
// PAUSA (solo temas dinámicos)
// ========================================
/**
* ¿Está pausado? (solo dinámicos)
* @return false para estáticos, true/false para dinámicos
*/
virtual bool isPaused() const { return false; }
/**
* Toggle pausa de animación (solo dinámicos, tecla Shift+D)
*/
virtual void togglePause() { }
};