From 13aedaacff030caa606f36937591c2ccdaa4c9e8 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 16 Sep 2025 06:38:13 +1000 Subject: [PATCH] fix positioning, add some debugging checks/features --- src/editor/editor.d | 46 ++++++++++++++++++++++ src/editor/ui.d | 93 +++++++++++++++++++++++++++++++++++++------- src/editor/widgets.d | 14 +++---- 3 files changed, 133 insertions(+), 20 deletions(-) diff --git a/src/editor/editor.d b/src/editor/editor.d index 9199e9d..2e9630f 100644 --- a/src/editor/editor.d +++ b/src/editor/editor.d @@ -112,6 +112,12 @@ CreateEditor(EditorCtx* ctx) return ed; } +debug +{ + __gshared u64 panel_count = 0; + __gshared UIPanel*[1024] panels = null; +} + void AddEditor(EditorCtx* ctx, UIPanel* target, Axis2D axis) { @@ -132,6 +138,11 @@ AddEditor(EditorCtx* ctx, UIPanel* target, Axis2D axis) PushPanel(target, second); SetFocusedPanel(second); + + panels[panel_count+0] = first; + panels[panel_count+1] = second; + + panel_count += 2; } else if(target.parent.axis == axis) { @@ -150,6 +161,32 @@ AddEditor(EditorCtx* ctx, UIPanel* target, Axis2D axis) } SetFocusedPanel(panel); + + debug + { + panels[panel_count] = panel; + + panel_count += 1; + } + } + + for(u64 i = 0; i < panel_count; i += 1) + { + bool root_found = false; + for(UIPanel* p = panels[i]; !Nil(p); p = p.parent) + { + if(p == ctx.base_panel) + { + root_found = true; + break; + } + } + + if(!root_found) + { + Logf("[DEBUG] panel %s unable to reach root", cast(char[])panels[panel_count].id); + assert(root_found); + } } } @@ -216,6 +253,15 @@ HandleInputs(EditorCtx* ctx, Inputs* inputs) AddEditor(ctx, GetFocusedPanel(), A2D.Y); } } break; + case Input.d: + { + if(node.value.md & (MD.LeftShift | MD.RightShift)) + { + static dbg = false; + dbg = !dbg; + SetDebug(dbg); + } + } break; case Input.Up: case Input.Down: case Input.Left: diff --git a/src/editor/ui.d b/src/editor/ui.d index 78f39b7..10c13fb 100644 --- a/src/editor/ui.d +++ b/src/editor/ui.d @@ -114,6 +114,7 @@ struct UICtx debug u32 item_count; debug u32 final_count; + debug bool dbg; } struct UIItemStackList @@ -480,6 +481,16 @@ SetAdjustment(Vec2 adj) g_ui_ctx.adjustment = adj; } +void +SetDebug(bool dbg) +{ + Logf("Set"); + debug + { + g_ui_ctx.dbg = dbg; + } +} + void SetBorderThickness(f32 value) { @@ -598,6 +609,11 @@ EndBuild() DrawUI(ctx, ctx.root); + if(ctx.dbg) + { + DrawDebugUI(ctx, ctx.root); + } + debug assert(ctx.item_count == ctx.final_count); BindBuffers(&ctx.rd, &ctx.buffers[ctx.f_idx].m_idx, &ctx.buffers[ctx.f_idx].m_vtx); @@ -712,14 +728,32 @@ CalcPositions(alias axis)(UIItem* item) i = i.next; pos = next_pos; } - else if(!Nil(i.parent.next)) + else for(UIItem* p = i.parent; true; p = p.parent) { - i = i.parent.next; - pos = i.parent.layout_axis == axis ? i.prev.rect.vec1.v[axis] : i.prev.rect.vec0.v[axis]; + if(!Nil(p.next)) + { + i = p.next; + pos = i.parent.layout_axis == axis ? i.prev.rect.vec1.v[axis] : i.prev.rect.vec0.v[axis]; + break; + } + + if(Nil(p)) + { + i = g_UI_NIL; + break; + } } - else + } +} + +void +DrawDebugUI(UICtx* ctx, UIItem* item) +{ + for(UIItem* i = item; !Nil(i); i = Recurse(i)) + { + if(i.flags & UIF.DrawBackground) { - break; + DrawPanelDebug(ctx, i); } } } @@ -884,10 +918,16 @@ MakeKey(u8[] id) { UIKey key; + bool hash_only = false; i64 pos = 0; u32 hash_count = 0; for(i64 i = id.length-1; i >= 0; i -= 1) { + if(i == 0 && id[i] == '#') + { + hash_only = true; + } + if(hash_count == 2) { if(id[i] == '#') @@ -906,21 +946,21 @@ MakeKey(u8[] id) } } - if(hash_count == 2) + if(hash_count < 2 || hash_only) { - key.text = id[0 .. pos]; + key.text = id; + key.hash = Hash(id); + } + else if(hash_count == 2) + { + key.text = id.ptr[0 .. pos]; key.hash = Hash(id); } else if(hash_count == 3) { - key.text = id[0 .. pos]; + key.text = id.ptr[0 .. pos]; key.hash = Hash(id[pos+hash_count .. $]); } - else - { - key.text = id; - key.hash = Hash(id); - } return key; } @@ -1047,6 +1087,33 @@ MakeMultiline(u8[] text, f32 width, u8[] parent_id, u64 line_no) return node; } +void +DrawPanelDebug(UICtx* ctx, UIItem* item) +{ + if(item.first) + { + u8[] text = item.key.text; + Rect rect = item.rect; + + f32 y = rect.y0 + (rect.y1 - rect.y0) / 3; + f32 x = rect.x0 + (rect.x1 - rect.x0) / 3; + f32 x0 = x; + for(u64 i = 0; i < text.length; i += 1) + { + DrawGlyph(&ctx.atlas_buf.atlas.glyphs[text[i]], ctx.atlas_buf.atlas.size/ctx.text_size, &x0, y); + } + + f32 pct = item.parent.layout_axis == A2D.X ? item.size_info[A2D.X].value : item.size_info[A2D.Y].value; + + u8[512] buf = 0; + (cast(char[])buf).sformat("%f%%", pct); + y += 16.0; + for(u64 i = 0; i < buf.length && buf[i] != 0; i += 1) + { + DrawGlyph(&ctx.atlas_buf.atlas.glyphs[buf[i]], ctx.atlas_buf.atlas.size/ctx.text_size, &x, y);; + } + } +} void DrawLine(UIItem* item) diff --git a/src/editor/widgets.d b/src/editor/widgets.d index a7a0cd2..a00cdce 100644 --- a/src/editor/widgets.d +++ b/src/editor/widgets.d @@ -70,7 +70,7 @@ Root() } UIItem* -Panel(UIPanel* panel) +Panel(UIPanel* panel, UIFlags flags = UIF.None) { UIItem* item = Get(panel.id); UIItem* separator = g_UI_NIL; @@ -107,7 +107,7 @@ Panel(UIPanel* panel) ); SetLayoutAxis(panel.axis); - BuildItem(item, UISize(ST.Percentage, x_pct), UISize(ST.Percentage, y_pct), UIF.DrawBackground|UIF.Clickable); + BuildItem(item, UISize(ST.Percentage, x_pct), UISize(ST.Percentage, y_pct), UIF.DrawBackground|flags); PushParent(item); @@ -173,15 +173,15 @@ void EditorView(UIPanel* panel) { UICtx* ctx = GetCtx(); - UIItem* item = Panel(panel); + UIItem* item = Panel(panel, UIF.Clickable); - TextPart* tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 14.0, 0); + TextPart* tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 16.0, 0); if(TextClicked(tp)) SetFocusedPanel(panel); - tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 14.0, 1); + tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 16.0, 1); if(TextClicked(tp)) SetFocusedPanel(panel); - tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 14.0, 2); + tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 16.0, 2); if(TextClicked(tp)) SetFocusedPanel(panel); - tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 14.0, 3); + tp = WrappedTextLine(CastStr!(u8)("Test 1234"), panel.id, 16.0, 3); if(TextClicked(tp)) SetFocusedPanel(panel); Signal(item);