This commit is contained in:
Alessandro Mauri 2026-03-11 17:37:20 +01:00
commit e193109570
10 changed files with 368 additions and 48 deletions

View file

@ -162,7 +162,7 @@ macro Id @compute_id(...)
{
Id id = (Id)$$LINE.hash() ^ (Id)@str_hash($$FILE);
$for var $i = 0; $i < $vacount; $i++:
id ^= (Id)$vaconst[$i].hash();
id ^= (Id)$vaexpr[$i].hash();
$endfor
return id;
}
@ -276,7 +276,9 @@ fn void? Ctx.frame_end(&ctx)
}
// 2. clear input fields
Point mdelta = ctx.current_input.mouse.pos - ctx.input.mouse.pos;
ctx.input = ctx.current_input;
ctx.input.mouse.delta = mdelta;
ctx.current_input.events = {};
ctx.current_input.mouse.scroll = {};
ctx.current_input.mouse.updated = BTN_NONE;

View file

@ -93,6 +93,7 @@ fn void? Ctx.input_window_size(&ctx, short width, short height)
ctx.current_input.events.resize = ctx.width != width || ctx.height != height;
ctx.width = width;
ctx.height = height;
if (ctx.current_input.events.resize) ctx.skip_frame = true;
}
// Window gained/lost focus

View file

@ -20,6 +20,18 @@ enum Anchor {
CENTER
}
bitstruct ResizeDirection : char {
bool x : 0;
bool y : 1;
}
bitstruct ResizeAnchor : char {
bool top : 0;
bool bottom : 1;
bool left : 2;
bool right : 3;
}
struct Layout {
Size w, h; // size of the CONTENT, does not include margin, border and padding
struct children { // the children size includes the children's margin/border/pading
@ -39,7 +51,7 @@ struct Layout {
Rect content_offset; // combined effect of margin, border and padding
}
// Returns the width and height of a @FIT() element based on it's wanted size (min/max)
// Returns the width and height of a @fit() element based on it's wanted size (min/max)
// and the content size, this function is used to both update the parent's children size and
// give the dimensions of a fit element
// TODO: test and cleanup this function
@ -104,8 +116,8 @@ macro Point Layout.get_dimensions(&el)
macro Point Elem.content_space(&e)
{
return {
.x = e.bounds.w - e.layout.content_offset.x - e.layout.content_offset.w,
.y = e.bounds.h - e.layout.content_offset.y - e.layout.content_offset.h,
.x = (short)max(e.bounds.w - e.layout.content_offset.x - e.layout.content_offset.w, 0),
.y = (short)max(e.bounds.h - e.layout.content_offset.y - e.layout.content_offset.h, 0),
};
}
@ -132,12 +144,6 @@ fn void update_parent_size(Elem* child, Elem* parent)
}
}
fn void update_children_bounds(Elem* child, Elem* parent)
{
if (child.layout.absolute) return;
parent.children_bounds = containing_rect(child.bounds + parent.layout.scroll_offset, parent.bounds);
}
macro Rect Elem.content_bounds(&elem) => elem.bounds.pad(elem.layout.content_offset);
// Assign the width and height of an element in the directions that it doesn't need to grow
@ -168,7 +174,7 @@ fn void resolve_grow_elements(Elem* e, Elem* p)
if (e.layout.absolute) { // absolute children do not need to share space
e.bounds.w = p.content_space().x;
} else if (p.layout.dir == ROW) { // grow along the axis, divide the parent size
short slot = (short)((p.content_space().x - p.layout.occupied) / p.layout.grow_children);
short slot = (short)max(((p.content_space().x - p.layout.occupied) / p.layout.grow_children), 0);
e.bounds.w = slot;
p.layout.grow_children--;
p.layout.occupied += slot;
@ -182,7 +188,7 @@ fn void resolve_grow_elements(Elem* e, Elem* p)
if (e.layout.absolute) { // absolute children do not need to share space
e.bounds.h = p.content_space().y;
} else if (p.layout.dir == COLUMN) { // grow along the axis, divide the parent size
short slot = (short)((p.content_space().y - p.layout.occupied) / p.layout.grow_children);
short slot = (short)max(((p.content_space().y - p.layout.occupied) / p.layout.grow_children), 0);
e.bounds.h = slot;
p.layout.grow_children--;
p.layout.occupied += slot;
@ -205,7 +211,7 @@ fn void resolve_placement(Elem* c, Elem* p)
// if the element has absolute position assign the origin and do not update the parent
if (cl.absolute) {
c.bounds.x = p.bounds.x + pl.content_offset.x + cl.origin.x;
c.bounds.y = p.bounds.x + pl.content_offset.x + cl.origin.y;
c.bounds.y = p.bounds.y + pl.content_offset.y + cl.origin.y;
return;
}
@ -295,6 +301,23 @@ fn void resolve_placement(Elem* c, Elem* p)
pl.origin.y += c.bounds.h;
default: unreachable("unknown layout direction");
}
// update the parent children_bounds
// FIXME: this causes scollbars to flicker in/out during resize because the current frames children_bounds are updated
// with the previous' frame child bounds. It would be better to implement another pass during layout.
// FIXME: this long way around to compute the children bounds works and reduces flickering, but it is very ugly
Rect ncb = c.children_bounds;
ncb.x = c.bounds.x;
ncb.y = c.bounds.y;
Rect cb = containing_rect(c.bounds, ncb);
Point o = p.layout.scroll_offset;
p.children_bounds = containing_rect(cb + o, p.children_bounds);
// reset the children bounds
c.children_bounds = {
.x = c.bounds.x,
.y = c.bounds.y
};
}
fn void? Ctx.layout_element_tree(&ctx)
@ -330,10 +353,8 @@ fn void? Ctx.layout_element_tree(&ctx)
Elem* c = ctx.find_elem(ctx.tree.get(ch))!;
if (ctx.tree.is_root(ch)) {
resolve_placement(p, &&{});
update_children_bounds(p, &&{});
} else {
resolve_placement(c, p);
update_children_bounds(c, p);
}
// FIXME: this stuff would be better elsewhere but we are already iteraring through all

