Les notificacions ara van per milisegons.

posibilitat d'establir el tems de una notificacio (util per a allargar les dels logros)
This commit is contained in:
2025-03-08 11:29:25 +01:00
parent fdb85ac393
commit 195d812d2a
4 changed files with 88 additions and 99 deletions

View File

@@ -267,16 +267,9 @@ void Game::checkDebugEvents(const SDL_Event &event)
break; break;
case SDL_SCANCODE_7: case SDL_SCANCODE_7:
Notifier::get()->show({"ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS"}, NotificationText::CENTER, -1, false, "F7"); Notifier::get()->show({"ACHIEVEMENT UNLOCKED!", "I LIKE MY MULTICOLOURED FRIENDS"}, NotificationText::CENTER, CHEEVO_NOTIFICATION_DURATION, -1, false, "F7");
break; break;
case SDL_SCANCODE_8:
Notifier::get()->show({"JAILDESIGNER", "IS LOGGED IN"}, NotificationText::LEFT, 4, false);
break;
case SDL_SCANCODE_9:
Notifier::get()->show({"JAILDESIGNER", "IS LOGGED IN"}, NotificationText::LEFT, 5, false);
break;
default: default:
break; break;
} }

View File

@@ -21,7 +21,7 @@ namespace globalInputs
else else
{ {
// Si la notificación de salir no está activa, muestra la notificación // Si la notificación de salir no está activa, muestra la notificación
Notifier::get()->show({code}, NotificationText::CENTER, -1, true, code); Notifier::get()->show({code}, NotificationText::CENTER, 2000, -1, true, code);
} }
} }

View File

