migrat a deltaTime screen.cpp i notifier.cpp
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_RenderFillRect, SDL_FRect, SDL_RenderClear
|
||||
|
||||
#include <algorithm> // Para remove_if
|
||||
#include <algorithm> // Para remove_if, min
|
||||
#include <string> // Para basic_string, string
|
||||
#include <utility>
|
||||
#include <vector> // Para vector
|
||||
@@ -32,7 +32,6 @@ Notifier::Notifier(const std::string& icon_file, std::shared_ptr<Text> text)
|
||||
icon_texture_(!icon_file.empty() ? std::make_unique<Texture>(renderer_, icon_file) : nullptr),
|
||||
text_(std::move(text)),
|
||||
bg_color_(param.notification.color),
|
||||
wait_time_(150),
|
||||
stack_(false),
|
||||
has_icons_(!icon_file.empty()) {}
|
||||
|
||||
@@ -43,13 +42,13 @@ void Notifier::render() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void Notifier::update() {
|
||||
// Actualiza el estado de las notificaciones
|
||||
void Notifier::update(float delta_time) {
|
||||
for (int i = 0; i < (int)notifications_.size(); ++i) {
|
||||
if (!shouldProcessNotification(i)) {
|
||||
break;
|
||||
}
|
||||
processNotification(i);
|
||||
processNotification(i, delta_time);
|
||||
}
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
@@ -59,52 +58,54 @@ auto Notifier::shouldProcessNotification(int index) const -> bool {
|
||||
return index <= 0 || notifications_[index - 1].state != State::RISING;
|
||||
}
|
||||
|
||||
void Notifier::processNotification(int index) {
|
||||
void Notifier::processNotification(int index, float delta_time) {
|
||||
auto& notification = notifications_[index];
|
||||
notification.counter++;
|
||||
notification.timer += delta_time;
|
||||
|
||||
playNotificationSoundIfNeeded(notification);
|
||||
updateNotificationState(index);
|
||||
updateNotificationState(index, delta_time);
|
||||
notification.sprite->setPosition(notification.rect);
|
||||
}
|
||||
|
||||
void Notifier::playNotificationSoundIfNeeded(const Notification& notification) {
|
||||
// Hace sonar la notificación en el primer frame
|
||||
if (notification.counter == 1 &&
|
||||
// Hace sonar la notificación al inicio
|
||||
if (notification.timer <= 0.016f &&
|
||||
param.notification.sound &&
|
||||
notification.state == State::RISING) {
|
||||
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
|
||||
}
|
||||
}
|
||||
|
||||
void Notifier::updateNotificationState(int index) {
|
||||
void Notifier::updateNotificationState(int index, float delta_time) {
|
||||
auto& notification = notifications_[index];
|
||||
|
||||
switch (notification.state) {
|
||||
case State::RISING:
|
||||
handleRisingState(index);
|
||||
handleRisingState(index, delta_time);
|
||||
break;
|
||||
case State::STAY:
|
||||
handleStayState(index);
|
||||
break;
|
||||
case State::VANISHING:
|
||||
handleVanishingState(index);
|
||||
handleVanishingState(index, delta_time);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Notifier::handleRisingState(int index) {
|
||||
void Notifier::handleRisingState(int index, float delta_time) {
|
||||
auto& notification = notifications_[index];
|
||||
|
||||
const float STEP = (float)notification.counter / notification.travel_dist;
|
||||
const int ALPHA = 255 * STEP;
|
||||
const float PIXELS_TO_MOVE = ANIMATION_SPEED_PX_PER_S * delta_time;
|
||||
const float PROGRESS = notification.timer * ANIMATION_SPEED_PX_PER_S / notification.travel_dist;
|
||||
const int ALPHA = static_cast<int>(255 * std::min(PROGRESS, 1.0f));
|
||||
|
||||
moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? 1 : -1);
|
||||
moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? PIXELS_TO_MOVE : -PIXELS_TO_MOVE);
|
||||
notification.texture->setAlpha(ALPHA);
|
||||
|
||||
if (notification.rect.y == notification.y) {
|
||||
if ((param.notification.pos_v == Position::TOP && notification.rect.y >= notification.y) ||
|
||||
(param.notification.pos_v == Position::BOTTOM && notification.rect.y <= notification.y)) {
|
||||
transitionToStayState(index);
|
||||
}
|
||||
}
|
||||
@@ -112,35 +113,37 @@ void Notifier::handleRisingState(int index) {
|
||||
void Notifier::handleStayState(int index) {
|
||||
auto& notification = notifications_[index];
|
||||
|
||||
if (notification.counter == wait_time_) {
|
||||
if (notification.timer >= STAY_DURATION_S) {
|
||||
notification.state = State::VANISHING;
|
||||
notification.counter = 0;
|
||||
notification.timer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void Notifier::handleVanishingState(int index) {
|
||||
void Notifier::handleVanishingState(int index, float delta_time) {
|
||||
auto& notification = notifications_[index];
|
||||
|
||||
const float STEP = notification.counter / (float)notification.travel_dist;
|
||||
const int ALPHA = 255 * (1 - STEP);
|
||||
const float PIXELS_TO_MOVE = ANIMATION_SPEED_PX_PER_S * delta_time;
|
||||
const float PROGRESS = notification.timer * ANIMATION_SPEED_PX_PER_S / notification.travel_dist;
|
||||
const int ALPHA = static_cast<int>(255 * (1 - std::min(PROGRESS, 1.0f)));
|
||||
|
||||
moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? -1 : 1);
|
||||
moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? -PIXELS_TO_MOVE : PIXELS_TO_MOVE);
|
||||
notification.texture->setAlpha(ALPHA);
|
||||
|
||||
if (notification.rect.y == notification.y - notification.travel_dist) {
|
||||
if (PROGRESS >= 1.0f) {
|
||||
notification.state = State::FINISHED;
|
||||
}
|
||||
}
|
||||
|
||||
void Notifier::moveNotificationVertically(Notification& notification, int direction) {
|
||||
notification.rect.y += direction;
|
||||
void Notifier::moveNotificationVertically(Notification& notification, float pixels_to_move) {
|
||||
notification.rect.y += pixels_to_move;
|
||||
}
|
||||
|
||||
void Notifier::transitionToStayState(int index) {
|
||||
auto& notification = notifications_[index];
|
||||
notification.state = State::STAY;
|
||||
notification.texture->setAlpha(255);
|
||||
notification.counter = 0;
|
||||
notification.rect.y = static_cast<float>(notification.y); // Asegurar posición exacta
|
||||
notification.timer = 0.0f;
|
||||
}
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
|
||||
@@ -32,7 +32,7 @@ class Notifier {
|
||||
|
||||
// --- Métodos principales ---
|
||||
void render(); // Dibuja las notificaciones por pantalla
|
||||
void update(); // Actualiza el estado de las notificaciones
|
||||
void update(float delta_time); // Actualiza el estado de las notificaciones
|
||||
|
||||
// --- Gestión de notificaciones ---
|
||||
void show(std::vector<std::string> texts, int icon = -1, const std::string &code = std::string()); // Muestra una notificación de texto por pantalla
|
||||
@@ -41,6 +41,10 @@ class Notifier {
|
||||
auto checkCode(const std::string &code) -> bool { return stringInVector(getCodes(), code); } // Comprueba si hay alguna notificación con un código concreto
|
||||
|
||||
private:
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
static constexpr float STAY_DURATION_S = 2.5f; // Tiempo que se ve la notificación (150 frames @ 60fps)
|
||||
static constexpr float ANIMATION_SPEED_PX_PER_S = 60.0f; // Velocidad de animación (1 pixel/frame @ 60fps)
|
||||
|
||||
// --- Enums privados ---
|
||||
enum class State {
|
||||
RISING, // Apareciendo
|
||||
@@ -63,7 +67,7 @@ class Notifier {
|
||||
std::string code; // Código identificador de la notificación
|
||||
State state{State::RISING}; // Estado de la notificación
|
||||
Shape shape{Shape::SQUARED}; // Forma de la notificación
|
||||
int counter{0}; // Contador de tiempo
|
||||
float timer{0.0f}; // Timer en segundos
|
||||
int y{0}; // Posición vertical
|
||||
int travel_dist{0}; // Distancia a recorrer
|
||||
|
||||
@@ -82,7 +86,7 @@ class Notifier {
|
||||
// --- Variables de estado ---
|
||||
std::vector<Notification> notifications_; // Lista de notificaciones activas
|
||||
Color bg_color_; // Color de fondo de las notificaciones
|
||||
int wait_time_; // Tiempo que se ve la notificación
|
||||
// Nota: wait_time_ eliminado, ahora se usa STAY_DURATION_S
|
||||
bool stack_; // Indica si las notificaciones se apilan
|
||||
bool has_icons_; // Indica si el notificador tiene textura para iconos
|
||||
|
||||
@@ -90,13 +94,13 @@ class Notifier {
|
||||
void clearFinishedNotifications(); // Elimina las notificaciones cuyo estado es FINISHED
|
||||
void clearAllNotifications(); // Elimina todas las notificaciones activas, sin importar el estado
|
||||
[[nodiscard]] auto shouldProcessNotification(int index) const -> bool; // Determina si una notificación debe ser procesada (según su estado y posición)
|
||||
void processNotification(int index); // Procesa una notificación en la posición dada: actualiza su estado y comportamiento visual
|
||||
void processNotification(int index, float delta_time); // Procesa una notificación en la posición dada: actualiza su estado y comportamiento visual
|
||||
static void playNotificationSoundIfNeeded(const Notification ¬ification); // Reproduce sonido asociado si es necesario (dependiendo del estado o contenido)
|
||||
void updateNotificationState(int index); // Actualiza el estado interno de una notificación (ej. de RISING a STAY)
|
||||
void handleRisingState(int index); // Lógica de animación para el estado RISING (apareciendo)
|
||||
void updateNotificationState(int index, float delta_time); // Actualiza el estado interno de una notificación (ej. de RISING a STAY)
|
||||
void handleRisingState(int index, float delta_time); // Lógica de animación para el estado RISING (apareciendo)
|
||||
void handleStayState(int index); // Lógica para mantener una notificación visible en el estado STAY
|
||||
void handleVanishingState(int index); // Lógica de animación para el estado VANISHING (desapareciendo)
|
||||
static void moveNotificationVertically(Notification ¬ification, int direction); // Mueve verticalmente una notificación en una dirección dada (útil para animación en apilamiento)
|
||||
void handleVanishingState(int index, float delta_time); // Lógica de animación para el estado VANISHING (desapareciendo)
|
||||
static void moveNotificationVertically(Notification ¬ification, float pixels_to_move); // Mueve verticalmente una notificación con la cantidad de pixels especificada
|
||||
void transitionToStayState(int index); // Cambia el estado de una notificación de RISING a STAY cuando ha alcanzado su posición final
|
||||
|
||||
// --- Constructores y destructor privados (singleton) ---
|
||||
|
||||
Reference in New Issue
Block a user