View file

@ -1,5 +1,5 @@
module ugui;
import std::math;
// ---------------------------------------------------------------------------------- //
// RECTANGLE //
@ -244,8 +244,6 @@ macro uint Color.to_uint(c) => c.r | (c.g << 8) | (c.b << 16) | (c.a << 24);
// SIZE //
// ---------------------------------------------------------------------------------- //
macro short short.add_no_of(short a, short b) => (short)max(min((int)a + (int)b, short.max), short.min) @inline;
struct Size {
short min, max;
}
@ -258,8 +256,8 @@ macro bool Size.@is_grow(s) => (s.min == 0 && s.max == 0);
macro bool Size.@is_exact(s) => (s.min == s.max && s.min != 0);
macro bool Size.@is_fit(s) => (s.min != s.max);
macro Size Size.add(a, Size b) @operator_s(+) => {.min = a.min.add_no_of(b.min), .max = a.max.add_no_of(b.max)};
macro Size Size.sub(a, Size b) @operator_s(-) => {.min = a.min.add_no_of(-b.min), .max = a.max.add_no_of(-b.max)};
macro Size Size.add(a, Size b) @operator_s(+) => {.min = a.min.sat_add(b.min), .max = a.max.sat_add(b.max)};
macro Size Size.sub(a, Size b) @operator_s(-) => {.min = a.min.sat_sub(b.min), .max = a.max.sat_sub(b.max)};
macro Size Size.combine(a, Size b) => {.min = max(a.min, b.min), .max = min(a.max, b.max)};
macro Size Size.comb_max(a, Size b) => {.min = max(a.min, b.min), .max = max(a.max, b.max)};

View file

