From 1855da659abadcf4e9f5104853e2eb8b8f7d2d22 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 6 Oct 2025 07:56:01 +1100 Subject: [PATCH] wip fixing line wrapping and cursor position --- src/dlib | 2 +- src/editor/main.d | 13 +++++++++++++ src/editor/ui.d | 10 +++++++--- src/editor/widgets.d | 34 ++++++++++++++++++++++++++-------- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/dlib b/src/dlib index 92c4f8e..4f3082f 160000 --- a/src/dlib +++ b/src/dlib @@ -1 +1 @@ -Subproject commit 92c4f8eb665e9fc7a58dc9631bb6f26a0828a638 +Subproject commit 4f3082f71d5a877dc34163dac5f1b12a02d5a02c diff --git a/src/editor/main.d b/src/editor/main.d index d9a3a43..4e70bad 100644 --- a/src/editor/main.d +++ b/src/editor/main.d @@ -23,6 +23,7 @@ void main(string[] argv) EditorCtx ctx = InitEditorCtx(&window); + u64 no_ev_count; while (true) { Inputs* inputs = GetEvents(&window); @@ -31,6 +32,18 @@ void main(string[] argv) break; } + if(inputs.list.first == null) + { + no_ev_count += 1; + Pause(); + + if(no_ev_count >= 5) + { + no_ev_count = 0; + Yield(); + } + } + Cycle(&ctx, inputs); } } diff --git a/src/editor/ui.d b/src/editor/ui.d index 54fd1bb..26eca60 100644 --- a/src/editor/ui.d +++ b/src/editor/ui.d @@ -770,7 +770,8 @@ PrintNodes(UIItem* item) void FixViolations(UIItem* item) { - A2D axis = A2D.Y; + // TODO: Fix this + Axis2D axis = A2D.Y; for(UIItem* i = item; !Nil(i); i = Recurse(i)) { if(!Nil(i.parent)) @@ -779,12 +780,15 @@ FixViolations(UIItem* item) if(i.rect.vec0.v[axis] < bounds_start) { i.culling.vec0.v[axis] = bounds_start - i.rect.vec0.v[axis]; + i.rect.vec0.v[axis] -= i.culling.vec0.v[axis]; + //Logf("cull start %f", i.culling.vec0.v[axis]); } f32 bounds_end = i.parent.rect.vec1.v[axis] - i.parent.culling.vec1.v[axis]; if(i.rect.vec1.v[axis] > bounds_end) { i.culling.vec1.v[axis] = i.rect.vec1.v[axis] - bounds_end; + i.rect.vec1.v[axis] -= i.culling.vec1.v[axis]; } } } @@ -815,7 +819,7 @@ CalcPercentageSizes(UIItem* item) { if(i.size_info[axis].type == ST.Percentage) { - i.size.v[axis] = (i.parent.size.v[axis]*i.size_info[axis].value) + fabsf(i.adjustment.v[axis]); + i.size.v[axis] = (i.parent.size.v[axis]*i.size_info[axis].value) + i.adjustment.v[axis]; assert(!isNaN(i.size.v[axis])); } } @@ -1242,7 +1246,7 @@ MakeMultiline(u8[] text, f32 width, u8[] parent_id, u64 line_no, TS[] style = [] { f32 scaled_width = width * (g_ui_ctx.atlas_buf.atlas.size/g_ui_ctx.text_size); f32 text_width = CalcTextWidth(text); - + u64 line_count = cast(u64)(ceil(text_width/scaled_width)); Node!(TextBuffer)* node = null; if(line_count > 0) diff --git a/src/editor/widgets.d b/src/editor/widgets.d index dd4f954..2d898f1 100644 --- a/src/editor/widgets.d +++ b/src/editor/widgets.d @@ -374,7 +374,7 @@ EditorView(UIPanel* panel) GetLines(&ed.buf, &ed.linebufs, rows); } - LineCounter(panel.id, &panel.ed.buf, offset, line_offset, line_offset+line_rows, focused); + UIItem* lc = LineCounter(panel.id, &panel.ed.buf, offset, line_offset, line_offset+line_rows, focused); Container(ScratchName(panel.id, "lines"), UISize(ST.Percentage, 1.0), UISize(ST.Percentage, 1.0), A2D.Y); { @@ -399,23 +399,18 @@ EditorView(UIPanel* panel) if(buf.text.length > 0) { i64 line_no = i+line_offset; - if(pos.y == line_no && focused) - { - Push!("highlighted_char")(pos.x); - } if(sm == SM.Line && line_no >= sel.x && line_no <= sel.y) { } - TextPart* tp = WrappedTextLine(buf.text, panel.id, text_size, line_no, buf.style); + TextPart* tp = WrappedTextLine(buf.text, panel.id, text_size, line_no, buf.style, &pos); height += (text_size * tp.count); if(TextClicked(tp)) { SetFocusedPanel(panel); } } - Push!("highlighted_char")(-1); if(buf == ed.linebufs.first) { @@ -455,7 +450,7 @@ TextClicked(TextPart* text_part) } TextPart* -WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no, TS[] style = []) +WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no, TS[] style = [], U64Vec2* hl_pos) { Push!("color")(Vec4(1.0)); Push!("text_size")(text_size); @@ -464,6 +459,13 @@ WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no, TS[] styl TextPart* part = ScratchAlloc!(TextPart)(); part.item = g_UI_NIL; + i64 hl = -1; + if(hl_pos.y == line_no) + { + hl = hl_pos.x; + } + + u64 chars; TextPart* tp = part; Node!(TextBuffer)* lines = MakeMultiline(text, parent.size.x, parent_id, line_no, style); for(Node!(TextBuffer)* line = lines; line != null; line = line.next, tp = tp.next) @@ -476,6 +478,20 @@ WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no, TS[] styl tp.item.token_styles = line.value.style; + if(hl >= 0 && hl < tp.item.key.text.length+chars) + { + //Logf("hl %s %s %s", hl, tp.item.key.text.length, chars); + if(hl < tp.item.key.text.length+chars) + { + Push!("highlighted_char")(hl-chars); + hl = -1; + } + else + { + chars += tp.item.key.text.length; + } + } + Signal(tp.item); if(tp.item.signal & UIS.Clicked) @@ -484,6 +500,8 @@ WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no, TS[] styl } BuildItem(tp.item, UISize(ST.Percentage, 1.0), UISize(ST.Pixels, text_size), UIF.DrawText|UIF.Clickable|UIF.Draggable); + + Push!("highlighted_char")(-1); } Pop!("color");