afegits estils per a les notificacions (per a distinguir les de sistema de les dels logros)

This commit is contained in:
2025-11-16 20:19:30 +01:00
parent e4c10b6b75
commit 24a71395da
5 changed files with 77 additions and 45 deletions

View File

@@ -21,6 +21,27 @@
// [SINGLETON]
Notifier* Notifier::notifier = nullptr;
// Definición de estilos predefinidos
const Notifier::Style Notifier::Style::DEFAULT = {
.bg_color = static_cast<Uint8>(PaletteColor::BLUE),
.border_color = static_cast<Uint8>(PaletteColor::CYAN),
.text_color = static_cast<Uint8>(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<Uint8>(PaletteColor::MAGENTA),
.border_color = static_cast<Uint8>(PaletteColor::BRIGHT_MAGENTA),
.text_color = static_cast<Uint8>(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);
@@ -41,7 +62,6 @@ 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
@@ -70,12 +90,12 @@ void Notifier::update(float delta_time) {
if (notification.rect.y >= notification.y) {
notification.rect.y = notification.y;
notification.state = Status::STAY;
notification.start_time = SDL_GetTicks();
notification.elapsed_time = 0.0f;
}
break;
}
case Status::STAY: {
notification.elapsed_time = SDL_GetTicks() - notification.start_time;
notification.elapsed_time += delta_time;
if (notification.elapsed_time >= notification.display_duration) {
notification.state = Status::VANISHING;
}
@@ -115,7 +135,7 @@ void Notifier::clearFinishedNotifications() {
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) {
void Notifier::show(std::vector<std::string> texts, const Style& style, int icon, bool can_be_removed, const std::string& code) {
// Si no hay texto, acaba
if (texts.empty()) {
return;
@@ -143,10 +163,10 @@ void Notifier::show(std::vector<std::string> texts, TextAlign text_is, Uint32 di
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 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 = Shape::SQUARED;
const auto SHAPE = style.shape;
// Posición horizontal
float desp_h = ((Options::game.width / 2) - (WIDTH / 2));
@@ -170,7 +190,7 @@ void Notifier::show(std::vector<std::string> texts, TextAlign text_is, Uint32 di
n.travel_dist = TRAVEL_DIST;
n.texts = texts;
n.shape = SHAPE;
n.display_duration = display_duration;
n.display_duration = style.duration;
const float Y_POS = OFFSET + -TRAVEL_DIST;
n.rect = {.x = desp_h, .y = Y_POS, .w = WIDTH, .h = HEIGHT};
@@ -185,22 +205,22 @@ void Notifier::show(std::vector<std::string> texts, TextAlign text_is, Uint32 di
SDL_FRect rect;
if (SHAPE == Shape::ROUNDED) {
rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT};
n.surface->fillRect(&rect, bg_color_);
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
n.surface->fillRect(&rect, bg_color_);
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
n.surface->fillRect(&rect, bg_color_);
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
n.surface->fillRect(&rect, bg_color_);
n.surface->fillRect(&rect, style.bg_color);
}
else if (SHAPE == Shape::SQUARED) {
n.surface->clear(bg_color_);
n.surface->clear(style.bg_color);
SDL_FRect squared_rect = {0, 0, n.surface->getWidth(), n.surface->getHeight()};
n.surface->drawRectBorder(&squared_rect, static_cast<Uint8>(PaletteColor::CYAN));
n.surface->drawRectBorder(&squared_rect, style.border_color);
}
// Dibuja el icono de la notificación
@@ -212,7 +232,7 @@ void Notifier::show(std::vector<std::string> texts, TextAlign text_is, Uint32 di
}
// Escribe el texto de la notificación
const auto COLOR = static_cast<Uint8>(PaletteColor::WHITE);
const auto COLOR = style.text_color;
int iterator = 0;
for (const auto& text : texts) {
switch (text_is) {
@@ -239,7 +259,9 @@ void Notifier::show(std::vector<std::string> texts, TextAlign text_is, Uint32 di
notifications_.emplace_back(n);
// Reproduce el sonido de la notificación
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
if (style.play_sound && !style.sound_file.empty()) {
Audio::get()->playSound(style.sound_file, Audio::Group::INTERFACE);
}
}
// Indica si hay notificaciones activas