primer commit

This commit is contained in:
2025-11-23 11:44:31 +01:00
commit 6ada29eaf8
613 changed files with 484459 additions and 0 deletions

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

@@ -0,0 +1,289 @@
#include "game/ui/notifier.hpp"
#include <SDL3/SDL.h>
#include <algorithm> // Para remove_if
#include <iterator> // Para prev
#include <ranges> // Para reverse_view
#include <string> // Para string, basic_string
#include <vector> // Para vector
#include "core/audio/audio.hpp" // Para Audio
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/surface_sprite.hpp" // Para SSprite
#include "core/rendering/text.hpp" // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
#include "core/resources/resource_cache.hpp" // Para Resource
#include "game/options.hpp" // Para Options, options, NotificationPosition
#include "utils/delta_timer.hpp" // Para DeltaTimer
#include "utils/color.hpp" // Para Color
// [SINGLETON]
Notifier* Notifier::notifier = nullptr;
// Definición de estilos predefinidos
const Notifier::Style Notifier::Style::DEFAULT = {
.bg_color = Color::index(Color::Cpc::BLUE),
.border_color = Color::index(Color::Cpc::CYAN),
.text_color = Color::index(Color::Cpc::CYAN),
.shape = Notifier::Shape::SQUARED,
.text_align = Notifier::TextAlign::CENTER,
.duration = 2.0F,
.sound_file = "notify.wav",
.play_sound = false};
const Notifier::Style Notifier::Style::CHEEVO = {
.bg_color = Color::index(Color::Cpc::MAGENTA),
.border_color = Color::index(Color::Cpc::BRIGHT_MAGENTA),
.text_color = Color::index(Color::Cpc::WHITE),
.shape = Notifier::Shape::SQUARED,
.text_align = Notifier::TextAlign::CENTER,
.duration = 4.0F,
.sound_file = "notify.wav",
.play_sound = true};
// [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
auto Notifier::get() -> Notifier* {
return Notifier::notifier;
}
// Constructor
Notifier::Notifier(const std::string& icon_file, const std::string& text)
: icon_surface_(!icon_file.empty() ? Resource::Cache::get()->getSurface(icon_file) : nullptr),
text_(Resource::Cache::get()->getText(text)),
delta_timer_(std::make_unique<DeltaTimer>()),
has_icons_(!icon_file.empty()) {}
// Dibuja las notificaciones por pantalla
void Notifier::render() {
for (auto& notification : std::ranges::reverse_view(notifications_)) {
notification.sprite->render();
}
}
// Actualiza el estado de las notificaiones
void Notifier::update(float delta_time) {
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 == Status::RISING) {
break;
}
}
switch (notification.state) {
case Status::RISING: {
const float DISPLACEMENT = SLIDE_SPEED * delta_time;
notification.rect.y += DISPLACEMENT;
if (notification.rect.y >= notification.y) {
notification.rect.y = notification.y;
notification.state = Status::STAY;
notification.elapsed_time = 0.0F;
}
break;
}
case Status::STAY: {
notification.elapsed_time += delta_time;
if (notification.elapsed_time >= notification.display_duration) {
notification.state = Status::VANISHING;
}
break;
}
case Status::VANISHING: {
const float DISPLACEMENT = SLIDE_SPEED * delta_time;
notification.rect.y -= DISPLACEMENT;
const float TARGET_Y = notification.y - notification.travel_dist;
if (notification.rect.y <= TARGET_Y) {
notification.rect.y = TARGET_Y;
notification.state = Status::FINISHED;
}
break;
}
case Status::FINISHED:
break;
default:
break;
}
notification.sprite->setPosition(notification.rect);
}
clearFinishedNotifications();
}
// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() {
auto result = std::ranges::remove_if(notifications_, [](const Notification& notification) {
return notification.state == Status::FINISHED;
});
notifications_.erase(result.begin(), result.end());
}
void Notifier::show(std::vector<std::string> texts, const Style& style, 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
auto result = std::ranges::remove_if(texts, [](const std::string& s) { return s.empty(); });
texts.erase(result.begin(), result.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;
const TextAlign TEXT_IS = ICON_SPACE > 0 ? TextAlign::LEFT : style.text_align;
const float WIDTH = Options::game.width - (PADDING_OUT * 2);
const float HEIGHT = (TEXT_SIZE * texts.size()) + (PADDING_IN_V * 2);
const auto SHAPE = style.shape;
// Posición horizontal
float desp_h = ((Options::game.width / 2) - (WIDTH / 2));
;
// Posición vertical
const int DESP_V = PADDING_OUT;
// Offset
const auto TRAVEL_DIST = HEIGHT + PADDING_OUT;
const int TRAVEL_MOD = 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 = style.duration;
const float Y_POS = OFFSET + -TRAVEL_DIST;
n.rect = {.x = desp_h, .y = Y_POS, .w = WIDTH, .h = 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 == Shape::ROUNDED) {
rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT};
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
n.surface->fillRect(&rect, style.bg_color);
rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
n.surface->fillRect(&rect, style.bg_color);
}
else if (SHAPE == Shape::SQUARED) {
n.surface->clear(style.bg_color);
SDL_FRect squared_rect = {0, 0, n.surface->getWidth(), n.surface->getHeight()};
n.surface->drawRectBorder(&squared_rect, style.border_color);
}
// Dibuja el icono de la notificación
if (has_icons_ && icon >= 0 && texts.size() >= 2) {
auto sp = std::make_unique<SurfaceSprite>(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 auto COLOR = style.text_color;
int iterator = 0;
for (const auto& text : texts) {
switch (TEXT_IS) {
case TextAlign::LEFT:
text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, COLOR);
break;
case TextAlign::CENTER:
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, 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<SurfaceSprite>(n.surface, n.rect);
// Añade la notificación a la lista
notifications_.emplace_back(n);
// Reproduce el sonido de la notificación
if (style.play_sound && !style.sound_file.empty()) {
Audio::get()->playSound(style.sound_file, Audio::Group::INTERFACE);
}
}
// Indica si hay notificaciones activas
auto Notifier::isActive() -> bool { return !notifications_.empty(); }
// Finaliza y elimnina todas las notificaciones activas
void Notifier::clearNotifications() {
for (auto& notification : notifications_) {
if (notification.can_be_removed) {
notification.state = Status::FINISHED;
}
}
clearFinishedNotifications();
}
// Obtiene los códigos de las notificaciones
auto Notifier::getCodes() -> std::vector<std::string> {
std::vector<std::string> codes;
codes.reserve(notifications_.size());
for (const auto& notification : notifications_) {
codes.emplace_back(notification.code);
}
return codes;
}

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

@@ -0,0 +1,110 @@
#pragma once
#include <SDL3/SDL.h>
#include <memory> // Para shared_ptr
#include <string> // Para string, basic_string
#include <vector> // Para vector
class SurfaceSprite; // lines 8-8
class Surface; // lines 10-10
class Text; // lines 9-9
class DeltaTimer; // lines 11-11
class Notifier {
public:
// Justificado para las notificaciones
enum class TextAlign {
LEFT,
CENTER,
};
// Forma de las notificaciones
enum class Shape {
ROUNDED,
SQUARED,
};
// Estilo de notificación
struct Style {
Uint8 bg_color; // Color de fondo
Uint8 border_color; // Color del borde
Uint8 text_color; // Color del texto
Shape shape; // Forma (ROUNDED/SQUARED)
TextAlign text_align; // Alineación del texto
float duration; // Duración en segundos
std::string sound_file; // Archivo de sonido (vacío = sin sonido)
bool play_sound; // Si reproduce sonido
// Estilos predefinidos
static const Style DEFAULT;
static const Style CHEEVO;
};
// Gestión singleton
static void init(const std::string& icon_file, const std::string& text); // Inicialización
static void destroy(); // Destrucción
static auto get() -> Notifier*; // Acceso al singleton
// Métodos principales
void render(); // Renderizado
void update(float delta_time); // Actualización lógica
void show(
std::vector<std::string> texts,
const Style& style = Style::DEFAULT,
int icon = -1,
bool can_be_removed = true,
const std::string& code = std::string()); // Mostrar notificación
// Consultas
auto isActive() -> bool; // Indica si hay notificaciones activas
auto getCodes() -> std::vector<std::string>; // Obtiene códigos de notificaciones
private:
// Tipos anidados
enum class Status {
RISING,
STAY,
VANISHING,
FINISHED,
};
struct Notification {
std::shared_ptr<Surface> surface{nullptr};
std::shared_ptr<SurfaceSprite> sprite{nullptr};
std::vector<std::string> texts;
Status state{Status::RISING};
Shape shape{Shape::SQUARED};
SDL_FRect rect{0.0F, 0.0F, 0.0F, 0.0F};
int y{0};
int travel_dist{0};
std::string code;
bool can_be_removed{true};
int height{0};
float elapsed_time{0.0F};
float display_duration{0.0F};
};
// Constantes
static constexpr float ICON_SIZE = 16.0F;
static constexpr float PADDING_OUT = 0.0F;
static constexpr float SLIDE_SPEED = 120.0F; // Pixels per second for slide animations
// [SINGLETON] Objeto notifier
static Notifier* notifier;
// Métodos privados
void clearFinishedNotifications(); // Elimina las notificaciones finalizadas
void clearNotifications(); // Finaliza y elimina todas las notificaciones activas
// Constructor y destructor privados [SINGLETON]
Notifier(const std::string& icon_file, const std::string& text);
~Notifier() = default;
// Variables miembro
std::shared_ptr<Surface> icon_surface_; // Textura para los iconos
std::shared_ptr<Text> text_; // Objeto para dibujar texto
std::unique_ptr<DeltaTimer> delta_timer_; // Timer for frame-independent animations
std::vector<Notification> notifications_; // Lista de notificaciones activas
bool stack_{false}; // Indica si las notificaciones se apilan
bool has_icons_{false}; // Indica si el notificador tiene textura para iconos
};