97 static constexpr int32_t kMinimumReservedDepth = 32;
98 static constexpr int32_t kMinimumReservedBreadth = 256;
103 : m_styleManager(styleManager), m_elementDataStore(dataStore)
105 while (kMinimumReservedDepth > m_depths.size())
107 m_depths.emplace_back().Reserve(kMinimumReservedBreadth);
127 if (m_generation == 0) m_generation = 1;
129 m_anyAnimated =
false;
130 m_constructionModeActive =
true;
143 const bool unclosed = !m_parentStack.empty();
144 while (!m_parentStack.empty())
146 m_constructionModeActive =
false;
180 uint32_t fromLocal, uint32_t toLocal);
187 inline bool Empty()
const {
return m_elementCount == 0; }
200 return depth >= m_usedDepthCount ? 0 : m_depths[depth].Size();
225 return ref.
depth >= m_usedDepthCount ? false :
234 auto hierarchy = GetHierarchyInfoPtr(ref);
235 return (hierarchy ==
nullptr) ? 0 : hierarchy->childCount;
241 auto hierarchy = GetHierarchyInfoPtr(ref);
248 const HierarchyInfo *nodeHierarchy = GetHierarchyInfoPtr(ref);
249 if (nodeHierarchy ==
nullptr ||
279 const uint32_t index = m_depths[elementRef.
depth].customDraws[elementRef.
index];
280 return index != kNoCustomDraw ? &m_customDraws[index] :
nullptr;
296 const uint32_t index = m_depths[elementRef.
depth].customDraws[elementRef.
index];
297 return index != kNoCustomDraw ? &m_customDraws[index] :
nullptr;
302 return m_postDrawEntries[index];
309 if (range.Empty())
return {};
310 return { m_depths[parentRef.
depth + 1].elements.data() + range.begin, range.Size() };
316 return {m_depths[depth].elements.data() + range.
begin, range.
Size()};
322 return {m_depths[depth].elements.data() + range.
begin,
329 return {m_depths[depth].elementRects.data() + range.
begin, range.
Size()};
334 return {m_depths[depth].elementRects.data() + range.
begin, range.
Size()};
341 if (range.Empty())
return {};
342 return { m_depths[parentRef.
depth + 1].elements.data() + range.begin, range.Size() };
350 return GetChildRangeUnsafe(parentRef);
359 assert(!m_constructionModeActive);
360 if (
Empty())
return TopDownDepthScanTerminator();
367 assert(!m_constructionModeActive &&
"TopDownDepthScanEnd called during BeginConstruction/EndConstruction");
368 return TopDownDepthScanTerminator();
374 assert(!m_constructionModeActive);
375 if (
Empty())
return BottomUpDepthScanTerminator();
376 DepthScanCursor cursor{ .parentDepth = m_usedDepthCount - 1, .parentIndex = 0 };
383 assert(!m_constructionModeActive &&
"BottomUpDepthScanIterator called during BeginConstruction/EndConstruction");
384 return BottomUpDepthScanTerminator();
395 void AdvanceTopDownDepthScan(DepthScanCursor& cursor)
const;
398 void AdvanceBottomUpDepthScan(DepthScanCursor& cursor)
const;
402 inline const HierarchyInfo* GetHierarchyInfoPtr(
const ElementRef& elementRef)
const
405 return &m_depths[elementRef.depth].hierarchy[elementRef.index];
408 inline HierarchyInfo *GetHierarchyInfoPtr(
const ElementRef &elementRef)
411 return &m_depths[elementRef.depth].hierarchy[elementRef.index];
414 inline const HierarchyInfo* GetHierarchyInfoPtrUnsafe(
const ElementRef& elementRef)
const
416 return &m_depths[elementRef.depth].hierarchy[elementRef.index];
419 inline HierarchyInfo* GetHierarchyInfoPtrUnsafe(
const ElementRef& elementRef)
421 return &m_depths[elementRef.depth].hierarchy[elementRef.index];
425 IndexRange GetChildRangeUnsafe(
const ElementRef& parentRef)
const
427 auto& parent = m_depths[parentRef.depth].hierarchy[parentRef.index];
428 if (parent.childCount == 0)
return {};
429 return IndexRange{ parent.childOffset, parent.childOffset + parent.childCount };
433 void RotateDepthLevel(uint32_t depth, uint32_t begin, uint32_t mid, uint32_t end);
435 void RelinkChildrenOf(uint32_t depth, uint32_t begin, uint32_t end);
437 void RebuildSiblingLinks(uint32_t depth, uint32_t rangeBegin, uint32_t rangeEnd, uint32_t parentIndex);
447 std::vector<Element> elements;
448 std::vector<ElementRect> elementRects;
451 std::vector<uint32_t> customDraws;
452 std::vector<HierarchyInfo> hierarchy;
454 inline uint32_t Size()
const {
return static_cast<uint32_t
>(elements.size()); }
455 inline bool Empty()
const {
return elements.empty(); }
459 void Reserve(uint32_t capacity);
461 inline uint32_t PeekIndex()
const
466 inline uint32_t Add(int32_t parentIndex, Element&& element, ElementRect&& elementRect)
468 uint32_t index = Size();
469 elements.push_back(std::move(element));
470 elementRects.push_back(std::move(elementRect));
471 customDraws.push_back(kNoCustomDraw);
477 std::vector<DepthLevel> m_depths;
478 static constexpr uint32_t kNoCustomDraw = UINT32_MAX;
480 std::vector<ElementCustomDraw> m_customDraws;
482 ElementCustomDraw& CustomDrawFor(
const ElementRef& ref);
484 struct ParentStackEntry
487 uint32_t startChildIndex = 0;
493 bool childrenBegunBefore =
false;
496 std::vector<ParentStackEntry> m_parentStack;
502 bool m_childrenBegun =
false;
506 bool m_childrenBegunBeforeWindow =
false;
508 uint32_t m_elementCount = 0;
511 uint32_t m_usedDepthCount = 0;
513 uint64_t m_generation = 1;
514 bool m_constructionModeActive =
false;
515 bool m_anyAnimated =
false;
518 std::vector<PostDrawEntry> m_postDrawEntries;
519 std::vector<PostLayoutEntry> m_postLayoutEntries;
521 StyleManager &m_styleManager;
522 ElementDataStore* m_elementDataStore =
nullptr;
525 IDMap<ElementRef> m_idIndex;
526 void IndexElement(ID
id, ElementRef ref);
528 void ReindexSlots(uint32_t depth, uint32_t begin, uint32_t end);
530 ElementMove m_lastMove;
Per-element typed state keyed by ID, kept across frames (drag start, double-click timing).
Definition gui_element_data_store.h:22
The frame's element tree.
Definition gui_element_storage.h:96
const std::vector< PostLayoutEntry > & GetPostLayoutEntries() const
Definition gui_element_storage.h:291
PostDrawEntry & GetPostDrawEntryUnsafe(uint32_t index)
Definition gui_element_storage.h:300
const Element * GetElementPtr(const ElementRef &elementRef) const
Definition gui_element_storage.h:257
uint32_t NumberOfDepthLevels() const
Definition gui_element_storage.h:196
const ElementRect & GetElementRectUnsafe(const ElementRef &elementRef) const
Definition gui_element_storage.h:264
ElementCustomDraw * FindCustomDrawUnsafe(const ElementRef &elementRef)
As GetCustomDraw, for a valid ref, to fill in: null when it has none.
Definition gui_element_storage.h:294
uint32_t DepthSizeOf(uint32_t depth) const
Definition gui_element_storage.h:198
uint32_t GetChildCount(const ElementRef &ref) const
Definition gui_element_storage.h:232
const ElementCustomDraw * GetCustomDraw(const ElementRef &elementRef) const
The custom draws of elementRef, or null when it has none (most elements).
Definition gui_element_storage.h:276
const StyleManager & GetStyleManager() const noexcept
Definition gui_element_storage.h:111
ID GetLastItemID() const
The element last added at the level being built, its children done: after a control with parts,...
Definition gui_element_storage.h:193
BottomUpDepthScanRangeIterator BottomUpDepthScanRange() const
Definition gui_element_storage.h:388
ElementRef FindElementRefByID(ID id) const
The element with id; Invalid when not found (always for ID::Invalid).
friend struct TopDownDepthScanIterator
Definition gui_element_storage.h:356
ElementStorage(StyleManager &styleManager, ElementDataStore *dataStore=nullptr)
Definition gui_element_storage.h:102
Element & GetElementUnsafe(const ElementRef &elementRef)
Definition gui_element_storage.h:258
IndexRange GetChildRange(const ElementRef &parentRef) const
The indices of parentRef's children at the next depth; empty on invalid ref or no children.
Definition gui_element_storage.h:347
BottomUpDepthScanIterator BottomUpDepthScanEnd() const
Definition gui_element_storage.h:381
ElementStorageResult AddCustomDrawAfter(const ElementRef &ref, CustomDrawCallback callback)
Draws callback after ref's whole subtree, in the same space as AddCustomDraw.
bool HasAnimated() const
True when an element with ElementFlags::Animated was added since BeginConstruction: something on scre...
Definition gui_element_storage.h:136
ElementRect * GetElementRectPtr(const ElementRef &elementRef)
Definition gui_element_storage.h:261
ElementRef GetFirstElementRef() const
Definition gui_element_storage.h:229
bool Empty() const
Definition gui_element_storage.h:187
ElementStorageResult AddPostLayout(const ElementRef &ref, PostLayoutCallback callback)
Runs callback after layout, before rendering, with read-only final geometry.
std::span< Element > GetChildrenSpan(const ElementRef &parentRef)
Valid until the next BeginConstruction. Empty on invalid ref or no children.
Definition gui_element_storage.h:306
TopDownDepthScanRangeIterator TopDownDepthScanRange() const
Definition gui_element_storage.h:387
std::span< Element > GetChildrenSpanUnsafe(int depth, const IndexRange range)
Unchecked. Valid until the next BeginConstruction.
Definition gui_element_storage.h:314
TopDownDepthScanIterator TopDownDepthScanBegin() const
Definition gui_element_storage.h:357
ElementStorage(ElementStorage &&) noexcept=default
ElementRect & GetElementRectUnsafe(const ElementRef &elementRef)
Definition gui_element_storage.h:263
std::span< const Element > GetChildrenSpan(const ElementRef &parentRef) const
Valid until the next BeginConstruction. Empty on invalid ref or no children.
Definition gui_element_storage.h:338
friend struct BottomUpDepthScanIterator
Definition gui_element_storage.h:371
int2 ScreenToElementSpace(int2 screenPos, ElementRef ref) const
Converts a screen point into the space ref's own rect is stored in, applying each ancestor's inverse ...
bool SetContentTransform(const ElementRef &ref, const float3x2 &transform)
Sets the transform applied to ref's children at render / hit-test time (pan, zoom).
Definition gui_element_storage.h:268
bool IsValidRef(const ElementRef &ref) const
Definition gui_element_storage.h:223
ElementStorageResult BeginConstruction()
Starts building a new tree, clearing the previous one.
Definition gui_element_storage.h:123
Element * GetElementPtr(const ElementRef &elementRef)
The element, or nullptr for an invalid ref.
Definition gui_element_storage.h:255
AddResult Add(Element &&element, LayoutOverride &&constraints={})
Adds element after the previous one at the current nesting level.
size_t CustomDrawCount() const
How many elements have custom draws: the table holds those alone.
Definition gui_element_storage.h:283
ElementStorageResult AddCustomDraw(const ElementRef &ref, CustomDrawCallback callback)
Draws callback before ref's children render.
BottomUpDepthScanIterator BottomUpDepthScanBegin() const
Definition gui_element_storage.h:372
uint32_t GetCurrentNestingDepth() const
Definition gui_element_storage.h:195
std::span< const ElementRect > GetChildRectSpanUnsafe(int depth, const IndexRange range) const
Definition gui_element_storage.h:332
uint32_t ElementCount() const
Definition gui_element_storage.h:188
bool HasChildren(const ElementRef &ref) const
Definition gui_element_storage.h:230
ElementStorageResult BeginChildren()
Begins adding children to the last added element; elements without BeginChildren are leaves.
ElementStorageResult EndChildren()
Closes the innermost BeginChildren. Fails outside construction or without one open.
ElementRef GetLastAddedRef() const
Definition gui_element_storage.h:194
ElementStorageResult EndConstruction()
Finishes the tree; NotConstructing if not constructing.
Definition gui_element_storage.h:140
ID GetLastAddedID() const
Definition gui_element_storage.h:189
std::span< ElementRect > GetChildRectSpanUnsafe(int depth, const IndexRange range)
Definition gui_element_storage.h:327
const Element & GetElementUnsafe(const ElementRef &elementRef) const
Definition gui_element_storage.h:259
const ElementMove & LastMove() const
The slot rotations of the last MoveChild call, none when it moved nothing.
Definition gui_element_storage.h:183
IntRect ElementRectOnScreen(ElementRef ref) const
ref's border box on the screen: through its ancestors' contentTransforms (a canvas's pan and zoom),...
ElementRef GetNextSiblingRef(const ElementRef &ref) const
Definition gui_element_storage.h:246
const ElementRect * GetElementRectPtr(const ElementRef &elementRef) const
Definition gui_element_storage.h:262
TopDownDepthScanIterator TopDownDepthScanEnd() const
Definition gui_element_storage.h:365
std::span< const Element > GetChildrenSpanUnsafe(int depth, const IndexRange range) const
Unchecked. Valid until the next BeginConstruction.
Definition gui_element_storage.h:320
ElementRef GetParentRef(const ElementRef &ref) const
Definition gui_element_storage.h:238
ElementStorageResult MoveChild(uint32_t childDepth, uint32_t rangeBegin, uint32_t fromLocal, uint32_t toLocal)
Moves a child (with its subtree) to another sibling position, during construction.
const ID GetElementIDAt(ElementRef root, int2 position, ID *disabledHit=nullptr) const
The element at position that takes input, or Invalid.
Definition gui_renderer.h:78
Bakes serializable Styles into deduplicated runtime layout/visual styles; a StyleRef holds indices in...
Definition gui_style_manager.h:39
std::function< void(PostLayoutContext &ctx)> PostLayoutCallback
Runs after layout, before rendering, with read-only access to final rects/transforms.
Definition gui_element.h:93
Range< BottomUpDepthScanIterator > BottomUpDepthScanRangeIterator
Definition gui_element_iterators.h:132
TopDownDepthScanIterator & operator++()
Definition gui_element_storage.h:541
Range< TopDownDepthScanIterator > TopDownDepthScanRangeIterator
Definition gui_element_iterators.h:131
ScrollOffsetHandle
A handle to a stored scroll offset.
Definition gui_element_storage.h:31
bool Validate() const
Definition gui_element_storage.h:538
BottomUpDepthScanIterator & operator++()
Definition gui_element_storage.h:544
ElementStorageResult
Outcome of an ElementStorage call.
Definition gui_element_storage.h:39
bool Validate() const
Definition gui_element_storage.h:539
std::function< void(CustomDrawContext &ctx)> CustomDrawCallback
Render-time callback.
Definition gui_element.h:88
@ InvalidStyle
The element's style is invalid.
Definition gui_element_storage.h:46
@ NotConstructing
Called outside BeginConstruction/EndConstruction.
Definition gui_element_storage.h:41
@ MissingBeginChildren
EndChildren without an open BeginChildren.
Definition gui_element_storage.h:43
@ Success
Definition gui_element_storage.h:40
@ MissingEndChildren
BeginChildren left open when construction ended.
Definition gui_element_storage.h:44
@ UnknownProblem
An invalid ref or another unexpected state.
Definition gui_element_storage.h:47
@ MissingAdd
Called without first calling Add.
Definition gui_element_storage.h:42
@ DuplicateCall
Called again where once is allowed (e.g. BeginChildren twice).
Definition gui_element_storage.h:45
@ Invalid
Definition gui_image.h:99
Definition gui_logging.h:24
What Add returns: the new element's ref and status; true on success.
Definition gui_element_storage.h:52
bool operator==(const ElementRef &rhs) const
Definition gui_element_storage.h:61
bool operator!=(ElementStorageResult rhs) const
Definition gui_element_storage.h:59
ElementStorageResult status
Definition gui_element_storage.h:54
bool operator!=(const ElementRef &rhs) const
Definition gui_element_storage.h:62
ElementRef ref
Definition gui_element_storage.h:53
bool operator==(ElementStorageResult rhs) const
Definition gui_element_storage.h:58
Definition gui_element_iterators.h:89
const ElementStorage & storage
Definition gui_element_iterators.h:90
DepthScanCursor cursor
Definition gui_element_iterators.h:92
uint64_t generation
Definition gui_element_iterators.h:91
Definition gui_element_iterators.h:20
uint32_t parentDepth
Definition gui_element_iterators.h:21
static constexpr DepthScanCursor EndCursor()
Definition gui_element_iterators.h:49
uint32_t parentCountAtDepth
Definition gui_element_iterators.h:23
IndexRange childRange
This parent's children at parentDepth + 1.
Definition gui_element_iterators.h:26
void SetDone()
Definition gui_element_iterators.h:50
ElementRef CurrentRef() const
Definition gui_element_iterators.h:30
callback fires before the children render; the after-list fires after the whole subtree.
Definition gui_element.h:192
What a MoveChild did: the slot rotations it applied at each depth, so code that stores ElementRefs (e...
Definition gui_element.h:161
Definition gui_element.h:56
float3x2 contentTransform
Applied to children at render/hit-test time; layout ignores it except for scroll clamping.
Definition gui_element.h:71
An element's slot in ElementStorage: its depth and index within that depth.
Definition gui_element.h:126
static constexpr ElementRef Invalid()
Definition gui_element.h:134
uint32_t depth
Definition gui_element.h:127
uint32_t index
Index into the depth's array, which holds children of many parents.
Definition gui_element.h:130
Fired in registration order between layout and rendering.
Definition gui_element_storage.h:287
ElementRef ownerRef
Definition gui_element_storage.h:289
PostLayoutCallback callback
Definition gui_element_storage.h:288
Definition gui_element.h:32
Definition gui_element.h:100
static constexpr uint32_t kInvalidNextIndex
Definition gui_element.h:112
static constexpr HierarchyInfo Initialize(uint32_t parentIndex)
Definition gui_element.h:101
uint32_t nextSiblingIndex
Next sibling at same depth.
Definition gui_element.h:115
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
A half-open range [begin, end) of indices within one depth.
Definition gui_element.h:147
uint32_t begin
Definition gui_element.h:148
uint32_t Size() const
Definition gui_element.h:152
Definition gui_base_types.h:406
Per-element overrides of style layout properties.
Definition gui_constraints.h:151
Node in the flat post-draw linked list stored in ElementStorage.
Definition gui_element.h:182
Definition gui_element_iterators.h:64
const ElementStorage & storage
Definition gui_element_iterators.h:65
DepthScanCursor cursor
Definition gui_element_iterators.h:67
uint64_t generation
Definition gui_element_iterators.h:66
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition gui_base_types.h:218
Integer 2D vector.
Definition gui_base_types.h:18