Optimitzats alguns bucles en les notificacions

This commit is contained in:
2025-03-08 11:38:17 +01:00
parent 195d812d2a
commit 3a36bf6530

View File

@@ -45,46 +45,47 @@ Notifier::Notifier(const std::string &icon_file, const std::string &text)
// Dibuja las notificaciones por pantalla
void Notifier::render()
{
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it)
{
notifications_[i].sprite->render();
it->sprite->render();
}
}
// Actualiza el estado de las notificaiones
void Notifier::update()
{
for (int i = 0; i < (int)notifications_.size(); ++i)
for (auto &notification : notifications_)
{
// Si la notificación anterior está "saliendo", no hagas nada
if (i > 0)
if (!notifications_.empty() && &notification != &notifications_.front())
{
if (notifications_[i - 1].state == NotificationStatus::RISING)
auto &previous_notification = *(std::prev(&notification));
if (previous_notification.state == NotificationStatus::RISING)
{
break;
}
}
switch (notifications_[i].state)
switch (notification.state)
{
case NotificationStatus::RISING:
{
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
notifications_[i].rect.y += DIRECTION;
notification.rect.y += DIRECTION;
if (notifications_[i].rect.y == notifications_[i].y)
if (notification.rect.y == notification.y)
{
notifications_[i].state = NotificationStatus::STAY;
notifications_[i].start_time = SDL_GetTicks();
notification.state = NotificationStatus::STAY;
notification.start_time = SDL_GetTicks();
}
break;
}
case NotificationStatus::STAY:
{
notifications_[i].elapsed_time = SDL_GetTicks() - notifications_[i].start_time;
if (notifications_[i].elapsed_time >= notifications_[i].display_duration)
notification.elapsed_time = SDL_GetTicks() - notification.start_time;
if (notification.elapsed_time >= notification.display_duration)
{
notifications_[i].state = NotificationStatus::VANISHING;
notification.state = NotificationStatus::VANISHING;
}
break;
}
@@ -92,11 +93,11 @@ void Notifier::update()
case NotificationStatus::VANISHING:
{
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1;
notifications_[i].rect.y += DIRECTION;
notification.rect.y += DIRECTION;
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist)
if (notification.rect.y == notification.y - notification.travel_dist)
{
notifications_[i].state = NotificationStatus::FINISHED;
notification.state = NotificationStatus::FINISHED;
}
break;
}
@@ -108,7 +109,7 @@ void Notifier::update()
break;
}
notifications_[i].sprite->setPosition(notifications_[i].rect);
notification.sprite->setPosition(notification.rect);
}
clearFinishedNotifications();
@@ -117,13 +118,13 @@ void Notifier::update()
// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications()
{
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
notifications_.erase(
std::remove_if(notifications_.begin(), notifications_.end(),
[](const Notification &notification)
{
if (notifications_[i].state == NotificationStatus::FINISHED)
{
notifications_.erase(notifications_.begin() + i);
}
}
return notification.state == NotificationStatus::FINISHED;
}),
notifications_.end());
}
void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code)
@@ -194,7 +195,6 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Ui
const int TRAVEL_MOD = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
const int OFFSET = !notifications_.empty() ? notifications_.back().y + TRAVEL_MOD * notifications_.back().travel_dist : DESP_V;
// Crea la notificacion
Notification n;