@@ -39,7 +39,6 @@ Notifier::Notifier(const std::string &icon_file, const std::string &text)
: icon_surface_(!icon_file.empty() ? Resource::get()->getSurface(icon_file) : nullptr), : icon_surface_(!icon_file.empty() ? Resource::get()->getSurface(icon_file) : nullptr),
text_(Resource::get()->getText(text)), text_(Resource::get()->getText(text)),
bg_color_(options.notifications.color), bg_color_(options.notifications.color),
wait_time_(150),
stack_(false), stack_(false),
has_icons_(!icon_file.empty()) {} has_icons_(!icon_file.empty()) {}
@@ -66,75 +65,47 @@ void Notifier::update()
} }
} }
notifications_[i].counter++; switch (notifications_[i].state)
// Hace sonar la notificación en el primer frame
if (notifications_[i].counter == 1)
{ {
if (options.notifications.sound) case NotificationStatus::RISING:
{
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 == NotificationStatus::RISING)
{ {
// const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist); const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
// const int alpha = 255 * step; notifications_[i].rect.y += DIRECTION;
// constexpr int ALPHA = 255;
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{
notifications_[i].rect.y++;
}
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
notifications_[i].rect.y--;
}
// notifications_[i].surface->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].state = NotificationStatus::STAY;
// notifications_[i].surface->setAlpha(255); notifications_[i].start_time = SDL_GetTicks();
notifications_[i].counter = 0;
} }
break;
} }
case NotificationStatus::STAY:
else if (notifications_[i].state == NotificationStatus::STAY)
{ {
if (notifications_[i].counter == wait_time_) notifications_[i].elapsed_time = SDL_GetTicks() - notifications_[i].start_time;
if (notifications_[i].elapsed_time >= notifications_[i].display_duration)
{ {
notifications_[i].state = NotificationStatus::VANISHING; notifications_[i].state = NotificationStatus::VANISHING;
notifications_[i].counter = 0;
} }
break;
} }
else if (notifications_[i].state == NotificationStatus::VANISHING)
case NotificationStatus::VANISHING:
{ {
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1;
// const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist); notifications_[i].rect.y += DIRECTION;
// const int ALPHA = 255 * (1 - step);
// constexpr int ALPHA = 255;
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{
notifications_[i].rect.y--;
}
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
notifications_[i].rect.y++;
}
// notifications_[i].surface->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; notifications_[i].state = NotificationStatus::FINISHED;
} }
break;
}
case NotificationStatus::FINISHED:
break;
default:
break;
} }
notifications_[i].sprite->setPosition(notifications_[i].rect); notifications_[i].sprite->setPosition(notifications_[i].rect);
@@ -155,7 +126,7 @@ void Notifier::clearFinishedNotifications()
} }
} }
void Notifier::show(std::vector<std::string> texts, NotificationText text_is, int icon, bool can_be_removed, const std::string &code) void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code)
{ {
// Si no hay texto, acaba // Si no hay texto, acaba
if (texts.empty()) if (texts.empty())
@@ -179,11 +150,12 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
for (const auto &text : texts) for (const auto &text : texts)
{ {
if (text.length() > longest.length()) if (text.length() > longest.length())
{
longest = text; longest = text;
}
} }
// Inicializa variables // Inicializa variables
//const int text_size = text_->getCharacterSize();
const int text_size = 6; const int text_size = 6;
const auto PADDING_IN_H = text_size; const auto PADDING_IN_H = text_size;
const auto PADDING_IN_V = text_size / 2; const auto PADDING_IN_V = text_size / 2;
@@ -194,33 +166,34 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
const auto SHAPE = NotificationShape::SQUARED; const auto SHAPE = NotificationShape::SQUARED;
// Posición horizontal // Posición horizontal
auto desp_h = 0; int desp_h = 0;
if (options.notifications.getHorizontalPosition() == NotificationPosition::LEFT) switch (options.notifications.getHorizontalPosition())
{ {
case NotificationPosition::LEFT:
desp_h = PADDING_OUT_; desp_h = PADDING_OUT_;
} break;
else if (options.notifications.getHorizontalPosition() == NotificationPosition::CENTER)
{ case NotificationPosition::CENTER:
desp_h = ((options.game.width / 2) - (WIDTH / 2)); desp_h = ((options.game.width / 2) - (WIDTH / 2));
} break;
else if (options.notifications.getHorizontalPosition() == NotificationPosition::RIGHT)
{ case NotificationPosition::RIGHT:
desp_h = options.game.width - WIDTH - PADDING_OUT_; desp_h = options.game.width - WIDTH - PADDING_OUT_;
break;
default:
desp_h = 0;
break;
} }
// Posición vertical // Posición vertical
const int DESP_V = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? PADDING_OUT_ : options.game.height - HEIGHT - PADDING_OUT_; const int DESP_V = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? PADDING_OUT_ : options.game.height - HEIGHT - PADDING_OUT_;
// Offset // Offset
const auto TRAVEL_DIST = HEIGHT + PADDING_OUT_; const auto TRAVEL_DIST = HEIGHT + PADDING_OUT_;
auto offset = 0; const int TRAVEL_MOD = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP) const int OFFSET = !notifications_.empty() ? notifications_.back().y + TRAVEL_MOD * notifications_.back().travel_dist : DESP_V;
{
offset = !notifications_.empty() ? notifications_.back().y + notifications_.back().travel_dist : DESP_V;
}
else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
{
offset = !notifications_.empty() ? notifications_.back().y - notifications_.back().travel_dist : DESP_V;
}
// Crea la notificacion // Crea la notificacion
Notification n; Notification n;
@@ -228,12 +201,13 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
// Inicializa variables // Inicializa variables
n.code = code; n.code = code;
n.can_be_removed = can_be_removed; n.can_be_removed = can_be_removed;
n.y = offset; n.y = OFFSET;
n.travel_dist = TRAVEL_DIST; n.travel_dist = TRAVEL_DIST;
n.texts = texts; n.texts = texts;
n.shape = SHAPE; n.shape = SHAPE;
int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST); n.display_duration = display_duration;
n.rect = {desp_h, y_pos, WIDTH, HEIGHT}; const int Y_POS = OFFSET + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST);
n.rect = {desp_h, Y_POS, WIDTH, HEIGHT};
// Crea la textura // Crea la textura
n.surface = std::make_shared<Surface>(WIDTH, HEIGHT); n.surface = std::make_shared<Surface>(WIDTH, HEIGHT);
@@ -303,6 +277,9 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
// Añade la notificación a la lista // Añade la notificación a la lista
notifications_.emplace_back(n); notifications_.emplace_back(n);
// Reproduce el sonido de la notificación
JA_PlaySound(Resource::get()->getSound("notify.wav"));
} }
// Indica si hay notificaciones activas // Indica si hay notificaciones activas

View File

