defer non-layout-axis fill size calculations

This commit is contained in:
jacob 2025-11-05 19:30:38 -06:00
parent 0b98c24f46
commit f8f6ea9729
3 changed files with 41 additions and 26 deletions

View File

@ -31,10 +31,8 @@ UI_Box *UI_BuildLabelF_(char *fmt_cstr, ...)
UI_Box *UI_BuildSpacer(UI_Size size)
{
UI_Box *box = 0;
UI_Box *old_parent = UI_UseTop(Parent);
Axis axis = old_parent->child_layout_axis;
UI_PushEmptyStack();
{
UI_Push(Tint, 0);
@ -50,13 +48,11 @@ UI_Box *UI_BuildSpacer(UI_Size size)
UI_Box *UI_BuildDivider(UI_Size size)
{
UI_Box *box = 0;
UI_Box *old_parent = UI_UseTop(Parent);
u32 old_tint = UI_UseTop(Tint);
f32 old_border = UI_UseTop(Border);
u32 old_border_color = UI_UseTop(BorderColor);
Axis axis = old_parent->child_layout_axis;
UI_PushEmptyStack();
{
UI_Push(Parent, old_parent);

View File

@ -708,8 +708,6 @@ i64 UI_EndFrame(UI_Frame frame)
render_viewport.pos = VEC2(0, 0);
render_viewport.size = VEC2(draw_size.x, draw_size.y);
/* TODO: Ensure root is parent */
//////////////////////////////
//- Layout
@ -812,12 +810,13 @@ i64 UI_EndFrame(UI_Frame frame)
}
}
/* Calculate upwards-dependent sizes */
/* Calculate upwards-dependent sizes along layout axis */
for (u64 pre_index = 0; pre_index < boxes_count; ++pre_index)
{
UI_Box *box = boxes_pre[pre_index];
for (Axis axis = 0; axis < Axis_CountXY; ++axis)
if (box->parent)
{
Axis axis = box->parent->child_layout_axis;
UI_Size pref_size = box->pref_size[axis];
if (pref_size.kind == UI_SizeKind_Fill)
{
@ -867,6 +866,21 @@ i64 UI_EndFrame(UI_Frame frame)
}
}
/* Calculate upwards-dependent sizes along non-layout axis */
for (u64 pre_index = 0; pre_index < boxes_count; ++pre_index)
{
UI_Box *box = boxes_pre[pre_index];
if (box->parent)
{
Axis axis = !box->parent->child_layout_axis;
UI_Size pref_size = box->pref_size[axis];
if (pref_size.kind == UI_SizeKind_Fill)
{
box->solved_dims[axis] = box->parent->solved_dims[axis] * pref_size.v;
}
}
}
/* Solve violations */
for (u64 pre_index = 0; pre_index < boxes_count; ++pre_index)
{
@ -927,23 +941,7 @@ i64 UI_EndFrame(UI_Frame frame)
}
size_accum = adjusted_size_accum;
}
/* Initialize layout cursor based on alignment */
if (axis == box->child_layout_axis)
{
UI_AxisAlignment alignment = box->child_alignment[axis];
switch(alignment)
{
default: break;
case UI_AxisAlignment_Center:
{
box->layout_cursor = box_size / 2 - size_accum / 2;
} break;
case UI_AxisAlignment_End:
{
box->layout_cursor = box_size - size_accum;
} break;
}
}
box->final_children_size_accum[axis] = size_accum;
}
/* Solve floating violations */
for (UI_Box *child = box->first; child; child = child->next)
@ -968,6 +966,26 @@ i64 UI_EndFrame(UI_Frame frame)
UI_Box *box = boxes_pre[pre_index];
UI_Box *parent = box->parent;
/* Initialize layout cursor based on alignment */
{
Axis axis = box->child_layout_axis;
UI_AxisAlignment alignment = box->child_alignment[axis];
f32 box_size = box->solved_dims[axis];
f32 size_accum = box->final_children_size_accum[axis];
switch(alignment)
{
default: break;
case UI_AxisAlignment_Center:
{
box->layout_cursor = box_size / 2 - size_accum / 2;
} break;
case UI_AxisAlignment_End:
{
box->layout_cursor = box_size - size_accum;
} break;
}
}
/* Position */
{
f32 *dims_arr = box->solved_dims;

View File

@ -244,8 +244,9 @@ Struct(UI_Box)
//- Layout data
F_Run glyph_run;
F_Font *font;
f32 solved_dims[Axis_CountXY];
f32 layout_cursor;
f32 solved_dims[Axis_CountXY];
f32 final_children_size_accum[Axis_CountXY];
//- Post-layout data
Vec2 p0;