Trabajando en la animacion de las notificaciones
This commit is contained in:
@@ -7,7 +7,8 @@ Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textF
|
||||
{
|
||||
// Inicializa variables
|
||||
this->renderer = renderer;
|
||||
bgColor = {32, 32, 32};
|
||||
bgColor = {64, 64, 64};
|
||||
waitTime = 300;
|
||||
|
||||
// Crea objetos
|
||||
text = new Text(bitmapFile, textFile, renderer);
|
||||
@@ -41,6 +42,48 @@ void Notify::update()
|
||||
for (int i = 0; i < (int)notifications.size(); ++i)
|
||||
{
|
||||
notifications.at(i).counter++;
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications.at(i).state == ns_rising)
|
||||
{
|
||||
const float step = (notifications.at(i).counter / (float)notifications.at(i).texture->getHeight());
|
||||
const int alpha = 255 * step;
|
||||
|
||||
notifications.at(i).rect.y++;
|
||||
//notifications.at(i).texture->setAlpha(alpha);
|
||||
|
||||
if (notifications.at(i).rect.y == 0)
|
||||
{
|
||||
notifications.at(i).state = ns_stay;
|
||||
//notifications.at(i).texture->setAlpha(255);
|
||||
notifications.at(i).counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications.at(i).state == ns_stay)
|
||||
{
|
||||
if (notifications.at(i).counter == waitTime)
|
||||
{
|
||||
notifications.at(i).state = ns_vanishing;
|
||||
notifications.at(i).counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications.at(i).state == ns_vanishing)
|
||||
{
|
||||
|
||||
const float step = (notifications.at(i).counter / (float)notifications.at(i).texture->getHeight());
|
||||
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).texture->getHeight())
|
||||
{
|
||||
notifications.at(i).state = ns_finished;
|
||||
}
|
||||
}
|
||||
|
||||
notifications.at(i).sprite->setRect(notifications.at(i).rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
@@ -51,7 +94,7 @@ void Notify::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications.at(i).counter >= 300)
|
||||
if (notifications.at(i).state == ns_finished)
|
||||
{
|
||||
delete notifications.at(i).sprite;
|
||||
delete notifications.at(i).texture;
|
||||
@@ -75,6 +118,7 @@ void Notify::showText(std::string text)
|
||||
n.counter = 0;
|
||||
n.state = ns_rising;
|
||||
n.text = text;
|
||||
n.rect = {0, -height, width, height};
|
||||
|
||||
// Crea la textura
|
||||
n.texture = new Texture(renderer);
|
||||
@@ -86,7 +130,7 @@ void Notify::showText(std::string text)
|
||||
this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, desp / 2, text, 1, {255, 255, 255}, 1, {0, 0, 0});
|
||||
|
||||
// Crea el sprite
|
||||
n.sprite = new Sprite({0, 0, width, height}, n.texture, renderer);
|
||||
n.sprite = new Sprite(n.rect, n.texture, renderer);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications.push_back(n);
|
||||
|
||||
@@ -17,7 +17,8 @@ private:
|
||||
{
|
||||
ns_rising,
|
||||
ns_stay,
|
||||
ns_vanishing
|
||||
ns_vanishing,
|
||||
ns_finished
|
||||
};
|
||||
|
||||
struct notification_t
|
||||
@@ -27,6 +28,7 @@ private:
|
||||
notification_state_e state;
|
||||
Texture *texture;
|
||||
Sprite *sprite;
|
||||
SDL_Rect rect;
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
@@ -35,6 +37,7 @@ private:
|
||||
|
||||
// Variables
|
||||
color_t bgColor; // Color de fondo de las notificaciones
|
||||
int waitTime; // Tiempo que se ve la notificación
|
||||
std::vector<notification_t> notifications; // La lista de notificaciones activas
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
@@ -46,7 +49,7 @@ public:
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void update();
|
||||
|
||||
|
||||
// Constructor
|
||||
Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user