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:
1 parent
82b229222f
commit
dc8a7723e3
26 files changed
+2593
-210
No files matched your search
@@ -11,7 +11,9 @@
|
||||
#include "core/rendering/notifier.hpp"
|
||||
#include "core/rendering/render_info.hpp"
|
||||
#include "core/rendering/screen.hpp"
|
||||
#include "core/rendering/screenshot.hpp"
|
||||
#include "core/system/event_buffer.hpp"
|
||||
#include "core/ui/ui_config.hpp"
|
||||
#include "game/options.hpp"
|
||||
#include "game/options_apply.hpp"
|
||||
#include "utils/defaults.hpp"
|
||||
@@ -24,7 +26,7 @@ namespace GlobalInputs {
|
||||
// La lliguem a la durada de la notificació EXIT perquè el missatge i
|
||||
// la confirmació expiren alhora — si no, l'usuari podria veure el
|
||||
// text però ja no ser dins del rang de confirmació (o al revés).
|
||||
constexpr float EXIT_WINDOW_S = static_cast<float>(Notification::EXIT.time_ms) / 1000.0F;
|
||||
// El valor concret viu a data/ui.yaml (timings.exit_ms) via UiConfig.
|
||||
constexpr int MIN_ZOOM = 1;
|
||||
constexpr int MAX_ZOOM = 6;
|
||||
|
||||
@@ -67,6 +69,18 @@ namespace GlobalInputs {
|
||||
notifyLocale(NEW_VSYNC ? "input.vsync_on" : "input.vsync_off");
|
||||
}
|
||||
|
||||
// Volca el canvas ARGB actual a PNG dins de `screenshots/`. Si ha
|
||||
// fallat l'escriptura, el handler no mostra notificació (retorn buit).
|
||||
void onScreenshot() {
|
||||
const auto* buffer = Screen::get().getCanvasArgb();
|
||||
const int W = Screen::get().getCanvasWidth();
|
||||
const int H = Screen::get().getCanvasHeight();
|
||||
const std::string FILENAME = Screenshot::save(buffer, W, H);
|
||||
if (!FILENAME.empty()) {
|
||||
Notification::notify(Locale::get().text("input.screenshot") + ' ' + FILENAME);
|
||||
}
|
||||
}
|
||||
|
||||
void onToggleShader() {
|
||||
const bool NEW_ENABLED = !Options::video().shader.enabled;
|
||||
Options::setShaderEnabled(NEW_ENABLED);
|
||||
@@ -140,17 +154,13 @@ namespace GlobalInputs {
|
||||
Options::setVideoRenderInfoPosition(static_cast<Defaults::RenderInfoPosition>(NEXT));
|
||||
}
|
||||
|
||||
void onToggleRenderTime() {
|
||||
Options::setVideoRenderInfoShowTime(!Options::video().render_info_show_time);
|
||||
}
|
||||
|
||||
void onExit() {
|
||||
if (exit_window_s > 0.0F) {
|
||||
quit_requested = true;
|
||||
return;
|
||||
}
|
||||
exit_window_s = EXIT_WINDOW_S;
|
||||
Notification::notify(Locale::get().text("input.press_again_exit"), Notification::EXIT);
|
||||
exit_window_s = static_cast<float>(UiConfig::get().exitMs()) / 1000.0F;
|
||||
Notification::notify(Locale::get().text("input.press_again_exit"), Notification::exitPreset());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -171,6 +181,8 @@ namespace GlobalInputs {
|
||||
onToggleShader();
|
||||
} else if (SC == KeyConfig::get().scancode("toggle_vsync")) {
|
||||
onToggleVSync();
|
||||
} else if (SC == KeyConfig::get().scancode("screenshot")) {
|
||||
onScreenshot();
|
||||
} else if (SC == KeyConfig::get().scancode("next_shader")) {
|
||||
onNextShader();
|
||||
} else if (SC == KeyConfig::get().scancode("next_preset")) {
|
||||
@@ -179,8 +191,6 @@ namespace GlobalInputs {
|
||||
onNextScalingMode();
|
||||
} else if (SC == KeyConfig::get().scancode("toggle_render_info")) {
|
||||
onCycleRenderInfo();
|
||||
} else if (SC == KeyConfig::get().scancode("toggle_render_time")) {
|
||||
onToggleRenderTime();
|
||||
} else if (SC == KeyConfig::get().scancode("exit")) {
|
||||
onExit();
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <string>
|
||||
|
||||
#include "core/rendering/notifier.hpp"
|
||||
#include "utils/defaults.hpp"
|
||||
#include "core/ui/ui_config.hpp"
|
||||
|
||||
// Temes per a les notificacions de pantalla. Un "preset" captura els dos
|
||||
// colors (text i fons, índexs sobre la paleta "ui") i el temps de visibilitat
|
||||
// en mil·lisegons. Per a llançar una notificació amb un preset concret,
|
||||
// cridar Notification::notify(text, preset); si s'omet, s'usa DEFAULT.
|
||||
// en mil·lisegons. Els valors concrets viuen a data/ui.yaml (carregat per
|
||||
// UiConfig); per llançar una notificació amb un preset concret, cridar
|
||||
// Notification::notify(text, preset). Sense preset s'usa el DEFAULT.
|
||||
namespace Notification {
|
||||
|
||||
struct Preset {
|
||||
@@ -19,27 +20,36 @@ namespace Notification {
|
||||
int time_ms;
|
||||
};
|
||||
|
||||
// Tema per defecte: text clar sobre fons fosc. Els valors concrets
|
||||
// viuen a `Defaults::UI` — tocar l'aparença global es fa allà.
|
||||
constexpr Preset DEFAULT{
|
||||
.text_color = Defaults::UI::NOTIFICATION_TEXT,
|
||||
.bg_color = Defaults::UI::NOTIFICATION_BG,
|
||||
.time_ms = Defaults::UI::NOTIFICATION_TIME_MS,
|
||||
};
|
||||
// Preset per defecte: text clar sobre fons fosc. Es construeix en cada
|
||||
// crida a partir d'UiConfig perquè els valors no són constexpr: viuen a
|
||||
// data/ui.yaml i es carreguen a l'arrencada.
|
||||
[[nodiscard]] inline auto defaultPreset() -> Preset {
|
||||
return {
|
||||
.text_color = UiConfig::get().notificationText(),
|
||||
.bg_color = UiConfig::get().notificationBg(),
|
||||
.time_ms = UiConfig::get().notificationMs(),
|
||||
};
|
||||
}
|
||||
|
||||
// Preset per al missatge de confirmació d'eixida (ESC doble). Color
|
||||
// d'accent per a distingir-lo d'una notificació informativa; la durada
|
||||
// d'accent per distingir-lo d'una notificació informativa; la durada
|
||||
// coincideix amb la finestra durant la qual la segona pulsació d'ESC
|
||||
// confirma el tancament — veure GlobalInputs::onExit.
|
||||
constexpr Preset EXIT{
|
||||
.text_color = Defaults::UI::EXIT_TEXT,
|
||||
.bg_color = Defaults::UI::EXIT_BG,
|
||||
.time_ms = Defaults::UI::EXIT_TIME_MS,
|
||||
};
|
||||
[[nodiscard]] inline auto exitPreset() -> Preset {
|
||||
return {
|
||||
.text_color = UiConfig::get().exitText(),
|
||||
.bg_color = UiConfig::get().exitBg(),
|
||||
.time_ms = UiConfig::get().exitMs(),
|
||||
};
|
||||
}
|
||||
|
||||
// Helper de conveniència: aplica un preset (per defecte DEFAULT) cridant
|
||||
// la API crua de Notifier. La direcció game → core és la correcta.
|
||||
inline void notify(const std::string& text, const Preset& preset = DEFAULT) {
|
||||
// Helper de conveniència: aplica un preset (per defecte el DEFAULT)
|
||||
// cridant la API crua de Notifier. La direcció game → core és correcta.
|
||||
inline void notify(const std::string& text) {
|
||||
const auto P = defaultPreset();
|
||||
Notifier::get().notify(text, P.text_color, P.bg_color, P.time_ms);
|
||||
}
|
||||
inline void notify(const std::string& text, const Preset& preset) {
|
||||
Notifier::get().notify(text, preset.text_color, preset.bg_color, preset.time_ms);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
#include "core/rendering/screenshot.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "external/stb_image_write.h"
|
||||
|
||||
namespace Screenshot {
|
||||
|
||||
namespace {
|
||||
|
||||
// Crea la carpeta `screenshots/` si no existeix i torna la ruta
|
||||
// completa al fitxer. El paràmetre `filename` rep el nom nu (sense
|
||||
// directori) per poder mostrar-lo a la notificació.
|
||||
auto generateFilePath(std::string& filename) -> std::string {
|
||||
std::filesystem::create_directories("screenshots");
|
||||
|
||||
const auto NOW = std::chrono::system_clock::now();
|
||||
const std::time_t TIME = std::chrono::system_clock::to_time_t(NOW);
|
||||
const std::tm* tm = std::localtime(&TIME);
|
||||
|
||||
char timestamp[20];
|
||||
std::strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", tm);
|
||||
|
||||
filename = std::string("scr_") + timestamp + ".png";
|
||||
return "screenshots/" + filename;
|
||||
}
|
||||
|
||||
// Converteix ARGB8888 → RGBA8888 in-place i escriu el PNG. stb_image_write
|
||||
// espera RGBA en ordre byte (little-endian: R al byte baix, A al byte alt).
|
||||
auto writePng(std::uint32_t* buffer, int width, int height, const std::string& filepath) -> bool {
|
||||
const int TOTAL = width * height;
|
||||
for (int i = 0; i < TOTAL; ++i) {
|
||||
const std::uint32_t P = buffer[i];
|
||||
const std::uint32_t A = (P >> 24) & 0xFF;
|
||||
const std::uint32_t R = (P >> 16) & 0xFF;
|
||||
const std::uint32_t G = (P >> 8) & 0xFF;
|
||||
const std::uint32_t B = P & 0xFF;
|
||||
buffer[i] = R | (G << 8) | (B << 16) | (A << 24);
|
||||
}
|
||||
// NOLINTNEXTLINE(clang-analyzer-optin.portability.UnixAPI) — fals positiu dins de stb_image_write
|
||||
return stbi_write_png(filepath.c_str(), width, height, 4, buffer, width * 4) != 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto save(const std::uint32_t* buffer, int width, int height) -> std::string {
|
||||
// Còpia local per no tocar el canvas original durant la conversió.
|
||||
std::vector<std::uint32_t> copy(buffer, buffer + (static_cast<size_t>(width) * height));
|
||||
|
||||
std::string filename;
|
||||
const std::string FILEPATH = generateFilePath(filename);
|
||||
|
||||
return writePng(copy.data(), width, height, FILEPATH) ? filename : "";
|
||||
}
|
||||
|
||||
} // namespace Screenshot
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
// Volca un buffer ARGB8888 a un fitxer PNG dins de la carpeta `screenshots/`
|
||||
// (relativa al CWD). El nom es genera amb un timestamp i la funció retorna
|
||||
// el nom del fitxer creat, o una cadena buida si l'escriptura ha fallat.
|
||||
namespace Screenshot {
|
||||
|
||||
auto save(const std::uint32_t* buffer, int width, int height) -> std::string;
|
||||
|
||||
} // namespace Screenshot
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
#include "core/scene/scene.hpp"
|
||||
#include "core/system/event_buffer.hpp"
|
||||
#include "core/ui/ui_config.hpp"
|
||||
#include "game/debug_config.hpp"
|
||||
#include "game/options.hpp"
|
||||
#include "game/scenes/game_scene.hpp"
|
||||
@@ -32,7 +33,6 @@
|
||||
#include "game/scenes/logo_scene.hpp"
|
||||
#include "game/scenes/title_scene.hpp"
|
||||
#include "game/ui/service_menu.hpp"
|
||||
#include "game/ui/ui_palette.hpp"
|
||||
#include "utils/defaults.hpp"
|
||||
|
||||
Director::Director() {
|
||||
@@ -97,15 +97,19 @@ Director::Director() {
|
||||
// Hotkeys globals. GlobalInputs::update() les consumirà a cada frame.
|
||||
KeyConfig::get().load("data/input/keys.yaml");
|
||||
|
||||
// Tema d'UI (colors, timings, fonts, flags). Qualsevol clau absent a
|
||||
// data/ui.yaml cau al valor compilat a Defaults::UI.
|
||||
UiConfig::get().load("data/ui.yaml");
|
||||
|
||||
// Recursos compartits d'UI (paleta + fonts). Es carreguen abans que els
|
||||
// overlays perquè tots els reaprofiten sense duplicar la lògica. Els
|
||||
// noms de font viuen a Defaults::UI::Font (un únic lloc per canviar-los).
|
||||
Resource::Cache::get().loadPalette("ui", "data/palettes/sega-cube.pal");
|
||||
const std::string HUD_FONT_GIF_KEY = std::string(Defaults::UI::Font::HUD_NAME) + ".gif";
|
||||
Resource::Cache::get().loadSurface(HUD_FONT_GIF_KEY, Defaults::UI::Font::HUD_GIF);
|
||||
// noms de font viuen al fitxer data/ui.yaml via UiConfig.
|
||||
Resource::Cache::get().loadPalette("ui", "data/palettes/uzebox.pal");
|
||||
const std::string HUD_FONT_GIF_KEY = UiConfig::get().hudFont().name + ".gif";
|
||||
Resource::Cache::get().loadSurface(HUD_FONT_GIF_KEY, UiConfig::get().hudFont().gif);
|
||||
Resource::Cache::get().getSurface(HUD_FONT_GIF_KEY)->setPalette(Resource::Cache::get().getPalette("ui"));
|
||||
const std::string PLACEHOLDER_FONT_GIF_KEY = std::string(Defaults::UI::Font::PLACEHOLDER_NAME) + ".gif";
|
||||
Resource::Cache::get().loadSurface(PLACEHOLDER_FONT_GIF_KEY, Defaults::UI::Font::PLACEHOLDER_GIF);
|
||||
const std::string PLACEHOLDER_FONT_GIF_KEY = UiConfig::get().placeholderFont().name + ".gif";
|
||||
Resource::Cache::get().loadSurface(PLACEHOLDER_FONT_GIF_KEY, UiConfig::get().placeholderFont().gif);
|
||||
Resource::Cache::get().getSurface(PLACEHOLDER_FONT_GIF_KEY)->setPalette(Resource::Cache::get().getPalette("ui"));
|
||||
|
||||
// Música de la piràmide 1 (primer i únic track de moment). Carreguem ací
|
||||
@@ -126,41 +130,33 @@ Director::Director() {
|
||||
// i la injectem als consumidors (Notifier, RenderInfo) per a que cap
|
||||
// d'ells hagi de conèixer els noms dels recursos concrets.
|
||||
auto hud_font_surface = Resource::Cache::get().getSurface(HUD_FONT_GIF_KEY);
|
||||
auto hud_text = std::make_shared<Text>(hud_font_surface, Defaults::UI::Font::HUD_FNT);
|
||||
hud_text->setDefaultKerning(Defaults::UI::Font::HUD_KERNING);
|
||||
auto hud_text = std::make_shared<Text>(hud_font_surface, UiConfig::get().hudFont().fnt);
|
||||
hud_text->setDefaultKerning(UiConfig::get().hudFont().kerning);
|
||||
const auto& ui_palette = Resource::Cache::get().getPalette("ui");
|
||||
|
||||
RenderInfo::get().init(Options::buildRenderInfoFrame(UiColor::RENDER_INFO_TEXT, UiColor::RENDER_INFO_SHADOW), hud_text);
|
||||
RenderInfo::get().init(Options::buildRenderInfoFrame(UiConfig::get().renderInfoText(), UiConfig::get().renderInfoShadow()), hud_text);
|
||||
Notifier::get().init(hud_text, ui_palette);
|
||||
|
||||
// Font dels placeholders (LOGO / TITLE / INTRO). És una font distinta a
|
||||
// la de HUD perquè els placeholders desapareixeran amb les seues escenes
|
||||
// finals; mentrestant tenen estètica pròpia.
|
||||
auto placeholder_font_surface = Resource::Cache::get().getSurface(PLACEHOLDER_FONT_GIF_KEY);
|
||||
auto placeholder_text = std::make_shared<Text>(placeholder_font_surface, Defaults::UI::Font::PLACEHOLDER_FNT);
|
||||
placeholder_text->setDefaultKerning(Defaults::UI::Font::PLACEHOLDER_KERNING);
|
||||
auto placeholder_text = std::make_shared<Text>(placeholder_font_surface, UiConfig::get().placeholderFont().fnt);
|
||||
placeholder_text->setDefaultKerning(UiConfig::get().placeholderFont().kerning);
|
||||
|
||||
// Font del menú de servei (F9). El títol va amb la variant `display`
|
||||
// i els items amb `condensed-display` (més compacte perquè caben més
|
||||
// files). Els dos noms i kernings viuen a `Defaults::UI::Font`.
|
||||
auto load_menu_font = [&](const char* name, const char* gif, const char* fnt, int kerning) {
|
||||
const std::string KEY = std::string(name) + ".gif";
|
||||
Resource::Cache::get().loadSurface(KEY, gif);
|
||||
// files). Els dos noms i kernings viuen a data/ui.yaml via UiConfig.
|
||||
auto load_menu_font = [&](const UiConfig::Font& font) {
|
||||
const std::string KEY = font.name + ".gif";
|
||||
Resource::Cache::get().loadSurface(KEY, font.gif);
|
||||
Resource::Cache::get().getSurface(KEY)->setPalette(Resource::Cache::get().getPalette("ui"));
|
||||
auto text = std::make_shared<Text>(Resource::Cache::get().getSurface(KEY), fnt);
|
||||
text->setDefaultKerning(kerning);
|
||||
auto text = std::make_shared<Text>(Resource::Cache::get().getSurface(KEY), font.fnt);
|
||||
text->setDefaultKerning(font.kerning);
|
||||
return text;
|
||||
};
|
||||
auto menu_title_text = load_menu_font(
|
||||
Defaults::UI::Font::MENU_TITLE_NAME,
|
||||
Defaults::UI::Font::MENU_TITLE_GIF,
|
||||
Defaults::UI::Font::MENU_TITLE_FNT,
|
||||
Defaults::UI::Font::MENU_TITLE_KERNING);
|
||||
auto menu_items_text = load_menu_font(
|
||||
Defaults::UI::Font::MENU_ITEMS_NAME,
|
||||
Defaults::UI::Font::MENU_ITEMS_GIF,
|
||||
Defaults::UI::Font::MENU_ITEMS_FNT,
|
||||
Defaults::UI::Font::MENU_ITEMS_KERNING);
|
||||
auto menu_title_text = load_menu_font(UiConfig::get().menuTitleFont());
|
||||
auto menu_items_text = load_menu_font(UiConfig::get().menuItemsFont());
|
||||
ServiceMenu::get().init(menu_title_text, menu_items_text, ui_palette);
|
||||
|
||||
// Overlay hook: Screen::present() cridarà aquesta lambda al principi,
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
#include "core/ui/ui_config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
#include "external/fkyaml_node.hpp"
|
||||
#include "utils/defaults.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
// Lector de camp amb fallback: si la clau existeix al node, escriu a
|
||||
// `dst` el valor convertit; si no, deixa `dst` com estava (que al
|
||||
// constructor ja hem inicialitzat amb Defaults::UI).
|
||||
template <typename T>
|
||||
void readField(const fkyaml::node& node, const char* key, T& dst) {
|
||||
if (node.contains(key)) { dst = node[key].get_value<T>(); }
|
||||
}
|
||||
|
||||
// Variant per a Uint8 — fkyaml no serialitza a Uint8 directament, cal
|
||||
// passar per int i encaixar al rang 0..255 (valors fora de rang es
|
||||
// trunquen i s'avisen).
|
||||
void readColorIndex(const fkyaml::node& node, const char* key, Uint8& dst) {
|
||||
if (!node.contains(key)) { return; }
|
||||
const int VALUE = node[key].get_value<int>();
|
||||
if (VALUE < 0 || VALUE > 255) {
|
||||
std::cerr << "UiConfig: índex '" << key << "'=" << VALUE << " fora de rang 0..255 — ignorat\n";
|
||||
return;
|
||||
}
|
||||
dst = static_cast<Uint8>(VALUE);
|
||||
}
|
||||
|
||||
void readFont(const fkyaml::node& node, const char* key, UiConfig::Font& dst) {
|
||||
if (!node.contains(key)) { return; }
|
||||
const auto& f = node[key];
|
||||
readField(f, "name", dst.name);
|
||||
readField(f, "gif", dst.gif);
|
||||
readField(f, "fnt", dst.fnt);
|
||||
readField(f, "kerning", dst.kerning);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
UiConfig::UiConfig()
|
||||
: render_info_text_(Defaults::UI::RENDER_INFO_TEXT),
|
||||
render_info_shadow_(Defaults::UI::RENDER_INFO_SHADOW),
|
||||
canvas_bg_(Defaults::UI::CANVAS_BG),
|
||||
notification_text_(Defaults::UI::NOTIFICATION_TEXT),
|
||||
notification_bg_(Defaults::UI::NOTIFICATION_BG),
|
||||
exit_text_(Defaults::UI::EXIT_TEXT),
|
||||
exit_bg_(Defaults::UI::EXIT_BG),
|
||||
menu_bg_(Defaults::UI::MENU_BG),
|
||||
menu_border_(Defaults::UI::MENU_BORDER),
|
||||
menu_title_(Defaults::UI::MENU_TITLE),
|
||||
menu_label_(Defaults::UI::MENU_LABEL),
|
||||
menu_value_(Defaults::UI::MENU_VALUE),
|
||||
menu_cursor_(Defaults::UI::MENU_CURSOR),
|
||||
menu_disabled_(Defaults::UI::MENU_DISABLED),
|
||||
hud_bg_(Defaults::UI::HUD_BG),
|
||||
hud_label_(Defaults::UI::HUD_LABEL),
|
||||
hud_value_(Defaults::UI::HUD_VALUE),
|
||||
hud_flash_(Defaults::UI::HUD_FLASH),
|
||||
notification_ms_(Defaults::UI::NOTIFICATION_TIME_MS),
|
||||
exit_ms_(Defaults::UI::EXIT_TIME_MS),
|
||||
hud_entry_ms_(Defaults::UI::HUD_ENTRY_MS),
|
||||
hud_flash_ms_(Defaults::UI::HUD_FLASH_MS),
|
||||
hud_shake_ms_(Defaults::UI::HUD_SHAKE_MS),
|
||||
hud_font_{.name = Defaults::UI::Font::HUD_NAME, .gif = Defaults::UI::Font::HUD_GIF, .fnt = Defaults::UI::Font::HUD_FNT, .kerning = Defaults::UI::Font::HUD_KERNING},
|
||||
placeholder_font_{.name = Defaults::UI::Font::PLACEHOLDER_NAME, .gif = Defaults::UI::Font::PLACEHOLDER_GIF, .fnt = Defaults::UI::Font::PLACEHOLDER_FNT, .kerning = Defaults::UI::Font::PLACEHOLDER_KERNING},
|
||||
menu_title_font_{.name = Defaults::UI::Font::MENU_TITLE_NAME, .gif = Defaults::UI::Font::MENU_TITLE_GIF, .fnt = Defaults::UI::Font::MENU_TITLE_FNT, .kerning = Defaults::UI::Font::MENU_TITLE_KERNING},
|
||||
menu_items_font_{.name = Defaults::UI::Font::MENU_ITEMS_NAME, .gif = Defaults::UI::Font::MENU_ITEMS_GIF, .fnt = Defaults::UI::Font::MENU_ITEMS_FNT, .kerning = Defaults::UI::Font::MENU_ITEMS_KERNING} {
|
||||
}
|
||||
|
||||
auto UiConfig::get() -> UiConfig& {
|
||||
static UiConfig instance_;
|
||||
return instance_;
|
||||
}
|
||||
|
||||
void UiConfig::load(const std::string& file_path) {
|
||||
auto bytes = Resource::Helper::loadFile(file_path);
|
||||
if (bytes.empty()) {
|
||||
std::cerr << "UiConfig: no s'ha pogut obrir " << file_path << " — s'usaran els defaults compilats\n";
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const auto* begin = reinterpret_cast<const char*>(bytes.data());
|
||||
const auto* end = begin + bytes.size();
|
||||
auto yaml = fkyaml::node::deserialize(begin, end);
|
||||
|
||||
if (yaml.contains("colors")) {
|
||||
const auto& c = yaml["colors"];
|
||||
readColorIndex(c, "render_info_text", render_info_text_);
|
||||
readColorIndex(c, "render_info_shadow", render_info_shadow_);
|
||||
readColorIndex(c, "canvas_bg", canvas_bg_);
|
||||
readColorIndex(c, "notification_text", notification_text_);
|
||||
readColorIndex(c, "notification_bg", notification_bg_);
|
||||
readColorIndex(c, "exit_text", exit_text_);
|
||||
readColorIndex(c, "exit_bg", exit_bg_);
|
||||
readColorIndex(c, "menu_bg", menu_bg_);
|
||||
readColorIndex(c, "menu_border", menu_border_);
|
||||
readColorIndex(c, "menu_title", menu_title_);
|
||||
readColorIndex(c, "menu_label", menu_label_);
|
||||
readColorIndex(c, "menu_value", menu_value_);
|
||||
readColorIndex(c, "menu_cursor", menu_cursor_);
|
||||
readColorIndex(c, "menu_disabled", menu_disabled_);
|
||||
readColorIndex(c, "hud_bg", hud_bg_);
|
||||
readColorIndex(c, "hud_label", hud_label_);
|
||||
readColorIndex(c, "hud_value", hud_value_);
|
||||
readColorIndex(c, "hud_flash", hud_flash_);
|
||||
}
|
||||
|
||||
if (yaml.contains("timings")) {
|
||||
const auto& t = yaml["timings"];
|
||||
readField(t, "notification_ms", notification_ms_);
|
||||
readField(t, "exit_ms", exit_ms_);
|
||||
}
|
||||
|
||||
if (yaml.contains("fonts")) {
|
||||
const auto& f = yaml["fonts"];
|
||||
readFont(f, "hud", hud_font_);
|
||||
readFont(f, "placeholder", placeholder_font_);
|
||||
readFont(f, "menu_title", menu_title_font_);
|
||||
readFont(f, "menu_items", menu_items_font_);
|
||||
}
|
||||
|
||||
if (yaml.contains("hud")) {
|
||||
const auto& h = yaml["hud"];
|
||||
readField(h, "bg_enabled", hud_bg_enabled_);
|
||||
readField(h, "entry_ms", hud_entry_ms_);
|
||||
readField(h, "flash_ms", hud_flash_ms_);
|
||||
readField(h, "shake_ms", hud_shake_ms_);
|
||||
}
|
||||
|
||||
std::cout << "UiConfig: carregat des de " << file_path << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "UiConfig: error parsejant " << file_path << ": " << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
// Configuració del tema UI (índexs de paleta, timings, fonts i flags)
|
||||
// carregada des de data/ui.yaml. Cada camp s'inicialitza amb el valor
|
||||
// de Defaults::UI com a fallback: si el fitxer falta o li manca una
|
||||
// clau, el joc s'executa amb el tema compilat.
|
||||
//
|
||||
// Patró idèntic a Locale i KeyConfig: Meyers singleton + load(path).
|
||||
// No hi ha hot-reload — els canvis al YAML requereixen reiniciar.
|
||||
class UiConfig {
|
||||
public:
|
||||
struct Font {
|
||||
std::string name;
|
||||
std::string gif;
|
||||
std::string fnt;
|
||||
int kerning;
|
||||
};
|
||||
|
||||
static auto get() -> UiConfig&;
|
||||
|
||||
UiConfig(const UiConfig&) = delete;
|
||||
UiConfig(UiConfig&&) = delete;
|
||||
auto operator=(const UiConfig&) -> UiConfig& = delete;
|
||||
auto operator=(UiConfig&&) -> UiConfig& = delete;
|
||||
|
||||
void load(const std::string& file_path);
|
||||
|
||||
// Colors — índexs sobre la paleta "ui" (Resource::Cache).
|
||||
[[nodiscard]] auto renderInfoText() const -> Uint8 { return render_info_text_; }
|
||||
[[nodiscard]] auto renderInfoShadow() const -> Uint8 { return render_info_shadow_; }
|
||||
[[nodiscard]] auto canvasBg() const -> Uint8 { return canvas_bg_; }
|
||||
[[nodiscard]] auto notificationText() const -> Uint8 { return notification_text_; }
|
||||
[[nodiscard]] auto notificationBg() const -> Uint8 { return notification_bg_; }
|
||||
[[nodiscard]] auto exitText() const -> Uint8 { return exit_text_; }
|
||||
[[nodiscard]] auto exitBg() const -> Uint8 { return exit_bg_; }
|
||||
[[nodiscard]] auto menuBg() const -> Uint8 { return menu_bg_; }
|
||||
[[nodiscard]] auto menuBorder() const -> Uint8 { return menu_border_; }
|
||||
[[nodiscard]] auto menuTitle() const -> Uint8 { return menu_title_; }
|
||||
[[nodiscard]] auto menuLabel() const -> Uint8 { return menu_label_; }
|
||||
[[nodiscard]] auto menuValue() const -> Uint8 { return menu_value_; }
|
||||
[[nodiscard]] auto menuCursor() const -> Uint8 { return menu_cursor_; }
|
||||
[[nodiscard]] auto menuDisabled() const -> Uint8 { return menu_disabled_; }
|
||||
[[nodiscard]] auto hudBg() const -> Uint8 { return hud_bg_; }
|
||||
[[nodiscard]] auto hudLabel() const -> Uint8 { return hud_label_; }
|
||||
[[nodiscard]] auto hudValue() const -> Uint8 { return hud_value_; }
|
||||
[[nodiscard]] auto hudFlash() const -> Uint8 { return hud_flash_; }
|
||||
|
||||
// Timings (ms).
|
||||
[[nodiscard]] auto notificationMs() const -> int { return notification_ms_; }
|
||||
[[nodiscard]] auto exitMs() const -> int { return exit_ms_; }
|
||||
[[nodiscard]] auto hudEntryMs() const -> int { return hud_entry_ms_; }
|
||||
[[nodiscard]] auto hudFlashMs() const -> int { return hud_flash_ms_; }
|
||||
[[nodiscard]] auto hudShakeMs() const -> int { return hud_shake_ms_; }
|
||||
|
||||
// Fonts de UI.
|
||||
[[nodiscard]] auto hudFont() const -> const Font& { return hud_font_; }
|
||||
[[nodiscard]] auto placeholderFont() const -> const Font& { return placeholder_font_; }
|
||||
[[nodiscard]] auto menuTitleFont() const -> const Font& { return menu_title_font_; }
|
||||
[[nodiscard]] auto menuItemsFont() const -> const Font& { return menu_items_font_; }
|
||||
|
||||
// Flags visuals.
|
||||
[[nodiscard]] auto hudBgEnabled() const -> bool { return hud_bg_enabled_; }
|
||||
|
||||
private:
|
||||
UiConfig();
|
||||
~UiConfig() = default;
|
||||
|
||||
Uint8 render_info_text_;
|
||||
Uint8 render_info_shadow_;
|
||||
Uint8 canvas_bg_;
|
||||
Uint8 notification_text_;
|
||||
Uint8 notification_bg_;
|
||||
Uint8 exit_text_;
|
||||
Uint8 exit_bg_;
|
||||
Uint8 menu_bg_;
|
||||
Uint8 menu_border_;
|
||||
Uint8 menu_title_;
|
||||
Uint8 menu_label_;
|
||||
Uint8 menu_value_;
|
||||
Uint8 menu_cursor_;
|
||||
Uint8 menu_disabled_;
|
||||
Uint8 hud_bg_;
|
||||
Uint8 hud_label_;
|
||||
Uint8 hud_value_;
|
||||
Uint8 hud_flash_;
|
||||
|
||||
int notification_ms_;
|
||||
int exit_ms_;
|
||||
int hud_entry_ms_;
|
||||
int hud_flash_ms_;
|
||||
int hud_shake_ms_;
|
||||
|
||||
Font hud_font_;
|
||||
Font placeholder_font_;
|
||||
Font menu_title_font_;
|
||||
Font menu_items_font_;
|
||||
|
||||
bool hud_bg_enabled_{false};
|
||||
};
|
||||
Reference in new issue
Block a user