Actualizando las clases comunes a sus ultimas versiones
This commit is contained in:
@@ -1,24 +1,30 @@
|
||||
#include "notify.h"
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile)
|
||||
Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
|
||||
{
|
||||
// Inicializa variables
|
||||
this->renderer = renderer;
|
||||
bgColor = {64, 64, 64};
|
||||
this->options = options;
|
||||
bgColor = options->notifications.color;
|
||||
waitTime = 300;
|
||||
|
||||
// Crea objetos
|
||||
text = new Text(bitmapFile, textFile, renderer);
|
||||
texture = new Texture(renderer, bitmapFile);
|
||||
text = new Text(textFile, texture, renderer);
|
||||
sound = JA_LoadSound(soundFile.c_str());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Notify::~Notify()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete texture;
|
||||
delete text;
|
||||
JA_DeleteSound(sound);
|
||||
|
||||
for (auto notification : notifications)
|
||||
{
|
||||
@@ -32,7 +38,7 @@ void Notify::render()
|
||||
{
|
||||
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||
{
|
||||
notifications.at(i).sprite->render();
|
||||
notifications[i].sprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,49 +47,63 @@ void Notify::update()
|
||||
{
|
||||
for (int i = 0; i < (int)notifications.size(); ++i)
|
||||
{
|
||||
notifications.at(i).counter++;
|
||||
notifications[i].counter++;
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications.at(i).state == ns_rising)
|
||||
if (notifications[i].state == ns_rising)
|
||||
{
|
||||
const float step = ((float)notifications.at(i).counter / notifications.at(i).travelDist);
|
||||
const float step = ((float)notifications[i].counter / notifications[i].travelDist);
|
||||
const int alpha = 255 * step;
|
||||
|
||||
notifications.at(i).rect.y++;
|
||||
notifications.at(i).texture->setAlpha(alpha);
|
||||
|
||||
if (notifications.at(i).rect.y == notifications.at(i).y)
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
notifications.at(i).state = ns_stay;
|
||||
notifications.at(i).texture->setAlpha(255);
|
||||
notifications.at(i).counter = 0;
|
||||
notifications[i].rect.y++;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications[i].rect.y--;
|
||||
}
|
||||
notifications[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications[i].rect.y == notifications[i].y)
|
||||
{
|
||||
notifications[i].state = ns_stay;
|
||||
notifications[i].texture->setAlpha(255);
|
||||
notifications[i].counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications.at(i).state == ns_stay)
|
||||
else if (notifications[i].state == ns_stay)
|
||||
{
|
||||
if (notifications.at(i).counter == waitTime)
|
||||
if (notifications[i].counter == waitTime)
|
||||
{
|
||||
notifications.at(i).state = ns_vanishing;
|
||||
notifications.at(i).counter = 0;
|
||||
notifications[i].state = ns_vanishing;
|
||||
notifications[i].counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications.at(i).state == ns_vanishing)
|
||||
else if (notifications[i].state == ns_vanishing)
|
||||
{
|
||||
|
||||
const float step = (notifications.at(i).counter / (float)notifications.at(i).travelDist);
|
||||
const float step = (notifications[i].counter / (float)notifications[i].travelDist);
|
||||
const int alpha = 255 * (1 - step);
|
||||
|
||||
notifications.at(i).rect.y--;
|
||||
notifications.at(i).texture->setAlpha(alpha);
|
||||
|
||||
if (notifications.at(i).rect.y == notifications.at(i).y - notifications.at(i).travelDist)
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
notifications.at(i).state = ns_finished;
|
||||
notifications[i].rect.y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications[i].rect.y++;
|
||||
}
|
||||
notifications[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications[i].rect.y == notifications[i].y - notifications[i].travelDist)
|
||||
{
|
||||
notifications[i].state = ns_finished;
|
||||
}
|
||||
}
|
||||
|
||||
notifications.at(i).sprite->setRect(notifications.at(i).rect);
|
||||
notifications[i].sprite->setRect(notifications[i].rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
@@ -94,10 +114,10 @@ void Notify::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications.at(i).state == ns_finished)
|
||||
if (notifications[i].state == ns_finished)
|
||||
{
|
||||
delete notifications.at(i).sprite;
|
||||
delete notifications.at(i).texture;
|
||||
delete notifications[i].sprite;
|
||||
delete notifications[i].texture;
|
||||
notifications.erase(notifications.begin() + i);
|
||||
}
|
||||
}
|
||||
@@ -106,24 +126,67 @@ void Notify::clearFinishedNotifications()
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void Notify::showText(std::string text)
|
||||
{
|
||||
// Crea constantes
|
||||
// Inicializa variables
|
||||
const int width = this->text->lenght(text) + (this->text->getCharacterSize() * 2);
|
||||
const int height = this->text->getCharacterSize() * 2;
|
||||
const int despH = this->text->getCharacterSize() / 2;
|
||||
const int despV = despH;
|
||||
const int travelDist = height + despV;
|
||||
const int offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
|
||||
const int padding = (this->text->getCharacterSize() / 2);
|
||||
|
||||
// Posición horizontal
|
||||
int despH = 0;
|
||||
if (options->notifications.posH == pos_left)
|
||||
{
|
||||
despH = padding;
|
||||
}
|
||||
else if (options->notifications.posH == pos_middle)
|
||||
{
|
||||
despH = ((options->screen.windowWidth * options->windowSize) / 2 - (width / 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
despH = (options->screen.windowWidth * options->windowSize) - width - padding;
|
||||
}
|
||||
|
||||
// Posición vertical
|
||||
int despV = 0;
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
despV = padding;
|
||||
}
|
||||
else
|
||||
{
|
||||
despV = (options->screen.windowHeight * options->windowSize) - height - padding;
|
||||
}
|
||||
|
||||
const int travelDist = height + padding;
|
||||
|
||||
// Offset
|
||||
int offset = 0;
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV;
|
||||
}
|
||||
|
||||
// Crea la notificacion
|
||||
notification_t n;
|
||||
|
||||
// inicializa variables
|
||||
// Inicializa variables
|
||||
n.y = offset;
|
||||
n.travelDist = travelDist;
|
||||
n.counter = 0;
|
||||
n.state = ns_rising;
|
||||
n.text = text;
|
||||
n.rect = {despH, offset - travelDist, width, height};
|
||||
if (options->notifications.posV == pos_top)
|
||||
{
|
||||
n.rect = {despH, offset - travelDist, width, height};
|
||||
}
|
||||
else
|
||||
{
|
||||
n.rect = {despH, offset + travelDist, width, height};
|
||||
}
|
||||
|
||||
// Crea la textura
|
||||
n.texture = new Texture(renderer);
|
||||
@@ -132,13 +195,20 @@ void Notify::showText(std::string text)
|
||||
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, despV, text, 1, {255, 255, 255}, 1, {0, 0, 0});
|
||||
this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, padding, text, 1, {255, 255, 255}, 1, {0, 0, 0});
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
// Crea el sprite
|
||||
n.sprite = new Sprite(n.rect, n.texture, renderer);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications.push_back(n);
|
||||
|
||||
// Reproduce el sonido de la notificación
|
||||
if (options->notifications.sound)
|
||||
{
|
||||
JA_PlaySound(sound);
|
||||
}
|
||||
}
|
||||
|
||||
// Indica si hay notificaciones activas
|
||||
@@ -148,6 +218,6 @@ bool Notify::active()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user