@@ -9,6 +9,11 @@ class SSprite; // lines 9-9
class Text; // lines 10-10 class Text; // lines 10-10
class Surface; // lines 11-11 class Surface; // lines 11-11
// Constantes
constexpr Uint32 DEFAULT_NOTIFICATION_DURATION = 2000;
constexpr Uint32 CHEEVO_NOTIFICATION_DURATION = 4000;
// Justificado para las notificaciones
enum class NotificationText enum class NotificationText
{ {
LEFT, LEFT,
@@ -41,24 +46,39 @@ private:
struct Notification struct Notification
{ {
std::shared_ptr<Surface> surface; std::shared_ptr<Surface> surface; // Superficie asociada a la notificación
std::shared_ptr<SSprite> sprite; std::shared_ptr<SSprite> sprite; // Sprite asociado para gráficos o animaciones
std::vector<std::string> texts; std::vector<std::string> texts; // Lista de textos incluidos en la notificación
int counter; NotificationStatus state; // Estado actual de la notificación (RISING, SHOWING, etc.)
NotificationStatus state; NotificationShape shape; // Forma de la notificación (ej. SQUARED o ROUNDED)
NotificationShape shape; SDL_Rect rect; // Dimensiones y posición de la notificación en pantalla
SDL_Rect rect; int y; // Posición actual en el eje Y
int y; int travel_dist; // Distancia a recorrer (por ejemplo, en animaciones)
int travel_dist; std::string code; // Código identificador único para esta notificación
std::string code; // Permite asignar un código a la notificación bool can_be_removed; // Indica si la notificación puede ser eliminada
bool can_be_removed; int height; // Altura de la notificación
int height; Uint32 start_time; // Momento en que se creó la notificación
Uint32 elapsed_time; // Tiempo transcurrido desde la creación
Uint32 display_duration; // Duración total para mostrar la notificación
// Constructor // Constructor
explicit Notification() explicit Notification()
: surface(nullptr), sprite(nullptr), texts(), counter(0), state(NotificationStatus::RISING), : surface(nullptr), // Inicializar superficie como nula
shape(NotificationShape::SQUARED), rect{0, 0, 0, 0}, y(0), travel_dist(0), code(""), sprite(nullptr), // Inicializar sprite como nulo
can_be_removed(true), height(0) {} texts(), // Inicializar lista de textos vacía
state(NotificationStatus::RISING), // Estado inicial como "RISING"
shape(NotificationShape::SQUARED), // Forma inicial como "SQUARED"
rect{0, 0, 0, 0}, // Rectángulo inicial vacío
y(0), // Posición Y inicializada a 0
travel_dist(0), // Distancia inicializada a 0
code(""), // Código identificador vacío
can_be_removed(true), // Inicialmente se puede eliminar
height(0), // Altura inicializada a 0
start_time(0), // Tiempo de creación inicializado a 0
elapsed_time(0), // Tiempo transcurrido inicializado a 0
display_duration(0) // Duración inicializada a 0
{
}
}; };
std::shared_ptr<Surface> icon_surface_; // Textura para los iconos de las notificaciones std::shared_ptr<Surface> icon_surface_; // Textura para los iconos de las notificaciones
@@ -66,7 +86,6 @@ private:
// Variables // Variables
Uint8 bg_color_; // Color de fondo de las notificaciones Uint8 bg_color_; // Color de fondo de las notificaciones
int wait_time_; // Tiempo que se ve la notificación
std::vector<Notification> notifications_; // La lista de notificaciones activas std::vector<Notification> notifications_; // La lista de notificaciones activas
bool stack_; // Indica si las notificaciones se apilan bool stack_; // Indica si las notificaciones se apilan
bool has_icons_; // Indica si el notificador tiene textura para iconos bool has_icons_; // Indica si el notificador tiene textura para iconos
@@ -102,7 +121,7 @@ public:
void update(); void update();
// Muestra una notificación de texto por pantalla // Muestra una notificación de texto por pantalla
void show(std::vector<std::string> texts, NotificationText text_is = NotificationText::LEFT, int icon = -1, bool can_be_removed = true, const std::string &code = std::string()); void show(std::vector<std::string> texts, NotificationText text_is = NotificationText::LEFT, Uint32 display_duration = DEFAULT_NOTIFICATION_DURATION, int icon = -1, bool can_be_removed = true, const std::string &code = std::string());
// Indica si hay notificaciones activas // Indica si hay notificaciones activas
bool isActive(); bool isActive();