From f8f6ea9729bf32ee3ca80e4d7bfe8b75ec80626f Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 5 Nov 2025 19:30:38 -0600 Subject: [PATCH] defer non-layout-axis fill size calculations --- src/ui/ui_common.c | 4 ---- src/ui/ui_core.c | 60 ++++++++++++++++++++++++++++++---------------- src/ui/ui_core.h | 3 ++- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/ui/ui_common.c b/src/ui/ui_common.c index e524f7ab..a1cbd40d 100644 --- a/src/ui/ui_common.c +++ b/src/ui/ui_common.c @@ -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); diff --git a/src/ui/ui_core.c b/src/ui/ui_core.c index a367d35a..3163cc07 100644 --- a/src/ui/ui_core.c +++ b/src/ui/ui_core.c @@ -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; diff --git a/src/ui/ui_core.h b/src/ui/ui_core.h index 2ab0bcce..480ecbe3 100644 --- a/src/ui/ui_core.h +++ b/src/ui/ui_core.h @@ -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;