canvi de pc enmig de la enfangà
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
#include "notifier.h"
|
||||
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
||||
#include <string> // Para basic_string, string, char_traits
|
||||
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "screen.h"
|
||||
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
||||
#include <string> // Para string
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "resource.h"
|
||||
#include "options.h"
|
||||
|
||||
// [SINGLETON]
|
||||
Notifier *Notifier::notifier_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Notifier::init(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
|
||||
void Notifier::init(const std::string &icon_file, std::shared_ptr<Text> text)
|
||||
{
|
||||
Notifier::notifier_ = new Notifier(iconFile, bitmapFile, textFile, soundFile);
|
||||
Notifier::notifier_ = new Notifier(icon_file, text);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
@@ -30,45 +34,21 @@ Notifier *Notifier::get()
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Notifier::Notifier(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
|
||||
{
|
||||
// Inicializa variables
|
||||
renderer_ = Screen::get()->getRenderer();
|
||||
bg_color_ = options.notifications.color;
|
||||
wait_time_ = 300;
|
||||
|
||||
// Crea objetos
|
||||
icon_texture_ = new Texture(renderer_, iconFile);
|
||||
text_texture_ = new Texture(renderer_, bitmapFile);
|
||||
text_ = new Text(textFile, text_texture_, renderer_);
|
||||
sound_ = JA_LoadSound(soundFile.c_str());
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Notifier::~Notifier()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
delete text_texture_;
|
||||
delete icon_texture_;
|
||||
delete text_;
|
||||
JA_DeleteSound(sound_);
|
||||
|
||||
for (auto notification : notifications_)
|
||||
{
|
||||
delete notification.sprite;
|
||||
delete notification.texture;
|
||||
}
|
||||
}
|
||||
Notifier::Notifier(std::string icon_file, std::shared_ptr<Text> text)
|
||||
: renderer_(Screen::get()->getRenderer()),
|
||||
icon_texture_(!icon_file.empty() ? std::make_unique<Texture>(renderer_, icon_file) : nullptr),
|
||||
text_(text),
|
||||
bg_color_(options.notifications.color),
|
||||
wait_time_(150),
|
||||
stack_(false),
|
||||
has_icons_(!icon_file.empty()) {}
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void Notifier::render()
|
||||
{
|
||||
if (active())
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
|
||||
{
|
||||
for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it)
|
||||
{
|
||||
it->sprite->render();
|
||||
}
|
||||
notifications_[i].sprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +60,7 @@ void Notifier::update()
|
||||
// Si la notificación anterior está "saliendo", no hagas nada
|
||||
if (i > 0)
|
||||
{
|
||||
if (notifications_[i - 1].state == ns_rising)
|
||||
if (notifications_[i - 1].state == NotificationStatus::RISING)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -93,24 +73,25 @@ void Notifier::update()
|
||||
{
|
||||
if (options.notifications.sound)
|
||||
{
|
||||
if (notifications_[i].state == ns_rising)
|
||||
{ // Reproduce el sonido de la notificación
|
||||
JA_PlaySound(sound_);
|
||||
if (notifications_[i].state == NotificationStatus::RISING)
|
||||
{
|
||||
// Reproduce el sonido de la notificación
|
||||
JA_PlaySound(Resource::get()->getSound("notify.wav"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications_[i].state == ns_rising)
|
||||
if (notifications_[i].state == NotificationStatus::RISING)
|
||||
{
|
||||
const float step = ((float)notifications_[i].counter / notifications_[i].travelDist);
|
||||
const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist);
|
||||
const int alpha = 255 * step;
|
||||
|
||||
if (options.notifications.getVerticalPosition() == "UPPER")
|
||||
if (options.notifications.getVerticalPosition() == "TOP")
|
||||
{
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
else if (options.notifications.getVerticalPosition() == "BOTTOM")
|
||||
else
|
||||
{
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
@@ -118,43 +99,43 @@ void Notifier::update()
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y)
|
||||
{
|
||||
notifications_[i].state = ns_stay;
|
||||
notifications_[i].state = NotificationStatus::STAY;
|
||||
notifications_[i].texture->setAlpha(255);
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications_[i].state == ns_stay)
|
||||
else if (notifications_[i].state == NotificationStatus::STAY)
|
||||
{
|
||||
if (notifications_[i].counter == wait_time_)
|
||||
{
|
||||
notifications_[i].state = ns_vanishing;
|
||||
notifications_[i].state = NotificationStatus::VANISHING;
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications_[i].state == ns_vanishing)
|
||||
else if (notifications_[i].state == NotificationStatus::VANISHING)
|
||||
{
|
||||
|
||||
const float step = (notifications_[i].counter / (float)notifications_[i].travelDist);
|
||||
const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist);
|
||||
const int alpha = 255 * (1 - step);
|
||||
|
||||
if (options.notifications.getVerticalPosition() == "UPPER")
|
||||
if (options.notifications.getVerticalPosition() == "TOP")
|
||||
{
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
else if (options.notifications.getVerticalPosition() == "BOTTOM")
|
||||
else
|
||||
{
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
notifications_[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travelDist)
|
||||
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist)
|
||||
{
|
||||
notifications_[i].state = ns_finished;
|
||||
notifications_[i].state = NotificationStatus::FINISHED;
|
||||
}
|
||||
}
|
||||
|
||||
notifications_[i].sprite->setRect(notifications_[i].rect);
|
||||
notifications_[i].sprite->setPosition(notifications_[i].rect);
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
@@ -165,87 +146,95 @@ void Notifier::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications_[i].state == ns_finished)
|
||||
if (notifications_[i].state == NotificationStatus::FINISHED)
|
||||
{
|
||||
delete notifications_[i].sprite;
|
||||
delete notifications_[i].texture;
|
||||
notifications_.erase(notifications_.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Muestra una notificación de texto por pantalla;
|
||||
void Notifier::show(std::string text1, std::string text2, int icon)
|
||||
void Notifier::show(std::vector<std::string> texts, int icon, const std::string &code)
|
||||
{
|
||||
// Si no hay texto, acaba
|
||||
if (texts.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Si las notificaciones no se apilan, elimina las anteriores
|
||||
if (!stack_)
|
||||
{
|
||||
clearNotifications();
|
||||
}
|
||||
|
||||
// Elimina las cadenas vacías
|
||||
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s)
|
||||
{ return s.empty(); }),
|
||||
texts.end());
|
||||
|
||||
// Encuentra la cadena más larga
|
||||
std::string longest;
|
||||
for (const auto &text : texts)
|
||||
{
|
||||
if (text.length() > longest.length())
|
||||
longest = text;
|
||||
}
|
||||
|
||||
// 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) + (padding * 2) + iconSpace;
|
||||
const int height = (text_->getCharacterSize() * 2) + (padding * 2);
|
||||
constexpr int icon_size = 16;
|
||||
constexpr int padding_out = 1;
|
||||
const auto padding_in_h = text_->getCharacterSize();
|
||||
const auto padding_in_v = text_->getCharacterSize() / 2;
|
||||
const int icon_space = icon >= 0 ? icon_size + padding_in_h : 0;
|
||||
const int width = text_->lenght(longest) + (padding_in_h * 2) + icon_space;
|
||||
const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
|
||||
const auto shape = NotificationShape::SQUARED;
|
||||
|
||||
// Posición horizontal
|
||||
int despH = 0;
|
||||
auto desp_h = 0;
|
||||
if (options.notifications.getHorizontalPosition() == "LEFT")
|
||||
{
|
||||
despH = padding;
|
||||
desp_h = padding_out;
|
||||
}
|
||||
else if (options.notifications.getHorizontalPosition() == "CENTER")
|
||||
{
|
||||
despH = (options.game.width - width) / 2;
|
||||
desp_h = ((param.game.width / 2) - (width / 2));
|
||||
}
|
||||
else if (options.notifications.getHorizontalPosition() == "RIGHT")
|
||||
else
|
||||
{
|
||||
despH = options.game.width - width - padding;
|
||||
desp_h = param.game.width - width - padding_out;
|
||||
}
|
||||
|
||||
// Posición vertical
|
||||
int despV = 0;
|
||||
if (options.notifications.getVerticalPosition() == "UPPER")
|
||||
{
|
||||
despV = padding;
|
||||
}
|
||||
else
|
||||
{
|
||||
despV = options.game.height - height - padding;
|
||||
}
|
||||
|
||||
const int travelDist = height + padding;
|
||||
const int desp_v = (param.notification.pos_v == NotifyPosition::TOP) ? padding_out : (param.game.height - height - padding_out);
|
||||
|
||||
// Offset
|
||||
int offset = 0;
|
||||
if (options.notifications.getVerticalPosition() == "UPPER")
|
||||
const auto travel_dist = height + padding_out;
|
||||
auto offset = 0;
|
||||
if (param.notification.pos_v == NotifyPosition::TOP)
|
||||
{
|
||||
offset = static_cast<int>(notifications_.size()) > 0 ? notifications_.back().y + travelDist : despV;
|
||||
offset = !notifications_.empty() ? notifications_.back().y + travel_dist : desp_v;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = static_cast<int>(notifications_.size()) > 0 ? notifications_.back().y - travelDist : despV;
|
||||
offset = !notifications_.empty() ? notifications_.back().y - travel_dist : desp_v;
|
||||
}
|
||||
|
||||
// Crea la notificacion
|
||||
notification_t n;
|
||||
Notification n;
|
||||
|
||||
// Inicializa variables
|
||||
n.code = code;
|
||||
n.y = offset;
|
||||
n.travelDist = travelDist;
|
||||
n.counter = 0;
|
||||
n.state = ns_rising;
|
||||
n.text1 = text1;
|
||||
n.text2 = text2;
|
||||
if (options.notifications.getVerticalPosition() == "UPPER")
|
||||
{
|
||||
n.rect = {despH, offset - travelDist, width, height};
|
||||
}
|
||||
else
|
||||
{
|
||||
n.rect = {despH, offset + travelDist, width, height};
|
||||
}
|
||||
n.travel_dist = travel_dist;
|
||||
n.texts = texts;
|
||||
n.shape = shape;
|
||||
auto y_pos = offset + (param.notification.pos_v == NotifyPosition::TOP ? -travel_dist : travel_dist);
|
||||
n.rect = {desp_h, y_pos, width, height};
|
||||
|
||||
// Crea la textura
|
||||
n.texture = new Texture(renderer_);
|
||||
n.texture->createBlank(renderer_, width, height, SDL_TEXTUREACCESS_TARGET);
|
||||
n.texture = std::make_shared<Texture>(renderer_);
|
||||
n.texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
|
||||
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Prepara para dibujar en la textura
|
||||
@@ -254,49 +243,78 @@ void Notifier::show(std::string text1, std::string text2, int icon)
|
||||
// Dibuja el fondo de la notificación
|
||||
SDL_SetRenderDrawColor(renderer_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
|
||||
SDL_Rect rect;
|
||||
rect = {4, 0, width - (4 * 2), height};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
if (shape == NotificationShape::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);
|
||||
rect = {0, 4, width, height - (4 * 2)};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
}
|
||||
|
||||
else if (shape == NotificationShape::SQUARED)
|
||||
{
|
||||
SDL_RenderClear(renderer_);
|
||||
}
|
||||
|
||||
// Dibuja el icono de la notificación
|
||||
if (icon >= 0)
|
||||
if (has_icons_ && icon >= 0 && texts.size() >= 2)
|
||||
{
|
||||
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, icon_texture_, renderer_);
|
||||
sp->setPos({padding, padding, iconSize, iconSize});
|
||||
sp->setClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
|
||||
auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size});
|
||||
sp->setPosition({padding_in_h, padding_in_v, icon_size, icon_size});
|
||||
sp->setSpriteClip({icon_size * (icon % 10), icon_size * (icon / 10), icon_size, icon_size});
|
||||
sp->render();
|
||||
delete sp;
|
||||
}
|
||||
|
||||
// Escribe el texto de la notificación
|
||||
Color color = {255, 255, 255};
|
||||
if (text2 != "")
|
||||
{ // Dos lineas de texto
|
||||
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 + iconSpace, (height / 2) - (text_->getCharacterSize() / 2), text1, color);
|
||||
const Color color{255, 255, 255};
|
||||
int iterator = 0;
|
||||
for (const auto &text : texts)
|
||||
{
|
||||
text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
|
||||
++iterator;
|
||||
}
|
||||
|
||||
// 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 = std::make_shared<Sprite>(n.texture, n.rect);
|
||||
|
||||
// Deja la notificación invisible
|
||||
n.texture->setAlpha(0);
|
||||
|
||||
// Añade la notificación a la lista
|
||||
notifications_.push_back(n);
|
||||
notifications_.emplace_back(n);
|
||||
}
|
||||
|
||||
// Indica si hay notificaciones activas
|
||||
bool Notifier::isActive() { return !notifications_.empty(); }
|
||||
|
||||
// Finaliza y elimnina todas las notificaciones activas
|
||||
void Notifier::clearNotifications()
|
||||
{
|
||||
for (auto ¬ification : notifications_)
|
||||
{
|
||||
notification.state = NotificationStatus::FINISHED;
|
||||
}
|
||||
|
||||
clearFinishedNotifications();
|
||||
}
|
||||
|
||||
// Obtiene los códigos de las notificaciones
|
||||
std::vector<std::string> Notifier::getCodes()
|
||||
{
|
||||
std::vector<std::string> codes;
|
||||
for (const auto ¬ification : notifications_)
|
||||
{
|
||||
codes.emplace_back(notification.code);
|
||||
}
|
||||
return codes;
|
||||
}
|
||||
Reference in New Issue
Block a user