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

View File

@@ -36,7 +36,8 @@ private:
struct notification_t
{
std::string text;
std::string text1;
std::string text2;
int counter;
notification_state_e state;
notification_position_e position;
@@ -77,7 +78,7 @@ public:
~Notify();
// Muestra una notificación de texto por pantalla;
void showText(std::string text1 = "", std::string text2 = "");
void showText(std::string text1 = "", std::string text2 = "", int icon = -1);
// Indica si hay notificaciones activas
bool active();

View File

@@ -370,9 +370,9 @@ void Screen::updateNotifier()
}
// Muestra una notificación de texto por pantalla;
void Screen::showNotification(std::string text1, std::string text2)
void Screen::showNotification(std::string text1, std::string text2, int icon)
{
notify->showText(text1, text2);
notify->showText(text1, text2, icon);
}
// Dibuja las notificaciones
@@ -383,9 +383,9 @@ void Screen::renderNotifications()
return;
}
// SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
notify->render();
// SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
}
// Establece el tamaño de las notificaciones

View File

@@ -132,7 +132,7 @@ public:
void updateNotifier();
// Muestra una notificación de texto por pantalla;
void showNotification(std::string text1 = "", std::string text2 = "");
void showNotification(std::string text1 = "", std::string text2 = "", int icon = -1);
};
#endif

View File

@@ -161,7 +161,19 @@ void Game::checkEventHandler()
break;
case SDL_SCANCODE_F6:
screen->showNotification("1234567890");
screen->showNotification("ACHIEVEMENT UNLOCKED!", "FINISH THE GAME", 2);
break;
case SDL_SCANCODE_F7:
screen->showNotification("NO ICON");
break;
case SDL_SCANCODE_F8:
screen->showNotification("NO ICON", "NO TEXT");
break;
case SDL_SCANCODE_F9:
screen->showNotification("JAILDESIGNER", "LOGGED IN", 1);
break;
#endif