pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
#include "notifier.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_RenderFillRect, SDL_FRect, SDL_RenderClear
|
||||
#include <SDL3/SDL.h> // Para SDL_RenderFillRect, SDL_FRect, SDL_RenderClear
|
||||
|
||||
#include <algorithm> // Para remove_if
|
||||
#include <string> // Para basic_string, string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "audio.h" // Para Audio
|
||||
#include "param.h" // Para Param, param, ParamNotification, ParamGame
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
#include "audio.h" // Para Audio
|
||||
#include "param.h" // Para Param, param, ParamNotification, ParamGame
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "text.h" // Para Text
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Singleton
|
||||
Notifier *Notifier::instance_ = nullptr;
|
||||
@@ -35,24 +36,18 @@ Notifier::Notifier(std::string icon_file, std::shared_ptr<Text> text)
|
||||
has_icons_(!icon_file.empty()) {}
|
||||
|
||||
// Dibuja las notificaciones por pantalla
|
||||
void Notifier::render()
|
||||
{
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
|
||||
{
|
||||
void Notifier::render() {
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i) {
|
||||
notifications_[i].sprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las notificaiones
|
||||
void Notifier::update()
|
||||
{
|
||||
for (int i = 0; i < (int)notifications_.size(); ++i)
|
||||
{
|
||||
void Notifier::update() {
|
||||
for (int i = 0; i < (int)notifications_.size(); ++i) {
|
||||
// Si la notificación anterior está "saliendo", no hagas nada
|
||||
if (i > 0)
|
||||
{
|
||||
if (notifications_[i - 1].state == NotificationStatus::RISING)
|
||||
{
|
||||
if (i > 0) {
|
||||
if (notifications_[i - 1].state == NotificationStatus::RISING) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -60,12 +55,9 @@ void Notifier::update()
|
||||
notifications_[i].counter++;
|
||||
|
||||
// Hace sonar la notificación en el primer frame
|
||||
if (notifications_[i].counter == 1)
|
||||
{
|
||||
if (param.notification.sound)
|
||||
{
|
||||
if (notifications_[i].state == NotificationStatus::RISING)
|
||||
{
|
||||
if (notifications_[i].counter == 1) {
|
||||
if (param.notification.sound) {
|
||||
if (notifications_[i].state == NotificationStatus::RISING) {
|
||||
// Reproduce el sonido de la notificación
|
||||
Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
|
||||
}
|
||||
@@ -73,55 +65,41 @@ void Notifier::update()
|
||||
}
|
||||
|
||||
// Comprueba los estados
|
||||
if (notifications_[i].state == NotificationStatus::RISING)
|
||||
{
|
||||
if (notifications_[i].state == NotificationStatus::RISING) {
|
||||
const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist);
|
||||
const int alpha = 255 * step;
|
||||
|
||||
if (param.notification.pos_v == NotifyPosition::TOP)
|
||||
{
|
||||
if (param.notification.pos_v == NotifyPosition::TOP) {
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
notifications_[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y)
|
||||
{
|
||||
if (notifications_[i].rect.y == notifications_[i].y) {
|
||||
notifications_[i].state = NotificationStatus::STAY;
|
||||
notifications_[i].texture->setAlpha(255);
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (notifications_[i].state == NotificationStatus::STAY)
|
||||
{
|
||||
if (notifications_[i].counter == wait_time_)
|
||||
{
|
||||
else if (notifications_[i].state == NotificationStatus::STAY) {
|
||||
if (notifications_[i].counter == wait_time_) {
|
||||
notifications_[i].state = NotificationStatus::VANISHING;
|
||||
notifications_[i].counter = 0;
|
||||
}
|
||||
}
|
||||
else if (notifications_[i].state == NotificationStatus::VANISHING)
|
||||
{
|
||||
|
||||
} else if (notifications_[i].state == NotificationStatus::VANISHING) {
|
||||
const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist);
|
||||
const int alpha = 255 * (1 - step);
|
||||
|
||||
if (param.notification.pos_v == NotifyPosition::TOP)
|
||||
{
|
||||
if (param.notification.pos_v == NotifyPosition::TOP) {
|
||||
notifications_[i].rect.y--;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
notifications_[i].rect.y++;
|
||||
}
|
||||
notifications_[i].texture->setAlpha(alpha);
|
||||
|
||||
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist)
|
||||
{
|
||||
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) {
|
||||
notifications_[i].state = NotificationStatus::FINISHED;
|
||||
}
|
||||
}
|
||||
@@ -133,40 +111,32 @@ void Notifier::update()
|
||||
}
|
||||
|
||||
// Elimina las notificaciones finalizadas
|
||||
void Notifier::clearFinishedNotifications()
|
||||
{
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (notifications_[i].state == NotificationStatus::FINISHED)
|
||||
{
|
||||
void Notifier::clearFinishedNotifications() {
|
||||
for (int i = (int)notifications_.size() - 1; i >= 0; --i) {
|
||||
if (notifications_[i].state == NotificationStatus::FINISHED) {
|
||||
notifications_.erase(notifications_.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Notifier::show(std::vector<std::string> texts, int icon, const std::string &code)
|
||||
{
|
||||
void Notifier::show(std::vector<std::string> texts, int icon, const std::string &code) {
|
||||
// Si no hay texto, acaba
|
||||
if (texts.empty())
|
||||
{
|
||||
if (texts.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Si las notificaciones no se apilan, elimina las anteriores
|
||||
if (!stack_)
|
||||
{
|
||||
if (!stack_) {
|
||||
clearAllNotifications();
|
||||
}
|
||||
|
||||
// Elimina las cadenas vacías
|
||||
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s)
|
||||
{ return s.empty(); }),
|
||||
texts.end());
|
||||
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)
|
||||
{
|
||||
for (const auto &text : texts) {
|
||||
if (text.length() > longest.length())
|
||||
longest = text;
|
||||
}
|
||||
@@ -183,23 +153,22 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
|
||||
// Posición horizontal
|
||||
float desp_h = 0;
|
||||
switch (param.notification.pos_h)
|
||||
{
|
||||
case NotifyPosition::LEFT:
|
||||
desp_h = PADDING_OUT;
|
||||
break;
|
||||
switch (param.notification.pos_h) {
|
||||
case NotifyPosition::LEFT:
|
||||
desp_h = PADDING_OUT;
|
||||
break;
|
||||
|
||||
case NotifyPosition::MIDDLE:
|
||||
desp_h = ((param.game.width / 2) - (WIDTH / 2));
|
||||
break;
|
||||
case NotifyPosition::MIDDLE:
|
||||
desp_h = ((param.game.width / 2) - (WIDTH / 2));
|
||||
break;
|
||||
|
||||
case NotifyPosition::RIGHT:
|
||||
desp_h = param.game.width - WIDTH - PADDING_OUT;
|
||||
break;
|
||||
case NotifyPosition::RIGHT:
|
||||
desp_h = param.game.width - WIDTH - PADDING_OUT;
|
||||
break;
|
||||
|
||||
default:
|
||||
desp_h = 0;
|
||||
break;
|
||||
default:
|
||||
desp_h = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Posición vertical
|
||||
@@ -208,8 +177,8 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
// Offset
|
||||
const auto TRAVEL_DIST = HEIGHT + PADDING_OUT;
|
||||
auto OFFSET = notifications_.empty()
|
||||
? DESP_V
|
||||
: notifications_.back().y + (param.notification.pos_v == NotifyPosition::TOP ? TRAVEL_DIST : -TRAVEL_DIST);
|
||||
? DESP_V
|
||||
: notifications_.back().y + (param.notification.pos_v == NotifyPosition::TOP ? TRAVEL_DIST : -TRAVEL_DIST);
|
||||
|
||||
// Crea la notificacion
|
||||
Notification n;
|
||||
@@ -234,8 +203,7 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
// Dibuja el fondo de la notificación
|
||||
SDL_SetRenderDrawColor(renderer_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
|
||||
SDL_FRect rect;
|
||||
if (SHAPE == NotificationShape::ROUNDED)
|
||||
{
|
||||
if (SHAPE == NotificationShape::ROUNDED) {
|
||||
rect = {4, 0, WIDTH - (4 * 2), HEIGHT};
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
|
||||
@@ -249,14 +217,12 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
SDL_RenderFillRect(renderer_, &rect);
|
||||
}
|
||||
|
||||
else if (SHAPE == NotificationShape::SQUARED)
|
||||
{
|
||||
else if (SHAPE == NotificationShape::SQUARED) {
|
||||
SDL_RenderClear(renderer_);
|
||||
}
|
||||
|
||||
// Dibuja el icono de la notificación
|
||||
if (has_icons_ && icon >= 0 && texts.size() >= 2)
|
||||
{
|
||||
if (has_icons_ && icon >= 0 && texts.size() >= 2) {
|
||||
auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_FRect){0, 0, ICON_SIZE, ICON_SIZE});
|
||||
sp->setPosition({PADDING_IN_H, PADDING_IN_V, ICON_SIZE, ICON_SIZE});
|
||||
sp->setSpriteClip(SDL_FRect{
|
||||
@@ -270,8 +236,7 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
// Escribe el texto de la notificación
|
||||
const Color color{255, 255, 255};
|
||||
int iterator = 0;
|
||||
for (const auto &text : texts)
|
||||
{
|
||||
for (const auto &text : texts) {
|
||||
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + iterator * (text_->getCharacterSize() + 1), text, color);
|
||||
++iterator;
|
||||
}
|
||||
@@ -290,10 +255,8 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string
|
||||
}
|
||||
|
||||
// Finaliza y elimnina todas las notificaciones activas
|
||||
void Notifier::clearAllNotifications()
|
||||
{
|
||||
for (auto ¬ification : notifications_)
|
||||
{
|
||||
void Notifier::clearAllNotifications() {
|
||||
for (auto ¬ification : notifications_) {
|
||||
notification.state = NotificationStatus::FINISHED;
|
||||
}
|
||||
|
||||
@@ -301,11 +264,9 @@ void Notifier::clearAllNotifications()
|
||||
}
|
||||
|
||||
// Obtiene los códigos de las notificaciones
|
||||
std::vector<std::string> Notifier::getCodes()
|
||||
{
|
||||
std::vector<std::string> Notifier::getCodes() {
|
||||
std::vector<std::string> codes;
|
||||
for (const auto ¬ification : notifications_)
|
||||
{
|
||||
for (const auto ¬ification : notifications_) {
|
||||
codes.emplace_back(notification.code);
|
||||
}
|
||||
return codes;
|
||||
|
||||
Reference in New Issue
Block a user