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);

View File

@@ -34,6 +34,12 @@ private:
bottomRight
};
enum notification_shape_t
{
notification_shape_rounded,
notification_shape_squared,
};
struct notification_t
{
std::string text1;
@@ -46,6 +52,7 @@ private:
SDL_Rect rect;
int y;
int travelDist;
notification_shape_t shape;
};
// Objetos y punteros

View File

@@ -37,6 +37,9 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *
shakeEffect.origin = 0;
attenuateEffect = false;
// Crea los objetos
notify = new Notify(renderer, asset->get("notify.png"), asset->get("8bithud.png"), asset->get("8bithud.txt"), asset->get("notify.wav"), options);
// Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00};
@@ -53,6 +56,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *
// Destructor
Screen::~Screen()
{
delete notify;
SDL_DestroyTexture(gameCanvas);
}
@@ -232,7 +236,7 @@ void Screen::setVideoMode(int videoMode)
// Camibia entre pantalla completa y ventana
void Screen::switchVideoMode()
{
options->video.mode = (options->video.mode == VIDEO_MODE_WINDOW) ? VIDEO_MODE_FULLSCREEN : VIDEO_MODE_WINDOW;
options->video.mode = options->video.mode == VIDEO_MODE_WINDOW ? VIDEO_MODE_FULLSCREEN : VIDEO_MODE_WINDOW;
setVideoMode(options->video.mode);
}
@@ -309,21 +313,29 @@ void Screen::checkInput()
if (input->checkInput(input_window_fullscreen, DO_NOT_ALLOW_REPEAT))
{
switchVideoMode();
const std::string mode = options->video.mode == VIDEO_MODE_WINDOW ? "Window" : "Fullscreen";
showNotification(mode + " mode");
}
else if (input->checkInput(input_window_dec_size, DO_NOT_ALLOW_REPEAT))
{
decWindowSize();
const std::string size = std::to_string(options->video.window.size);
showNotification("Window size x" + size);
}
else if (input->checkInput(input_window_inc_size, DO_NOT_ALLOW_REPEAT))
{
incWindowSize();
const std::string size = std::to_string(options->video.window.size);
showNotification("Window size x" + size);
}
else if (input->checkInput(input_video_shaders, DO_NOT_ALLOW_REPEAT))
{
switchShaders();
const std::string value = options->video.shaders ? "on" : "off";
showNotification("Shaders " + value);
}
}