677 lines
32 KiB
C++
677 lines
32 KiB
C++
#include "core/rendering/menu.hpp"
|
|
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "core/input/key_config.hpp"
|
|
#include "core/locale/locale.hpp"
|
|
#include "core/rendering/overlay.hpp"
|
|
#include "core/rendering/screen.hpp"
|
|
#include "core/rendering/text.hpp"
|
|
#include "core/system/director.hpp"
|
|
#include "game/defines.hpp"
|
|
#include "game/options.hpp"
|
|
#include "utils/easing.hpp"
|
|
#include "version.h"
|
|
|
|
namespace Menu {
|
|
|
|
// --- Constants visuals ---
|
|
static constexpr int SCREEN_W = 320;
|
|
static constexpr int SCREEN_H = 200;
|
|
|
|
static constexpr int BOX_W = 220;
|
|
|
|
static constexpr Uint32 BG_COLOR = 0xFF1A0E0E; // fons marró fosc (ABGR)
|
|
static constexpr Uint8 BG_ALPHA = 220; // semi-transparent
|
|
static constexpr Uint32 BORDER_COLOR = 0xFFFFFF00; // cyan
|
|
static constexpr Uint32 TITLE_COLOR = 0xFFFFFFFF; // blanc
|
|
static constexpr Uint32 LABEL_COLOR = 0xFFCCCCCC; // gris clar
|
|
static constexpr Uint32 VALUE_COLOR = 0xFFFFFF00; // cyan
|
|
static constexpr Uint32 CURSOR_COLOR = 0xFF00FFFF; // groc
|
|
static constexpr Uint32 EMPTY_COLOR = 0xFF888888; // gris
|
|
|
|
static constexpr int TITLE_PAD_Y = 4;
|
|
static constexpr int ITEM_PAD_X = 10;
|
|
static constexpr int ITEM_SPACING = 11;
|
|
static constexpr int BOTTOM_PAD = 6;
|
|
static constexpr int HEADER_H = TITLE_PAD_Y + 8 /*charH*/ + 2 + 4; // títol + línia + gap
|
|
static constexpr int SUBTITLE_H = 8 + 3; // línia de subtítol + gap
|
|
|
|
// --- Animació ---
|
|
static constexpr float OPEN_SPEED = 8.0F; // 1.0 / 0.125s
|
|
static constexpr float CLOSE_SPEED = 10.0F; // 1.0 / 0.1s (una mica més ràpida que l'obertura)
|
|
static constexpr float HEIGHT_RATE = 12.0F; // smoothing exponencial de l'alçada (~150 ms al 90%)
|
|
|
|
// --- Items ---
|
|
enum class ItemKind { Toggle,
|
|
Cycle,
|
|
IntRange,
|
|
Submenu,
|
|
KeyBind,
|
|
Action };
|
|
|
|
struct Item {
|
|
const char* label;
|
|
ItemKind kind;
|
|
std::function<std::string()> getValue; // opcional
|
|
std::function<void(int dir)> change; // per Toggle/Cycle/IntRange
|
|
std::function<void()> enter; // per Submenu i Action
|
|
SDL_Scancode* scancode{nullptr}; // per KeyBind
|
|
std::function<bool()> visible; // nullptr ⇒ sempre visible
|
|
};
|
|
|
|
struct Page {
|
|
const char* title;
|
|
std::vector<Item> items;
|
|
int cursor{0};
|
|
std::string subtitle; // opcional — si no buit, es dibuixa sota el títol
|
|
};
|
|
|
|
static bool isVisible(const Item& it) { return !it.visible || it.visible(); }
|
|
|
|
// Troba el pròxim ítem visible en direcció `dir` (±1) a partir de `from`.
|
|
// Si cap és visible retorna `from`.
|
|
static int nextVisibleCursor(const Page& p, int from, int dir) {
|
|
const int n = static_cast<int>(p.items.size());
|
|
if (n <= 0) return from;
|
|
for (int i = 1; i <= n; ++i) {
|
|
int idx = ((from + dir * i) % n + n) % n;
|
|
if (isVisible(p.items[idx])) return idx;
|
|
}
|
|
return from;
|
|
}
|
|
|
|
// --- Estat ---
|
|
static std::vector<Page> stack_;
|
|
static std::unique_ptr<Text> font_;
|
|
static float open_anim_{0.0F}; // 0 = tancat, 1 = obert
|
|
static float animated_h_{0.0F}; // alçada actual animada (smoothing cap al target visible)
|
|
static Uint32 last_ticks_{0};
|
|
static SDL_Scancode* capturing_{nullptr}; // != null → esperant tecla per assignar
|
|
static bool closing_{false}; // true mentre l'animació de tancament és en curs
|
|
|
|
// --- Transició entre pàgines ---
|
|
static constexpr float TRANSITION_SPEED = 5.5F; // ~180 ms
|
|
static Page transition_outgoing_{"", {}, 0};
|
|
static bool transition_active_{false};
|
|
static float transition_progress_{1.0F};
|
|
static int transition_dir_{+1}; // +1 endavant, -1 enrere
|
|
|
|
// Helpers per triggerar transicions
|
|
static void pushPage(Page newPage) {
|
|
transition_outgoing_ = stack_.back();
|
|
stack_.push_back(std::move(newPage));
|
|
transition_active_ = true;
|
|
transition_progress_ = 0.0F;
|
|
transition_dir_ = +1;
|
|
}
|
|
static void popPage() {
|
|
transition_outgoing_ = stack_.back();
|
|
stack_.pop_back();
|
|
transition_active_ = true;
|
|
transition_progress_ = 0.0F;
|
|
transition_dir_ = -1;
|
|
}
|
|
|
|
// --- Helpers ---
|
|
|
|
static std::string yesNo(bool b) { return b ? Locale::get("menu.values.yes") : Locale::get("menu.values.no"); }
|
|
static std::string onOff(bool b) { return b ? Locale::get("menu.values.on") : Locale::get("menu.values.off"); }
|
|
|
|
// --- Builders de pàgines ---
|
|
|
|
static Page buildVideo();
|
|
static Page buildAudio();
|
|
static Page buildControls();
|
|
static Page buildGame();
|
|
static Page buildSystem();
|
|
|
|
static Page buildRoot() {
|
|
Page p{Locale::get("menu.titles.root"), {}, 0};
|
|
p.items.push_back({Locale::get("menu.items.video"), ItemKind::Submenu, nullptr, nullptr, [] { pushPage(buildVideo()); }, nullptr});
|
|
p.items.push_back({Locale::get("menu.items.audio"), ItemKind::Submenu, nullptr, nullptr, [] { pushPage(buildAudio()); }, nullptr});
|
|
p.items.push_back({Locale::get("menu.items.controls"), ItemKind::Submenu, nullptr, nullptr, [] { pushPage(buildControls()); }, nullptr});
|
|
p.items.push_back({Locale::get("menu.items.game"), ItemKind::Submenu, nullptr, nullptr, [] { pushPage(buildGame()); }, nullptr});
|
|
p.items.push_back({Locale::get("menu.items.system"), ItemKind::Submenu, nullptr, nullptr, [] { pushPage(buildSystem()); }, nullptr});
|
|
return p;
|
|
}
|
|
|
|
static Page buildVideo() {
|
|
Page p{Locale::get("menu.titles.video"), {}, 0};
|
|
|
|
// Zoom i fullscreen: sense sentit a WASM (el navegador posseix el canvas)
|
|
#ifndef __EMSCRIPTEN__
|
|
p.items.push_back({Locale::get("menu.items.zoom"), ItemKind::IntRange, [] {
|
|
char buf[16];
|
|
std::snprintf(buf, sizeof(buf), "%dX", Screen::get()->getZoom());
|
|
return std::string(buf); }, [](int dir) {
|
|
if (dir < 0) Screen::get()->decZoom();
|
|
else if (dir > 0) Screen::get()->incZoom(); }, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.screen"), ItemKind::Toggle, [] { return std::string(Screen::get()->isFullscreen() ? Locale::get("menu.values.fullscreen") : Locale::get("menu.values.windowed")); }, [](int) { Screen::get()->toggleFullscreen(); }, nullptr, nullptr, nullptr});
|
|
#endif
|
|
|
|
// Opcions visuals generals (sempre visibles)
|
|
p.items.push_back({Locale::get("menu.items.aspect_4_3"), ItemKind::Toggle, [] { return yesNo(Options::video.aspect_ratio_4_3); }, [](int) { Screen::get()->toggleAspectRatio(); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.vsync"), ItemKind::Toggle, [] { return onOff(Options::video.vsync); }, [](int) { Screen::get()->toggleVSync(); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.scaling_mode"), ItemKind::Cycle, [] {
|
|
switch (Options::video.scaling_mode) {
|
|
case Options::ScalingMode::DISABLED: return std::string(Locale::get("menu.values.scaling_disabled"));
|
|
case Options::ScalingMode::STRETCH: return std::string(Locale::get("menu.values.scaling_stretch"));
|
|
case Options::ScalingMode::LETTERBOX: return std::string(Locale::get("menu.values.scaling_letterbox"));
|
|
case Options::ScalingMode::OVERSCAN: return std::string(Locale::get("menu.values.scaling_overscan"));
|
|
case Options::ScalingMode::INTEGER: return std::string(Locale::get("menu.values.scaling_integer"));
|
|
}
|
|
return std::string(Locale::get("menu.values.scaling_integer")); }, [](int dir) { Screen::get()->cycleScalingMode(dir); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.texture_filter"), ItemKind::Cycle, [] { return std::string(Options::video.texture_filter == Options::TextureFilter::LINEAR
|
|
? Locale::get("menu.values.linear")
|
|
: Locale::get("menu.values.nearest")); }, [](int dir) { Screen::get()->cycleTextureFilter(dir); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.internal_resolution"), ItemKind::IntRange, [] {
|
|
char buf[16];
|
|
std::snprintf(buf, sizeof(buf), "%dX", Options::video.internal_resolution);
|
|
return std::string(buf); }, [](int dir) { Screen::get()->changeInternalResolution(dir); }, nullptr, nullptr, nullptr});
|
|
|
|
// Bloc shaders: no disponible a WASM (NO_SHADERS, sense SDL3 GPU a WebGL2)
|
|
#ifndef __EMSCRIPTEN__
|
|
p.items.push_back({Locale::get("menu.items.shader"), ItemKind::Toggle, [] { return onOff(Options::video.shader_enabled); }, [](int) { Screen::get()->toggleShaders(); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.shader_type"), ItemKind::Cycle, [] { return std::string(Screen::get()->getActiveShaderName()); }, [](int dir) {
|
|
if (dir < 0) Screen::get()->prevShaderType();
|
|
else Screen::get()->nextShaderType(); }, nullptr, nullptr, [] { return Options::video.shader_enabled; }});
|
|
|
|
p.items.push_back({Locale::get("menu.items.preset"), ItemKind::Cycle, [] { return std::string(Screen::get()->getCurrentPresetName()); }, [](int dir) {
|
|
if (dir < 0) Screen::get()->prevPreset();
|
|
else Screen::get()->nextPreset(); }, nullptr, nullptr, [] { return Options::video.shader_enabled; }});
|
|
|
|
p.items.push_back({Locale::get("menu.items.supersampling"), ItemKind::Toggle, [] { return onOff(Options::video.supersampling); }, [](int) { Screen::get()->toggleSupersampling(); }, nullptr, nullptr, [] {
|
|
if (!Options::video.shader_enabled) return false;
|
|
const char* name = Screen::get()->getActiveShaderName();
|
|
return name && std::string(name) == "POSTFX"; }});
|
|
#endif
|
|
|
|
// Informació de render
|
|
p.items.push_back({Locale::get("menu.items.render_info"), ItemKind::Cycle, [] {
|
|
switch (Options::render_info.position) {
|
|
case Options::RenderInfoPosition::OFF: return std::string(Locale::get("menu.values.off"));
|
|
case Options::RenderInfoPosition::TOP: return std::string(Locale::get("menu.values.top"));
|
|
case Options::RenderInfoPosition::BOTTOM: return std::string(Locale::get("menu.values.bottom"));
|
|
}
|
|
return std::string(Locale::get("menu.values.off")); }, [](int dir) { Overlay::cycleRenderInfo(dir); }, nullptr, nullptr, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.uptime"), ItemKind::Toggle, [] { return onOff(Options::render_info.show_time); }, [](int) { Options::render_info.show_time = !Options::render_info.show_time; }, nullptr, nullptr, [] { return Options::render_info.position != Options::RenderInfoPosition::OFF; }});
|
|
|
|
return p;
|
|
}
|
|
|
|
// Converteix volum 0..1 a percentatge i ho formata com "50%"
|
|
static std::string volPct(float v) {
|
|
int pct = static_cast<int>(v * 100.0F + 0.5F);
|
|
if (pct < 0) pct = 0;
|
|
if (pct > 100) pct = 100;
|
|
char buf[8];
|
|
std::snprintf(buf, sizeof(buf), "%d%%", pct);
|
|
return std::string(buf);
|
|
}
|
|
|
|
// Canvi +/- d'un volum en steps de 0.05 (5%) amb clamping
|
|
static void stepVolume(float& v, int dir) {
|
|
v += (dir >= 0 ? 0.05F : -0.05F);
|
|
if (v < 0.0F) v = 0.0F;
|
|
if (v > 1.0F) v = 1.0F;
|
|
Options::applyAudio();
|
|
}
|
|
|
|
static Page buildControls() {
|
|
Page p{Locale::get("menu.titles.controls"), {}, 0};
|
|
p.items.push_back({Locale::get("menu.items.move_up"), ItemKind::KeyBind, nullptr, nullptr, nullptr, &Options::keys_game.up});
|
|
p.items.push_back({Locale::get("menu.items.move_down"), ItemKind::KeyBind, nullptr, nullptr, nullptr, &Options::keys_game.down});
|
|
p.items.push_back({Locale::get("menu.items.move_left"), ItemKind::KeyBind, nullptr, nullptr, nullptr, &Options::keys_game.left});
|
|
p.items.push_back({Locale::get("menu.items.move_right"), ItemKind::KeyBind, nullptr, nullptr, nullptr, &Options::keys_game.right});
|
|
p.items.push_back({Locale::get("menu.items.menu_key"), ItemKind::KeyBind, nullptr, nullptr, nullptr, KeyConfig::scancodePtr("menu_toggle")});
|
|
return p;
|
|
}
|
|
|
|
static Page buildAudio() {
|
|
Page p{Locale::get("menu.titles.audio"), {}, 0};
|
|
|
|
p.items.push_back({Locale::get("menu.items.master_enable"), ItemKind::Toggle, [] { return onOff(Options::audio.enabled); }, [](int) {
|
|
Options::audio.enabled = !Options::audio.enabled;
|
|
Options::applyAudio(); }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.master_volume"), ItemKind::IntRange, [] { return volPct(Options::audio.volume); }, [](int dir) { stepVolume(Options::audio.volume, dir); }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.music"), ItemKind::Toggle, [] { return onOff(Options::audio.music.enabled); }, [](int) {
|
|
Options::audio.music.enabled = !Options::audio.music.enabled;
|
|
Options::applyAudio(); }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.music_volume"), ItemKind::IntRange, [] { return volPct(Options::audio.music.volume); }, [](int dir) { stepVolume(Options::audio.music.volume, dir); }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.sounds"), ItemKind::Toggle, [] { return onOff(Options::audio.sound.enabled); }, [](int) {
|
|
Options::audio.sound.enabled = !Options::audio.sound.enabled;
|
|
Options::applyAudio(); }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.sounds_volume"), ItemKind::IntRange, [] { return volPct(Options::audio.sound.volume); }, [](int dir) { stepVolume(Options::audio.sound.volume, dir); }, nullptr});
|
|
|
|
return p;
|
|
}
|
|
|
|
static Page buildGame() {
|
|
Page p{Locale::get("menu.titles.game"), {}, 0};
|
|
|
|
p.items.push_back({Locale::get("menu.items.use_new_logo"), ItemKind::Toggle, [] { return yesNo(Options::game.use_new_logo); }, [](int) { Options::game.use_new_logo = !Options::game.use_new_logo; }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.show_title_credits"), ItemKind::Toggle, [] { return yesNo(Options::game.show_title_credits); }, [](int) { Options::game.show_title_credits = !Options::game.show_title_credits; }, nullptr});
|
|
|
|
p.items.push_back({Locale::get("menu.items.show_preload"), ItemKind::Toggle, [] { return yesNo(Options::game.show_preload); }, [](int) { Options::game.show_preload = !Options::game.show_preload; }, nullptr});
|
|
|
|
return p;
|
|
}
|
|
|
|
static Page buildSystem() {
|
|
Page p{Locale::get("menu.titles.system"), {}, 0};
|
|
p.subtitle = std::string("v") + Texts::VERSION + " (" + Version::GIT_HASH + ")";
|
|
|
|
p.items.push_back({Locale::get("menu.items.restart"), ItemKind::Action, nullptr, nullptr, [] {
|
|
if (Director::get()) Director::get()->requestRestart();
|
|
},
|
|
nullptr,
|
|
nullptr});
|
|
|
|
#ifndef __EMSCRIPTEN__
|
|
p.items.push_back({Locale::get("menu.items.exit_game"), ItemKind::Action, nullptr, nullptr, [] {
|
|
if (Director::get()) Director::get()->requestQuit();
|
|
},
|
|
nullptr,
|
|
nullptr});
|
|
#endif
|
|
|
|
return p;
|
|
}
|
|
|
|
// --- Dibuix ---
|
|
|
|
// Alpha blending per pixel sobre el buffer ARGB (ABGR en memòria)
|
|
static void blendRect(Uint32* buf, int x, int y, int w, int h, Uint32 src_argb, Uint8 src_alpha) {
|
|
const Uint8 sa = src_alpha;
|
|
const Uint8 sr = src_argb & 0xFF;
|
|
const Uint8 sg = (src_argb >> 8) & 0xFF;
|
|
const Uint8 sb = (src_argb >> 16) & 0xFF;
|
|
const Uint8 inv = 255 - sa;
|
|
for (int row = y; row < y + h; row++) {
|
|
if (row < 0 || row >= SCREEN_H) continue;
|
|
for (int col = x; col < x + w; col++) {
|
|
if (col < 0 || col >= SCREEN_W) continue;
|
|
Uint32* p = &buf[col + row * SCREEN_W];
|
|
Uint32 dst = *p;
|
|
Uint8 dr = dst & 0xFF;
|
|
Uint8 dg = (dst >> 8) & 0xFF;
|
|
Uint8 db = (dst >> 16) & 0xFF;
|
|
Uint8 r = (sr * sa + dr * inv) / 255;
|
|
Uint8 g = (sg * sa + dg * inv) / 255;
|
|
Uint8 b = (sb * sa + db * inv) / 255;
|
|
*p = 0xFF000000u | (static_cast<Uint32>(b) << 16) | (static_cast<Uint32>(g) << 8) | r;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void fillRect(Uint32* buf, int x, int y, int w, int h, Uint32 color) {
|
|
for (int row = y; row < y + h; row++) {
|
|
if (row < 0 || row >= SCREEN_H) continue;
|
|
for (int col = x; col < x + w; col++) {
|
|
if (col < 0 || col >= SCREEN_W) continue;
|
|
buf[col + row * SCREEN_W] = color;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void drawBorder(Uint32* buf, int x, int y, int w, int h, Uint32 color) {
|
|
fillRect(buf, x, y, w, 1, color);
|
|
fillRect(buf, x, y + h - 1, w, 1, color);
|
|
fillRect(buf, x, y, 1, h, color);
|
|
fillRect(buf, x + w - 1, y, 1, h, color);
|
|
}
|
|
|
|
// Mida final de la caixa segons el nombre d'items *visibles*.
|
|
// body = (N-1) * ITEM_SPACING + charH — així BOTTOM_PAD és el buit real
|
|
// sota el text del darrer ítem, no un buit extra per sobre d'un "slot" buit.
|
|
static int boxHeight(const Page& page) {
|
|
int n = 0;
|
|
for (const auto& it : page.items) {
|
|
if (isVisible(it)) ++n;
|
|
}
|
|
int body = (n == 0) ? 8 : (n - 1) * ITEM_SPACING + 8;
|
|
int header = HEADER_H + (page.subtitle.empty() ? 0 : SUBTITLE_H);
|
|
return header + body + BOTTOM_PAD;
|
|
}
|
|
|
|
// --- API pública ---
|
|
|
|
void init() {
|
|
font_ = std::make_unique<Text>("fonts/8bithud.fnt", "fonts/8bithud.gif");
|
|
stack_.clear();
|
|
open_anim_ = 0.0F;
|
|
closing_ = false;
|
|
last_ticks_ = SDL_GetTicks();
|
|
}
|
|
|
|
void destroy() {
|
|
font_.reset();
|
|
stack_.clear();
|
|
closing_ = false;
|
|
}
|
|
|
|
// "Actiu": accepta input. Durant l'animació de tancament la pila encara
|
|
// té pàgines però ja no ha de processar tecles.
|
|
auto isOpen() -> bool {
|
|
return !stack_.empty() && !closing_;
|
|
}
|
|
|
|
// "Visible": encara hi ha caixa per pintar (incloent close animation).
|
|
auto isVisible() -> bool {
|
|
return !stack_.empty();
|
|
}
|
|
|
|
void toggle() {
|
|
if (closing_ && !stack_.empty()) {
|
|
// Cancel·la el tancament en curs — continua l'animació cap a "obert"
|
|
// des del valor actual d'open_anim_.
|
|
closing_ = false;
|
|
last_ticks_ = SDL_GetTicks();
|
|
return;
|
|
}
|
|
if (isOpen()) {
|
|
close();
|
|
} else {
|
|
stack_.push_back(buildRoot());
|
|
open_anim_ = 0.0F;
|
|
closing_ = false;
|
|
animated_h_ = static_cast<float>(boxHeight(stack_.back()));
|
|
last_ticks_ = SDL_GetTicks();
|
|
}
|
|
}
|
|
|
|
// close() no buida la pila immediatament: marca closing_ i deixa que
|
|
// render() faça decréixer open_anim_ fins a 0. En aquell moment es neteja
|
|
// l'estat. Si es crida estant ja tancat o tancant-se, no-op.
|
|
void close() {
|
|
if (stack_.empty() || closing_) return;
|
|
closing_ = true;
|
|
capturing_ = nullptr;
|
|
transition_active_ = false;
|
|
transition_progress_ = 1.0F;
|
|
last_ticks_ = SDL_GetTicks();
|
|
}
|
|
|
|
auto isCapturing() -> bool {
|
|
return capturing_ != nullptr;
|
|
}
|
|
|
|
void captureKey(SDL_Scancode sc) {
|
|
if (!capturing_) return;
|
|
if (sc == SDL_SCANCODE_ESCAPE) {
|
|
// Cancel·la
|
|
capturing_ = nullptr;
|
|
return;
|
|
}
|
|
*capturing_ = sc;
|
|
capturing_ = nullptr;
|
|
}
|
|
|
|
void handleKey(SDL_Scancode sc) {
|
|
if (!isOpen()) return;
|
|
Page& page = stack_.back();
|
|
if (page.items.empty()) {
|
|
// Pàgina buida — només backspace surt
|
|
if (sc == SDL_SCANCODE_BACKSPACE) {
|
|
if (stack_.size() > 1)
|
|
popPage();
|
|
else
|
|
close();
|
|
}
|
|
return;
|
|
}
|
|
// Si el cursor està sobre un ítem ocultat (p. ex. una acció anterior el va ocultar),
|
|
// reubica'l al pròxim visible abans de processar l'entrada.
|
|
if (!isVisible(page.items[page.cursor])) {
|
|
page.cursor = nextVisibleCursor(page, page.cursor, +1);
|
|
}
|
|
switch (sc) {
|
|
case SDL_SCANCODE_UP:
|
|
page.cursor = nextVisibleCursor(page, page.cursor, -1);
|
|
break;
|
|
case SDL_SCANCODE_DOWN:
|
|
page.cursor = nextVisibleCursor(page, page.cursor, +1);
|
|
break;
|
|
case SDL_SCANCODE_LEFT:
|
|
if (page.items[page.cursor].kind != ItemKind::Submenu &&
|
|
page.items[page.cursor].change) {
|
|
page.items[page.cursor].change(-1);
|
|
}
|
|
break;
|
|
case SDL_SCANCODE_RIGHT:
|
|
if (page.items[page.cursor].kind != ItemKind::Submenu &&
|
|
page.items[page.cursor].change) {
|
|
page.items[page.cursor].change(+1);
|
|
}
|
|
break;
|
|
case SDL_SCANCODE_RETURN:
|
|
case SDL_SCANCODE_KP_ENTER:
|
|
if (page.items[page.cursor].kind == ItemKind::Submenu ||
|
|
page.items[page.cursor].kind == ItemKind::Action) {
|
|
if (page.items[page.cursor].enter) page.items[page.cursor].enter();
|
|
} else if (page.items[page.cursor].kind == ItemKind::KeyBind) {
|
|
capturing_ = page.items[page.cursor].scancode;
|
|
} else if (page.items[page.cursor].change) {
|
|
page.items[page.cursor].change(+1);
|
|
}
|
|
break;
|
|
case SDL_SCANCODE_BACKSPACE:
|
|
if (stack_.size() > 1)
|
|
popPage();
|
|
else
|
|
close();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
// Després de qualsevol acció, si el cursor quedara sobre un ítem ocult
|
|
// (possible si una acció ha canviat la visibilitat pròpia de l'ítem actual,
|
|
// edge case defensiu), salta al següent visible.
|
|
if (!stack_.empty()) {
|
|
Page& top = stack_.back();
|
|
if (!top.items.empty() && !isVisible(top.items[top.cursor])) {
|
|
top.cursor = nextVisibleCursor(top, top.cursor, +1);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuixa el contingut d'una pàgina amb un offset horitzontal i un rang de clip.
|
|
// box_x/box_y són les coordenades de la caixa (per calcular posicions relatives);
|
|
// clip_x_min/clip_x_max limiten on es dibuixa text i la línia separadora.
|
|
static void renderPageContent(Uint32* pixel_data, const Page& page, int box_x, int box_y, int x_offset, int clip_x_min, int clip_x_max, int clip_y_min, int clip_y_max) {
|
|
// Títol
|
|
int title_w = font_->width(page.title);
|
|
int title_x = box_x + (BOX_W - title_w) / 2 + x_offset;
|
|
font_->drawClipped(pixel_data, title_x, box_y + TITLE_PAD_Y, page.title, TITLE_COLOR, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
|
|
// Línia sota el títol (també lliscada) — clippada manualment
|
|
int title_line_y = box_y + TITLE_PAD_Y + font_->charHeight() + 2;
|
|
if (title_line_y >= clip_y_min && title_line_y < clip_y_max) {
|
|
int line_x = box_x + 4 + x_offset;
|
|
int line_w = BOX_W - 8;
|
|
int line_start = line_x < clip_x_min ? clip_x_min : line_x;
|
|
int line_end = (line_x + line_w) > clip_x_max ? clip_x_max : (line_x + line_w);
|
|
if (line_end > line_start) {
|
|
fillRect(pixel_data, line_start, title_line_y, line_end - line_start, 1, BORDER_COLOR);
|
|
}
|
|
}
|
|
|
|
// Subtítol opcional (sota la línia del títol, abans dels items)
|
|
int items_y = title_line_y + 4;
|
|
if (!page.subtitle.empty()) {
|
|
int sub_w = font_->width(page.subtitle.c_str());
|
|
int sub_x = box_x + (BOX_W - sub_w) / 2 + x_offset;
|
|
font_->drawClipped(pixel_data, sub_x, items_y, page.subtitle.c_str(), LABEL_COLOR, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
items_y += SUBTITLE_H;
|
|
}
|
|
// Compta visibles — si cap, dibuixa placeholder (caixa totalment col·lapsada però oberta)
|
|
int visible_count = 0;
|
|
for (const auto& it : page.items)
|
|
if (isVisible(it)) ++visible_count;
|
|
if (visible_count == 0) {
|
|
const char* empty_text = Locale::get("menu.values.empty");
|
|
int ew = font_->width(empty_text);
|
|
font_->drawClipped(pixel_data, box_x + (BOX_W - ew) / 2 + x_offset, items_y + 2, empty_text, EMPTY_COLOR, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
return;
|
|
}
|
|
|
|
int y_slot = 0; // índex de fila visible (independent de l'índex real de l'ítem)
|
|
for (size_t i = 0; i < page.items.size(); i++) {
|
|
const Item& item = page.items[i];
|
|
if (!isVisible(item)) continue;
|
|
int y = items_y + y_slot * ITEM_SPACING;
|
|
++y_slot;
|
|
bool selected = (static_cast<int>(i) == page.cursor);
|
|
Uint32 label_color = selected ? CURSOR_COLOR : LABEL_COLOR;
|
|
|
|
// Action: sense valor a la dreta — centrem el label amb el cursor just a l'esquerra.
|
|
if (item.kind == ItemKind::Action) {
|
|
int lw = font_->width(item.label);
|
|
int lx = box_x + (BOX_W - lw) / 2 + x_offset;
|
|
if (selected) {
|
|
font_->drawClipped(pixel_data, lx - font_->width("> "), y, ">", CURSOR_COLOR, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
}
|
|
font_->drawClipped(pixel_data, lx, y, item.label, label_color, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
continue;
|
|
}
|
|
|
|
if (selected) {
|
|
font_->drawClipped(pixel_data, box_x + 4 + x_offset, y, ">", CURSOR_COLOR, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
}
|
|
|
|
font_->drawClipped(pixel_data, box_x + ITEM_PAD_X + x_offset, y, item.label, label_color, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
|
|
if (item.kind == ItemKind::Submenu) {
|
|
const char* arrow = ">>";
|
|
int aw = font_->width(arrow);
|
|
Uint32 ac = selected ? CURSOR_COLOR : VALUE_COLOR;
|
|
font_->drawClipped(pixel_data, box_x + BOX_W - ITEM_PAD_X - aw + x_offset, y, arrow, ac, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
} else if (item.kind == ItemKind::KeyBind) {
|
|
bool this_capturing = (capturing_ == item.scancode);
|
|
const char* text = this_capturing ? Locale::get("menu.values.press_key") : (item.scancode ? SDL_GetScancodeName(*item.scancode) : "");
|
|
if (!text || !*text) text = Locale::get("menu.values.unknown");
|
|
int tw = font_->width(text);
|
|
Uint32 tc = this_capturing ? 0xFF00FFFF : (selected ? CURSOR_COLOR : VALUE_COLOR);
|
|
font_->drawClipped(pixel_data, box_x + BOX_W - ITEM_PAD_X - tw + x_offset, y, text, tc, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
} else if (item.getValue) {
|
|
std::string value = item.getValue();
|
|
int value_w = font_->width(value.c_str());
|
|
Uint32 value_color = selected ? CURSOR_COLOR : VALUE_COLOR;
|
|
font_->drawClipped(pixel_data, box_x + BOX_W - ITEM_PAD_X - value_w + x_offset, y, value.c_str(), value_color, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
}
|
|
}
|
|
}
|
|
|
|
void render(Uint32* pixel_data) {
|
|
if (!isVisible() || !font_ || !pixel_data) return;
|
|
|
|
// Delta time
|
|
Uint32 now = SDL_GetTicks();
|
|
float dt = static_cast<float>(now - last_ticks_) / 1000.0F;
|
|
last_ticks_ = now;
|
|
if (closing_) {
|
|
open_anim_ -= CLOSE_SPEED * dt;
|
|
if (open_anim_ <= 0.0F) {
|
|
// Animació de tancament completada — buida l'estat de veritat.
|
|
open_anim_ = 0.0F;
|
|
stack_.clear();
|
|
animated_h_ = 0.0F;
|
|
closing_ = false;
|
|
return;
|
|
}
|
|
} else if (open_anim_ < 1.0F) {
|
|
open_anim_ += OPEN_SPEED * dt;
|
|
if (open_anim_ > 1.0F) open_anim_ = 1.0F;
|
|
}
|
|
|
|
// Avança transició
|
|
if (transition_active_) {
|
|
transition_progress_ += TRANSITION_SPEED * dt;
|
|
if (transition_progress_ >= 1.0F) {
|
|
transition_progress_ = 1.0F;
|
|
transition_active_ = false;
|
|
}
|
|
}
|
|
|
|
const Page& page = stack_.back();
|
|
const int current_h = boxHeight(page);
|
|
|
|
// Smoothing exponencial de l'alçada cap al target (pàgina actual + ítems visibles).
|
|
// Permet que el menú reaccione amb animació quan una opció canvia la visibilitat
|
|
// d'altres ítems en calent (p. ex. shader=off → shader_type/preset/supersampling).
|
|
if (animated_h_ <= 0.0F) {
|
|
animated_h_ = static_cast<float>(current_h);
|
|
} else {
|
|
float diff = static_cast<float>(current_h) - animated_h_;
|
|
if (std::fabs(diff) < 0.5F) {
|
|
animated_h_ = static_cast<float>(current_h);
|
|
} else {
|
|
float t = HEIGHT_RATE * dt;
|
|
if (t > 1.0F) t = 1.0F;
|
|
animated_h_ += diff * t;
|
|
}
|
|
}
|
|
|
|
float eased = Easing::outQuad(open_anim_);
|
|
|
|
// Calcula alçada (amb transició si escau)
|
|
int target_h = static_cast<int>(animated_h_);
|
|
if (transition_active_) {
|
|
int outgoing_h = boxHeight(transition_outgoing_);
|
|
float tp = Easing::outQuad(transition_progress_);
|
|
target_h = Easing::lerpInt(outgoing_h, static_cast<int>(animated_h_), tp);
|
|
}
|
|
|
|
// Caixa creix verticalment durant l'obertura
|
|
int box_h = static_cast<int>(target_h * eased);
|
|
if (box_h < 2) box_h = 2;
|
|
int box_x = (SCREEN_W - BOX_W) / 2;
|
|
int box_y = (SCREEN_H - box_h) / 2;
|
|
|
|
// Fons semi-transparent (alpha escalat per l'animació d'obertura)
|
|
Uint8 alpha = static_cast<Uint8>(BG_ALPHA * eased);
|
|
blendRect(pixel_data, box_x, box_y, BOX_W, box_h, BG_COLOR, alpha);
|
|
|
|
// El contingut només apareix quan la caixa és prou gran
|
|
if (open_anim_ >= 0.9F) {
|
|
int clip_x_min = box_x + 1;
|
|
int clip_x_max = box_x + BOX_W - 1;
|
|
int clip_y_min = box_y + 1;
|
|
int clip_y_max = box_y + box_h - 1;
|
|
|
|
if (transition_active_) {
|
|
float tp = Easing::outQuad(transition_progress_);
|
|
int out_offset = static_cast<int>(-transition_dir_ * BOX_W * tp);
|
|
int new_offset = static_cast<int>(transition_dir_ * BOX_W * (1.0F - tp));
|
|
renderPageContent(pixel_data, transition_outgoing_, box_x, box_y, out_offset, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
renderPageContent(pixel_data, page, box_x, box_y, new_offset, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
} else {
|
|
renderPageContent(pixel_data, page, box_x, box_y, 0, clip_x_min, clip_x_max, clip_y_min, clip_y_max);
|
|
}
|
|
}
|
|
|
|
// Vora per damunt de tot
|
|
drawBorder(pixel_data, box_x, box_y, BOX_W, box_h, BORDER_COLOR);
|
|
}
|
|
|
|
} // namespace Menu
|