Files
aee-arcade/source/core/rendering/notifier.cpp
T

152 lines
5.8 KiB
C++

#include "core/rendering/notifier.hpp"
#include <algorithm>
#include <numeric>
#include <utility>
#include "core/rendering/screen.hpp"
#include "core/rendering/text.hpp"
#include "core/ui/ui_config.hpp"
#include "utils/defines.hpp"
namespace {
// Parteix `text` en una llista de línies que caben cadascuna dins
// `max_width`. Algoritme de projecte_2026: tall per espais, la paraula
// que no cap passa a la línia següent. Les paraules aïllades més
// llargues que `max_width` es queden a la seva pròpia línia sense
// trencar-les (cas improbable amb els pretty_names de gamepad retallats
// a ~25 caràcters).
auto wrapToWidth(const std::string& text, int max_width, const Text& font, int kerning) -> std::vector<std::string> {
std::vector<std::string> lines;
if (text.empty()) {
lines.emplace_back();
return lines;
}
std::string current;
std::size_t i = 0;
while (i < text.size()) {
const std::size_t SPACE_POS = text.find(' ', i);
const bool LAST = (SPACE_POS == std::string::npos);
const std::string WORD = text.substr(i, LAST ? std::string::npos : SPACE_POS - i);
if (current.empty()) {
current = WORD;
} else {
std::string candidate = current;
candidate += ' ';
candidate += WORD;
if (font.length(candidate, kerning) <= max_width) {
current = std::move(candidate);
} else {
lines.push_back(current);
current = WORD;
}
}
if (LAST) { break; }
i = SPACE_POS + 1;
}
lines.push_back(current);
return lines;
}
} // namespace
auto Notifier::get() -> Notifier& {
static Notifier instance_;
return instance_;
}
Notifier::~Notifier() = default;
void Notifier::init(std::shared_ptr<Text> text, const Palette& ui_palette) {
text_ = std::move(text);
ui_palette_ = ui_palette;
}
void Notifier::update(int delta_ms) {
const auto& geo = UiConfig::get().notifier();
const float DT = static_cast<float>(delta_ms) / 1000.0F;
switch (status_) {
case Status::RISING:
y_ += geo.slide_speed * DT;
if (y_ >= static_cast<float>(geo.margin_top + geo.border_px)) {
y_ = static_cast<float>(geo.margin_top + geo.border_px);
status_ = Status::ACTIVE;
}
break;
case Status::ACTIVE:
remaining_ms_ -= delta_ms;
if (remaining_ms_ <= 0) { status_ = Status::VANISHING; }
break;
case Status::VANISHING:
y_ -= geo.slide_speed * DT;
if (y_ <= static_cast<float>(-current_box_h_ - geo.border_px)) {
y_ = static_cast<float>(-current_box_h_ - geo.border_px - geo.margin_top);
status_ = Status::HIDDEN;
}
break;
default:
break;
}
}
void Notifier::render() {
if (status_ == Status::HIDDEN || text_ == nullptr || current_lines_.empty()) { return; }
const auto& geo = UiConfig::get().notifier();
const int BOX_X = (GameCanvas::WIDTH - current_box_w_) / 2;
const int BOX_Y = static_cast<int>(y_);
// Fons sòlid amb la paleta UI que ens ha injectat el caller a init()
// (ARGB directe al canvas, sense tocar cap surface paletada).
Screen::get().fillRect(BOX_X, BOX_Y, current_box_w_, current_box_h_, ui_palette_[current_bg_color_]);
// Vora externa: 4 línies d'1 px del color del text, paral·leles a cada
// costat i una mica fora de la caixa. Cada segment es queda dins del
// rang del costat corresponent perquè les cantonades no es cobrisquen.
const std::uint32_t BORDER_ARGB = ui_palette_[current_text_color_];
Screen::get().fillRect(BOX_X, BOX_Y - geo.border_px, current_box_w_, geo.border_px, BORDER_ARGB);
Screen::get().fillRect(BOX_X, BOX_Y + current_box_h_, current_box_w_, geo.border_px, BORDER_ARGB);
Screen::get().fillRect(BOX_X - geo.border_px, BOX_Y, geo.border_px, current_box_h_, BORDER_ARGB);
Screen::get().fillRect(BOX_X + current_box_w_, BOX_Y, geo.border_px, current_box_h_, BORDER_ARGB);
const int LINE_STEP = Rendering::Hud::FONT_SIZE_PX + geo.line_gap;
int line_y = BOX_Y + geo.padding_top;
for (const auto& line : current_lines_) {
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
GameCanvas::CENTER_X,
line_y,
line,
1,
current_text_color_);
line_y += LINE_STEP;
}
}
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.
if (text_ == nullptr) { return; }
const auto& geo = UiConfig::get().notifier();
const int MAX_TEXT_WIDTH = GameCanvas::WIDTH - (2 * SIDE_MARGIN) - (2 * geo.padding_h);
current_lines_ = wrapToWidth(text, MAX_TEXT_WIDTH, *text_, 1);
// Amplada = max de les línies + padding. Alçada = N*glif + (N-1)*gap + padding asimètric.
const int MAX_LINE_W = std::accumulate(current_lines_.begin(), current_lines_.end(), 0, [this](int acc, const std::string& s) { return std::max(acc, text_->length(s, 1)); });
current_box_w_ = MAX_LINE_W + (2 * geo.padding_h);
const int N = static_cast<int>(current_lines_.size());
current_box_h_ = (N * Rendering::Hud::FONT_SIZE_PX) + ((N - 1) * geo.line_gap) + geo.padding_top + geo.padding_bottom;
current_text_color_ = text_color;
current_bg_color_ = bg_color;
remaining_ms_ = time_ms;
y_ = static_cast<float>(-current_box_h_ - geo.border_px - geo.margin_top);
status_ = Status::RISING;
}