From 7de757ec01f7c5d3ecf5693c225386cb97d1f05d Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 3 Nov 2025 16:23:59 -0600 Subject: [PATCH] use ui fit size kind for text contents as well --- src/pp/pp.c | 45 ++++++++++++++++++---- src/pp/pp_widgets.c | 2 +- src/ui/ui_common.c | 4 +- src/ui/ui_core.c | 91 ++++++++++++++++++--------------------------- src/ui/ui_core.h | 6 +-- 5 files changed, 79 insertions(+), 69 deletions(-) diff --git a/src/pp/pp.c b/src/pp/pp.c index 5643602b..556109d8 100644 --- a/src/pp/pp.c +++ b/src/pp/pp.c @@ -1962,6 +1962,37 @@ void PP_UpdateUser(void) UI_SetNext(Height, UI_PIX(size.y, 0)); UI_Box *lister_box = UI_BuildBox(Lit("lister")); g->lister_key = lister_box->key; + + UI_PushCheckpoint(); + { + UI_SetNext(Parent, lister_box); + UI_SetNext(LayoutAxis, Axis_X); + UI_SetNext(Width, UI_FILL(1, 0)); + UI_SetNext(Height, UI_TXT(1, 0)); + // UI_SetNext(BackgroundColor, 0); + // UI_SetNext(Border, 0); + UI_Box *title_bar = UI_BuildBox(Zstr); + { + UI_Push(Parent, title_bar); + UI_Push(Width, UI_FILL(1, 0)); + UI_Push(TextPadding, 0); + UI_Push(BackgroundColor, 0); + UI_Push(Border, 0); + { + UI_Box *left_box = UI_BuildBox(Zstr); + } + { + UI_SetNext(Text, Lit("Title")); + UI_SetNext(Flags, UI_BoxFlag_DrawText); + UI_SetNext(Width, UI_FIT(1)); + UI_Box *title_box = UI_BuildBox(Zstr); + } + { + UI_Box *right_box = UI_BuildBox(Zstr); + } + } + } + UI_PopCheckpoint(); } UI_PopCheckpoint(); } @@ -1991,7 +2022,7 @@ void PP_UpdateUser(void) UI_BuildLabelF("Blended world tick: %F", FmtUint(g->ss_blended->tick)); UI_BuildLabelF("Blended world time: %F", FmtFloat(SecondsFromNs(g->ss_blended->sim_time_ns))); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("Average local sim publish dt: %F", FmtFloat(SecondsFromNs(g->average_local_to_user_snapshot_publish_dt_ns))); UI_BuildLabelF("Local sim last known tick: %F", FmtUint(g->local_sim_last_known_tick)); @@ -2002,7 +2033,7 @@ void PP_UpdateUser(void) UI_BuildLabelF("Render time: %F", FmtFloat(SecondsFromNs(g->render_time_ns))); UI_BuildLabelF("Local player: [%F]", FmtUid(local_player->key.uid)); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); Vec2 world_cursor = g->world_cursor; UI_BuildLabelF("Cursor world: %F, %F", FmtFloat(world_cursor.x), FmtFloat(world_cursor.y)); @@ -2015,7 +2046,7 @@ void PP_UpdateUser(void) Vec2I32 tile_chunk_cursor = PP_TileChunkIndexFromWorldTileIndex(world_tile_cursor); UI_BuildLabelF("Cursor tile chunk: %F, %F", FmtSint(tile_chunk_cursor.x), FmtSint(tile_chunk_cursor.y)); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("Network read: %F mbit/s", FmtFloat((f64)g->net_bytes_read.last_second * 8 / 1000 / 1000)); @@ -2024,18 +2055,18 @@ void PP_UpdateUser(void) UI_BuildLabelF("Ping (real): %F ms", FmtFloat(SecondsFromNs(local_player->player_last_rtt_ns) * 1000)); UI_BuildLabelF("Ping (average): %F ms", FmtFloat(local_player->player_average_rtt_seconds * 1000)); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("Memory committed: %F MiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_COMMITTED) / 1024 / 1024)); UI_BuildLabelF("Virtual memory reserved: %F TiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_RESERVED) / 1024 / 1024 / 1024 / 1024)); UI_BuildLabelF("Arenas allocated: %F", FmtUint(GetGstat(GSTAT_NUM_ARENAS))); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("GPU dedicated memory usage: %F MiB", FmtFloat((f64)gpu_stats.local_committed / 1024 / 1024)); UI_BuildLabelF("GPU shared memory usage: %F MiB", FmtFloat((f64)gpu_stats.non_local_committed / 1024 / 1024)); - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("GPU resources: %F", FmtUint(gpu_stats.driver_resources_allocated)); UI_BuildLabelF("GPU descriptors: %F", FmtUint(gpu_stats.driver_descriptors_allocated)); @@ -2043,7 +2074,7 @@ void PP_UpdateUser(void) //UI_BuildLabelF(\n")); #if RtcIsEnabled - UI_BuildSpacer(UI_EM(1.5, 0)); + UI_BuildSpacer(UI_TXT(1, 0)); UI_BuildLabelF("Debug steps: %F", FmtUint(GetGstat(GSTAT_DEBUG_STEPS))); //UI_BuildLabelF(\n")); #endif diff --git a/src/pp/pp_widgets.c b/src/pp/pp_widgets.c index 0ff42917..70162a4a 100644 --- a/src/pp/pp_widgets.c +++ b/src/pp/pp_widgets.c @@ -133,7 +133,7 @@ UI_Box *PP_BuildDebugConsole(b32 minimized) UI_SetNext(TextPadding, 6); UI_SetNext(Text, text); UI_SetNext(Width, UI_FILL(1, 0)); - UI_SetNext(Height, UI_TXT(1)); + UI_SetNext(Height, UI_FIT(1)); UI_SetNext(Flags, UI_BoxFlag_DrawText); UI_Box *log_textbox = UI_BuildBox(Zstr); } diff --git a/src/ui/ui_common.c b/src/ui/ui_common.c index 21948838..f7ba3a00 100644 --- a/src/ui/ui_common.c +++ b/src/ui/ui_common.c @@ -3,8 +3,8 @@ UI_Box *UI_BuildLabel(String text) { - UI_SetNext(Width, UI_TXT(0)); - UI_SetNext(Height, UI_TXT(0)); + UI_SetNext(Width, UI_FIT(1)); + UI_SetNext(Height, UI_FIT(1)); UI_SetNext(Text, text); UI_SetNext(Flags, UI_BoxFlag_DrawText); UI_Box *box = UI_BuildBox(Zstr); diff --git a/src/ui/ui_core.c b/src/ui/ui_core.c index 094b52ac..4ed7784d 100644 --- a/src/ui/ui_core.c +++ b/src/ui/ui_core.c @@ -229,10 +229,14 @@ UI_Box *UI_BuildBox(String seed) DllPushBackNP(bin->first, bin->last, box, next_in_bin, prev_in_bin); back_box = UI_BackBoxFromKey(key); } - box->parent = parent; box->key = key; ++g->boxes_count; + /* Insert into parent */ + DllPushBack(parent->first, parent->last, box); + box->parent = parent; + ++parent->count; + /* Persist data from back box */ if (back_box != 0) { @@ -252,18 +256,19 @@ UI_Box *UI_BuildBox(String seed) box->font_size = UI_UseTop(FontSize); box->text_padding = UI_UseTop(TextPadding); box->rounding = UI_UseTop(Rounding); - box->display_text = UI_UseTop(Text); + box->text = UI_UseTop(Text); box->floating_pos = UI_UseTop(FloatingPos); /* Prefetch font */ - if (box->display_text.len > 0) + if (box->text.len > 0) { box->font = F_LoadFontAsync(box->font_resource, box->font_size); + if (box->font) + { + box->glyph_run = F_RunFromString(g->build_arena, box->font, box->text); + } } - DllPushBack(parent->first, parent->last, box); - ++parent->count; - return box; } @@ -322,28 +327,6 @@ void UI_BeginBuild(ControllerEventsArray controller_events) } - // /* Refresh initial box events */ - // for (u64 pre_index = g->boxes_count; pre_index-- > 0;) - // { - // UI_Box *box = g->boxes_pre[pre_index]; - // if (g->cursor_pos.x > box->p0.x && g->cursor_pos.x < box->p1.x && - // g->cursor_pos.y > box->p0.y && g->cursor_pos.y < box->p1.y) - // { - // box->event.flags |= UI_EventFlag_Hovered; - // hovered_box = box; - // } - // else - // { - // box->event.flags &= ~UI_EventFlag_Hovered; - // hovered_box = 0; - // } - - // if (box->event.flags & UI_EventFlag_Active) - // { - // active_box = box; - // } - // } - /* Update state from controller events */ for (u64 cev_index = 0; cev_index < controller_events.count; ++cev_index) { @@ -360,16 +343,19 @@ void UI_BeginBuild(ControllerEventsArray controller_events) for (u64 pre_index = g->boxes_count; pre_index-- > 0;) { UI_Box *box = g->boxes_pre[pre_index]; - if (g->cursor_pos.x > box->p0.x && g->cursor_pos.x < box->p1.x && - g->cursor_pos.y > box->p0.y && g->cursor_pos.y < box->p1.y) + if (box->key.hash != 0) { - if (hovered_box) + if (g->cursor_pos.x > box->p0.x && g->cursor_pos.x < box->p1.x && + g->cursor_pos.y > box->p0.y && g->cursor_pos.y < box->p1.y) { - hovered_box->event.flags &= ~UI_EventFlag_Hovered; + if (hovered_box) + { + hovered_box->event.flags &= ~UI_EventFlag_Hovered; + } + box->event.flags |= UI_EventFlag_Hovered; + hovered_box = box; + break; } - box->event.flags |= UI_EventFlag_Hovered; - hovered_box = box; - break; } } } break; @@ -583,16 +569,12 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) { box->solved_dims[axis] = pref_size.v; } - else if (pref_size.kind == UI_SizeKind_TextContents) + else if (pref_size.kind == UI_SizeKind_Fit && (box->flags & UI_BoxFlag_DrawText)) { /* TODO: Distinguish between baseline alignment & visual alignment */ f32 text_size = 0; - if (box->display_text.len > 0 && box->font) + if (box->font) { - if (box->glyph_run.count == 0) - { - box->glyph_run = F_RunFromString(g->build_arena, box->font, box->display_text); - } if (axis == Axis_X) { if (box->glyph_run.count > 0) @@ -625,7 +607,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) for (UI_Box *ancestor = box->parent; ancestor; ancestor = ancestor->parent) { UI_Size tmp = ancestor->pref_size[axis]; - if (tmp.kind == UI_SizeKind_Pixel || tmp.kind == UI_SizeKind_TextContents) + if (tmp.kind == UI_SizeKind_Pixel || (tmp.kind == UI_SizeKind_Fit && (box->flags & UI_BoxFlag_DrawText))) { ancestor_size = ancestor->solved_dims[axis]; break; @@ -643,7 +625,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) for (Axis axis = 0; axis < Axis_CountXY; ++axis) { UI_Size pref_size = box->pref_size[axis]; - if (pref_size.kind == UI_SizeKind_Fit) + if (pref_size.kind == UI_SizeKind_Fit && !(box->flags & UI_BoxFlag_DrawText)) { f32 accum = 0; for (UI_Box *child = box->first; child; child = child->next) @@ -746,23 +728,23 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) { UI_Box *box = boxes_pre[pre_index]; UI_Box *parent = box->parent; - Vec2 final_size = VEC2(box->solved_dims[0], box->solved_dims[1]); + Vec2 dims = VEC2(box->solved_dims[0], box->solved_dims[1]); + Vec2 pos = ZI; if (box->flags & UI_BoxFlag_Floating) { /* Calculate floating box pos */ Vec2 offset = box->floating_pos; - box->p0 = AddVec2(parent->p0, offset); + pos = AddVec2(parent->p0, offset); if (box->flags & UI_BoxFlag_ClampFloatingX) { - f32 overshoot = MaxF32(0, (box->p0.x + final_size.x) - parent->p1.x); - box->p0.x = MaxF32(parent->p0.x, box->p0.x - overshoot); + f32 overshoot = MaxF32(0, (pos.x + dims.x) - parent->p1.x); + pos.x = MaxF32(parent->p0.x, pos.x - overshoot); } if (box->flags & UI_BoxFlag_ClampFloatingY) { - f32 overshoot = MaxF32((box->p0.y + final_size.y) - parent->p1.y, 0); - box->p0.y = MaxF32(parent->p0.y, box->p0.y - overshoot); + f32 overshoot = MaxF32((pos.y + dims.y) - parent->p1.y, 0); + pos.y = MaxF32(parent->p0.y, pos.y - overshoot); } - box->p1 = AddVec2(box->p0, final_size); } else { @@ -772,11 +754,12 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) b32 is_layout_x = parent->layout_axis == Axis_X; f32 layout_cursor = parent->layout_cursor; Vec2 offset = VEC2(layout_cursor * is_layout_x, layout_cursor * !is_layout_x); - parent->layout_cursor += final_size.x * is_layout_x + final_size.y * !is_layout_x; - box->p0 = AddVec2(parent->p0, offset); + parent->layout_cursor += dims.x * is_layout_x + dims.y * !is_layout_x; + pos = AddVec2(parent->p0, offset); } - box->p1 = AddVec2(box->p0, final_size); } + box->p0 = pos; + box->p1 = AddVec2(pos, dims); } ////////////////////////////// @@ -829,8 +812,6 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf) Texture2DRid tex_rid = GPU_Texture2DRidFromResource(box->font->texture); Vec2 inv_font_image_size = VEC2(1.0f / (f32)box->font->image_width, 1.0f / (f32)box->font->image_height); - String text = box->display_text; - f32 ascent = box->font->ascent; f32 descent = box->font->descent; f32 padding = box->text_padding; diff --git a/src/ui/ui_core.h b/src/ui/ui_core.h index 8d1fb969..6ecc411b 100644 --- a/src/ui/ui_core.h +++ b/src/ui/ui_core.h @@ -17,7 +17,6 @@ Enum(UI_SizeKind) UI_SizeKind_Fit, UI_SizeKind_Fill, UI_SizeKind_Pixel, - UI_SizeKind_TextContents, }; Struct(UI_Size) @@ -161,7 +160,7 @@ Struct(UI_Box) f32 text_padding; f32 rounding; Vec2 floating_pos; - String display_text; + String text; Axis layout_axis; ResourceKey font_resource; f32 font_size; @@ -275,8 +274,7 @@ UI_Style UI_StyleFromTopNode(UI_StyleKind kind, b32 use); #define UI_PIX(_v, _s) UI_SIZE(UI_SizeKind_Pixel, (_v), (_s)) #define UI_FIT(_s) UI_SIZE(UI_SizeKind_Fit, 0, (_s)) #define UI_FILL(_v, _s) UI_SIZE(UI_SizeKind_Fill, (_v), (_s)) -#define UI_TXT(_s) UI_SIZE(UI_SizeKind_TextContents, 0, (_s)) -#define UI_EM(_v, _s) UI_SIZE(UI_SizeKind_Pixel, (f32)UI_PeekTop(FontSize) * (_v), (_s)) +#define UI_TXT(_v, _s) UI_SIZE(UI_SizeKind_Pixel, (f32)UI_PeekTop(FontSize) * (4.0 / 3.0) * (_v), (_s)) //////////////////////////////////////////////////////////// //~ Box