9.6 KiB
UI System: Template-Style Elements for a Robust Editor UI
Context
The Seele engine's UI system (external/Seele/src/Engine/UI/) currently has:
Element— RTTI base (ref-counted, virtualapplyStyle/layout/render), children asArray<Element*>/OElementStyle— plain struct of numeric/enum fields- "Style classes" — empty structs with
static void apply(Style&)(Style/Class.h), soDiv<W_Full>compiles a tailwindCSS-like style in at compile time Div,Text— the only two elements;UIPassrenders a flatArray<UIRender>- No input pipeline (only a dead
onClickinAttributes), no runtime theming, noPanel
Feasibility verdict (answer to the original question): the approach is sound — no critical flaw. The risks are practical: (1) input must be a data-driven pipeline on the RTTI base, never template args; (2) instantiation/compile-time explosion — mitigated by a curated alias set (tailwind design tokens), exactly as the user wants; (3) asset-load ordering (fonts/PFontAsset must be loaded before first layout — today Text does style.fontFamily->shapeText with no null check); (4) runtime theming needs a thin override layer alongside the static template defaults; (5) two concrete bugs in Class.h (see below).
Target: robust editor UI. Built once, rendered fast, explicit mutations that are not cumbersome. Runtime theming + responsive layout are in scope. Complex effects/animations are out of scope (later, as additional components/styles).
Design (two layers, per user requirements)
- Static layer (compile-time): template style classes, as today. Curated alias set using tailwind's design tokens:
- spacing scale:
0,1,2,3,4,5,6,8,10,12,16,20,24,32,40,48,64,80(px) →M_*,P_* - font sizes: tailwind's 11 steps (12→96 with real line-heights; the current
Text_*aliases already track these — keep names, fix values where they deviate) - colors: a fixed palette (tailwind gray/red/green/blue/amber + a few neutral UI colors) →
BG_*,C_*(text). No open-ended color template args in the engine — keeps instantiation count bounded. - Intermediate steps:
StyleClassis a concept, not a closed set. Users can declare local classes in their own namespace, e.g.struct W_37 { static void apply(Style& s){ s.width=37; s.widthType=Pixel; } };— no engine change needed. This satisfies "declare intermediate steps locally."
- spacing scale:
- Runtime layer (theming/responsiveness):
UI::Themestruct (scale factor, palette, font map) + aThemeContextthe style pipeline passes alongside the parentStyle.applyStyleresolves semantic tokens against the theme (e.g.M_4→4 * theme.spacingScale,BG_Red→theme.colors.red). Template classes stay the API; they emit semantic values, the theme scales/maps them at runtime. Window resize stays fully responsive becauselayout(viewport)already takes the viewport and Percent dimensions re-resolve — no new mechanism, only make sure nothing caches viewport-dependent results across frames.- Explicit mutation API:
Element::setStyle(overrides)/Element::setVisible(bool)etc. — a small, explicit set of setters that dirty the element. No free-form "set any field" API.
- Explicit mutation API:
Files to modify (all in external/Seele/src/Engine/UI/ unless noted)
| File | Change |
|---|---|
Style/Class.h |
Fix FontClass/StringLiteral; restructure aliases to the curated scale; theme-aware apply(Style&, const Theme&) |
Style/Style.h |
Add semantic-token fields (token ids instead of raw values for themed properties), dirty flag |
Style/Theme.h (new) |
Theme struct: scale, palette, fonts; default instance |
Attributes.h |
Real input attributes: onClick, onHover, onFocus, onText, onScroll |
Element.h/.cpp |
Input pipeline (see below); id/tag for explicit mutation; setVisible etc.; null-check font before shapeText |
Element/Div.h |
Keep; route through new applyStyle signature |
Element/Panel.h (new) |
Panel<...> = block container with standard chrome (bg, padding, border) as defaults that the user's template args override |
Element/Text.h |
Route through new signature; null-font safety |
Element/Button.h (new) |
first interactive element: click + hover + focus states |
System.h/.cpp (new cpp) |
Frame pipeline: input() → style() (only dirty subtrees) → layout(viewport) (dirty or viewport-changed) → render(); owns Theme |
Input/ (new, small) |
HitTest: reverse mapping from UIRender → Element* (add back-ref in UIRender), top-most-wins picking |
../Component/KeyboardInput.h |
Expose last mouse pos + pressed buttons (already computed there) to the UI system |
../Graphics/RenderPass/UIPass.h/.cpp |
Feed KeyboardInput's mouse state into UI::System::input(); no other changes — it already renders the flat list fine |
Reuse (do not reinvent)
- Mouse/key/scroll state already aggregated in
System::KeyboardInput(src/Engine/System/KeyboardInput.h) — feed from there; do not add new OS callbacks. GameView/Windowinput plumbing (src/Engine/Window/GameView.cpp:100-103,Window.cpp:33-39) already routes GLFW callbacks intoKeyboardInput— end of the road.UIPassalready consumes the flatArray<UIRender>and does per-element + glyph rendering — no GPU-side changes for this scope.AssetRegistry::findFont(folder, name)(src/Engine/Asset/AssetRegistry.h:28) — the real font API;FontClassis currently calling it wrong (2 args for the lookup, name includes NUL).DEFINE_REF/OElementlifetime — keep as-is; hit-test back-refs must be weak (store a generation/id, not a raw pointer, or document that the UI tree outlives any query — simplest: back-ref is fine because the UI tree is built once and lives as long as the view; note the risk in code).- Flow layout in
Element::layout/flowLayout— keep, it already handles Percent widths for responsiveness.
Steps
- 1. Theme foundation —
Style/Theme.h(scale, palette, font map, defaultTheme); extendStylewith the semantic-token representation + adirtyflag;apply(Style&, const Theme&)new signature onStyleClassconcept. - 2. Fix + curate
Class.h— deleteStringLiteral,FontClassbecomes a template on aconst char*tag resolved at runtime againstTheme::fonts(fixes the NUL + wrong-findFontbugs at once); regenerateM_*/P_*/Text_*/BG_*/C_*from the curated scale via the existingDECLARE_*macro pattern. - 3. Plumb theme through the pipeline —
Element::calcStyle/applyStylecarryconst Theme&(fromSystem);Div/Text/Panelupdated to the new signature. - 4. Retained-tree hygiene —
dirtyflag:setStyle/setVisible/text-setter mark the element;System::style()re-styles only dirty subtrees;layoutre-runs when dirty or viewport size changed;render()always emits the flat list. This is the "built once, render fast, explicit mutation" core. - 5. Input: data-driven on the base class —
AttributesgainsonClick/onHover/onFocus/onText/onScroll; addElement* backRef(or id) toUIRender; newInput/hit-test: walk last-frameUIRenderlist top-most-wins, dispatch callbacks;System::input(mouse, keys, scroll)called fromUIPass::beginFrameusingKeyboardInput's state (expose getters there). - 6. Focus + text input —
SystemholdsPElement focused; keyboard text routes to focused element'sonText; Tab/Shift-Tab move focus over elements withfocusableattribute. (Needed for editor: search boxes, inspector fields.) - 7.
Panel+Button—Panel= styled block container (defaults overridable by the user's template args);Button=Panel+onClick+ hover/focus visual states via style overrides (states are data toggled by the input pipeline, never template args). - 8. Null-font + asset-order guard —
Text::layoutno-ops (and asserts in debug) iffontFamilyis null; document the invariant "UI assets loaded before firstSystem::style()" — check the editor's load order insrc/Editor/main.cppand enforce if needed. - 9. Demo — in
MeshShadingDemo(src/), a minimal UI scene:Panel<M_4, BG_Gray900>with aButtonthat toggles aText's content and a themed color, proving: build-once, fast render, explicit mutation, hover/click, runtime theme scale change.
Verification
- Build engine + demo (existing CMake/variant setup) — compiles clean, C++23.
- Run through the editor's PlayView (dll path, existing pipeline) — the demo UI appears.
- Manual checklist: hover highlights; click fires
onClickand mutates text/color without rebuilding the tree; window resize keepsW_Full/Percent elements responsive; changingThemescale at runtime (a debug key) re-scales spacing/typography next frame with a single re-layout of the dirty tree. - Compile-time sanity: build a second
Panelvariant with a different alias set and confirm the demo's build time doesn't jump (curated-set cap working). - No visual regressions in the existing editor inspector (uses the same
UIPass).
Explicit non-goals (out of scope, per user)
- Flexbox/Grid layout (
InnerDisplayType::Flex/Gridexist as enums but are unimplemented) — later. - Scroll containers, overflow, virtualized lists — later.
- Animations/transition components — later (the "additional components and styles" the user mentioned; the dirty-flag + per-frame render loop already gives them a hook point).
- Drag & drop, IME/composition, accessibility tree — later.
- Free-form runtime style strings (no
style="..."parsing) — mutation stays typed and explicit.