Cambiado el tamaño de las notificaciones. Posibilidad de añadir un icono

This commit is contained in:
2022-12-29 11:36:44 +01:00
parent 510a6ca718
commit b2061c86d2
5 changed files with 66 additions and 41 deletions

View File

@@ -126,13 +126,15 @@ void Notify::clearFinishedNotifications()
}
// Muestra una notificación de texto por pantalla;
void Notify::showText(std::string text1, std::string text2)
void Notify::showText(std::string text1, std::string text2, int icon)
{
// Inicializa variables
const int iconSize = 16;
const int padding = text->getCharacterSize();
const int iconSpace = icon >= 0 ? iconSize + padding : 0;
const std::string txt = text1.length() > text2.length() ? text1 : text2;
const int width = text->lenght(txt) + text->getCharacterSize() + 16 + 4;
const int height = text->getCharacterSize() * 3;
const int padding = text->getCharacterSize() / 2;
const int width = text->lenght(txt) + (padding * 2) + iconSpace;
const int height = (text->getCharacterSize() * 2) + (padding * 2);
// Posición horizontal
int despH = 0;
@@ -181,7 +183,8 @@ void Notify::showText(std::string text1, std::string text2)
n.travelDist = travelDist;
n.counter = 0;
n.state = ns_rising;
n.text = text1;
n.text1 = text1;
n.text2 = text2;
if (options->notifications.posV == pos_top)
{
n.rect = {despH, offset - travelDist, width, height};
@@ -191,46 +194,55 @@ void Notify::showText(std::string text1, std::string text2)
n.rect = {despH, offset + travelDist, width, height};
}
// Prepara el sprite con el icono
Sprite *sp = new Sprite({0, 0, 16, 16}, iconTexture, renderer);
sp->setPos({padding, padding, 16, 16});
sp->setSpriteClip({16 * 2, 0, 16, 16});
// Crea la textura
n.texture = new Texture(renderer);
n.texture->createBlank(renderer, width, height, SDL_TEXTUREACCESS_TARGET);
n.texture->setAsRenderTarget(renderer);
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
// SDL_RenderClear(renderer);
SDL_Rect rect;
rect = {padding, 0, width - (padding * 2), height};
SDL_RenderFillRect(renderer, &rect);
rect = {padding / 2, 1, width - padding, height - 2};
SDL_RenderFillRect(renderer, &rect);
rect = {1, padding / 2, width - 2, height - padding};
SDL_RenderFillRect(renderer, &rect);
rect = {0, padding, width, height - (padding * 2)};
SDL_RenderFillRect(renderer, &rect);
sp->render();
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Prepara para dibujar en la textura
n.texture->setAsRenderTarget(renderer);
// Dibuja el fondo de la notificación
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_Rect rect;
rect = {4, 0, width - (4 * 2), height};
SDL_RenderFillRect(renderer, &rect);
rect = {4 / 2, 1, width - 4, height - 2};
SDL_RenderFillRect(renderer, &rect);
rect = {1, 4 / 2, width - 2, height - 4};
SDL_RenderFillRect(renderer, &rect);
rect = {0, 4, width, height - (4 * 2)};
SDL_RenderFillRect(renderer, &rect);
// Dibuja el icono de la notificación
if (icon >= 0)
{
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture, renderer);
sp->setPos({padding, padding, iconSize, iconSize});
sp->setSpriteClip({iconSize * icon, 0, iconSize, iconSize});
sp->render();
delete sp;
}
// Escribe el texto de la notificación
color_t color = {255, 255, 255};
if (text2 != "")
{ // Dos lineas de texto
text->writeColored(padding + 16 + 4, padding, text1, color);
text->writeColored(padding + 16 + 4, padding + text->getCharacterSize() + 1, text2, color);
text->writeColored(padding + iconSpace, padding, text1, color);
text->writeColored(padding + iconSpace, padding + text->getCharacterSize() + 1, text2, color);
}
else
{ // Una linea de texto
text->writeColored(padding + 16 + 4, (height / 2) - (text->getCharacterSize() / 2), text1, color);
text->writeColored(padding + iconSpace, (height / 2) - (text->getCharacterSize() / 2), text1, color);
}
SDL_SetRenderTarget(renderer, nullptr);
delete sp;
// Crea el sprite
// Deja de dibujar en la textura
SDL_SetRenderTarget(renderer, nullptr);
// Crea el sprite de la notificación
n.sprite = new Sprite(n.rect, n.texture, renderer);
// Añade la notificación a la lista