#include "game/ui/notifier.hpp" #include #include // Para remove_if #include // Para prev #include // Para reverse_view #include // Para string, basic_string #include // Para vector #include "core/audio/audio.hpp" // Para Audio #include "core/rendering/screen.hpp" // Para Screen #include "core/rendering/surface.hpp" // Para Surface #include "core/rendering/surface_sprite.hpp" // Para SSprite #include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG #include "core/resources/resource_cache.hpp" // Para Resource #include "game/options.hpp" // Para Options, options, NotificationPosition #include "utils/delta_timer.hpp" // Para DeltaTimer #include "utils/utils.hpp" // Para PaletteColor // [SINGLETON] Notifier* Notifier::notifier = nullptr; // Definición de estilos predefinidos const Notifier::Style Notifier::Style::DEFAULT = { .bg_color = static_cast(PaletteColor::BLUE), .border_color = static_cast(PaletteColor::CYAN), .text_color = static_cast(PaletteColor::CYAN), .shape = Notifier::Shape::SQUARED, .text_align = Notifier::TextAlign::CENTER, .duration = 2.0F, .sound_file = "notify.wav", .play_sound = false}; const Notifier::Style Notifier::Style::CHEEVO = { .bg_color = static_cast(PaletteColor::MAGENTA), .border_color = static_cast(PaletteColor::BRIGHT_MAGENTA), .text_color = static_cast(PaletteColor::WHITE), .shape = Notifier::Shape::SQUARED, .text_align = Notifier::TextAlign::CENTER, .duration = 4.0F, .sound_file = "notify.wav", .play_sound = true}; // [SINGLETON] Crearemos el objeto con esta función estática void Notifier::init(const std::string& icon_file, const std::string& text) { Notifier::notifier = new Notifier(icon_file, text); } // [SINGLETON] Destruiremos el objeto con esta función estática void Notifier::destroy() { delete Notifier::notifier; } // [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él auto Notifier::get() -> Notifier* { return Notifier::notifier; } // Constructor Notifier::Notifier(const std::string& icon_file, const std::string& text) : icon_surface_(!icon_file.empty() ? Resource::Cache::get()->getSurface(icon_file) : nullptr), text_(Resource::Cache::get()->getText(text)), delta_timer_(std::make_unique()), has_icons_(!icon_file.empty()) {} // Dibuja las notificaciones por pantalla void Notifier::render() { for (auto& notification : std::ranges::reverse_view(notifications_)) { notification.sprite->render(); } } // Actualiza el estado de las notificaiones void Notifier::update(float delta_time) { for (auto& notification : notifications_) { // Si la notificación anterior está "saliendo", no hagas nada if (!notifications_.empty() && ¬ification != ¬ifications_.front()) { const auto& previous_notification = *(std::prev(¬ification)); if (previous_notification.state == Status::RISING) { break; } } switch (notification.state) { case Status::RISING: { const float DISPLACEMENT = SLIDE_SPEED * delta_time; notification.rect.y += DISPLACEMENT; if (notification.rect.y >= notification.y) { notification.rect.y = notification.y; notification.state = Status::STAY; notification.elapsed_time = 0.0F; } break; } case Status::STAY: { notification.elapsed_time += delta_time; if (notification.elapsed_time >= notification.display_duration) { notification.state = Status::VANISHING; } break; } case Status::VANISHING: { const float DISPLACEMENT = SLIDE_SPEED * delta_time; notification.rect.y -= DISPLACEMENT; const float TARGET_Y = notification.y - notification.travel_dist; if (notification.rect.y <= TARGET_Y) { notification.rect.y = TARGET_Y; notification.state = Status::FINISHED; } break; } case Status::FINISHED: break; default: break; } notification.sprite->setPosition(notification.rect); } clearFinishedNotifications(); } // Elimina las notificaciones finalizadas void Notifier::clearFinishedNotifications() { auto result = std::ranges::remove_if(notifications_, [](const Notification& notification) { return notification.state == Status::FINISHED; }); notifications_.erase(result.begin(), result.end()); } void Notifier::show(std::vector texts, const Style& style, int icon, bool can_be_removed, const std::string& code) { // Si no hay texto, acaba if (texts.empty()) { return; } // Si las notificaciones no se apilan, elimina las anteriores if (!stack_) { clearNotifications(); } // Elimina las cadenas vacías auto result = std::ranges::remove_if(texts, [](const std::string& s) { return s.empty(); }); texts.erase(result.begin(), result.end()); // Encuentra la cadena más larga std::string longest; for (const auto& text : texts) { if (text.length() > longest.length()) { longest = text; } } // Inicializa variables const int TEXT_SIZE = 6; const auto PADDING_IN_H = TEXT_SIZE; const auto PADDING_IN_V = TEXT_SIZE / 2; const int ICON_SPACE = icon >= 0 ? ICON_SIZE + PADDING_IN_H : 0; const TextAlign TEXT_IS = ICON_SPACE > 0 ? TextAlign::LEFT : style.text_align; const float WIDTH = Options::game.width - (PADDING_OUT * 2); const float HEIGHT = (TEXT_SIZE * texts.size()) + (PADDING_IN_V * 2); const auto SHAPE = style.shape; // Posición horizontal float desp_h = ((Options::game.width / 2) - (WIDTH / 2)); ; // Posición vertical const int DESP_V = PADDING_OUT; // Offset const auto TRAVEL_DIST = HEIGHT + PADDING_OUT; const int TRAVEL_MOD = 1; const int OFFSET = !notifications_.empty() ? notifications_.back().y + (TRAVEL_MOD * notifications_.back().travel_dist) : DESP_V; // Crea la notificacion Notification n; // Inicializa variables n.code = code; n.can_be_removed = can_be_removed; n.y = OFFSET; n.travel_dist = TRAVEL_DIST; n.texts = texts; n.shape = SHAPE; n.display_duration = style.duration; const float Y_POS = OFFSET + -TRAVEL_DIST; n.rect = {.x = desp_h, .y = Y_POS, .w = WIDTH, .h = HEIGHT}; // Crea la textura n.surface = std::make_shared(WIDTH, HEIGHT); // Prepara para dibujar en la textura auto previuos_renderer = Screen::get()->getRendererSurface(); Screen::get()->setRendererSurface(n.surface); // Dibuja el fondo de la notificación SDL_FRect rect; if (SHAPE == Shape::ROUNDED) { rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT}; n.surface->fillRect(&rect, style.bg_color); rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2}; n.surface->fillRect(&rect, style.bg_color); rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4}; n.surface->fillRect(&rect, style.bg_color); rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)}; n.surface->fillRect(&rect, style.bg_color); } else if (SHAPE == Shape::SQUARED) { n.surface->clear(style.bg_color); SDL_FRect squared_rect = {0, 0, n.surface->getWidth(), n.surface->getHeight()}; n.surface->drawRectBorder(&squared_rect, style.border_color); } // Dibuja el icono de la notificación if (has_icons_ && icon >= 0 && texts.size() >= 2) { auto sp = std::make_unique(icon_surface_, (SDL_FRect){0, 0, ICON_SIZE, ICON_SIZE}); sp->setPosition({PADDING_IN_H, PADDING_IN_V, ICON_SIZE, ICON_SIZE}); sp->setClip((SDL_FRect){ICON_SIZE * (icon % 10), ICON_SIZE * (icon / 10), ICON_SIZE, ICON_SIZE}); sp->render(); } // Escribe el texto de la notificación const auto COLOR = style.text_color; int iterator = 0; for (const auto& text : texts) { switch (TEXT_IS) { case TextAlign::LEFT: text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, COLOR); break; case TextAlign::CENTER: text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, WIDTH / 2, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, 1, COLOR); break; default: break; } ++iterator; } // Deja de dibujar en la textura Screen::get()->setRendererSurface(previuos_renderer); // Crea el sprite de la notificación n.sprite = std::make_shared(n.surface, n.rect); // Añade la notificación a la lista notifications_.emplace_back(n); // Reproduce el sonido de la notificación if (style.play_sound && !style.sound_file.empty()) { Audio::get()->playSound(style.sound_file, Audio::Group::INTERFACE); } } // Indica si hay notificaciones activas auto Notifier::isActive() -> bool { return !notifications_.empty(); } // Finaliza y elimnina todas las notificaciones activas void Notifier::clearNotifications() { for (auto& notification : notifications_) { if (notification.can_be_removed) { notification.state = Status::FINISHED; } } clearFinishedNotifications(); } // Obtiene los códigos de las notificaciones auto Notifier::getCodes() -> std::vector { std::vector codes; codes.reserve(notifications_.size()); for (const auto& notification : notifications_) { codes.emplace_back(notification.code); } return codes; }