afegides notificacions per als inputs externs al joc

This commit is contained in:
2024-08-03 13:55:16 +02:00
parent 256959505d
commit 958a4d1d99
13 changed files with 78 additions and 33 deletions

View File

@@ -10,7 +10,7 @@ Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapF
this->renderer = renderer;
this->options = options;
bgColor = options->notification.color;
waitTime = 300;
waitTime = 100;
// Crea objetos
iconTexture = new Texture(renderer, iconFile);
@@ -151,17 +151,20 @@ 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 int paddingOut = 1;
const int paddingIn = text->getCharacterSize() / 2;
const int iconSpace = icon >= 0 ? iconSize + paddingIn : 0;
const std::string txt = text1.length() > text2.length() ? text1 : text2;
const int width = text->lenght(txt) + (padding * 2) + iconSpace;
const int height = (text->getCharacterSize() * 2) + (padding * 2);
const int width = text->lenght(txt) + (paddingIn * 2) + iconSpace;
const int numTexts = text2 == "" ? 1 : 2;
const int height = (text->getCharacterSize() * numTexts) + (paddingIn * 2);
const notification_shape_t shape = notification_shape_squared;
// Posición horizontal
int despH = 0;
if (options->notification.posH == pos_left)
{
despH = padding;
despH = paddingOut;
}
else if (options->notification.posH == pos_middle)
{
@@ -169,21 +172,21 @@ void Notify::showText(std::string text1, std::string text2, int icon)
}
else
{
despH = options->video.gameWidth - width - padding;
despH = options->video.gameWidth - width - paddingOut;
}
// Posición vertical
int despV = 0;
if (options->notification.posV == pos_top)
{
despV = padding;
despV = paddingOut;
}
else
{
despV = options->video.gameHeight - height - padding;
despV = options->video.gameHeight - height - paddingOut;
}
const int travelDist = height + padding;
const int travelDist = height + paddingOut;
// Offset
int offset = 0;
@@ -206,6 +209,7 @@ void Notify::showText(std::string text1, std::string text2, int icon)
n.state = ns_rising;
n.text1 = text1;
n.text2 = text2;
n.shape = shape;
if (options->notification.posV == pos_top)
{
n.rect = {despH, offset - travelDist, width, height};
@@ -226,23 +230,32 @@ void Notify::showText(std::string text1, std::string text2, int icon)
// 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);
if (shape == notification_shape_rounded)
{
rect = {4, 0, width - (4 * 2), height};
SDL_RenderFillRect(renderer, &rect);
rect = {4 / 2, 1, width - 4, height - 2};
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 = {1, 4 / 2, width - 2, height - 4};
SDL_RenderFillRect(renderer, &rect);
rect = {0, 4, width, height - (4 * 2)};
SDL_RenderFillRect(renderer, &rect);
}
else if (shape == notification_shape_squared)
{
SDL_RenderClear(renderer);
}
rect = {0, 4, width, height - (4 * 2)};
SDL_RenderFillRect(renderer, &rect);
// Dibuja el icono de la notificación
if (icon >= 0)
if (icon >= 0 && numTexts == 2)
{
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture);
sp->setPos({padding, padding, iconSize, iconSize});
sp->setPos({paddingIn, paddingIn, iconSize, iconSize});
sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
sp->render();
delete sp;
@@ -250,21 +263,21 @@ void Notify::showText(std::string text1, std::string text2, int icon)
// Escribe el texto de la notificación
color_t color = {255, 255, 255};
if (text2 != "")
if (numTexts == 2)
{ // Dos lineas de texto
text->writeColored(padding + iconSpace, padding, text1, color);
text->writeColored(padding + iconSpace, padding + text->getCharacterSize() + 1, text2, color);
text->writeColored(paddingIn + iconSpace, paddingIn, text1, color);
text->writeColored(paddingIn + iconSpace, paddingIn + text->getCharacterSize() + 1, text2, color);
}
else
{ // Una linea de texto
text->writeColored(padding + iconSpace, (height / 2) - (text->getCharacterSize() / 2), text1, color);
text->writeColored(paddingIn + iconSpace, paddingIn, text1, color);
}
// 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);
n.sprite = new Sprite(n.rect, n.texture);
// Deja la notificación invisible
n.texture->setAlpha(0);