Files
aee-arcade/source/core/rendering/notifier.cpp
T
JailDesignerandClaude Opus 4.7 9434e4e87d refactor(core/rendering): desacoplar Notifier de game/ui (COUP-01a)
core/rendering/notifier.hpp ja no inclou game/ui/notification_presets.hpp,
i Notifier només exposa l'API crua notify(text, text_color, bg_color,
time_ms). El helper de conveniència basat en Preset es trasllada com a
funció lliure Notification::notify() a game/ui/notification_presets.hpp
(direcció game → core, correcta). Els callers de global_inputs.cpp
passen a usar el helper i afegeixen l'include explícit.

Sota l'auditoria de core/rendering (docs/audit-core-rendering.md).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 09:06:40 +02:00

79 lines
2.6 KiB
C++

#include "core/rendering/notifier.hpp"
#include "core/rendering/screen.hpp"
#include "core/rendering/surface.hpp"
#include "core/rendering/text.hpp"
#include "core/resources/resource_cache.hpp"
#include "utils/defines.hpp"
auto Notifier::get() -> Notifier& {
static Notifier instance_;
return instance_;
}
Notifier::~Notifier() = default;
void Notifier::init() {
// El Director preloade la paleta "ui" i la surface "8bithud.gif" abans
// de cridar aquest init(); ací només cal construir el Text.
auto font_surface = Resource::Cache::get().getSurface("8bithud.gif");
text_ = std::make_unique<Text>(font_surface, "data/font/8bithud.fnt");
}
void Notifier::update(int delta_ms) {
const float DT = static_cast<float>(delta_ms) / 1000.0F;
switch (status_) {
case Status::RISING:
y_ += SLIDE_SPEED * DT;
if (y_ >= static_cast<float>(MARGIN_TOP)) {
y_ = static_cast<float>(MARGIN_TOP);
status_ = Status::ACTIVE;
}
break;
case Status::ACTIVE:
remaining_ms_ -= delta_ms;
if (remaining_ms_ <= 0) { status_ = Status::VANISHING; }
break;
case Status::VANISHING:
y_ -= SLIDE_SPEED * DT;
if (y_ <= static_cast<float>(-HEIGHT)) {
y_ = static_cast<float>(-HEIGHT - MARGIN_TOP);
status_ = Status::HIDDEN;
}
break;
default:
break;
}
}
void Notifier::render() {
if (status_ == Status::HIDDEN || text_ == nullptr || current_text_.empty()) { return; }
const int TEXT_W = text_->length(current_text_, 1);
const int BOX_W = TEXT_W + (2 * PADDING_H);
const int BOX_X = (GameCanvas::WIDTH - BOX_W) / 2;
const int BOX_Y = static_cast<int>(y_);
// Fons sòlid a través de la paleta "ui" (ARGB directe al canvas, sense
// tocar cap surface paletada).
const auto& ui_palette = Resource::Cache::get().getPalette("ui");
Screen::get().fillRect(BOX_X, BOX_Y, BOX_W, HEIGHT, ui_palette[current_bg_color_]);
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
GameCanvas::CENTER_X,
BOX_Y + PADDING_V,
current_text_,
1,
current_text_color_);
}
void Notifier::notify(const std::string& text, Uint8 text_color, Uint8 bg_color, int time_ms) {
// Política simple: la nova notificació reemplaça la que hi havia. Per
// gestionar-ne una cua, canviar ací per una FIFO quan calga.
current_text_ = text;
current_text_color_ = text_color;
current_bg_color_ = bg_color;
remaining_ms_ = time_ms;
status_ = Status::RISING;
}