fix alloc functions after rename, clean up editor ui code a bit (actually deleting code this time)

This commit is contained in:
Matthew 2025-10-12 17:26:02 +11:00
parent ada1f5a70a
commit 3ad296ba0f
7 changed files with 95 additions and 90 deletions

@ -1 +1 @@
Subproject commit 0be80e3a3ec2acfc51f6591803bbe3f889389d22 Subproject commit cd9e3bb36a68c4c3fd8248afe8abc9930e759f38

@ -1 +1 @@
Subproject commit 4f3082f71d5a877dc34163dac5f1b12a02d5a02c Subproject commit a97309cb04e0e802047586a5bacbacc9b8e9649b

View File

@ -47,6 +47,8 @@ struct LineBuffers
LineBuffer* first; LineBuffer* first;
LineBuffer* last; LineBuffer* last;
u64 count; u64 count;
u64 start;
u64 end;
} }
struct LineBuffer struct LineBuffer
@ -125,7 +127,7 @@ CreateFlatBuffer(u8[] data)
void void
Change(FlatBuffer* fb, u8[] data) Change(FlatBuffer* fb, u8[] data)
{ {
FreeArray(fb.data); Free(fb.data);
u64 cap = data.length > 0 ? RoundUp(cast(u64)(data.length) * 2, KB(4)) : KB(4); u64 cap = data.length > 0 ? RoundUp(cast(u64)(data.length) * 2, KB(4)) : KB(4);
@ -157,7 +159,7 @@ Fix(FlatBuffer* fb)
with(fb) with(fb)
{ {
Reset(&ls_arena); Reset(&ls_arena);
line_starts = AllocArray!(LineStart)(&ls_arena, lf_count+1); line_starts = Alloc!(LineStart)(&ls_arena, lf_count+1);
line_starts[0].pos = 0; line_starts[0].pos = 0;
u16 ignore, pending_level, level; u16 ignore, pending_level, level;
@ -223,8 +225,8 @@ Insert(FlatBuffer* fb, u8[] insert, u64 length, u64 pos)
{ {
if(fb.length + length > fb.data.length) if(fb.length + length > fb.data.length)
{ {
fb.tk.buffer = ReallocArray!(TokenStyle)(fb.tk.buffer, fb.data.length*2); fb.tk.buffer = Realloc!(TokenStyle)(fb.tk.buffer, fb.data.length*2);
fb.data = ReallocArray!(u8)(fb.data, fb.data.length*2); fb.data = Realloc!(u8)(fb.data, fb.data.length*2);
} }
u64 prev_lf = fb.lf_count; u64 prev_lf = fb.lf_count;
@ -281,7 +283,7 @@ Insert(FlatBuffer* fb, u8[] insert, u64 length, u64 pos)
u64 temp_len = fb.length-pos; u64 temp_len = fb.length-pos;
u8[] temp = AllocCopySlice!(u8)(&fb.arena, fb.data, pos, temp_len); u8[] temp = Alloc!(u8)(&fb.arena, fb.data, pos, temp_len);
fb.data[pos .. pos+length] = insert[0 .. length]; fb.data[pos .. pos+length] = insert[0 .. length];
pos += length; pos += length;
@ -425,29 +427,29 @@ GetLines(FlatBuffer* fb, LineBuffers* linebufs, u64 start_line, u64 length)
linebufs.count = 0; linebufs.count = 0;
u64 total_lines = fb.line_starts.length; u64 total_lines = fb.line_starts.length;
start_line = Min(start_line, total_lines); linebufs.start = Min(start_line, total_lines);
u64 end_line = Min(start_line + length, total_lines); linebufs.end = Min(start_line + length, total_lines);
if(fb.length == 0) if(fb.length == 0)
{ {
linebufs.first.text = Alloc!(u8)(&linebufs.arena, 1); linebufs.first.text = Alloc!(u8)(&linebufs.arena, 1);
linebufs.first.style = Alloc!(TS)(&linebufs.arena, 1); linebufs.first.style = Alloc!(TS)(&linebufs.arena, 1);
} }
else if(start_line == end_line) with(fb) else if(linebufs.start == linebufs.end) with(fb)
{ {
SliceLineBuffer(fb, linebufs, linebufs.first, line_starts[start_line].pos, LineLength(fb, start_line)); SliceLineBuffer(fb, linebufs, linebufs.first, line_starts[linebufs.start].pos, LineLength(fb, linebufs.start));
} }
else with(linebufs) else with(linebufs)
{ {
LineBuffer* current = first; LineBuffer* current = first;
for(u64 i = start_line; i < end_line; i += 1) for(u64 i = start; i < end; i += 1)
{ {
u64 start = fb.line_starts[i].pos; u64 start_pos = fb.line_starts[i].pos;
u64 len = LineLength(fb, i); u64 len = LineLength(fb, i);
if(len > 0) if(len > 0)
{ {
SliceLineBuffer(fb, linebufs, current, start, len); SliceLineBuffer(fb, linebufs, current, start_pos, len);
current.next = Alloc!(LineBuffer)(&arena); current.next = Alloc!(LineBuffer)(&arena);
current = current.next; current = current.next;
} }
@ -913,7 +915,7 @@ Delete(FlatBuffer* fb, u64 length, u64 pos)
u8[] temp; u8[] temp;
if(end != fb.length) if(end != fb.length)
{ {
temp = AllocArray!(u8)(&fb.arena, fb.length-end); temp = Alloc!(u8)(&fb.arena, fb.length-end);
temp[0 .. temp.length] = fb.data[end .. fb.length]; temp[0 .. temp.length] = fb.data[end .. fb.length];
fb.data[pos .. pos+temp.length] = temp[0 .. temp.length]; fb.data[pos .. pos+temp.length] = temp[0 .. temp.length];
} }

View File

@ -209,7 +209,7 @@ InitEditorCtx(PlatformWindow* window)
count += 1; count += 1;
} }
ctx.file_names = AllocArray!(u8[])(&ctx.arena, count); ctx.file_names = Alloc!(u8[])(&ctx.arena, count);
count = 0; count = 0;
foreach(DirEntry e; dirEntries(".", SpanMode.breadth)) foreach(DirEntry e; dirEntries(".", SpanMode.breadth))
@ -217,7 +217,7 @@ InitEditorCtx(PlatformWindow* window)
if(indexOf(e.name, ".git") != -1 || e.isDir) continue; if(indexOf(e.name, ".git") != -1 || e.isDir) continue;
u64 start = indexOf(e.name, "./") == 0 ? 2 : 0; u64 start = indexOf(e.name, "./") == 0 ? 2 : 0;
ctx.file_names[count++] = AllocCopy!(u8)(&ctx.arena, CastStr!(u8)(e.name[start .. $])); ctx.file_names[count++] = Alloc!(u8)(&ctx.arena, CastStr!(u8)(e.name[start .. $]));
} }
} }
catch(Exception e) catch(Exception e)
@ -234,7 +234,7 @@ CreatePanel(EditorCtx* ctx, SizeType size_type = ST.Percentage)
{ {
UIPanel* p = Alloc!(UIPanel)(&ctx.arena); UIPanel* p = Alloc!(UIPanel)(&ctx.arena);
p.axis = A2D.Y; p.axis = A2D.Y;
p.id = AllocArray!(u8)(&ctx.arena, 10); p.id = Alloc!(u8)(&ctx.arena, 10);
p.pct = 1.0; p.pct = 1.0;
(cast(char[])p.id).sformat("##%08s", ctx.panel_id); (cast(char[])p.id).sformat("##%08s", ctx.panel_id);
p.parent = p.first = p.last = p.next = p.prev = g_UI_NIL_PANEL; p.parent = p.first = p.last = p.next = p.prev = g_UI_NIL_PANEL;
@ -866,7 +866,7 @@ GetCommands(CmdPalette* cmd)
]; ];
Reset(&cmd.arena); Reset(&cmd.arena);
cmd.commands = AllocArray!(Command)(&cmd.arena, cmd_list.length); cmd.commands = Alloc!(Command)(&cmd.arena, cmd_list.length);
cmd.params = []; cmd.params = [];
u8[] str = cmd.buffer[0 .. cmd.icount]; u8[] str = cmd.buffer[0 .. cmd.icount];
@ -893,7 +893,7 @@ GetCommands(CmdPalette* cmd)
cmd.commands = cmd.commands[0 .. count]; cmd.commands = cmd.commands[0 .. count];
} }
cmd.opt_strs = AllocArray!(u8[])(&cmd.arena, cmd.commands.length); cmd.opt_strs = Alloc!(u8[])(&cmd.arena, cmd.commands.length);
for(u64 i = 0; i < cmd.commands.length; i += 1) for(u64 i = 0; i < cmd.commands.length; i += 1)
{ {
cmd.opt_strs[i] = cmd.commands[i].name; cmd.opt_strs[i] = cmd.commands[i].name;
@ -1021,7 +1021,7 @@ Check(CmdPalette* cmd, u64 length)
{ {
if(cmd.icount+length >= cmd.buffer.length) if(cmd.icount+length >= cmd.buffer.length)
{ {
cmd.buffer = ReallocArray!(u8)(cmd.buffer, cmd.buffer.length*2); cmd.buffer = Realloc!(u8)(cmd.buffer, cmd.buffer.length*2);
} }
} }
@ -1047,8 +1047,8 @@ PopulateParams(CmdPalette* cmd, u8[][] strs)
u8[] param = GetParam(cmd); u8[] param = GetParam(cmd);
if(cmd.params.length == 0 || !param.length) if(cmd.params.length == 0 || !param.length)
{ {
cmd.params = AllocArray!(Parameter)(&cmd.cmd_arena, strs.length); cmd.params = Alloc!(Parameter)(&cmd.cmd_arena, strs.length);
cmd.opt_strs = AllocArray!(u8[])(&cmd.cmd_arena, strs.length); cmd.opt_strs = Alloc!(u8[])(&cmd.cmd_arena, strs.length);
for(u64 i = 0; i < cmd.params.length; i += 1) for(u64 i = 0; i < cmd.params.length; i += 1)
{ {
@ -1060,7 +1060,7 @@ PopulateParams(CmdPalette* cmd, u8[][] strs)
if(param.length) if(param.length)
{ {
cmd.opt_strs = AllocArray!(u8[])(&cmd.cmd_arena, strs.length); cmd.opt_strs = Alloc!(u8[])(&cmd.cmd_arena, strs.length);
u64 matches; u64 matches;
for(u64 i = 0; i < cmd.params.length; i += 1) for(u64 i = 0; i < cmd.params.length; i += 1)

View File

@ -436,7 +436,7 @@ void
ResetTokenizer(FlatBuffer* fb) ResetTokenizer(FlatBuffer* fb)
{ {
Reset(&fb.tk.arena); Reset(&fb.tk.arena);
FreeArray(fb.tk.buffer); Free(fb.tk.buffer);
fb.tk.buffer = Alloc!(TS)(fb.data.length); fb.tk.buffer = Alloc!(TS)(fb.data.length);
fb.tk.pos = 0; fb.tk.pos = 0;

View File

@ -1250,12 +1250,12 @@ MakeMultiline(u8[] text, f32 width, TS[] style = [])
{ {
u64 len = i-start; u64 len = i-start;
u8[] str = ScratchAllocCopySlice!(u8)(text, start, len); u8[] str = ScratchAlloc!(u8)(text, start, len);
TS[] stl = []; TS[] stl = [];
if(style.length > 0) if(style.length > 0)
{ {
stl = ScratchAllocCopySlice!(TS)(style, start, len); stl = ScratchAlloc!(TS)(style, start, len);
} }
Node!(TextBuffer)* n = node; Node!(TextBuffer)* n = node;

View File

@ -77,7 +77,7 @@ struct UIPanel
i64 start_row; i64 start_row;
i64 end_row; i64 end_row;
Vec2 pos; Rect rect;
Vec2 size; Vec2 size;
} }
@ -153,19 +153,21 @@ SetPanelSizes(UIPanel* panel)
{ {
UICtx* ctx = GetCtx(); UICtx* ctx = GetCtx();
panel.size.x = ctx.res.x; panel.size.x = ctx.res.x;
panel.size.y = ctx.res.y; panel.size.y = ctx.res.y;
panel.pos = 0.0; panel.rect.vec0 = 0.0;
panel.rect.vec1 = 0.0;
static foreach(axis; A2D.min .. A2D.max) static foreach(axis; A2D.min .. A2D.max)
{ {
for(UIPanel* p = panel; !Nil(p); p = Recurse(p)) for(UIPanel* p = panel; !Nil(p); p = Recurse(p))
{ {
f32 pos = p.pos.v[axis]; f32 pos = p.rect.vec0.v[axis];
for(UIPanel* c = p.first; !Nil(c); c = c.next) for(UIPanel* c = p.first; !Nil(c); c = c.next)
{ {
c.pos.v[axis] = pos; c.size.v[axis] = p.axis == axis ? c.pct * p.size.v[axis] : p.size.v[axis];
c.size.v[axis] = p.axis == axis ? c.pct * p.size.v[axis] : p.size.v[axis]; c.rect.vec0.v[axis] = pos;
c.rect.vec1.v[axis] = pos + c.size.v[axis];
if(axis == p.axis) if(axis == p.axis)
{ {
@ -197,79 +199,64 @@ Panel2(UIPanel* panel)
UIPanel* parent = panel.parent; UIPanel* parent = panel.parent;
UIPanel* prev = panel.prev; UIPanel* prev = panel.prev;
Vec2 click_adjust = Vec2( Axis2D pax = parent.axis;
Vec2 adj = Vec2(
parent.axis == A2D.X ? 10 : 0, parent.axis == A2D.X ? 10 : 0,
parent.axis == A2D.Y ? 10 : 0 parent.axis == A2D.Y ? 10 : 0
); );
if(!Nil(prev)) Vec2 p0 = panel.rect.vec0-adj;
Vec2 p1 = panel.rect.vec1+adj;
if(!Nil(prev)) with(panel)
{ {
Vec2 p0 = panel.pos-click_adjust; if(Dragged(item, p0, p1) && item.dragged.v[pax] != 0.0)
Vec2 p1 = panel.pos+panel.size+click_adjust;
if(Dragged(item, p0, p1))
{ {
A2D axis = parent.axis; f32 mov_pct = Remap(item.dragged.v[pax], 0.0, size.v[pax], 0.0, 1.0);
f32 p_start = parent.pos.v[axis]; if(CheckPanelBounds(pct + mov_pct) && CheckPanelBounds(panel.prev.pct - mov_pct))
f32 p_end = p_start + parent.size.v[axis];
f32 drag = item.dragged.v[axis];
if(drag != 0.0)
{ {
f32 pct = Remap(drag, 0.0, p_start-p_end, 0.0, 1.0); pct += mov_pct;
if(CheckPanelBounds(panel.pct - pct) && CheckPanelBounds(panel.prev.pct + pct)) panel.prev.pct -= mov_pct;
{
panel.pct -= pct;
panel.prev.pct += pct;
}
} }
} }
} }
if(panel.ed != null) if(panel.ed != null)
{ {
if(Clicked(item, panel.pos, panel.pos+panel.size-click_adjust)) if(Clicked(item, p0, p1))
{ {
SetFocusedPanel(panel); SetFocusedPanel(panel);
} }
DrawBorderedRect(panel.pos, panel.size, 2.0, 2.0, 0.1, DEFAULT_COL, focused ? HL_BORDER_COL : DEFAULT_BORDER_COL); DrawBorderedRect(panel.rect.vec0, panel.size, 2.0, 2.0, 0.1, DEFAULT_COL, focused ? HL_BORDER_COL : DEFAULT_BORDER_COL);
i64 rows = cast(i64)ceil(panel.size.y/TEXT_SIZE); i64 rows = cast(i64)ceil(panel.size.y/TEXT_SIZE);
GetLines(&ed.buf, &ed.linebufs, rows); GetLines(&ed.buf, &ed.linebufs, rows);
i64 offset = ed.buf.offset;
i64 end = offset + ed.linebufs.count;
char[64] ch_buf = '\0'; char[64] ch_buf = '\0';
char[] fmt = ch_buf.sformat("%%0%ss", u64(end.toChars().length)); char[] fmt = ch_buf.sformat("%%0%ss", u64(ed.linebufs.end.toChars().length));
u8[] end_line_text = ScratchAlloc!(u8)(end.toChars().length); u8[] end_line_text = ScratchAlloc!(u8)(ed.linebufs.end.toChars().length, '0');
end_line_text[] = '0';
f32 lcw = CalcTextWidth(end_line_text); f32 lcw = CalcTextWidth(end_line_text);
f32 padding = 4.0; f32 padding = 4.0;
DrawRect(panel.pos, panel.pos+Vec2(lcw+padding*2, panel.size.y), 0.0, 0.0, focused ? LC_HL_COLOR : LC_COLOR); DrawRect(panel.rect.vec0, panel.rect.vec1+Vec2(lcw+padding*2, panel.size.y), 0.0, 0.0, focused ? LC_HL_COLOR : LC_COLOR);
f32 x = panel.pos.x; f32 x = panel.rect.x0;
f32 y = panel.pos.y + TEXT_SIZE; f32 y = panel.rect.y0 + TEXT_SIZE;
auto atlas = &ctx.atlas_buf.atlas; FontAtlas* atlas = &ctx.atlas_buf.atlas;
bool edit = EditModeActive(); bool edit = EditModeActive();
U64Vec2 pos = VecPos(&ed.buf); U64Vec2 pos = VecPos(&ed.buf);
u64 i; u64 i;
for(auto buf = ed.linebufs.first; buf != null; buf = buf.next, i += 1) for(auto buf = ed.linebufs.first; buf != null; buf = buf.next, i += 1)
{ {
f32 x_pos = x + padding; f32 x_pos = x + padding;
char[32] line_buf = '\0';
char[] line_str = sformat(line_buf, fmt, offset+i);
foreach(j; 0 .. line_str.length) DrawLineCount(atlas, fmt, &x_pos, y, ed.linebufs.start+i);
{
DrawChar(&ctx.atlas_buf.atlas, line_str[j], &x_pos, y, Vec4(1.0));
}
x_pos += padding; x_pos += padding;
@ -281,27 +268,13 @@ Panel2(UIPanel* panel)
if(pos.y == i) if(pos.y == i)
{ {
f32 hl_x = x + lcw + padding*2.0; DrawCursor(atlas, l.text, pos.x, x+lcw+padding*2.0, y, ch_offset, edit);
Glyph* g = GetGlyph(' ');
foreach(j; 0 .. l.text.length)
{
bool hl = j == pos.x-ch_offset;
if(hl)
{
break;
}
g = j == l.text.length-1 ? GetGlyph(' ') : GetGlyph(l.text[j]);
hl_x += GlyphWidth(g);
}
DrawRect(hl_x, y, atlas, cast(u8)g.ch, Vec4(1.0), edit);
} }
foreach(j; 0 .. l.text.length) foreach(j; 0 .. l.text.length)
{ {
bool hl = pos.y == i && !edit && j == pos.x-ch_offset; bool hl = pos.y == i && !edit && j == pos.x-ch_offset;
DrawChar(&ctx.atlas_buf.atlas, l.text[j], &x_pos, y, hl ? Vec4(Vec3(0.0), 1.0) : SYNTAX_COLORS[l.style[j]]); DrawChar(atlas, l.text[j], &x_pos, y, hl ? Vec4(Vec3(0.0), 1.0) : SYNTAX_COLORS[l.style[j]]);
} }
y += TEXT_SIZE; y += TEXT_SIZE;
@ -312,6 +285,37 @@ Panel2(UIPanel* panel)
} }
} }
pragma(inline) void
DrawCursor(FontAtlas* atlas, u8[] text, u64 ch_x, f32 x, f32 y, u64 offset, bool edit)
{
Glyph* g = GetGlyph(' ');
foreach(j; 0 .. text.length)
{
bool hl = j == ch_x-offset;
if(hl)
{
break;
}
g = j == text.length-1 ? GetGlyph(' ') : GetGlyph(text[j]);
x += GlyphWidth(g);
}
DrawRect(x, y, atlas, cast(u8)g.ch, Vec4(1.0), edit);
}
pragma(inline) void
DrawLineCount(FontAtlas* atlas, char[] fmt, f32* x_pos, f32 y, u64 line)
{
char[32] line_buf = '\0';
char[] line_str = sformat(line_buf, fmt, line);
foreach(j; 0 .. line_str.length)
{
DrawChar(atlas, line_str[j], x_pos, y, Vec4(1.0));
}
}
f32 f32
LineCounter2(UIPanel* panel, i64 start_row, i64 end_row) LineCounter2(UIPanel* panel, i64 start_row, i64 end_row)
{ {
@ -338,14 +342,13 @@ LineCounter2(UIPanel* panel, i64 start_row, i64 end_row)
sprintf(cast(char*)line_counts[i].ptr, fmt.ptr, start_row+i); sprintf(cast(char*)line_counts[i].ptr, fmt.ptr, start_row+i);
} }
max_line_text = ScratchAlloc!(u8)(width); max_line_text = ScratchAlloc!(u8)(width, '0');
max_line_text[] = cast(u8)'0';
} }
lc_width = CalcTextWidth(max_line_text) + padding*2.0; lc_width = CalcTextWidth(max_line_text) + padding*2.0;
f32 x = panel.pos.x + padding; f32 x = panel.rect.x0 + padding;
f32 y = panel.pos.y + TEXT_SIZE; f32 y = panel.rect.y0 + TEXT_SIZE;
foreach(i; 0 .. line_counts.length) foreach(i; 0 .. line_counts.length)
{ {