2025-08-28 06:57:25 +10:00

185 lines
3.2 KiB
D

import dlib.aliases;
import dlib.math;
import dlib.util;
import dlib.alloc;
import dlib.fonts;
import vulkan;
import editor;
enum BaseFlags : u32
{
Clickable = (1<<0),
ViewScroll = (1<<1),
DrawText = (1<<2),
DrawBackground = (1<<3),
DrawBorder = (1<<4),
Resizeable = (1<<5),
Draggable = (1<<6),
}
union Rect
{
struct
{
Vec2 vec0;
Vec2 vec1;
};
struct
{
f32 x0;
f32 y0;
f32 x1;
f32 y1;
};
}
alias UIHash = u64;
alias UIPair = KVPair!(UIHash, UIItem*);
struct UIItem
{
BaseFlags flags;
Rect rect;
}
struct UIContext
{
HashTable!(UIHash, UIItem*) items;
Arena arena;
UIBuffer buffer;
Vec4[4] bg_cols;
Vec4[4] border_color;
}
void
Reset(UIContext* ctx)
{
ctx.buffer.count = 0;
}
UIContext
CreateUIContext(Vertex[] vtx, u32[] idx)
{
UIContext ctx = {
items: CreateHashTable!(UIHash, UIItem*)(16),
arena: CreateArena(MB(8)),
buffer: {
vtx: vtx,
idx: idx,
},
};
return ctx;
}
void
SetBgCols(UIContext* ctx, Vec4[4] cols)
{
ctx.bg_cols = cols;
}
void
SetBorderCols(UIContext* ctx, Vec4[4] cols)
{
ctx.border_color = cols;
}
UIItem*
Find(UIContext* ctx, u8[] id)
{
u64 hash = Hash(id);
Result!(UIItem*) result = ctx.items[hash];
if (!result.ok)
{
result.value = Alloc!(UIItem)(&ctx.arena);
Push(&ctx.items, hash, result.value);
}
return result.value;
}
bool
UIWindow(UIContext* ctx, Rect rect, f32 border_px, u8[] text)
{
UIItem* item = Find(ctx, text);
return false;
}
pragma(inline): void
DrawGlyph(UIContext* ctx, Glyph* glyph, f32 scale, f32* x_pos, f32 y, Vec4 col = Vec4(1.0))
{
if (glyph.atlas_left != glyph.atlas_right)
{
Vertex* v = ctx.buffer.vtx.ptr + ctx.buffer.count;
f32 r = glyph.plane_right * scale;
f32 l = glyph.plane_left * scale;
f32 w = r - l;
f32 h = (glyph.plane_bottom - glyph.plane_top) * scale;
f32 y_pos = glyph.plane_top * scale;
v.dst_start.x = *x_pos + l;
v.dst_start.y = y - y_pos;
v.dst_end.x = *x_pos + w + l;
v.dst_end.y = y + h - y_pos;
v.src_start.x = glyph.atlas_left;
v.src_start.y = glyph.atlas_top;
v.src_end.x = glyph.atlas_right;
v.src_end.y = glyph.atlas_bottom;
v.cols[0] = col;
v.cols[1] = col;
v.cols[2] = col;
v.cols[3] = col;
v.texture = 1;
AddUIIndices(ctx);
}
*x_pos += glyph.advance * scale;
}
pragma(inline) void
DrawRect(UIContext* ctx, f32 p0_x, f32 p0_y, f32 p1_x, f32 p1_y, f32 border, f32 corner, f32 softness, f32 raised, Vec4 col)
{
DrawRect(ctx, p0_x, p0_y, p1_x, p1_y, border, corner, softness, raised, [col, col, col, col]);
}
pragma(inline) void
DrawRect(UIContext* ctx, f32 p0_x, f32 p0_y, f32 p1_x, f32 p1_y, f32 border, f32 corner, f32 softness, f32 raised, Vec4[4] cols)
{
// Y reversed
Vertex* v = ctx.buffer.vtx.ptr + ctx.buffer.count;
v.dst_start.x = p0_x;
v.dst_start.y = p0_y;
v.dst_end.x = p1_x;
v.dst_end.y = p1_y;
v.cols = cols;
v.border_thickness = border;
v.corner_radius = corner;
v.edge_softness = softness;
v.raised = raised;
AddUIIndices(ctx);
}
void
AddUIIndices(UIContext* ctx)
{
ctx.buffer.idx[0] = 0;
ctx.buffer.idx[1] = 1;
ctx.buffer.idx[2] = 2;
ctx.buffer.idx[3] = 2;
ctx.buffer.idx[4] = 1;
ctx.buffer.idx[5] = 3;
ctx.buffer.count += 1;
}