fix offsets, update dlib, fix buffer realloc

This commit is contained in:
Matthew 2025-09-26 04:12:58 +10:00
parent 8b71f1239c
commit 822dd5ea71
5 changed files with 108 additions and 32 deletions

@ -1 +1 @@
Subproject commit 85177acdd04a21d0248051f718ef1e57e12f090e
Subproject commit 27d62beb4195c08bd25bffaa0d9c72b523d971f4

View File

@ -111,9 +111,8 @@ Insert(FlatBuffer* fb, u8[] insert, u64 length, u64 pos)
{
if(fb.length + length > fb.data.length)
{
FlatBuffer new_buf = CreateFlatBuffer(fb.data);
MFreeArray(fb.data);
*fb = new_buf;
fb.tk.buffer = ReallocArray!(TokenStyle)(fb.tk.buffer, fb.data.length*2);
fb.data = ReallocArray!(u8)(fb.data, fb.data.length*2);
}
u64 prev_lf = fb.lf_count;

View File

@ -70,8 +70,14 @@ Cycle(EditorCtx* ctx, Inputs* inputs)
g_input_mode = ctx.state == ES.InputMode;
BeginBuild(inputs);
if(ctx.state == ES.CmdOpen)
{
CommandPalette();
}
DrawPanels(ctx.base_panel);
EndBuild();
@ -258,6 +264,7 @@ HandleInputs(EditorCtx* ctx, Inputs* inputs)
{
if(node.value.md & (MD.LeftShift | MD.RightShift))
{
Logf("cmd open");
ctx.state = ES.CmdOpen;
taken = true;
}

View File

@ -44,6 +44,7 @@ enum UIFlags
Clickable = 0x08,
Draggable = 0x10,
TextInput = 0x20,
Window = 0x40,
}
alias UIF = UIFlags;
@ -122,8 +123,6 @@ struct UICtx
struct UIStack(T)
{
UIStack!(T)* next;
T value;
}
@ -354,9 +353,12 @@ ClearStacks(UICtx* ctx)
static foreach(i; 0 .. ctx.tupleof.length)
{
{
alias T = typeof(ctx.tupleof[i]);
static if(
isPointer!(typeof(ctx.tupleof[i])) &&
hasMember!(typeof(ctx.tupleof[i]), "value") &&
isPointer!(T) &&
hasMember!(T, "value") &&
is(typeof(*ctx.tupleof[i]): UIStack!(typeof(ctx.tupleof[i].value)))
)
{
@ -365,6 +367,7 @@ ClearStacks(UICtx* ctx)
}
}
}
}
void
Push(string id, T)(T value)
@ -373,8 +376,12 @@ Push(string id, T)(T value)
import std.traits;
enum string s = "g_ui_ctx.@".replace("@", id);
alias Y = typeof(mixin(s));
static if(isPointer!(typeof(mixin(s))) && hasMember!(typeof(*mixin(s)), "next"))
enum is_ptr = isPointer!(Y);
enum has_mem = is_ptr && hasMember!(typeof(*mixin(s)), "next");
static if(is_ptr && has_mem)
{
enum stack = true;
alias U = typeof(mixin(s~".value"));
@ -414,12 +421,14 @@ Pop(string stack)()
import std.traits;
enum string s = "g_ui_ctx.@".replace("@", stack);
enum string v = s~".value";
enum string n = s~".next";
static assert(isPointer!(typeof(mixin(s))) && hasMember!(typeof(*mixin(s)), "next"));
assert(mixin(s) != null);
auto value = mixin(s~".value");
mixin(s) = mixin(s~".next");
auto value = mixin(v);
mixin(s) = mixin(n);
return value;
}
@ -750,7 +759,7 @@ CalcFixedSizes(UIItem* item)
{
if(i.size_info[axis].type == ST.Pixels)
{
i.size.v[axis] = i.size_info[axis].value + i.adjustment.v[axis] + fabsf(i.offset.v[axis]) + (i.padding.v[axis] * 2.0);
i.size.v[axis] = i.size_info[axis].value + i.adjustment.v[axis] + (i.padding.v[axis] * 2.0);
assert(!isNaN(i.size.v[axis]));
}
}
@ -766,7 +775,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.offset.v[axis];
i.size.v[axis] = (i.parent.size.v[axis]*i.size_info[axis].value) + fabsf(i.adjustment.v[axis]);
assert(!isNaN(i.size.v[axis]));
}
}
@ -779,14 +788,24 @@ CalcPositions(alias axis)(UIItem* item)
f32 pos = 0.0;
for(UIItem* i = item; !Nil(i);)
{
f32 end_pos = pos + i.size.v[axis] + i.offset.v[axis];
i.rect.vec0.v[axis] = pos + i.offset.v[axis];
f32 end_pos = 0.0, next_pos = 0.0;
if(i.flags & UIF.Window)
{
end_pos = i.size.v[axis] + i.offset.v[axis];
i.rect.vec0.v[axis] = i.offset.v[axis];
i.rect.vec1.v[axis] = end_pos;
}
else
{
end_pos = pos + i.size.v[axis];
i.rect.vec0.v[axis] = pos + i.offset.v[axis];
i.rect.vec1.v[axis] = end_pos + i.offset.v[axis];
assert(!isNaN(i.rect.vec0.v[axis]));
assert(!isNaN(i.rect.vec1.v[axis]));
f32 next_pos = i.parent.layout_axis == axis ? end_pos : pos;
next_pos = i.parent.layout_axis == axis ? end_pos : pos;
}
if(!Nil(i.first))
{
@ -900,13 +919,13 @@ RecurseB(UIItem* item)
}
void
BuildItem(UIItem* item, UISize size_x, UISize size_y, UIFlags properties)
BuildItem(UIItem* item, UISize size_x, UISize size_y, UIFlags flags)
{
UICtx* ctx = GetCtx();
item.first = item.last = item.next = item.prev = g_UI_NIL;
item.first = item.last = item.next = item.prev = item.parent = g_UI_NIL;
item.flags = properties;
item.flags = flags;
item.size_info[A2D.X] = size_x;
item.size_info[A2D.Y] = size_y;
item.level = ctx.panel_level;
@ -924,11 +943,28 @@ BuildItem(UIItem* item, UISize size_x, UISize size_y, UIFlags properties)
item.culling.vec0 = Vec2(0.0);
item.culling.vec1 = Vec2(0.0);
if(flags & UIF.Window)
{
assert(ctx.root != g_UI_NIL);
auto n = ctx.root;
for(;;)
{
if(Nil(n.next))
{
n.next = item;
item.prev = n;
break;
}
}
}
else
{
item.parent = ctx.top_parent == g_UI_NIL_NODE ? g_UI_NIL : ctx.top_parent.item;
if(!Nil(item.parent))
{
DLLPush(item.parent, item, g_UI_NIL);
}
}
debug ctx.item_count += 1;
}

View File

@ -411,16 +411,18 @@ TextClicked(TextPart* text_part)
{
bool result = false;
alias result res;
for(TextPart* tp = text_part; !Nil(tp.item); tp = tp.next)
{
if(tp.item.signal & UIS.Clicked)
{
result = true;
res = true;
break;
}
}
return result;
return res;
}
TextPart*
@ -458,6 +460,38 @@ WrappedTextLine(u8[] text, u8[] parent_id, f32 text_size, u64 line_no)
return part;
}
UIItem*
CommandPalette()
{
Vec2 size = RootSize();
f32 x = size.x*0.3;
f32 y = size.y*0.2;
f32 w = size.x*0.4;
f32 h = size.y*0.3;
Logf("%s %s %s %s %s", size.v, x, y, w, h);
UIItem* window = Window(CastStr!(u8)("##cmd_palette"), x, y, w, h);
return window;
}
UIItem*
Window(u8[] id, f32 x, f32 y, f32 w, f32 h)
{
UIItem* item = Get(id);
Push!("offset")(Vec2(x, y));
BuildItem(item, UISize(ST.Pixels, x), UISize(ST.Pixels, y), UIF.Window|UIF.DrawBackground|UIF.DrawBorder);
Pop!("offset")();
return item;
}
void
EndPanel()
{