87 lines
2.8 KiB
C++
87 lines
2.8 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_ >= 0.0F) {
|
|
y_ = 0.0F;
|
|
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);
|
|
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) {
|
|
notify(text, Notification::DEFAULT);
|
|
}
|
|
|
|
void Notifier::notify(const std::string& text, const Notification::Preset& preset) {
|
|
notify(text, preset.text_color, preset.bg_color, preset.time_ms);
|
|
}
|
|
|
|
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;
|
|
}
|