@ -15,31 +15,34 @@ struct ElemDiv {
bool on;
float value;
}
ResizeAnchor resize_anchor;
ResizeAnchor resize_now;
ResizeDirection resized; // the element has been manually resized
Point resize_size;
}
macro Ctx.@center(&ctx, LayoutDirection dir = ROW, ...; @body())
{
ctx.@div(@grow(), @grow(), dir, CENTER) {
return ctx.@div(@grow(), @grow(), dir, CENTER) {
@body();
}!;
}
macro Ctx.@row(&ctx, Anchor anchor = TOP_LEFT, ...; @body())
{
ctx.@div(@fit(), @fit(), ROW, anchor: anchor) {
return ctx.@div(@fit(), @fit(), ROW, anchor: anchor) {
@body();
}!;
}
macro Ctx.@column(&ctx, Anchor anchor = TOP_LEFT, ...; @body())
{
ctx.@div(@fit(), @fit(), COLUMN, anchor: anchor) {
return ctx.@div(@fit(), @fit(), COLUMN, anchor: anchor) {
@body();
}!;
}
// useful macro to start and end a div, capturing the trailing block
macro Ctx.@div(&ctx,
Size width = @grow, Size height = @grow,
@ -47,25 +50,26 @@ macro Ctx.@div(&ctx,
Anchor anchor = TOP_LEFT,
bool absolute = false, Point off = {},
bool scroll_x = false, bool scroll_y = false,
ResizeAnchor resize = {},
...;
@body()
)
{
ctx.div_begin(width, height, dir, anchor, absolute, off, scroll_x, scroll_y, $vasplat)!;
ctx.div_begin(width, height, dir, anchor, absolute, off, scroll_x, scroll_y, resize, $vasplat)!;
@body();
return ctx.div_end()!;
}
macro Ctx.div_begin(&ctx,
Size width = @grow(), Size height = @grow(),
LayoutDirection dir = ROW,
Anchor anchor = TOP_LEFT,
bool absolute = false, Point off = {},
bool scroll_x = false, bool scroll_y = false,
LayoutDirection dir, Anchor anchor,
bool absolute, Point off,
bool scroll_x, bool scroll_y,
ResizeAnchor resize,
...
)
{
return ctx.div_begin_id(@compute_id($vasplat), width, height, dir, anchor, absolute, off, scroll_x, scroll_y);
return ctx.div_begin_id(@compute_id($vasplat), width, height, dir, anchor, absolute, off, scroll_x, scroll_y, resize);
}
fn void? Ctx.div_begin_id(&ctx,
@ -74,7 +78,8 @@ fn void? Ctx.div_begin_id(&ctx,
LayoutDirection dir,
Anchor anchor,
bool absolute, Point off,
bool scroll_x, bool scroll_y
bool scroll_x, bool scroll_y,
ResizeAnchor resize
)
{
id = ctx.gen_id(id)!;
@ -87,7 +92,21 @@ fn void? Ctx.div_begin_id(&ctx,
elem.div.scroll_x.enabled = scroll_x;
elem.div.scroll_y.enabled = scroll_y;
elem.div.resize_anchor = resize;
// check if the div is resizeable
bool resized = elem.div.resized && (resize != (ResizeAnchor){});
if (resize != (ResizeAnchor){}) {
// if the element was not resized yet then the size is as-specified
// if the element was resized the size is the same as the last frame
if (elem.div.resized.x == true) {
width = @exact(elem.div.resize_size.x);
}
if (elem.div.resized.y == true) {
height = @exact(elem.div.resize_size.y);
}
}
// update layout with correct info
elem.layout = {
.w = width,
@ -105,8 +124,8 @@ fn void? Ctx.div_begin_id(&ctx,
ctx.push_rect(elem.bounds.pad(style.margin), elem.z_index, style)!;
// update the ctx scissor, it HAS to be after drawing the background
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset);
ctx.push_scissor(elem.bounds.pad(elem.layout.content_offset), elem.z_index)!;
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset).max({0,0,0,0});
ctx.push_scissor(ctx.div_scissor, elem.z_index)!;
//elem.events = ctx.get_elem_events(elem);
@ -155,16 +174,73 @@ fn Id? Ctx.div_end(&ctx)
elem.div.scroll_x.value = math::clamp(elem.div.scroll_x.value, 0.0f, 1.0f);
}
}
ctx.scrollbar(vsid_raw, &elem.div.scroll_x.value, max((float)bc.x / cbc.x, (float)0.15), false)!;
ctx.scrollbar(hsid_raw, &elem.div.scroll_x.value, max((float)bc.x / cbc.x, (float)0.15), false)!;
elem.layout.scroll_offset.x = (short)(elem.div.scroll_x.value*(float)(elem.children_bounds.w-elem.bounds.w));
} else {
elem.div.scroll_x.value = 0;
}
// check resize action
/*
* top border
* +-------------------------------------------+
* | +---------------------------------------+ |
* | | | |
* | | | |
* | | | |
* | | | |
* left | | | | right
* border | | | | border
* | | | |
* | | | |
* | | | |
* | | | |
* | +---------------------------------------+ |
* +-------------------------------------------+
* bottom border
*/
if (elem.div.resize_anchor != (ResizeAnchor){}) {
Rect b = elem.bounds;
Rect s = style.border + style.margin + style.padding;
Rect b_l = {.x = b.x, .y = b.y, .w = s.x, .h = b.h};
Rect b_r = {.x = b.x+b.w-s.w, .y = b.y, .w = s.w, .h = b.h};
Rect b_t = {.x = b.x, .y = b.y, .w = b.w, .h = s.y};
Rect b_b = {.x = b.x, .y = b.y+b.h, .w = b.w, .h = s.h};
Rect content_bounds = elem.bounds.pad(elem.layout.content_offset);
Point m = ctx.input.mouse.pos;
if (elem.events.mouse_hover && elem.events.mouse_press) {
if (elem.div.resize_anchor.right && m.in_rect(b_r)) elem.div.resize_now.right = true;
if (elem.div.resize_anchor.left && m.in_rect(b_l)) elem.div.resize_now.left = true;
if (elem.div.resize_anchor.top && m.in_rect(b_t)) elem.div.resize_now.top = true;
if (elem.div.resize_anchor.bottom && m.in_rect(b_b)) elem.div.resize_now.bottom = true;
} else if (ctx.is_mouse_released(BTN_ANY)) {
elem.div.resize_now = {};
}
if (elem.div.resize_now.right == true) {
elem.div.resized.x = true;
elem.div.resize_size.x = content_bounds.w - (elem.bounds.x + elem.bounds.w - m.x);
}
if (elem.div.resize_now.bottom == true) {
elem.div.resized.y = true;
elem.div.resize_size.y = content_bounds.h - (elem.bounds.y + elem.bounds.h - m.y);
}
if (elem.div.resize_now.left == true) {
elem.div.resized.x = true;
elem.div.resize_size.x = content_bounds.w - (elem.bounds.x - m.x);
}
if (elem.div.resize_now.top == true) {
elem.div.resized.y = true;
elem.div.resize_size.y = content_bounds.h - (elem.bounds.y - m.y);
}
}
// the active_div returns to the parent of the current one
ctx.active_div = ctx.tree.parentof(ctx.active_div)!;
Elem* parent = ctx.get_parent()!;
ctx.div_scissor = parent.bounds.pad(parent.layout.content_offset);
ctx.div_scissor = parent.bounds.pad(parent.layout.content_offset).max({0,0,0,0});
ctx.reset_scissor(elem.z_index)!;
update_parent_size(elem, parent);
@ -172,6 +248,21 @@ fn Id? Ctx.div_end(&ctx)
return elem.id;
}
<* @param[&inout] state *>
macro Ctx.@popup(&ctx, bool* state,
Point pos,
Size width, Size height,
LayoutDirection dir = ROW, Anchor anchor = TOP_LEFT,
bool scroll_x = false, bool scroll_y = false,
...; @body())
{
if (*state) {
*state = ctx.popup_begin(pos, width, height, dir, anchor, scroll_x, scroll_y)!;
@body();
ctx.div_end()!;
}
}
macro bool? Ctx.popup_begin(&ctx, Point pos,
Size width, Size height,
LayoutDirection dir = ROW, Anchor anchor = TOP_LEFT,
@ -221,8 +312,8 @@ fn bool? Ctx.popup_begin_id(&ctx,
ctx.push_rect(elem.bounds.pad(style.margin), elem.z_index, style)!;
// update the ctx scissor, it HAS to be after drawing the background
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset);
ctx.push_scissor(elem.bounds.pad(elem.layout.content_offset), elem.z_index)!;
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset).max({0,0,0,0});
ctx.push_scissor(ctx.div_scissor, elem.z_index)!;
//elem.events = ctx.get_elem_events(elem);
@ -232,6 +323,5 @@ fn bool? Ctx.popup_begin_id(&ctx,
}
// TODO: check active
// TODO: check resizeable
return true;
}

View file

@ -1,16 +1,56 @@
module ugui;
macro Ctx.separator(&ctx, int width, int height, ...)
macro Ctx.separator(&ctx, Size width, Size height, ...)
=> ctx.separator_id(@compute_id($vasplat), width, height);
fn void? Ctx.separator_id(&ctx, Id id, int width, int height)
fn void? Ctx.separator_id(&ctx, Id id, Size width, Size height)
{
id = ctx.gen_id(id)!;
Elem* parent, elem;
ctx.get_elem(id, ETYPE_NONE)!.unpack(&elem, &parent);
elem.layout.w = @exact((short)width);
elem.layout.h = @exact((short)height);
elem.layout.w = width;
elem.layout.h = height;
update_parent_size(elem, parent);
}
macro Ctx.hor_line(&ctx, ...)
=> ctx.hor_line_id(@compute_id($vasplat));
fn void? Ctx.hor_line_id(&ctx, Id id)
{
id = ctx.gen_id(id)!;
Elem* parent, elem;
ctx.get_elem(id, ETYPE_NONE)!.unpack(&elem, &parent);
Style* style = ctx.styles.get_style(@str_hash("separator"));
elem.layout.w = @grow();
elem.layout.h = @exact(style.size);
elem.layout.content_offset = style.margin + style.border + style.padding;
update_parent_size(elem, parent);
Rect r = elem.bounds.pad(elem.layout.content_offset);
ctx.push_rect(r, elem.z_index, style)!;
}
macro Ctx.ver_line(&ctx, ...)
=> ctx.ver_line_id(@compute_id($vasplat));
fn void? Ctx.ver_line_id(&ctx, Id id)
{
id = ctx.gen_id(id)!;
Elem* parent, elem;
ctx.get_elem(id, ETYPE_NONE)!.unpack(&elem, &parent);
Style* style = ctx.styles.get_style(@str_hash("separator"));
elem.layout.w = @exact(style.size);
elem.layout.h = @grow();
elem.layout.content_offset = style.margin + style.border + style.padding;
update_parent_size(elem, parent);
Rect r = elem.bounds.pad(elem.layout.content_offset);
ctx.push_rect(r, elem.z_index, style)!;
}

View file

@ -140,23 +140,22 @@ fn void? Ctx.scrollbar(&ctx, Id id, float *value, float handle_percent, bool ver
Style* style = ctx.styles.get_style(@str_hash("scrollbar"));
Rect pb = parent.bounds.pad(parent.layout.content_offset);
elem.bounds.x = vertical ? pb.bottom_right().x - style.size: pb.x;
elem.bounds.y = vertical ? pb.y : pb.bottom_right().y - style.size;
if (vertical) {
elem.layout.w = @exact(style.size);
elem.layout.h = @grow();
elem.bounds.x -= style.margin.x + style.margin.w + style.border.x + style.border.w;
elem.layout.origin.x = pb.w - style.size;
elem.layout.origin.y = 0;
} else {
elem.layout.w = @grow();
elem.layout.h = @exact(style.size);
elem.bounds.y -= style.margin.y + style.margin.h + style.border.y + style.border.h;
elem.layout.origin.x = 0;
elem.layout.origin.y = pb.h - style.size;
}
elem.layout.content_offset = style.margin + style.border + style.padding;
elem.layout.absolute = true;
update_parent_size(elem, parent);
Rect content_bounds = elem.bounds.pad(elem.layout.content_offset);
//elem.events = ctx.get_elem_events(elem);
short o = vertical ? content_bounds.y : content_bounds.x;
short m = vertical ? ctx.input.mouse.pos.y : ctx.input.mouse.pos.x;

View file

@ -21,8 +21,8 @@ fn void? Ctx.sprite_id(&ctx, Id id, String name, short size = 0)
short height = sprite.h;
if (size > 0) {
if (sprite.w >= sprite.h) {
width = size;
height = (short)(size * (float)height/width);
width = size;
} else {
width = (short)(size * (float)width/height);
height = size;