Quan canvia de RendererSurface, torna a deixar la que havia, no la de per defecte

This commit is contained in:
2025-03-06 12:09:59 +01:00
parent 60c926a9c9
commit f2bfc970bc
11 changed files with 75 additions and 59 deletions

View File

@@ -183,13 +183,13 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
}
// Inicializa variables
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;
text_is = icon_space > 0 ? NotificationText::LEFT : text_is;
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;
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;
text_is = ICON_SPACE > 0 ? NotificationText::LEFT : text_is;
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;
@@ -199,25 +199,25 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
}
else if (options.notifications.getHorizontalPosition() == NotificationPosition::CENTER)
{
desp_h = ((options.game.width / 2) - (width / 2));
desp_h = ((options.game.width / 2) - (WIDTH / 2));
}
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() == NotificationPosition::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() == NotificationPosition::TOP)
{
offset = !notifications_.empty() ? notifications_.back().y + notifications_.back().travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y + notifications_.back().travel_dist : DESP_V;
}
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
offset = !notifications_.empty() ? notifications_.back().y - notifications_.back().travel_dist : desp_v;
offset = !notifications_.empty() ? notifications_.back().y - notifications_.back().travel_dist : DESP_V;
}
// Crea la notificacion
@@ -227,36 +227,37 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
n.code = code;
n.can_be_removed = can_be_removed;
n.y = offset;
n.travel_dist = travel_dist;
n.travel_dist = TRAVEL_DIST;
n.texts = texts;
n.shape = shape;
int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -travel_dist : travel_dist);
n.rect = {desp_h, y_pos, width, height};
n.shape = SHAPE;
int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST);
n.rect = {desp_h, y_pos, WIDTH, HEIGHT};
// Crea la textura
n.surface = std::make_shared<Surface>(width, height);
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_Rect rect;
if (shape == NotificationShape::ROUNDED)
if (SHAPE == NotificationShape::ROUNDED)
{
rect = {4, 0, width - (4 * 2), height};
rect = {4, 0, WIDTH - (4 * 2), HEIGHT};
n.surface->fillRect(&rect, bg_color_);
rect = {4 / 2, 1, width - 4, height - 2};
rect = {4 / 2, 1, WIDTH - 4, HEIGHT - 2};
n.surface->fillRect(&rect, bg_color_);
rect = {1, 4 / 2, width - 2, height - 4};
rect = {1, 4 / 2, WIDTH - 2, HEIGHT - 4};
n.surface->fillRect(&rect, bg_color_);
rect = {0, 4, width, height - (4 * 2)};
rect = {0, 4, WIDTH, HEIGHT - (4 * 2)};
n.surface->fillRect(&rect, bg_color_);
}
else if (shape == NotificationShape::SQUARED)
else if (SHAPE == NotificationShape::SQUARED)
{
n.surface->clear(bg_color_);
}
@@ -265,36 +266,37 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
if (has_icons_ && icon >= 0 && texts.size() >= 2)
{
auto sp = std::make_unique<SSprite>(icon_surface_, (SDL_Rect){0, 0, ICON_SIZE_, ICON_SIZE_});
sp->setPosition({padding_in_h, padding_in_v, 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();
}
// Escribe el texto de la notificación
const Uint8 color = stringToColor("white");
const Uint8 COLOR = stringToColor("white");
int iterator = 0;
for (const auto &text : texts)
{
if (text_is == NotificationText::LEFT)
switch (text_is)
{
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);
case NotificationText::LEFT:
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + iterator * (text_->getCharacterSize() + 1), text, COLOR);
break;
case NotificationText::CENTER:
text_->writeDX(TEXT_CENTER | TEXT_COLOR, WIDTH / 2, PADDING_IN_V + iterator * (text_->getCharacterSize() + 1), text, 1, COLOR);
break;
default:
break;
}
++iterator;
}
// Deja de dibujar en la textura
Screen::get()->setRendererSurface(nullptr);
Screen::get()->setRendererSurface(previuos_renderer);
// Crea el sprite de la notificación
n.sprite = std::make_shared<SSprite>(n.surface, n.rect);
// Deja la notificación invisible
// n.surface->setAlpha(0);
// Añade la notificación a la lista
notifications_.emplace_back(n);
}