diff --git a/source/notifier.cpp b/source/notifier.cpp index 432cfe2..71cefab 100644 --- a/source/notifier.cpp +++ b/source/notifier.cpp @@ -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 ¬ification : notifications_) { // Si la notificación anterior está "saliendo", no hagas nada - if (i > 0) + if (!notifications_.empty() && ¬ification != ¬ifications_.front()) { - if (notifications_[i - 1].state == NotificationStatus::RISING) + auto &previous_notification = *(std::prev(¬ification)); + 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) - { - if (notifications_[i].state == NotificationStatus::FINISHED) - { - notifications_.erase(notifications_.begin() + i); - } - } + notifications_.erase( + std::remove_if(notifications_.begin(), notifications_.end(), + [](const Notification ¬ification) + { + return notification.state == NotificationStatus::FINISHED; + }), + notifications_.end()); } void Notifier::show(std::vector texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code) @@ -188,13 +189,12 @@ void Notifier::show(std::vector texts, NotificationText text_is, Ui // Posición vertical const int DESP_V = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? PADDING_OUT_ : options.game.height - HEIGHT - PADDING_OUT_; - + // Offset const auto TRAVEL_DIST = HEIGHT + PADDING_OUT_; 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;