animacio al pixel del carrusel feta, falla el color que no transiciona
This commit is contained in:
@@ -49,6 +49,8 @@ Scoreboard::Scoreboard()
|
||||
score_.at(i) = 0;
|
||||
mult_.at(i) = 0;
|
||||
continue_counter_.at(i) = 0;
|
||||
carousel_prev_index_.at(i) = -1; // Inicializar a -1 para detectar primera inicialización
|
||||
enter_name_ref_.at(i) = nullptr;
|
||||
}
|
||||
|
||||
panel_.at(static_cast<size_t>(Id::LEFT)).mode = Mode::SCORE;
|
||||
@@ -90,38 +92,52 @@ Scoreboard::~Scoreboard() {
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el carrusel y detecta cambios para iniciar animación
|
||||
void Scoreboard::setCarousel(Id id, const std::string& carousel) {
|
||||
// Configura la animación del carrusel
|
||||
void Scoreboard::setCarouselAnimation(Id id, int selected_index, EnterName* enter_name_ptr) {
|
||||
size_t idx = static_cast<size_t>(id);
|
||||
|
||||
if (carousel.empty()) {
|
||||
carousel_.at(idx) = carousel;
|
||||
// Guardar referencia a EnterName
|
||||
enter_name_ref_.at(idx) = enter_name_ptr;
|
||||
|
||||
if (!enter_name_ptr || selected_index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcular el índice central del nuevo carrusel
|
||||
int new_center_index = carousel.size() / 2;
|
||||
|
||||
// Si es la primera vez o cambió el índice, establecer nuevo target
|
||||
if (carousel_.at(idx).empty()) {
|
||||
// Primera inicialización: posicionar directamente sin animar
|
||||
carousel_position_.at(idx) = static_cast<float>(new_center_index);
|
||||
carousel_target_.at(idx) = static_cast<float>(new_center_index);
|
||||
carousel_prev_index_.at(idx) = new_center_index;
|
||||
// Primera inicialización: posicionar directamente sin animar
|
||||
if (carousel_prev_index_.at(idx) == -1) {
|
||||
carousel_position_.at(idx) = static_cast<float>(selected_index);
|
||||
carousel_target_.at(idx) = static_cast<float>(selected_index);
|
||||
carousel_prev_index_.at(idx) = selected_index;
|
||||
} else {
|
||||
// Calcular índice central del carrusel anterior
|
||||
int prev_center_index = carousel_.at(idx).size() / 2;
|
||||
// Detectar cambio en el índice del carácter seleccionado
|
||||
int prev_index = carousel_prev_index_.at(idx);
|
||||
|
||||
// Detectar si cambió (LEFT o RIGHT fue presionado)
|
||||
if (new_center_index != prev_center_index) {
|
||||
// Establecer nuevo target (la animación se hará en updateCarouselAnimation)
|
||||
carousel_target_.at(idx) = static_cast<float>(new_center_index);
|
||||
if (selected_index != prev_index) {
|
||||
// Calcular dirección del movimiento
|
||||
int direction = selected_index - prev_index;
|
||||
|
||||
// Obtener tamaño de la lista para manejar wrap-around
|
||||
const int LIST_SIZE = enter_name_ptr->getCharacterList().size();
|
||||
|
||||
// Manejar wrap-around circular
|
||||
if (direction > LIST_SIZE / 2) {
|
||||
direction = -(LIST_SIZE - direction); // Wrap backward (ej: Z → A)
|
||||
} else if (direction < -LIST_SIZE / 2) {
|
||||
direction = LIST_SIZE + direction; // Wrap forward (ej: A → Z)
|
||||
}
|
||||
|
||||
// Normalizar a -1 o +1
|
||||
direction = (direction > 0) ? 1 : ((direction < 0) ? -1 : 0);
|
||||
|
||||
if (direction != 0) {
|
||||
// Actualizar target con movimiento relativo
|
||||
carousel_target_.at(idx) = carousel_position_.at(idx) + static_cast<float>(direction);
|
||||
|
||||
// Guardar nuevo índice
|
||||
carousel_prev_index_.at(idx) = selected_index;
|
||||
}
|
||||
}
|
||||
|
||||
carousel_prev_index_.at(idx) = new_center_index;
|
||||
}
|
||||
|
||||
carousel_.at(idx) = carousel;
|
||||
}
|
||||
|
||||
// Transforma un valor numérico en una cadena de 7 cifras
|
||||
@@ -498,8 +514,15 @@ void Scoreboard::renderSeparator() {
|
||||
|
||||
// Pinta el carrusel de caracteres con efecto de color LERP y animación suave
|
||||
void Scoreboard::renderCarousel(size_t panel_index, int center_x, int y) {
|
||||
const std::string& carousel = carousel_.at(panel_index);
|
||||
if (carousel.empty()) {
|
||||
// Obtener referencia a EnterName
|
||||
EnterName* enter_name = enter_name_ref_.at(panel_index);
|
||||
if (!enter_name) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Obtener la lista completa de caracteres
|
||||
const std::string& char_list = enter_name->getCharacterList();
|
||||
if (char_list.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -510,11 +533,11 @@ void Scoreboard::renderCarousel(size_t panel_index, int center_x, int y) {
|
||||
constexpr int VISIBLE_LETTERS = 9;
|
||||
constexpr int HALF_VISIBLE = VISIBLE_LETTERS / 2; // 4
|
||||
|
||||
// Posición flotante actual del carrusel
|
||||
// Posición flotante actual del carrusel (índice en character_list_)
|
||||
const float carousel_pos = carousel_position_.at(panel_index);
|
||||
|
||||
// Calcular ancho promedio de una letra (asumimos ancho uniforme para simplificar)
|
||||
std::string sample_char(1, carousel[carousel.size() / 2]);
|
||||
// Calcular ancho promedio de una letra (asumimos ancho uniforme)
|
||||
std::string sample_char(1, char_list[0]);
|
||||
const int AVG_CHAR_WIDTH = text_->length(sample_char, 1);
|
||||
const int CHAR_STEP = AVG_CHAR_WIDTH + EXTRA_SPACING;
|
||||
|
||||
@@ -522,28 +545,29 @@ void Scoreboard::renderCarousel(size_t panel_index, int center_x, int y) {
|
||||
const float fractional_offset = carousel_pos - std::floor(carousel_pos);
|
||||
const int pixel_offset = static_cast<int>(fractional_offset * CHAR_STEP);
|
||||
|
||||
// Índice base (centro del carrusel)
|
||||
// Índice base en character_list_ (centro del carrusel)
|
||||
const int base_index = static_cast<int>(std::floor(carousel_pos));
|
||||
const int char_list_size = static_cast<int>(char_list.size());
|
||||
|
||||
// Calcular posición X inicial (centrar el conjunto de 9 letras)
|
||||
int start_x = center_x - (HALF_VISIBLE * CHAR_STEP) - (AVG_CHAR_WIDTH / 2) - pixel_offset;
|
||||
|
||||
// Detectar si el carrusel está en movimiento
|
||||
const bool is_animating = std::abs(carousel_pos - carousel_target_.at(panel_index)) > 0.01f;
|
||||
|
||||
// Renderizar las 9 letras visibles
|
||||
for (int i = -HALF_VISIBLE; i <= HALF_VISIBLE; ++i) {
|
||||
// Índice real en la lista de caracteres (con wrap-around circular)
|
||||
// Índice real en character_list_ (con wrap-around circular)
|
||||
int char_index = base_index + i;
|
||||
const int carousel_size = static_cast<int>(carousel.size());
|
||||
|
||||
// Wrap-around circular
|
||||
while (char_index < 0) {
|
||||
char_index += carousel_size;
|
||||
}
|
||||
while (char_index >= carousel_size) {
|
||||
char_index -= carousel_size;
|
||||
char_index = char_index % char_list_size;
|
||||
if (char_index < 0) {
|
||||
char_index += char_list_size;
|
||||
}
|
||||
|
||||
// Obtener el carácter
|
||||
std::string single_char(1, carousel[char_index]);
|
||||
// Obtener el carácter directamente de character_list_
|
||||
std::string single_char(1, char_list[char_index]);
|
||||
|
||||
// Calcular distancia flotante al centro visual
|
||||
const float distance_from_center = std::abs(static_cast<float>(i) + fractional_offset);
|
||||
@@ -551,13 +575,22 @@ void Scoreboard::renderCarousel(size_t panel_index, int center_x, int y) {
|
||||
// Calcular color con LERP dinámico
|
||||
Color letter_color;
|
||||
|
||||
if (distance_from_center < 0.5f) {
|
||||
// Letra central: animated_color_
|
||||
if (distance_from_center < 0.5f && !is_animating) {
|
||||
// Letra central Y carrusel quieto: animated_color_
|
||||
letter_color = animated_color_;
|
||||
} else {
|
||||
// Letras laterales: LERP desde text_color1_ hacia color_
|
||||
// Factor: 0.0 cerca del centro, 0.85 en los extremos (85% fade)
|
||||
const float LERP_FACTOR = std::min((distance_from_center - 0.5f) / (HALF_VISIBLE - 0.5f), 1.0f) * 0.85f;
|
||||
// Todas las demás letras O carrusel en movimiento: LERP
|
||||
float base_lerp;
|
||||
if (distance_from_center < 0.5f) {
|
||||
// Letra muy cerca del centro durante animación: 0% (color brillante)
|
||||
base_lerp = 0.0f;
|
||||
} else {
|
||||
// Letras alejadas: lerp proporcional a la distancia
|
||||
base_lerp = (distance_from_center - 0.5f) / (HALF_VISIBLE - 0.5f);
|
||||
base_lerp = std::min(base_lerp, 1.0f);
|
||||
}
|
||||
|
||||
const float LERP_FACTOR = base_lerp * 0.85f;
|
||||
letter_color = text_color1_.LERP(color_, LERP_FACTOR);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user