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);
}
+6 -15
View File
@@ -5,9 +5,9 @@
#include <array>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "core/rendering/surface.hpp" // Palette
#include "game/entities/event_bus.hpp"
class Text;
@@ -41,7 +41,7 @@ class Hud {
WIN,
EMPTY };
Hud(EventBus& bus, std::shared_ptr<Text> font, const Palette& ui_palette);
Hud(EventBus& bus, std::shared_ptr<Text> font);
~Hud();
Hud(const Hud&) = delete;
@@ -89,15 +89,6 @@ class Hud {
static constexpr int LINE1_Y = 4;
static constexpr int LINE2_Y = 13;
// Durada de l'animació d'entrada — els tres blocs alhora, amb easing
// outCubic. 400 ms és prou per a percebre el moviment sense fer
// esperar massa al jugador.
static constexpr int ENTRY_MS = 400;
// Durades per defecte d'efectes transitoris.
static constexpr int FLASH_MS = 250;
static constexpr int SHAKE_MS = 300;
// Vides inicials per jugador (placeholder fins que GameScene ho
// configure des del Config del nivell).
static constexpr int DEFAULT_LIVES = 3;
@@ -112,8 +103,9 @@ class Hud {
int h{0};
// Animació d'entrada: ms restants fins a quedar-se a la posició
// final. Quan val 0 el bloc està quiet.
int entry_ms_left{ENTRY_MS};
// final. Quan val 0 el bloc està quiet. El Hud ctor l'inicialitza
// amb la durada viva a data/ui.yaml (hud.entry_ms).
int entry_ms_left{0};
// Vector inicial des d'on entra: {-LEFT_W,0} per al bloc esquerre,
// {0,-HEIGHT_PX} per al central i {+RIGHT_W,0} per al dret.
int entry_offset_x{0};
@@ -143,7 +135,7 @@ class Hud {
// Subrutines del mode `Idle` per als tres blocs. Reben el rectangle
// ja desplaçat (x, y) on han d'escriure.
void renderIdleSide(int x, int y, int w, const Block& blk, int player_index, const char* label) const;
void renderIdleSide(int x, int y, int w, const Block& blk, int player_index, const std::string& label) const;
void renderIdleCenter(int x, int y, int w) const;
// Calcula l'offset transitori (entrada + shake) en píxels per a un
@@ -155,7 +147,6 @@ class Hud {
std::vector<EventBus::SubId> subs_;
std::shared_ptr<Text> font_;
Palette ui_palette_{};
std::array<Block, 3> blocks_;
std::array<PlayerStats, 2> players_;
+11 -11
View File
@@ -14,9 +14,9 @@
#include "core/rendering/screen.hpp"
#include "core/rendering/text.hpp"
#include "core/system/event_buffer.hpp"
#include "core/ui/ui_config.hpp"
#include "game/options.hpp"
#include "game/options_apply.hpp"
#include "game/ui/ui_palette.hpp"
#include "utils/defaults.hpp"
#include "utils/defines.hpp"
#include "version.h"
@@ -844,8 +844,8 @@ auto ServiceMenu::targetBoxHeight(const Page& page) const -> int {
}
void ServiceMenu::renderBox(int box_x, int box_y, int box_w, int box_h, float eased) const {
const std::uint32_t BG_ARGB = ui_palette_[UiColor::MENU_BG];
const std::uint32_t BORDER_ARGB = ui_palette_[UiColor::MENU_BORDER];
const std::uint32_t BG_ARGB = ui_palette_[UiConfig::get().menuBg()];
const std::uint32_t BORDER_ARGB = ui_palette_[UiConfig::get().menuBorder()];
const auto ALPHA = static_cast<std::uint8_t>(std::lround(BG_ALPHA * eased));
// Fons semi-transparent (el joc queda visible apagat per darrere).
@@ -877,7 +877,7 @@ void ServiceMenu::renderPage(const Page& page, int box_x, int box_y, int box_w)
cursor_y,
Locale::get().text(page.title_key),
TITLE_KERNING,
UiColor::MENU_TITLE);
UiConfig::get().menuTitle());
cursor_y += TITLE_H + GAP_Y - TITLE_DESCENDER_TRIM;
if (!page.subtitle.empty()) {
@@ -886,11 +886,11 @@ void ServiceMenu::renderPage(const Page& page, int box_x, int box_y, int box_w)
cursor_y,
page.subtitle,
TITLE_KERNING,
UiColor::MENU_TITLE);
UiConfig::get().menuTitle());
cursor_y += TITLE_H + GAP_Y - TITLE_DESCENDER_TRIM;
}
Screen::get().fillRect(box_x + PAD_X, cursor_y, box_w - (2 * PAD_X), 1, ui_palette_[UiColor::MENU_BORDER]);
Screen::get().fillRect(box_x + PAD_X, cursor_y, box_w - (2 * PAD_X), 1, ui_palette_[UiConfig::get().menuBorder()]);
cursor_y += 1 + GAP_Y;
bool first_item = true;
@@ -907,14 +907,14 @@ void ServiceMenu::renderPage(const Page& page, int box_x, int box_y, int box_w)
: Locale::get().text(item.label_key);
const std::string VALUE = formatValue(item);
Uint8 label_color = UiColor::MENU_LABEL;
Uint8 label_color = UiConfig::get().menuLabel();
if (item.kind == Item::Kind::LABEL) {
// Mateix to que el títol de la pàgina: les LABELs actuen com
// a subtítol-divisor (p.ex. "Jugador 1" a Teclat, "ver.X.Y" a
// Sistema) i han de tenir la mateixa presència visual.
label_color = UiColor::MENU_TITLE;
label_color = UiConfig::get().menuTitle();
} else if (SELECTED) {
label_color = UiColor::MENU_CURSOR;
label_color = UiConfig::get().menuCursor();
}
int label_x = LABEL_X;
@@ -931,7 +931,7 @@ void ServiceMenu::renderPage(const Page& page, int box_x, int box_y, int box_w)
cursor_y,
"> ",
ITEM_KERNING,
UiColor::MENU_CURSOR);
UiConfig::get().menuCursor());
}
items_font_->writeDX(Text::COLOR_FLAG,
@@ -951,7 +951,7 @@ void ServiceMenu::renderPage(const Page& page, int box_x, int box_y, int box_w)
cursor_y,
VALUE,
ITEM_KERNING,
SELECTED ? UiColor::MENU_CURSOR : UiColor::MENU_VALUE);
SELECTED ? UiConfig::get().menuCursor() : UiConfig::get().menuValue());
}
cursor_y += ITEM_H;
-22
View File
@@ -1,22 +0,0 @@
#pragma once
#include "utils/defaults.hpp"
// Alias semàntics als índexs de paleta UI (viuen a `Defaults::UI`). Canviar
// l'aparença d'un component només requereix tocar-los allà; ací només
// reexportem amb els noms que consumeix el codi del joc.
namespace UiColor {
constexpr auto RENDER_INFO_TEXT = Defaults::UI::RENDER_INFO_TEXT;
constexpr auto RENDER_INFO_SHADOW = Defaults::UI::RENDER_INFO_SHADOW;
constexpr auto CANVAS_BG = Defaults::UI::CANVAS_BG;
constexpr auto MENU_BG = Defaults::UI::MENU_BG;
constexpr auto MENU_BORDER = Defaults::UI::MENU_BORDER;
constexpr auto MENU_TITLE = Defaults::UI::MENU_TITLE;
constexpr auto MENU_LABEL = Defaults::UI::MENU_LABEL;
constexpr auto MENU_VALUE = Defaults::UI::MENU_VALUE;
constexpr auto MENU_CURSOR = Defaults::UI::MENU_CURSOR;
constexpr auto MENU_DISABLED = Defaults::UI::MENU_DISABLED;
} // namespace UiColor