Les notificacions ara accepten un vector de cadenes en lloc de una o dos cadenes

This commit is contained in:
2024-11-03 18:12:46 +01:00
parent 69a92cba66
commit f29eb2f411
6 changed files with 61 additions and 75 deletions

View File

@@ -2,12 +2,14 @@
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
#include <string> // Para string
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "param.h" // Para Param, param, ParamNotification, Par...
#include "screen.h" // Para Screen
#include "sprite.h" // Para Sprite
#include "text.h" // Para Text
#include "texture.h" // Para Texture
#include <algorithm>
#include <vector>
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "param.h" // Para Param, param, ParamNotification, Par...
#include "screen.h" // Para Screen
#include "sprite.h" // Para Sprite
#include "text.h" // Para Text
#include "texture.h" // Para Texture
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Notifier *Notifier::notifier_ = nullptr;
@@ -51,7 +53,9 @@ Notifier::~Notifier()
void Notifier::render()
{
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
{
notifications_[i].sprite->render();
}
}
// Actualiza el estado de las notificaiones
@@ -154,39 +158,41 @@ void Notifier::clearFinishedNotifications()
}
}
void Notifier::showText(std::string text1, std::string text2, int icon, const std::string &code)
void Notifier::showText(std::vector<std::string> texts, int icon, const std::string &code)
{
// Cuenta el número de textos a mostrar
const int num_texts = !text1.empty() + !text2.empty();
// Si no hay texto, acaba
if (num_texts == 0)
if (texts.empty())
{
return;
}
// Si solo hay un texto, lo coloca en la primera variable
if (num_texts == 1)
{
text1 += text2;
text2.clear();
}
// Si las notificaciones no se apilan, elimina las anteriores
if (!stack_)
{
clearNotifications();
}
// Elimina las cadenas vacías
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s)
{ return s.empty(); }),
texts.end());
// Encuentra la cadena más larga
std::string longest;
for (const auto &text : texts)
{
if (text.length() > longest.length())
longest = text;
}
// Inicializa variables
constexpr auto icon_size = 16;
constexpr auto padding_out = 1;
constexpr int icon_size = 16;
constexpr int padding_out = 1;
const auto padding_in_h = text_->getCharacterSize();
const auto padding_in_v = text_->getCharacterSize() / 2;
const auto icon_space = icon >= 0 ? icon_size + padding_in_h : 0;
const std::string txt = text1.length() > text2.length() ? text1 : text2;
const auto width = text_->lenght(txt) + (padding_in_h * 2) + icon_space;
const auto height = (text_->getCharacterSize() * num_texts) + (padding_in_v * 2);
const int icon_space = icon >= 0 ? icon_size + padding_in_h : 0;
const int width = text_->lenght(longest) + (padding_in_h * 2) + icon_space;
const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
const auto shape = NotificationShape::SQUARED;
// Posición horizontal
@@ -212,11 +218,11 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
auto offset = 0;
if (param.notification.pos_v == NotifyPosition::TOP)
{
offset = (int)notifications_.size() > 0 ? notifications_.back().y + travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y + travel_dist : desp_v;
}
else
{
offset = (int)notifications_.size() > 0 ? notifications_.back().y - travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y - travel_dist : desp_v;
}
// Crea la notificacion
@@ -226,8 +232,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
n.code = code;
n.y = offset;
n.travel_dist = travel_dist;
n.text1 = text1;
n.text2 = text2;
n.texts = texts;
n.shape = shape;
auto y_pos = offset + (param.notification.pos_v == NotifyPosition::TOP ? -travel_dist : travel_dist);
n.rect = {desp_h, y_pos, width, height};
@@ -264,7 +269,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
}
// Dibuja el icono de la notificación
if (has_icons_ && icon >= 0 && num_texts == 2)
if (has_icons_ && icon >= 0 && texts.size() >= 2)
{
auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size});
sp->setPosition({padding_in_h, padding_in_v, icon_size, icon_size});
@@ -273,15 +278,12 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
}
// Escribe el texto de la notificación
Color color{255, 255, 255};
if (num_texts == 2)
{ // Dos lineas de texto
text_->writeColored(padding_in_h + icon_space, padding_in_v, text1, color);
text_->writeColored(padding_in_h + icon_space, padding_in_v + text_->getCharacterSize() + 1, text2, color);
}
else
{ // Una linea de texto
text_->writeColored(padding_in_h + icon_space, padding_in_v, text1, color);
const Color color{255, 255, 255};
int iterator = 0;
for (const auto &text : texts)
{
text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
++iterator;
}
// Deja de dibujar en la textura
@@ -294,26 +296,18 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
n.texture->setAlpha(0);
// Añade la notificación a la lista
notifications_.push_back(n);
notifications_.emplace_back(n);
}
// Indica si hay notificaciones activas
bool Notifier::isActive()
{
if ((int)notifications_.size() > 0)
{
return true;
}
return false;
}
bool Notifier::isActive() { return !notifications_.empty(); }
// Finaliza y elimnina todas las notificaciones activas
void Notifier::clearNotifications()
{
for (int i = 0; i < (int)notifications_.size(); ++i)
for (auto &notification : notifications_)
{
notifications_[i].status = NotificationStatus::FINISHED;
notification.status = NotificationStatus::FINISHED;
}
clearFinishedNotifications();
@@ -323,9 +317,9 @@ void Notifier::clearNotifications()
std::vector<std::string> Notifier::getCodes()
{
std::vector<std::string> codes;
for (int i = 0; i < (int)notifications_.size(); ++i)
for (const auto &notification : notifications_)
{
codes.push_back(notifications_[i].code);
codes.emplace_back(notification.code);
}
return codes;
}