2026-07-22 18:09:06 +02:00
|
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
2026-05-02 21:08:20 +02:00
|
|
|
|
|
|
|
|
export module Crafter.Graphics:UIComponents;
|
|
|
|
|
import std;
|
|
|
|
|
import :UI;
|
|
|
|
|
import :Font;
|
|
|
|
|
import :FontAtlas;
|
|
|
|
|
|
|
|
|
|
// Tier 3: stateless presentation functions. These append items to the user's
|
|
|
|
|
// QuadItem / GlyphItem buffers — they do NOT dispatch. The user dispatches
|
|
|
|
|
// via UIRenderer::DispatchQuads / DispatchText after their onBuild fills
|
|
|
|
|
// everything, so a frame stays one quads dispatch + one text dispatch
|
|
|
|
|
// regardless of how many components were drawn.
|
|
|
|
|
//
|
|
|
|
|
// State for components that need it (hovered, pressed, dragging, t01) is the
|
|
|
|
|
// USER's responsibility — these functions are pure presentation.
|
|
|
|
|
//
|
|
|
|
|
// EXTENSION MODEL: each function below is short on purpose. If you want a
|
|
|
|
|
// hexagon button, an icon-with-label button, a tristate checkbox — copy the
|
|
|
|
|
// function body into your code and modify it. There is no override hook.
|
|
|
|
|
|
|
|
|
|
export namespace Crafter {
|
2026-05-03 02:45:38 +02:00
|
|
|
// Aggregate for the standard item buffers + the optional text-shaping
|
|
|
|
|
// deps. Build one per frame in onBuild and pass it to component calls.
|
|
|
|
|
// Any pointer left null causes the corresponding component to be a no-op
|
|
|
|
|
// (so a frame that doesn't draw images can leave `images` unset).
|
2026-05-02 21:08:20 +02:00
|
|
|
struct UIBuffer {
|
|
|
|
|
QuadItem* quads = nullptr;
|
|
|
|
|
std::uint32_t* quadCount = nullptr;
|
|
|
|
|
std::uint32_t quadCap = 0;
|
|
|
|
|
|
|
|
|
|
GlyphItem* glyphs = nullptr;
|
|
|
|
|
std::uint32_t* glyphCount = nullptr;
|
|
|
|
|
std::uint32_t glyphCap = 0;
|
|
|
|
|
|
2026-05-03 02:45:38 +02:00
|
|
|
ImageItem* images = nullptr;
|
|
|
|
|
std::uint32_t* imageCount = nullptr;
|
|
|
|
|
std::uint32_t imageCap = 0;
|
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
FontAtlas* atlas = nullptr; // for text-emitting components
|
|
|
|
|
UIRenderer* renderer = nullptr; // for ShapeText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ─── per-component color blocks ─────────────────────────────────────
|
|
|
|
|
// Inline POD aggregates. Users compose their own application-level theme
|
|
|
|
|
// by holding a few of these together; the library has no Theme type.
|
|
|
|
|
|
|
|
|
|
struct ButtonColors {
|
|
|
|
|
std::array<float, 4> bg;
|
|
|
|
|
std::array<float, 4> bgHover;
|
|
|
|
|
std::array<float, 4> bgPressed;
|
|
|
|
|
std::array<float, 4> text;
|
|
|
|
|
std::array<float, 4> border = {0, 0, 0, 0};
|
|
|
|
|
float cornerRadius = 0;
|
|
|
|
|
float borderThickness = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CheckboxColors {
|
|
|
|
|
std::array<float, 4> bg;
|
|
|
|
|
std::array<float, 4> bgHover;
|
|
|
|
|
std::array<float, 4> check;
|
|
|
|
|
std::array<float, 4> border = {0, 0, 0, 0};
|
|
|
|
|
float cornerRadius = 4;
|
|
|
|
|
float borderThickness = 1;
|
|
|
|
|
float checkInset = 4; // px on each side
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SliderColors {
|
|
|
|
|
std::array<float, 4> track;
|
|
|
|
|
std::array<float, 4> trackFilled;
|
|
|
|
|
std::array<float, 4> thumb;
|
|
|
|
|
std::array<float, 4> thumbHover;
|
|
|
|
|
float trackHeight = 4;
|
|
|
|
|
float thumbRadius = 8;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ProgressColors {
|
|
|
|
|
std::array<float, 4> bg;
|
|
|
|
|
std::array<float, 4> fill;
|
|
|
|
|
float cornerRadius = 0;
|
|
|
|
|
};
|
|
|
|
|
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
// TextAlign lives in the :UI partition (used by UIRenderer::ShapeText).
|
2026-05-03 02:45:38 +02:00
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
// ─── component functions ───────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
// Background quad (color depends on state) + centered label glyphs.
|
|
|
|
|
void DrawButton(UIBuffer& buf, Rect r, std::string_view label,
|
|
|
|
|
bool hovered, bool pressed,
|
|
|
|
|
Font& font, float fontSize,
|
|
|
|
|
const ButtonColors& c);
|
|
|
|
|
|
|
|
|
|
// Outlined quad + a smaller filled inset quad when `checked`.
|
|
|
|
|
void DrawCheckbox(UIBuffer& buf, Rect r, bool checked, bool hovered,
|
|
|
|
|
const CheckboxColors& c);
|
|
|
|
|
|
|
|
|
|
// Thin track quad split at `t01` into filled/empty + a circular thumb
|
|
|
|
|
// (drawn as a quad with cornerRadius = thumbRadius).
|
|
|
|
|
void DrawSlider(UIBuffer& buf, Rect r, float t01, bool dragging,
|
|
|
|
|
const SliderColors& c);
|
|
|
|
|
|
|
|
|
|
// Background quad + a filled quad clipped to t01 of the inner width.
|
|
|
|
|
void DrawProgressBar(UIBuffer& buf, Rect r, float t01,
|
|
|
|
|
const ProgressColors& c);
|
2026-05-03 02:45:38 +02:00
|
|
|
|
|
|
|
|
// Single-line text emit. `(x, baselineY)` is the start position; horizontal
|
|
|
|
|
// alignment shifts the run after shaping. Returns the advance width that
|
|
|
|
|
// was written. No line-wrap, no kerning — same shaping rules as
|
|
|
|
|
// UIRenderer::ShapeText (which this calls).
|
|
|
|
|
float DrawText(UIBuffer& buf, std::string_view text,
|
|
|
|
|
float x, float baselineY,
|
|
|
|
|
Font& font, float fontSize,
|
|
|
|
|
std::array<float, 4> color,
|
|
|
|
|
TextAlign align = TextAlign::Left);
|
|
|
|
|
|
|
|
|
|
// Sampled image quad. The texture and sampler must already have heap
|
|
|
|
|
// slots (UIRenderer::RegisterImage / RegisterSampler). `uv` is
|
|
|
|
|
// {u0, v0, u1, v1} into the source texture; defaults to the full image.
|
|
|
|
|
void DrawImage(UIBuffer& buf, Rect r,
|
|
|
|
|
std::uint32_t textureSlot, std::uint32_t samplerSlot,
|
|
|
|
|
std::array<float, 4> tint = {1, 1, 1, 1},
|
|
|
|
|
std::array<float, 4> uv = {0, 0, 1, 1});
|
2026-05-02 21:08:20 +02:00
|
|
|
}
|