267 lines
9.2 KiB
C++
267 lines
9.2 KiB
C++
#include "game/ui/notifier.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <algorithm> // Para remove_if
|
|
#include <iterator> // Para prev
|
|
#include <ranges> // Para reverse_view
|
|
#include <string> // Para string, basic_string
|
|
#include <vector> // 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, TEXT_COLOR
|
|
#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;
|
|
|
|
// [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<DeltaTimer>()),
|
|
bg_color_(Options::notifications.color),
|
|
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.start_time = SDL_GetTicks();
|
|
}
|
|
break;
|
|
}
|
|
case Status::STAY: {
|
|
notification.elapsed_time = SDL_GetTicks() - notification.start_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<std::string> texts, TextAlign text_is, Uint32 display_duration, 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;
|
|
text_is = ICON_SPACE > 0 ? TextAlign::LEFT : text_is;
|
|
const float WIDTH = Options::game.width - (PADDING_OUT * 2);
|
|
const float HEIGHT = (TEXT_SIZE * texts.size()) + (PADDING_IN_V * 2);
|
|
const auto SHAPE = Shape::SQUARED;
|
|
|
|
// 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 = display_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<Surface>(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, bg_color_);
|
|
|
|
rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
|
|
n.surface->fillRect(&rect, bg_color_);
|
|
|
|
rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
|
|
n.surface->fillRect(&rect, bg_color_);
|
|
|
|
rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
|
|
n.surface->fillRect(&rect, bg_color_);
|
|
}
|
|
|
|
else if (SHAPE == Shape::SQUARED) {
|
|
n.surface->clear(bg_color_);
|
|
SDL_FRect squared_rect = {0, 0, n.surface->getWidth(), n.surface->getHeight()};
|
|
n.surface->drawRectBorder(&squared_rect, static_cast<Uint8>(PaletteColor::CYAN));
|
|
}
|
|
|
|
// Dibuja el icono de la notificación
|
|
if (has_icons_ && icon >= 0 && texts.size() >= 2) {
|
|
auto sp = std::make_unique<SurfaceSprite>(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 = static_cast<Uint8>(PaletteColor::WHITE);
|
|
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 | TEXT_COLOR, 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<SurfaceSprite>(n.surface, n.rect);
|
|
|
|
// Añade la notificación a la lista
|
|
notifications_.emplace_back(n);
|
|
|
|
// Reproduce el sonido de la notificación
|
|
Audio::get()->playSound("notify.wav", 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::string> {
|
|
std::vector<std::string> codes;
|
|
codes.reserve(notifications_.size());
|
|
for (const auto& notification : notifications_) {
|
|
codes.emplace_back(notification.code);
|
|
}
|
|
return codes;
|
|
} |