redistribuida la carpeta source

This commit is contained in:
2025-10-26 13:02:45 +01:00
parent 9676e5bc2f
commit 8f49e442de
164 changed files with 303 additions and 30479 deletions

278
source/game/ui/notifier.cpp Normal file
View File

@@ -0,0 +1,278 @@
#include "game/ui/notifier.hpp"
#include <SDL3/SDL.h>
#include <algorithm> // Para remove_if
#include <iterator> // Para prev
#include <string> // Para string, basic_string
#include <vector> // Para vector
#include "external/jail_audio.h" // Para JA_PlaySound
#include "game/gameplay/options.hpp" // Para Options, options, NotificationPosition
#include "core/resources/resource.hpp" // Para Resource
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface_sprite.hpp" // Para SSprite
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/text.hpp" // Para Text, TEXT_CENTER, TEXT_COLOR
#include "utils/utils.hpp" // Para PaletteColor
// [SINGLETON]
Notifier* Notifier::notifier_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Notifier::init(const std::string& icon_file, const std::string& text) {
Notifier::notifier_ = new Notifier(icon_file, text);
}
// [SINGLETON] Destruiremos el objeto con esta función estática
void Notifier::destroy() {
delete Notifier::notifier_;
}
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
Notifier* Notifier::get() {
return Notifier::notifier_;
}
// Constructor
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),
stack_(false),
has_icons_(!icon_file.empty()) {}
// Dibuja las notificaciones por pantalla
void Notifier::render() {
for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it) {
it->sprite->render();
}
}
// Actualiza el estado de las notificaiones
void Notifier::update() {
for (auto& notification : notifications_) {
// Si la notificación anterior está "saliendo", no hagas nada
if (!notifications_.empty() && &notification != &notifications_.front()) {
const auto& PREVIOUS_NOTIFICATION = *(std::prev(&notification));
if (PREVIOUS_NOTIFICATION.state == NotificationStatus::RISING) {
break;
}
}
switch (notification.state) {
case NotificationStatus::RISING: {
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
notification.rect.y += DIRECTION;
if (notification.rect.y == notification.y) {
notification.state = NotificationStatus::STAY;
notification.start_time = SDL_GetTicks();
}
break;
}
case NotificationStatus::STAY: {
notification.elapsed_time = SDL_GetTicks() - notification.start_time;
if (notification.elapsed_time >= notification.display_duration) {
notification.state = NotificationStatus::VANISHING;
}
break;
}
case NotificationStatus::VANISHING: {
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1;
notification.rect.y += DIRECTION;
if (notification.rect.y == notification.y - notification.travel_dist) {
notification.state = NotificationStatus::FINISHED;
}
break;
}
case NotificationStatus::FINISHED:
break;
default:
break;
}
notification.sprite->setPosition(notification.rect);
}
clearFinishedNotifications();
}
// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() {
notifications_.erase(
std::remove_if(notifications_.begin(), notifications_.end(), [](const Notification& notification) {
return notification.state == NotificationStatus::FINISHED;
}),
notifications_.end());
}
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
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 text_size = 6;
const auto PADDING_IN_H = text_size;
const auto PADDING_IN_V = text_size / 2;
const int ICON_SPACE = icon >= 0 ? ICON_SIZE_ + PADDING_IN_H : 0;
text_is = ICON_SPACE > 0 ? NotificationText::LEFT : text_is;
const float WIDTH = options.game.width - (PADDING_OUT_ * 2);
const float HEIGHT = (text_size * texts.size()) + (PADDING_IN_V * 2);
const auto SHAPE = NotificationShape::SQUARED;
// Posición horizontal
float desp_h = 0;
switch (options.notifications.getHorizontalPosition()) {
case NotificationPosition::LEFT:
desp_h = PADDING_OUT_;
break;
case NotificationPosition::CENTER:
desp_h = ((options.game.width / 2) - (WIDTH / 2));
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_;
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;
// Inicializa variables
n.code = code;
n.can_be_removed = can_be_removed;
n.y = OFFSET;
n.travel_dist = TRAVEL_DIST;
n.texts = texts;
n.shape = SHAPE;
n.display_duration = display_duration;
const float 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<Surface>(WIDTH, HEIGHT);
// Prepara para dibujar en la textura
auto previuos_renderer = Screen::get()->getRendererSurface();
Screen::get()->setRendererSurface(n.surface);
// Dibuja el fondo de la notificación
SDL_FRect rect;
if (SHAPE == NotificationShape::ROUNDED) {
rect = {4, 0, WIDTH - (4 * 2), HEIGHT};
n.surface->fillRect(&rect, bg_color_);
rect = {4 / 2, 1, WIDTH - 4, HEIGHT - 2};
n.surface->fillRect(&rect, bg_color_);
rect = {1, 4 / 2, WIDTH - 2, HEIGHT - 4};
n.surface->fillRect(&rect, bg_color_);
rect = {0, 4, WIDTH, HEIGHT - (4 * 2)};
n.surface->fillRect(&rect, bg_color_);
}
else if (SHAPE == NotificationShape::SQUARED) {
n.surface->clear(bg_color_);
SDL_FRect squared_rect = {0, 0, n.surface->getWidth(), n.surface->getHeight()};
n.surface->drawRectBorder(&squared_rect, static_cast<Uint8>(PaletteColor::CYAN));
}
// Dibuja el icono de la notificación
if (has_icons_ && icon >= 0 && texts.size() >= 2) {
auto sp = std::make_unique<SSprite>(icon_surface_, (SDL_FRect){0, 0, ICON_SIZE_, ICON_SIZE_});
sp->setPosition({PADDING_IN_H, PADDING_IN_V, ICON_SIZE_, ICON_SIZE_});
sp->setClip((SDL_FRect){ICON_SIZE_ * (icon % 10), ICON_SIZE_ * (icon / 10), ICON_SIZE_, ICON_SIZE_});
sp->render();
}
// Escribe el texto de la notificación
const Uint8 COLOR = static_cast<Uint8>(PaletteColor::WHITE);
int iterator = 0;
for (const auto& text : texts) {
switch (text_is) {
case NotificationText::LEFT:
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + iterator * (text_size + 1), text, COLOR);
break;
case NotificationText::CENTER:
text_->writeDX(TEXT_CENTER | TEXT_COLOR, WIDTH / 2, PADDING_IN_V + iterator * (text_size + 1), text, 1, COLOR);
break;
default:
break;
}
++iterator;
}
// Deja de dibujar en la textura
Screen::get()->setRendererSurface(previuos_renderer);
// Crea el sprite de la notificación
n.sprite = std::make_shared<SSprite>(n.surface, n.rect);
// 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
bool Notifier::isActive() { return !notifications_.empty(); }
// Finaliza y elimnina todas las notificaciones activas
void Notifier::clearNotifications() {
for (auto& notification : notifications_) {
if (notification.can_be_removed) {
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& notification : notifications_) {
codes.emplace_back(notification.code);
}
return codes;
}

126
source/game/ui/notifier.hpp Normal file
View File

@@ -0,0 +1,126 @@
#pragma once
#include <SDL3/SDL.h>
#include <memory> // Para shared_ptr
#include <string> // Para string, basic_string
#include <vector> // Para vector
class SSprite; // lines 8-8
class Surface; // lines 10-10
class Text; // lines 9-9
// Constantes
constexpr Uint32 DEFAULT_NOTIFICATION_DURATION = 2000;
constexpr Uint32 CHEEVO_NOTIFICATION_DURATION = 4000;
// Justificado para las notificaciones
enum class NotificationText {
LEFT,
CENTER,
};
class Notifier {
private:
// Constantes
static constexpr float ICON_SIZE_ = 16.0F;
static constexpr float PADDING_OUT_ = 0.0F;
// [SINGLETON] Objeto notifier
static Notifier* notifier_;
enum class NotificationStatus {
RISING,
STAY,
VANISHING,
FINISHED,
};
enum class NotificationShape {
ROUNDED,
SQUARED,
};
struct Notification {
std::shared_ptr<Surface> surface; // Superficie asociada a la notificación
std::shared_ptr<SSprite> sprite; // Sprite asociado para gráficos o animaciones
std::vector<std::string> 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_FRect 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), // 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<Surface> icon_surface_; // Textura para los iconos de las notificaciones
std::shared_ptr<Text> text_; // Objeto para dibujar texto
// Variables
Uint8 bg_color_; // Color de fondo de las notificaciones
std::vector<Notification> 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
// Elimina las notificaciones finalizadas
void clearFinishedNotifications();
// Finaliza y elimnina todas las notificaciones activas
void clearNotifications();
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos notifier desde fuera
// Constructor
Notifier(const std::string& icon_file, const std::string& text);
// Destructor
~Notifier() = default;
public:
// [SINGLETON] Crearemos el objeto con esta función estática
static void init(const std::string& icon_file, const std::string& text);
// [SINGLETON] Destruiremos el objeto con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
static Notifier* get();
// Dibuja las notificaciones por pantalla
void render();
// Actualiza el estado de las notificaiones
void update();
// Muestra una notificación de texto por pantalla
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
bool isActive();
// Obtiene los códigos de las notificaciones
std::vector<std::string> getCodes();
};