Skip to content

Commit

Permalink
Fix z index sorting of tree roots
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbarker committed Jan 11, 2025
1 parent 44fb89c commit 2fcb4cc
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion clay.h
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ CLAY__TYPEDEF(Clay__LayoutElementTreeRoot, struct {

Clay__LayoutElementTreeRoot CLAY__LAYOUT_ELEMENT_TREE_ROOT_DEFAULT = CLAY__DEFAULT_STRUCT;

// __GENERATED__ template array_define,array_allocate,array_add,array_get TYPE=Clay__LayoutElementTreeRoot NAME=Clay__LayoutElementTreeRootArray DEFAULT_VALUE=&CLAY__LAYOUT_ELEMENT_TREE_ROOT_DEFAULT
// __GENERATED__ template array_define,array_allocate,array_add,array_get,array_set TYPE=Clay__LayoutElementTreeRoot NAME=Clay__LayoutElementTreeRootArray DEFAULT_VALUE=&CLAY__LAYOUT_ELEMENT_TREE_ROOT_DEFAULT
#pragma region generated
CLAY__TYPEDEF(Clay__LayoutElementTreeRootArray, struct
{
Expand All @@ -1347,6 +1347,12 @@ Clay__LayoutElementTreeRoot *Clay__LayoutElementTreeRootArray_Add(Clay__LayoutEl
Clay__LayoutElementTreeRoot *Clay__LayoutElementTreeRootArray_Get(Clay__LayoutElementTreeRootArray *array, int32_t index) {
return Clay__Array_RangeCheck(index, array->length) ? &array->internalArray[index] : &CLAY__LAYOUT_ELEMENT_TREE_ROOT_DEFAULT;
}
void Clay__LayoutElementTreeRootArray_Set(Clay__LayoutElementTreeRootArray *array, int32_t index, Clay__LayoutElementTreeRoot value) {
if (Clay__Array_RangeCheck(index, array->capacity)) {
array->internalArray[index] = value;
array->length = index < array->length ? array->length : index + 1;
}
}
#pragma endregion
// __GENERATED__ template

Expand Down Expand Up @@ -2457,6 +2463,20 @@ void Clay__CalculateFinalLayout() {
// Calculate sizing along the Y axis
Clay__SizeContainersAlongAxis(false);

// Sort tree roots by z-index
int32_t sortMax = context->layoutElementTreeRoots.length - 1;
while (sortMax > 0) { // todo dumb bubble sort
for (int32_t i = 0; i < sortMax; ++i) {
Clay__LayoutElementTreeRoot current = *Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i);
Clay__LayoutElementTreeRoot *next = Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i + 1);
if (next->zIndex < current.zIndex) {
Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i, *next);
Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i + 1, current);
}
}
sortMax--;
}

// Calculate final positions and generate render commands
context->renderCommands.length = 0;
dfsBuffer.length = 0;
Expand Down

0 comments on commit 2fcb4cc

Please sign in to comment.