diff --git a/source/game.cpp b/source/game.cpp index c53cc35..690201c 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -267,16 +267,9 @@ void Game::checkDebugEvents(const SDL_Event &event) break; 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; - 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: break; } diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp index 13385c5..c308cd8 100644 --- a/source/global_inputs.cpp +++ b/source/global_inputs.cpp @@ -21,7 +21,7 @@ namespace globalInputs else { // 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); } } diff --git a/source/notifier.cpp b/source/notifier.cpp index 6e9b1eb..432cfe2 100644 --- a/source/notifier.cpp +++ b/source/notifier.cpp @@ -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), text_(Resource::get()->getText(text)), bg_color_(options.notifications.color), - wait_time_(150), stack_(false), has_icons_(!icon_file.empty()) {} @@ -66,75 +65,47 @@ void Notifier::update() } } - notifications_[i].counter++; - - // Hace sonar la notificación en el primer frame - if (notifications_[i].counter == 1) + switch (notifications_[i].state) { - if (options.notifications.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 == NotificationStatus::RISING) + case NotificationStatus::RISING: { - // const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist); - // const int alpha = 255 * 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); + const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1; + notifications_[i].rect.y += DIRECTION; if (notifications_[i].rect.y == notifications_[i].y) { notifications_[i].state = NotificationStatus::STAY; - // notifications_[i].surface->setAlpha(255); - notifications_[i].counter = 0; + notifications_[i].start_time = SDL_GetTicks(); } + break; } - - else if (notifications_[i].state == NotificationStatus::STAY) + case 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].counter = 0; } + break; } - else if (notifications_[i].state == NotificationStatus::VANISHING) + + case NotificationStatus::VANISHING: { - - // const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist); - // 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); + const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1; + notifications_[i].rect.y += DIRECTION; if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) { notifications_[i].state = NotificationStatus::FINISHED; } + break; + } + + case NotificationStatus::FINISHED: + break; + + default: + break; } notifications_[i].sprite->setPosition(notifications_[i].rect); @@ -155,7 +126,7 @@ void Notifier::clearFinishedNotifications() } } -void Notifier::show(std::vector texts, NotificationText text_is, int icon, bool can_be_removed, const std::string &code) +void Notifier::show(std::vector texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code) { // Si no hay texto, acaba if (texts.empty()) @@ -179,11 +150,12 @@ void Notifier::show(std::vector texts, NotificationText text_is, in for (const auto &text : texts) { if (text.length() > longest.length()) + { longest = text; + } } // Inicializa variables - //const int text_size = text_->getCharacterSize(); const int text_size = 6; const auto PADDING_IN_H = text_size; const auto PADDING_IN_V = text_size / 2; @@ -194,33 +166,34 @@ void Notifier::show(std::vector texts, NotificationText text_is, in const auto SHAPE = NotificationShape::SQUARED; // Posición horizontal - auto desp_h = 0; - if (options.notifications.getHorizontalPosition() == NotificationPosition::LEFT) + int desp_h = 0; + switch (options.notifications.getHorizontalPosition()) { + case NotificationPosition::LEFT: desp_h = PADDING_OUT_; - } - else if (options.notifications.getHorizontalPosition() == NotificationPosition::CENTER) - { + break; + + case NotificationPosition::CENTER: desp_h = ((options.game.width / 2) - (WIDTH / 2)); - } - else if (options.notifications.getHorizontalPosition() == NotificationPosition::RIGHT) - { + break; + + case NotificationPosition::RIGHT: desp_h = options.game.width - WIDTH - PADDING_OUT_; + break; + + default: + desp_h = 0; + break; } // Posición vertical const int DESP_V = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? PADDING_OUT_ : options.game.height - HEIGHT - PADDING_OUT_; + // Offset const auto TRAVEL_DIST = HEIGHT + PADDING_OUT_; - auto offset = 0; - if (options.notifications.getVerticalPosition() == NotificationPosition::TOP) - { - 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; - } + const int TRAVEL_MOD = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1; + const int OFFSET = !notifications_.empty() ? notifications_.back().y + TRAVEL_MOD * notifications_.back().travel_dist : DESP_V; + // Crea la notificacion Notification n; @@ -228,12 +201,13 @@ void Notifier::show(std::vector texts, NotificationText text_is, in // Inicializa variables n.code = code; n.can_be_removed = can_be_removed; - n.y = offset; + n.y = OFFSET; n.travel_dist = TRAVEL_DIST; n.texts = texts; n.shape = SHAPE; - int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -TRAVEL_DIST : TRAVEL_DIST); - n.rect = {desp_h, y_pos, WIDTH, HEIGHT}; + n.display_duration = display_duration; + 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 n.surface = std::make_shared(WIDTH, HEIGHT); @@ -303,6 +277,9 @@ void Notifier::show(std::vector texts, NotificationText text_is, in // Añade la notificación a la lista notifications_.emplace_back(n); + + // Reproduce el sonido de la notificación + JA_PlaySound(Resource::get()->getSound("notify.wav")); } // Indica si hay notificaciones activas diff --git a/source/notifier.h b/source/notifier.h index 287a99d..479b4ac 100644 --- a/source/notifier.h +++ b/source/notifier.h @@ -9,6 +9,11 @@ class SSprite; // lines 9-9 class Text; // lines 10-10 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 { LEFT, @@ -41,24 +46,39 @@ private: struct Notification { - std::shared_ptr surface; - std::shared_ptr sprite; - std::vector texts; - int counter; - NotificationStatus state; - NotificationShape shape; - SDL_Rect rect; - int y; - int travel_dist; - std::string code; // Permite asignar un código a la notificación - bool can_be_removed; - int height; + std::shared_ptr surface; // Superficie asociada a la notificación + std::shared_ptr sprite; // Sprite asociado para gráficos o animaciones + std::vector texts; // Lista de textos incluidos en la notificación + NotificationStatus state; // Estado actual de la notificación (RISING, SHOWING, etc.) + NotificationShape shape; // Forma de la notificación (ej. SQUARED o ROUNDED) + SDL_Rect rect; // Dimensiones y posición de la notificación en pantalla + int y; // Posición actual en el eje Y + int travel_dist; // Distancia a recorrer (por ejemplo, en animaciones) + std::string code; // Código identificador único para esta notificación + bool can_be_removed; // Indica si la notificación puede ser eliminada + int height; // Altura de la notificación + 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 explicit Notification() - : surface(nullptr), sprite(nullptr), texts(), counter(0), state(NotificationStatus::RISING), - shape(NotificationShape::SQUARED), rect{0, 0, 0, 0}, y(0), travel_dist(0), code(""), - can_be_removed(true), height(0) {} + : surface(nullptr), // Inicializar superficie como nula + sprite(nullptr), // Inicializar sprite como nulo + 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 icon_surface_; // Textura para los iconos de las notificaciones @@ -66,7 +86,6 @@ private: // Variables Uint8 bg_color_; // Color de fondo de las notificaciones - int wait_time_; // Tiempo que se ve la notificación std::vector notifications_; // La lista de notificaciones activas bool stack_; // Indica si las notificaciones se apilan bool has_icons_; // Indica si el notificador tiene textura para iconos @@ -102,7 +121,7 @@ public: void update(); // Muestra una notificación de texto por pantalla - void show(std::vector texts, NotificationText text_is = NotificationText::LEFT, int icon = -1, bool can_be_removed = true, const std::string &code = std::string()); + void show(std::vector 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 bool isActive();