Adds docs/ — a navigation layer on top of AGENTS.md for future coding agents working on the Seele engine and the MeshShadingDemo harness. - docs/README.md: index + 30-second orientation + how-to-use workflow. - docs/cross-repo.md: the engine<->demo contract (2-symbol ABI, the ComponentSystem<...> demo pattern with the FlyCamSystem example, the CMake in-tree vs install(EXPORT) split, a where-to-look task map). - docs/subsystems/*.md: 16 per-subsystem deep-dives (scene/ECS, systems, game-interface, window, graphics backends, render graph, materials, physics, UI, assets, platform, core utils). Each maps public types to their defining headers and gives file:line pointers + gotchas. All cross-links verified resolving. This is documentation only — no source changes.
8.7 KiB
UI (flexbox layout, element tree, theming)
Scope:
src/Engine/UI/— a from-scratch, dependency-free flexbox layout engine, an element-tree widget system (Div/Button/Text/Panel), and a theme/style system. No ImGui, no Dear ImGui — this is custom and it's the "app UI" (overlays, HUD, menus), not the 3D scene.
The UI is a separate render path from the 3D scene: it produces UIRender quads (colored
rectangles + glyph quads) that are composited on top of the 3D frame. Layout is a real two-phase
flexbox (measure → place), and the whole thing is driven incrementally — only dirty subtrees
re-style/re-layout.
Key types & where they live
| Type | Defined in |
|---|---|
UI::System |
src/Engine/UI/System.h (impl System.cpp) — the per-frame driver |
UI::Element (base widget) |
src/Engine/UI/Element.h (impl Element.cpp) |
UI::Div<...>, UI::Button<...>, UI::Text, UI::Panel |
src/Engine/UI/Element/{Div,Button,Text,Panel}.h |
UI::FlexLayout |
src/Engine/UI/FlexLayout.h (impl FlexLayout.cpp) — the layout engine |
UI::Style, UI::Theme, UI::Class<...> |
src/Engine/UI/Style/{Style,Theme,Class,Fonts}.h |
UI::Attributes (widget callbacks/props) |
src/Engine/UI/Attributes.h |
UIRender (a quad to draw: color rect or glyph) |
src/Engine/UI/Element.h / src/Engine/Graphics/ |
Display, Direction, DimensionType, ContentDirection |
src/Engine/UI/Style/Style.h (enums) |
DECLARE_REF(System) exists in UI/System.h — System is ref-countable. The element tree itself
(OElement/PElement) is also a ref-counted tree (the DEFINE_REF(Element) in Element.h).
The frame pipeline (the thing to understand first)
UI::System (UI/System.h) runs this, once per frame, from the owning view, before 3D layout
matters but as part of the render path:
processInput() # mouse/key/scroll events → update hover/focus state
beginFrame(viewport) # style dirty subtrees → layout (if dirty or viewport changed) → render
├─ styleRec(root) # walk the tree; applyStyle on elements whose style is dirty
├─ if layoutDirty → FlexLayout::layout(viewport, root)
└─ renderElements = render() # collect UIRender quads for the compositor
render() → Array<UIRender> # the actual quads the view blits over the 3D frame
Incrementality is the core design. The tree is built once; mutations go through element setters
(setText, setChildren, style setters) which call markDirty(). Only the affected subtree
re-styles/re-lays-out. setTheme forces one full re-style (markAllDirty + layoutDirty).
lastViewport is cached: if the viewport didn't change and nothing is dirty, layout is skipped
entirely — the UI is cheap on idle frames.
The flexbox engine (FlexLayout)
UI::FlexLayout (UI/FlexLayout.h) is a genuine two-phase flexbox:
- Phase 1 — measure (
measure): top-down, resolvesDimensionType::Autosizes against content (text is measured viafont->shapeText), handlesmin/maxclamps, and computes the main/cross sizes of every node. Text measurement is the only expensive part — it's what makesText'sAutowidth/height work. - Phase 2 — place (
place/placeFlex): walks again, assigning finalRectbounds. SupportsDirection(row/row-reverse/column/column-reverse), wrap,justify(content alignment along the main axis),align(cross axis), margins, andContentDirection(start/center/end). - It works on an internal flat node list (
FlexNode) built from the element tree — the elementElementcarries aFlexNode*back-pointer, andFlexLayoutnever touches the tree's pointer structure directly during layout. This is what keeps layout O(n) and cache-friendly.
Gotcha: FlexNode/layout state is reused between frames (the FlexNode* on each
Element is stable). If you add a new widget, you must make sure it produces a valid FlexNode
in applyStyle/measurement or layout will read stale bounds. The comment block at the top of
System.h documents the frame invariants — read it before touching layout.
The widget set
The widget vocabulary is small and explicit — there are no "accidental" empty widgets:
| Widget | Base | Notes |
|---|---|---|
Div<classes...> |
Element |
container. Div() is deleted — you must name style classes (Div<MyStyles::card>) or you can't create one. This is intentional: it forces a style/attrs choice. |
Button<classes...> |
Div<...> |
wires onHover/onHoverLeave/onClick/onScroll/onKey/onFocus/onBlur callbacks in Attributes; key-enter fires onClick; hover/press state is tracked and re-marked dirty on change. |
Text |
Element |
holds a string; setText marks dirty; resolves a font by name at style time (see below); renders glyph quads from UIRender per glyph. |
Panel<classes...> |
Div<...> |
themed convenience: theme.color(ColorToken::Gray900) bg, white text, theme.spacing(8) padding. |
Styling is template-based: Div<classes...> / Button<classes...> / Panel<classes...> take
a variadic list of style-class types at compile time (the Style/Class.h machinery). applyStyle
merges: theme defaults → class styles → parent inheritance → per-instance overrides. This is the
"CSS class" model done with C++ templates instead of strings.
Fonts & text — the INVARAIT (read this)
The System.h header carries an explicit invariant (verbatim from source):
INVARAIT: fonts referenced by style classes must be imported (
AssetImporter::importFont) before the firststyle()call.Textfalls back toTheme::defaultFontand renders empty (with an assert in debug) otherwise.
Text::applyStyleresolves the font by name viaAssetRegistry::findFont("", style.fontName)— a string literal can't be a template arg, so the font is a named runtime lookup. If the name is empty it usesTheme::defaultFont.- If the named font isn't in the
AssetRegistryat style time, the glyphs array is left empty and the text renders as a zero-size (invisible) box — in release you get nothing, no crash. This is the #1 "my UI text doesn't show" bug. Fix: import the font before you build the tree. - Fonts are
OwningPtr<Font>/ managed by the asset system — see assets.md and serialization.md for the import path.
How the UI actually gets drawn (cross-subsystem)
UI::System::render() returns Array<UIRender>. The owning view (the thing that drives
System::beginFrame each frame) takes that array and blits the quads over the 3D render graph
output. UIRender is a small struct (either a filled color rect, or a glyph: font + glyph index +
uv + rect + color). It is not a separate render pass in the RenderGraph in the same sense as
3D passes — it's a flat quad list the view composites. If you're looking for the "UI render pass"
in the render graph, it's this quad list, handled at the view level.
Gotchas
Div()deleted — always construct with a style-class template arg, or the widget won't compile. This is by design, not a bug.- Empty text = font not imported. No assert in release. Import fonts before first style pass.
- Layout state is cached and reused — new widgets must emit a valid
FlexNode. StaleFlexNode*= garbage bounds. markDirtyis the only way to invalidate. Directly mutating aStylefield withoutmarkDirty()means the change won't be re-styled next frame. Use the setters.- The UI thread is the frame thread. All
Systemmethods run during the view's frame callback (see window.md). Don't callbeginFrame/renderfrom elsewhere. - Templates +
classes...— the widget class list is a compile-time token; you can't "dynamically add a class" at runtime. Runtime theming goes throughsetTheme(full re-style) or per-instance style setters (subtree dirty), not by changing the template arg.
Where to look
| What you're doing | Start here |
|---|---|
| Add a new widget | src/Engine/UI/Element/ (copy Panel.h as a template) + Element.h base |
| Make a style class | src/Engine/UI/Style/Class.h (the template machinery) |
| Change the theme (colors/spacing/fonts) | src/Engine/UI/Style/Theme.h + System::setTheme |
| Fix "text not rendering" | confirm font imported (AssetImporter::importFont) before first frame; check Text::applyStyle AssetRegistry::findFont |
| Layout bug (wrong bounds) | src/Engine/UI/FlexLayout.cpp (measure then place) |
| Add an input callback to a widget | src/Engine/UI/Attributes.h + the widget's onMouseEvent/onKeyEvent override |
| Understand the frame invariants | top of src/Engine/UI/System.h (the comment block) |