feat(ui): captura F9, tema a data/ui.yaml i retocs estètics

Captura de pantalla amb F9: volca el canvas ARGB a
screenshots/scr_<timestamp>.png via stb_image_write.

Tot el tema d'UI (colors, timings, fonts, flag hud.bg_enabled)
passa a data/ui.yaml via UiConfig; els valors de Defaults::UI
queden com a fallbacks si el fitxer falta o li manca alguna clau.

Retocs estètics: paleta "uzebox" per a la UI, strings del HUD
(P1/P2/V/T/PIRAMIDE/SALA) traduïts a locale, i fons del marcador
opcional amb backdrop continu de franja per evitar el trencament
visual durant el shake o l'animació d'entrada dels panells.
This commit is contained in:
JailDesigner committed 2026-04-24 10:58:02 +02:00
1 parent 82b229222f
commit dc8a7723e3
26 files changed
+2593 -210

No files matched your search

+47 -41
View File
@@ -11,22 +11,21 @@
#include <utility>
#include <variant>
#include "core/locale/locale.hpp"
#include "core/rendering/screen.hpp"
#include "core/rendering/text.hpp"
#include "game/ui/ui_palette.hpp"
#include "core/resources/resource_cache.hpp"
#include "core/ui/ui_config.hpp"
#include "utils/defaults.hpp"
#include "utils/defines.hpp"
#include "utils/easing.hpp"
namespace {
// Colors HUD reaprofitats de la paleta UI: les etiquetes ("P1", "V",
// "T") usen un to apagat per crear jerarquia visual respecte als
// valors. El flash temporal usa un to càlid (groc del títol del menú).
constexpr Uint8 LABEL_COLOR = UiColor::MENU_LABEL; // 47
constexpr Uint8 VALUE_COLOR = UiColor::MENU_VALUE; // 197
constexpr Uint8 BG_COLOR = UiColor::MENU_BG; // 40
constexpr Uint8 FLASH_COLOR = Defaults::UI::MENU_TITLE; // 213
// Els colors del HUD viuen a data/ui.yaml (colors.hud_label/value/flash)
// via UiConfig. Les etiquetes ("P1", "V", "T") usen un to apagat per
// crear jerarquia visual respecte als valors; el flash temporal usa un
// to càlid que contrasta amb el valor normal.
// Amplada nominal d'una cel·la de dígit. Imposem monoespaiat als
// comptadors per evitar jitter visible quan canvien valors (Notifier
@@ -66,16 +65,18 @@ namespace {
} // namespace
Hud::Hud(EventBus& bus, std::shared_ptr<Text> font, const Palette& ui_palette)
Hud::Hud(EventBus& bus, std::shared_ptr<Text> font)
: bus_(bus),
font_(std::move(font)),
ui_palette_(ui_palette) {
font_(std::move(font)) {
// Geometria fixa dels tres blocs. Sumen exactament GameCanvas::WIDTH.
// La durada de l'animació d'entrada ve de data/ui.yaml via UiConfig.
const int ENTRY_MS = UiConfig::get().hudEntryMs();
blocks_[0] = Block{
.x = 0,
.y = 0,
.w = LEFT_W,
.h = HEIGHT_PX,
.entry_ms_left = ENTRY_MS,
.entry_offset_x = -LEFT_W,
.entry_offset_y = 0,
};
@@ -84,6 +85,7 @@ Hud::Hud(EventBus& bus, std::shared_ptr<Text> font, const Palette& ui_palette)
.y = 0,
.w = CENTER_W,
.h = HEIGHT_PX,
.entry_ms_left = ENTRY_MS,
.entry_offset_x = 0,
.entry_offset_y = -HEIGHT_PX,
};
@@ -92,6 +94,7 @@ Hud::Hud(EventBus& bus, std::shared_ptr<Text> font, const Palette& ui_palette)
.y = 0,
.w = RIGHT_W,
.h = HEIGHT_PX,
.entry_ms_left = ENTRY_MS,
.entry_offset_x = RIGHT_W,
.entry_offset_y = 0,
};
@@ -113,8 +116,8 @@ Hud::Hud(EventBus& bus, std::shared_ptr<Text> font, const Palette& ui_palette)
constexpr std::size_t IDX = 0;
if (players_[IDX].lives > 0) { --players_[IDX].lives; }
auto& blk = blocks_[IDX]; // P1 → bloc esquerre
blk.flash_ms_left = FLASH_MS;
blk.shake_ms_left = SHAKE_MS;
blk.flash_ms_left = UiConfig::get().hudFlashMs();
blk.shake_ms_left = UiConfig::get().hudShakeMs();
blk.shake_intensity = 1.5F;
std::cout << "[HUD] player_hit dmg=" << (dmg != nullptr ? *dmg : 0)
<< " lives=" << players_[IDX].lives << '\n';
@@ -165,7 +168,7 @@ auto Hud::computeOffset(const Block& b) -> SDL_FPoint {
// Animació d'entrada: t va de 0 (recent iniciat) a 1 (acabada). Amb
// outCubic, el bloc arrenca ràpid i s'atura suaument al seu lloc.
if (b.entry_ms_left > 0) {
const float T = 1.0F - (static_cast<float>(b.entry_ms_left) / static_cast<float>(ENTRY_MS));
const float T = 1.0F - (static_cast<float>(b.entry_ms_left) / static_cast<float>(UiConfig::get().hudEntryMs()));
const float E = Easing::outCubic(T);
const float REMAIN = 1.0F - E; // 1 al principi → 0 al final
off.x += static_cast<float>(b.entry_offset_x) * REMAIN;
@@ -174,7 +177,7 @@ auto Hud::computeOffset(const Block& b) -> SDL_FPoint {
// Shake: amplitud decau linealment amb el temps restant.
if (b.shake_ms_left > 0) {
const float DECAY = static_cast<float>(b.shake_ms_left) / static_cast<float>(SHAKE_MS);
const float DECAY = static_cast<float>(b.shake_ms_left) / static_cast<float>(UiConfig::get().hudShakeMs());
const auto JITTER = shakeOffset(b.shake_intensity * DECAY, b.shake_ms_left);
off.x += JITTER.x;
off.y += JITTER.y;
@@ -184,6 +187,14 @@ auto Hud::computeOffset(const Block& b) -> SDL_FPoint {
}
void Hud::render() const {
// Backdrop de la franja del HUD. Es pinta abans dels tres blocs perquè,
// quan un bloc es mou (entry slide o shake), la zona que deixa al
// descobert segueix mostrant el mateix color — en lloc del tilemap de
// sota, que causaria un trencament visual.
if (UiConfig::get().hudBgEnabled()) {
const auto& pal = Resource::Cache::get().getPalette("ui");
Screen::get().fillRect(0, 0, LEFT_W + CENTER_W + RIGHT_W, HEIGHT_PX, pal[UiConfig::get().hudBg()]);
}
renderBlock(BlockId::LEFT);
renderBlock(BlockId::CENTER);
renderBlock(BlockId::RIGHT);
@@ -196,16 +207,12 @@ void Hud::renderBlock(BlockId id) const {
const int X = b.x + static_cast<int>(std::round(OFF.x));
const int Y = b.y + static_cast<int>(std::round(OFF.y));
// Fons sòlid del bloc (mateixa convenció que `Notifier::render`):
// ARGB directe al canvas, sense tocar surfaces paletades.
Screen::get().fillRect(X, Y, b.w, b.h, ui_palette_[BG_COLOR]);
switch (b.mode) {
case Mode::IDLE:
if (id == BlockId::LEFT) {
renderIdleSide(X, Y, b.w, b, 0, "P1");
renderIdleSide(X, Y, b.w, b, 0, Locale::get().text("hud.p1"));
} else if (id == BlockId::RIGHT) {
renderIdleSide(X, Y, b.w, b, 1, "P2");
renderIdleSide(X, Y, b.w, b, 1, Locale::get().text("hud.p2"));
} else {
renderIdleCenter(X, Y, b.w);
}
@@ -218,24 +225,23 @@ void Hud::renderBlock(BlockId id) const {
}
}
void Hud::renderIdleSide(int x, int y, int w, const Block& blk, int player_index, const char* label) const {
void Hud::renderIdleSide(int x, int y, int w, const Block& blk, int player_index, const std::string& label) const {
const auto& p = players_[static_cast<std::size_t>(player_index)];
const Uint8 SCORE_C = (blk.flash_ms_left > 0) ? FLASH_COLOR : VALUE_COLOR;
const Uint8 SCORE_C = (blk.flash_ms_left > 0) ? UiConfig::get().hudFlash() : UiConfig::get().hudValue();
// --- Línia 1: "<label> 000000" centrada ---
const std::string LBL{label};
const std::string SCORE = formatScore(p.score);
const int LBL_W = font_->length(LBL);
const int LBL_W = font_->length(label);
const int SC_W = font_->lengthMonoDigits(SCORE, MONO_DIGIT_W);
const int LINE1_W = LBL_W + LABEL_GAP + SC_W;
int xx = x + ((w - LINE1_W) / 2);
font_->writeColored(xx, y + LINE1_Y, LBL, LABEL_COLOR);
font_->writeColored(xx, y + LINE1_Y, label, UiConfig::get().hudLabel());
xx += LBL_W + LABEL_GAP;
font_->writeMonoDigits(xx, y + LINE1_Y, SCORE, SCORE_C, MONO_DIGIT_W);
// --- Línia 2: "V <vides> T <tresors>" centrada ---
const std::string V_LBL{"V"};
const std::string T_LBL{"T"};
// --- Línia 2: "<vides> <n> <tresors> <n>" centrada ---
const std::string V_LBL = Locale::get().text("hud.lives");
const std::string T_LBL = Locale::get().text("hud.treasures");
const std::string V_VAL = std::to_string(std::clamp(p.lives, 0, 9));
const std::string T_VAL = formatTwoDigits(p.treasures);
const int V_LW = font_->length(V_LBL);
@@ -244,35 +250,35 @@ void Hud::renderIdleSide(int x, int y, int w, const Block& blk, int player_index
const int T_VW = font_->lengthMonoDigits(T_VAL, MONO_DIGIT_W);
const int LINE2_W = V_LW + LABEL_GAP + V_VW + FIELD_GAP + T_LW + LABEL_GAP + T_VW;
xx = x + ((w - LINE2_W) / 2);
font_->writeColored(xx, y + LINE2_Y, V_LBL, LABEL_COLOR);
font_->writeColored(xx, y + LINE2_Y, V_LBL, UiConfig::get().hudLabel());
xx += V_LW + LABEL_GAP;
font_->writeMonoDigits(xx, y + LINE2_Y, V_VAL, VALUE_COLOR, MONO_DIGIT_W);
font_->writeMonoDigits(xx, y + LINE2_Y, V_VAL, UiConfig::get().hudValue(), MONO_DIGIT_W);
xx += V_VW + FIELD_GAP;
font_->writeColored(xx, y + LINE2_Y, T_LBL, LABEL_COLOR);
font_->writeColored(xx, y + LINE2_Y, T_LBL, UiConfig::get().hudLabel());
xx += T_LW + LABEL_GAP;
font_->writeMonoDigits(xx, y + LINE2_Y, T_VAL, VALUE_COLOR, MONO_DIGIT_W);
font_->writeMonoDigits(xx, y + LINE2_Y, T_VAL, UiConfig::get().hudValue(), MONO_DIGIT_W);
}
void Hud::renderIdleCenter(int x, int y, int w) const {
// --- Línia 1: "PIRAMIDE <n>" centrada ---
const std::string PIR_LBL{"PIRAMIDE"};
// --- Línia 1: "<piramide> <n>" centrada ---
const std::string PIR_LBL = Locale::get().text("hud.pyramid");
const std::string PIR_VAL = std::to_string(pyramid_);
const int PL_W = font_->length(PIR_LBL);
const int PV_W = font_->lengthMonoDigits(PIR_VAL, MONO_DIGIT_W);
const int LINE1_W = PL_W + LABEL_GAP + PV_W;
int xx = x + ((w - LINE1_W) / 2);
font_->writeColored(xx, y + LINE1_Y, PIR_LBL, LABEL_COLOR);
font_->writeColored(xx, y + LINE1_Y, PIR_LBL, UiConfig::get().hudLabel());
xx += PL_W + LABEL_GAP;
font_->writeMonoDigits(xx, y + LINE1_Y, PIR_VAL, VALUE_COLOR, MONO_DIGIT_W);
font_->writeMonoDigits(xx, y + LINE1_Y, PIR_VAL, UiConfig::get().hudValue(), MONO_DIGIT_W);
// --- Línia 2: "SALA <r>" centrada ---
const std::string SALA_LBL{"SALA"};
// --- Línia 2: "<sala> <r>" centrada ---
const std::string SALA_LBL = Locale::get().text("hud.room");
const std::string SALA_VAL = std::to_string(room_);
const int SL_W = font_->length(SALA_LBL);
const int SV_W = font_->lengthMonoDigits(SALA_VAL, MONO_DIGIT_W);
const int LINE2_W = SL_W + LABEL_GAP + SV_W;
xx = x + ((w - LINE2_W) / 2);
font_->writeColored(xx, y + LINE2_Y, SALA_LBL, LABEL_COLOR);
font_->writeColored(xx, y + LINE2_Y, SALA_LBL, UiConfig::get().hudLabel());
xx += SL_W + LABEL_GAP;
font_->writeMonoDigits(xx, y + LINE2_Y, SALA_VAL, VALUE_COLOR, MONO_DIGIT_W);
font_->writeMonoDigits(xx, y + LINE2_Y, SALA_VAL, UiConfig::get().hudValue(), MONO_DIGIT_W);
}