eliminate recursion from ui size calculation pass, replace with iterative equivalents

This commit is contained in:
Ryan Fleury
2025-04-25 08:25:00 -07:00
parent 0fbcd72a3e
commit fe46e61acb
+29 -50
View File
@@ -1616,29 +1616,23 @@ internal void
ui_calc_sizes_standalone__in_place_rec(UI_Box *root, Axis2 axis) ui_calc_sizes_standalone__in_place_rec(UI_Box *root, Axis2 axis)
{ {
ProfBeginFunction(); ProfBeginFunction();
for(UI_Box *b = root; !ui_box_is_nil(b); b = ui_box_rec_df_pre(b, root).next)
switch(root->pref_size[axis].kind) {
switch(b->pref_size[axis].kind)
{ {
default:{}break; default:{}break;
case UI_SizeKind_Pixels: case UI_SizeKind_Pixels:
{ {
root->fixed_size.v[axis] = root->pref_size[axis].value; b->fixed_size.v[axis] = b->pref_size[axis].value;
}break; }break;
case UI_SizeKind_TextContent: case UI_SizeKind_TextContent:
{ {
F32 padding = root->pref_size[axis].value; F32 padding = b->pref_size[axis].value;
F32 text_size = root->display_fruns.dim.x; F32 text_size = b->display_fruns.dim.x;
root->fixed_size.v[axis] = padding + text_size + root->text_padding*2; b->fixed_size.v[axis] = padding + text_size + b->text_padding*2;
}break; }break;
} }
//- rjf: recurse
for(UI_Box *child = root->first; !ui_box_is_nil(child); child = child->next)
{
ui_calc_sizes_standalone__in_place_rec(child, axis);
} }
ProfEnd(); ProfEnd();
} }
@@ -1646,18 +1640,16 @@ internal void
ui_calc_sizes_upwards_dependent__in_place_rec(UI_Box *root, Axis2 axis) ui_calc_sizes_upwards_dependent__in_place_rec(UI_Box *root, Axis2 axis)
{ {
ProfBeginFunction(); ProfBeginFunction();
for(UI_Box *b = root; !ui_box_is_nil(b); b = ui_box_rec_df_pre(b, root).next)
//- rjf: solve for all kinds that are upwards-dependent
switch(root->pref_size[axis].kind)
{ {
default: break; switch(b->pref_size[axis].kind)
{
// rjf: if root has a parent percentage, figure out its size default:{}break;
case UI_SizeKind_ParentPct: case UI_SizeKind_ParentPct:
{ {
// rjf: find parent that has a fixed size // rjf: find parent that has a fixed size
UI_Box *fixed_parent = &ui_nil_box; UI_Box *fixed_parent = &ui_nil_box;
for(UI_Box *p = root->parent; !ui_box_is_nil(p); p = p->parent) for(UI_Box *p = b->parent; !ui_box_is_nil(p); p = p->parent)
{ {
if(p->flags & (UI_BoxFlag_FixedWidth<<axis) || if(p->flags & (UI_BoxFlag_FixedWidth<<axis) ||
p->pref_size[axis].kind == UI_SizeKind_Pixels || p->pref_size[axis].kind == UI_SizeKind_Pixels ||
@@ -1669,20 +1661,14 @@ ui_calc_sizes_upwards_dependent__in_place_rec(UI_Box *root, Axis2 axis)
} }
} }
// rjf: figure out root's size on this axis // rjf: figure out box's size on this axis
F32 size = fixed_parent->fixed_size.v[axis] * root->pref_size[axis].value; F32 size = fixed_parent->fixed_size.v[axis] * b->pref_size[axis].value;
// rjf: mutate root to have this size // rjf: mutate box to have this size
root->fixed_size.v[axis] = size; b->fixed_size.v[axis] = size;
}break; }break;
} }
//- rjf: recurse
for(UI_Box *child = root->first; !ui_box_is_nil(child); child = child->next)
{
ui_calc_sizes_upwards_dependent__in_place_rec(child, axis);
} }
ProfEnd(); ProfEnd();
} }
@@ -1690,28 +1676,23 @@ internal void
ui_calc_sizes_downwards_dependent__in_place_rec(UI_Box *root, Axis2 axis) ui_calc_sizes_downwards_dependent__in_place_rec(UI_Box *root, Axis2 axis)
{ {
ProfBeginFunction(); ProfBeginFunction();
UI_BoxRec rec = {0};
//- rjf: recurse first. we may depend on children that have for(UI_Box *box = root; !ui_box_is_nil(box); box = rec.next)
// the same property
for(UI_Box *child = root->first; !ui_box_is_nil(child); child = child->next)
{ {
ui_calc_sizes_downwards_dependent__in_place_rec(child, axis); rec = ui_box_rec_df_pre(box, root);
} S32 pop_idx = 0;
for(UI_Box *b = box;
//- rjf: solve for all kinds that are downwards-dependent !ui_box_is_nil(b) && pop_idx <= rec.pop_count;
switch(root->pref_size[axis].kind) b = b->parent, pop_idx += 1)
{ {
default: break; if(b->pref_size[axis].kind == UI_SizeKind_ChildrenSum)
// rjf: sum children
case UI_SizeKind_ChildrenSum:
{ {
F32 sum = 0; F32 sum = 0;
for(UI_Box *child = root->first; !ui_box_is_nil(child); child = child->next) for(UI_Box *child = b->first; !ui_box_is_nil(child); child = child->next)
{ {
if(!(child->flags & (UI_BoxFlag_FloatingX<<axis))) if(!(child->flags & (UI_BoxFlag_FloatingX<<axis)))
{ {
if(axis == root->child_layout_axis) if(axis == b->child_layout_axis)
{ {
sum += child->fixed_size.v[axis]; sum += child->fixed_size.v[axis];
} }
@@ -1721,12 +1702,10 @@ ui_calc_sizes_downwards_dependent__in_place_rec(UI_Box *root, Axis2 axis)
} }
} }
} }
b->fixed_size.v[axis] = sum;
// rjf: figure out root's size on this axis }
root->fixed_size.v[axis] = sum; }
}break;
} }
ProfEnd(); ProfEnd();
} }