Nou engine de notificacions

This commit is contained in:
2025-02-27 19:03:57 +01:00
parent 2e11fec2cb
commit 59e766f5c3
9 changed files with 122 additions and 67 deletions

View File

@@ -16,7 +16,7 @@
Notifier *Notifier::notifier_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Notifier::init(const std::string &icon_file, std::shared_ptr<Text> text)
void Notifier::init(const std::string &icon_file, const std::string &text)
{
Notifier::notifier_ = new Notifier(icon_file, text);
}
@@ -34,7 +34,7 @@ Notifier *Notifier::get()
}
// Constructor
Notifier::Notifier(std::string &icon_file, const std::string &text)
Notifier::Notifier(const std::string &icon_file, const std::string &text)
: renderer_(Screen::get()->getRenderer()),
icon_texture_(!icon_file.empty() ? Resource::get()->getTexture(icon_file) : nullptr),
text_(Resource::get()->getText(text)),
@@ -87,11 +87,11 @@ void Notifier::update()
const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist);
const int alpha = 255 * step;
if (options.notifications.getVerticalPosition() == "TOP")
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{
notifications_[i].rect.y++;
}
else
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
notifications_[i].rect.y--;
}
@@ -119,11 +119,11 @@ void Notifier::update()
const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist);
const int alpha = 255 * (1 - step);
if (options.notifications.getVerticalPosition() == "TOP")
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{
notifications_[i].rect.y--;
}
else
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
notifications_[i].rect.y++;
}
@@ -153,7 +153,7 @@ void Notifier::clearFinishedNotifications()
}
}
void Notifier::show(std::vector<std::string> texts, int icon, const std::string &code)
void Notifier::show(std::vector<std::string> texts, NotificationText text_is, int icon, bool can_be_removed, const std::string &code)
{
// Si no hay texto, acaba
if (texts.empty())
@@ -181,43 +181,42 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
}
// Inicializa variables
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 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 icon_space = icon >= 0 ? ICON_SIZE_ + padding_in_h : 0;
text_is = icon_space > 0 ? NotificationText::LEFT : text_is;
// const int width = text_->lenght(longest) + (padding_in_h * 2) + icon_space;
const int width = options.game.width - (PADDING_OUT_ * 2);
const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
const auto shape = NotificationShape::SQUARED;
// Posición horizontal
auto desp_h = 0;
if (options.notifications.getHorizontalPosition() == "LEFT")
if (options.notifications.getHorizontalPosition() == NotificationPosition::LEFT)
{
desp_h = padding_out;
desp_h = PADDING_OUT_;
}
else if (options.notifications.getHorizontalPosition() == "CENTER")
else if (options.notifications.getHorizontalPosition() == NotificationPosition::CENTER)
{
desp_h = ((options.game.width / 2) - (width / 2));
}
else
else if (options.notifications.getHorizontalPosition() == NotificationPosition::RIGHT)
{
desp_h = options.game.width - width - padding_out;
desp_h = options.game.width - width - PADDING_OUT_;
}
// Posición vertical
const int desp_v = (options.notifications.getVerticalPosition() == "TOP") ? padding_out : (options.game.height - height - padding_out);
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 auto travel_dist = height + PADDING_OUT_;
auto offset = 0;
if (options.notifications.getVerticalPosition() == "TOP")
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{
offset = !notifications_.empty() ? notifications_.back().y + travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y + notifications_.back().travel_dist : desp_v;
}
else
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
offset = !notifications_.empty() ? notifications_.back().y - travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y - notifications_.back().travel_dist : desp_v;
}
// Crea la notificacion
@@ -225,11 +224,12 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
// 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;
int y_pos = offset + ((options.notifications.getVerticalPosition() == "TOP") ? -travel_dist : travel_dist);
int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -travel_dist : travel_dist);
n.rect = {desp_h, y_pos, width, height};
// Crea la textura
@@ -266,9 +266,9 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
// Dibuja el icono de la notificación
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});
sp->setClip({icon_size * (icon % 10), icon_size * (icon / 10), icon_size, icon_size});
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_});
sp->setClip({ICON_SIZE_ * (icon % 10), ICON_SIZE_ * (icon / 10), ICON_SIZE_, ICON_SIZE_});
sp->render();
}
@@ -277,7 +277,14 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
int iterator = 0;
for (const auto &text : texts)
{
text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
if (text_is == NotificationText::LEFT)
{
text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
}
else if (text_is == NotificationText::CENTER)
{
text_->writeDX(TEXT_CENTER | TEXT_COLOR, width / 2, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, 1, color);
}
++iterator;
}
@@ -302,7 +309,10 @@ void Notifier::clearNotifications()
{
for (auto &notification : notifications_)
{
notification.state = NotificationStatus::FINISHED;
if (notification.can_be_removed)
{
notification.state = NotificationStatus::FINISHED;
}
}
clearFinishedNotifications();