2024-11-07 18:35:20 +01:00
|
|
|
module ugui;
|
|
|
|
|
|
2024-11-17 21:36:46 +01:00
|
|
|
import std::io;
|
2024-12-18 14:58:40 +01:00
|
|
|
import std::math;
|
2024-11-07 18:35:20 +01:00
|
|
|
|
2024-12-17 11:26:59 +01:00
|
|
|
// div element
|
|
|
|
|
struct ElemDiv {
|
2024-12-19 00:29:30 +01:00
|
|
|
struct scroll_x {
|
|
|
|
|
bool enabled;
|
|
|
|
|
bool on;
|
|
|
|
|
float value;
|
|
|
|
|
}
|
|
|
|
|
struct scroll_y {
|
|
|
|
|
bool enabled;
|
|
|
|
|
bool on;
|
|
|
|
|
float value;
|
2024-12-17 11:26:59 +01:00
|
|
|
}
|
2025-12-04 19:44:46 +01:00
|
|
|
ResizeAnchor resize_anchor;
|
|
|
|
|
ResizeAnchor resize_now;
|
|
|
|
|
ResizeDirection resized; // the element has been manually resized
|
|
|
|
|
Point resize_size;
|
2024-12-17 11:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 22:16:54 +02:00
|
|
|
|
2025-10-08 22:16:17 +02:00
|
|
|
macro Ctx.@center(&ctx, LayoutDirection dir = ROW, ...; @body())
|
|
|
|
|
{
|
2025-10-26 17:48:42 +01:00
|
|
|
return ctx.@div(@grow(), @grow(), dir, CENTER) {
|
2025-10-08 22:16:17 +02:00
|
|
|
@body();
|
|
|
|
|
}!;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 21:39:00 +02:00
|
|
|
macro Ctx.@row(&ctx, Anchor anchor = TOP_LEFT, ...; @body())
|
|
|
|
|
{
|
2025-10-26 17:48:42 +01:00
|
|
|
return ctx.@div(@fit(), @fit(), ROW, anchor: anchor) {
|
2025-09-30 21:39:00 +02:00
|
|
|
@body();
|
|
|
|
|
}!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro Ctx.@column(&ctx, Anchor anchor = TOP_LEFT, ...; @body())
|
|
|
|
|
{
|
2025-10-26 17:48:42 +01:00
|
|
|
return ctx.@div(@fit(), @fit(), COLUMN, anchor: anchor) {
|
2025-09-30 21:39:00 +02:00
|
|
|
@body();
|
|
|
|
|
}!;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 22:16:54 +02:00
|
|
|
// useful macro to start and end a div, capturing the trailing block
|
2025-09-05 13:10:16 +02:00
|
|
|
macro Ctx.@div(&ctx,
|
|
|
|
|
Size width = @grow, Size height = @grow,
|
|
|
|
|
LayoutDirection dir = ROW,
|
|
|
|
|
Anchor anchor = TOP_LEFT,
|
2025-09-17 22:45:03 +02:00
|
|
|
bool absolute = false, Point off = {},
|
2025-09-05 13:10:16 +02:00
|
|
|
bool scroll_x = false, bool scroll_y = false,
|
2025-12-04 19:44:46 +01:00
|
|
|
ResizeAnchor resize = {},
|
2025-09-12 22:32:47 +02:00
|
|
|
...;
|
2025-09-05 13:10:16 +02:00
|
|
|
@body()
|
|
|
|
|
)
|
2025-08-14 22:16:54 +02:00
|
|
|
{
|
2025-12-04 19:44:46 +01:00
|
|
|
ctx.div_begin(width, height, dir, anchor, absolute, off, scroll_x, scroll_y, resize, $vasplat)!;
|
2025-08-14 22:16:54 +02:00
|
|
|
@body();
|
2025-09-12 22:45:23 +02:00
|
|
|
return ctx.div_end()!;
|
2025-08-14 22:16:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:10:16 +02:00
|
|
|
macro Ctx.div_begin(&ctx,
|
|
|
|
|
Size width = @grow(), Size height = @grow(),
|
2025-12-04 19:44:46 +01:00
|
|
|
LayoutDirection dir, Anchor anchor,
|
|
|
|
|
bool absolute, Point off,
|
|
|
|
|
bool scroll_x, bool scroll_y,
|
|
|
|
|
ResizeAnchor resize,
|
2025-09-05 13:10:16 +02:00
|
|
|
...
|
|
|
|
|
)
|
|
|
|
|
{
|
2025-12-04 19:44:46 +01:00
|
|
|
return ctx.div_begin_id(@compute_id($vasplat), width, height, dir, anchor, absolute, off, scroll_x, scroll_y, resize);
|
2025-09-05 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn void? Ctx.div_begin_id(&ctx,
|
|
|
|
|
Id id,
|
|
|
|
|
Size width, Size height,
|
|
|
|
|
LayoutDirection dir,
|
|
|
|
|
Anchor anchor,
|
2025-09-17 22:45:03 +02:00
|
|
|
bool absolute, Point off,
|
2025-12-04 19:44:46 +01:00
|
|
|
bool scroll_x, bool scroll_y,
|
|
|
|
|
ResizeAnchor resize
|
2025-09-05 13:10:16 +02:00
|
|
|
)
|
2024-11-07 18:35:20 +01:00
|
|
|
{
|
2025-06-30 18:24:50 +02:00
|
|
|
id = ctx.gen_id(id)!;
|
2024-11-07 18:35:20 +01:00
|
|
|
|
2025-10-03 15:19:52 +02:00
|
|
|
Elem* parent, elem;
|
|
|
|
|
ctx.get_elem(id, ETYPE_DIV)!.unpack(&elem, &parent);
|
2025-07-01 16:03:11 +02:00
|
|
|
ctx.active_div = elem.tree_idx;
|
2025-07-06 01:41:57 +02:00
|
|
|
|
2025-09-29 23:51:02 +02:00
|
|
|
Style* style = ctx.styles.get_style(@str_hash("div"));
|
2024-11-07 18:35:20 +01:00
|
|
|
|
2024-12-19 15:24:39 +01:00
|
|
|
elem.div.scroll_x.enabled = scroll_x;
|
|
|
|
|
elem.div.scroll_y.enabled = scroll_y;
|
2025-12-04 19:44:46 +01:00
|
|
|
elem.div.resize_anchor = resize;
|
2025-10-02 23:19:42 +02:00
|
|
|
|
2025-12-04 19:44:46 +01:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:10:16 +02:00
|
|
|
// update layout with correct info
|
|
|
|
|
elem.layout = {
|
|
|
|
|
.w = width,
|
|
|
|
|
.h = height,
|
|
|
|
|
.dir = dir,
|
|
|
|
|
.anchor = anchor,
|
2025-09-05 19:56:59 +02:00
|
|
|
.content_offset = style.margin + style.border + style.padding,
|
2025-09-17 22:45:03 +02:00
|
|
|
.absolute = absolute,
|
2024-12-18 14:58:40 +01:00
|
|
|
};
|
2025-09-17 22:45:03 +02:00
|
|
|
if (absolute) {
|
|
|
|
|
elem.layout.origin.x = off.x;
|
|
|
|
|
elem.layout.origin.y = off.y;
|
|
|
|
|
}
|
2025-09-12 22:32:47 +02:00
|
|
|
|
2025-10-02 23:19:42 +02:00
|
|
|
ctx.push_rect(elem.bounds.pad(style.margin), elem.z_index, style)!;
|
2024-11-07 18:35:20 +01:00
|
|
|
|
2025-09-14 20:32:31 +02:00
|
|
|
// update the ctx scissor, it HAS to be after drawing the background
|
2025-10-28 12:56:51 +01:00
|
|
|
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset).max({0,0,0,0});
|
|
|
|
|
ctx.push_scissor(ctx.div_scissor, elem.z_index)!;
|
2025-09-14 20:32:31 +02:00
|
|
|
|
2025-10-08 22:16:17 +02:00
|
|
|
//elem.events = ctx.get_elem_events(elem);
|
2024-12-18 14:58:40 +01:00
|
|
|
|
2024-11-07 18:35:20 +01:00
|
|
|
// TODO: check active
|
|
|
|
|
// TODO: check resizeable
|
2024-12-18 20:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-12 22:45:23 +02:00
|
|
|
fn Id? Ctx.div_end(&ctx)
|
2024-12-18 20:04:23 +01:00
|
|
|
{
|
2025-07-01 15:33:22 +02:00
|
|
|
Elem* elem = ctx.get_active_div()!;
|
2025-09-29 23:51:02 +02:00
|
|
|
Style* style = ctx.styles.get_style(@str_hash("div"));
|
|
|
|
|
Rect bounds = elem.bounds.pad(style.margin + style.border);
|
2025-02-01 01:01:13 +01:00
|
|
|
|
2024-12-18 14:58:40 +01:00
|
|
|
// set the scrollbar flag, is used in layout
|
2025-09-12 22:32:47 +02:00
|
|
|
Point cbc = elem.children_bounds.bottom_right();
|
|
|
|
|
Point bc = elem.bounds.bottom_right();
|
2024-12-18 14:58:40 +01:00
|
|
|
// horizontal overflow
|
2024-12-19 15:24:39 +01:00
|
|
|
elem.div.scroll_x.on = cbc.x > bc.x && elem.div.scroll_x.enabled;
|
2024-12-19 00:29:30 +01:00
|
|
|
// vertical overflow
|
2024-12-19 15:24:39 +01:00
|
|
|
elem.div.scroll_y.on = cbc.y > bc.y && elem.div.scroll_y.enabled;
|
2024-12-19 00:29:30 +01:00
|
|
|
|
2025-07-06 01:41:57 +02:00
|
|
|
Id hsid_raw = @str_hash("div_scrollbar_horizontal");
|
|
|
|
|
Id vsid_raw = @str_hash("div_scrollbar_vertical");
|
2025-09-29 23:51:02 +02:00
|
|
|
Id hsid_real = ctx.gen_id(hsid_raw)!;
|
|
|
|
|
Id vsid_real = ctx.gen_id(vsid_raw)!;
|
2024-12-18 14:58:40 +01:00
|
|
|
|
2024-12-19 15:24:39 +01:00
|
|
|
if (elem.div.scroll_y.on) {
|
2025-09-29 23:51:02 +02:00
|
|
|
if (ctx.input.events.mouse_scroll && ctx.hover_id == elem.id && !(ctx.get_mod() & KMOD_SHIFT)) {
|
2024-12-25 12:30:35 +01:00
|
|
|
elem.div.scroll_y.value += ctx.input.mouse.scroll.y * 0.07f;
|
|
|
|
|
elem.div.scroll_y.value = math::clamp(elem.div.scroll_y.value, 0.0f, 1.0f);
|
|
|
|
|
}
|
2025-09-29 23:51:02 +02:00
|
|
|
ctx.scrollbar(vsid_raw, &elem.div.scroll_y.value, max((float)bc.y / cbc.y, (float)0.15))!;
|
|
|
|
|
elem.layout.scroll_offset.y = (short)(elem.div.scroll_y.value*(float)(elem.children_bounds.h-elem.bounds.h));
|
|
|
|
|
} else {
|
|
|
|
|
elem.div.scroll_y.value = 0;
|
2024-11-17 21:36:46 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 23:51:02 +02:00
|
|
|
|
2024-12-19 15:24:39 +01:00
|
|
|
if (elem.div.scroll_x.on) {
|
2024-12-25 12:30:35 +01:00
|
|
|
if (ctx.input.events.mouse_scroll && ctx.hover_id == elem.id) {
|
2025-09-29 23:51:02 +02:00
|
|
|
if (ctx.get_mod() & KMOD_SHIFT) { // horizontal scroll with shift
|
|
|
|
|
elem.div.scroll_x.value += ctx.input.mouse.scroll.y * 0.07f;
|
|
|
|
|
elem.div.scroll_x.value = math::clamp(elem.div.scroll_x.value, 0.0f, 1.0f);
|
|
|
|
|
} else {
|
|
|
|
|
elem.div.scroll_x.value += ctx.input.mouse.scroll.x * 0.07f;
|
|
|
|
|
elem.div.scroll_x.value = math::clamp(elem.div.scroll_x.value, 0.0f, 1.0f);
|
|
|
|
|
}
|
2024-12-25 12:30:35 +01:00
|
|
|
}
|
2025-10-28 22:00:29 +01:00
|
|
|
ctx.scrollbar(hsid_raw, &elem.div.scroll_x.value, max((float)bc.x / cbc.x, (float)0.15), false)!;
|
2025-09-29 23:51:02 +02:00
|
|
|
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;
|
2024-12-18 14:58:40 +01:00
|
|
|
}
|
2024-11-07 18:35:20 +01:00
|
|
|
|
2025-12-04 19:44:46 +01:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 18:35:20 +01:00
|
|
|
// the active_div returns to the parent of the current one
|
2025-10-04 19:58:34 +02:00
|
|
|
ctx.active_div = ctx.tree.parentof(ctx.active_div)!;
|
2025-07-14 12:57:29 +02:00
|
|
|
Elem* parent = ctx.get_parent()!;
|
2025-10-28 12:56:51 +01:00
|
|
|
ctx.div_scissor = parent.bounds.pad(parent.layout.content_offset).max({0,0,0,0});
|
2025-10-02 23:19:42 +02:00
|
|
|
ctx.reset_scissor(elem.z_index)!;
|
2025-09-05 13:10:16 +02:00
|
|
|
|
|
|
|
|
update_parent_size(elem, parent);
|
2025-09-12 22:45:23 +02:00
|
|
|
|
|
|
|
|
return elem.id;
|
2024-11-07 18:35:20 +01:00
|
|
|
}
|
2025-10-02 23:19:42 +02:00
|
|
|
|
2025-10-27 11:02:11 +01:00
|
|
|
<* @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()!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 23:19:42 +02:00
|
|
|
macro bool? Ctx.popup_begin(&ctx, Point pos,
|
|
|
|
|
Size width, Size height,
|
|
|
|
|
LayoutDirection dir = ROW, Anchor anchor = TOP_LEFT,
|
|
|
|
|
bool scroll_x = false, bool scroll_y = false,
|
|
|
|
|
...
|
|
|
|
|
)
|
|
|
|
|
=> ctx.popup_begin_id(@compute_id($vasplat), pos, width, height, dir, anchor, scroll_x, scroll_y);
|
|
|
|
|
fn bool? Ctx.popup_begin_id(&ctx,
|
|
|
|
|
Id id,
|
|
|
|
|
Point pos,
|
|
|
|
|
Size width, Size height,
|
|
|
|
|
LayoutDirection dir, Anchor anchor,
|
|
|
|
|
bool scroll_x, bool scroll_y
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
id = ctx.gen_id(id)!;
|
|
|
|
|
|
2025-10-03 15:19:52 +02:00
|
|
|
Elem* parent, elem;
|
|
|
|
|
ctx.get_elem(id, ETYPE_DIV)!.unpack(&elem, &parent);
|
2025-10-02 23:19:42 +02:00
|
|
|
ctx.active_div = elem.tree_idx;
|
|
|
|
|
|
|
|
|
|
Style* style = ctx.styles.get_style(@str_hash("popup"));
|
|
|
|
|
|
|
|
|
|
elem.div.scroll_x.enabled = scroll_x;
|
|
|
|
|
elem.div.scroll_y.enabled = scroll_y;
|
|
|
|
|
elem.z_index++;
|
|
|
|
|
|
|
|
|
|
// update layout with correct info
|
|
|
|
|
elem.layout = {
|
|
|
|
|
.w = width,
|
|
|
|
|
.h = height,
|
|
|
|
|
.dir = dir,
|
|
|
|
|
.anchor = anchor,
|
|
|
|
|
.content_offset = style.margin + style.border + style.padding,
|
|
|
|
|
.absolute = true,
|
|
|
|
|
.origin.x = pos.x - parent.bounds.x,
|
|
|
|
|
.origin.y = pos.y - parent.bounds.y,
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-11 22:32:00 +02:00
|
|
|
// if the position is not the one expected then request a frame-skip to not flash appear in the
|
|
|
|
|
// wrong position for one frame
|
|
|
|
|
if (elem.bounds.position() != pos) {
|
|
|
|
|
ctx.skip_frame = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 23:19:42 +02:00
|
|
|
ctx.push_rect(elem.bounds.pad(style.margin), elem.z_index, style)!;
|
|
|
|
|
|
|
|
|
|
// update the ctx scissor, it HAS to be after drawing the background
|
2025-10-28 12:56:51 +01:00
|
|
|
ctx.div_scissor = elem.bounds.pad(elem.layout.content_offset).max({0,0,0,0});
|
|
|
|
|
ctx.push_scissor(ctx.div_scissor, elem.z_index)!;
|
2025-10-02 23:19:42 +02:00
|
|
|
|
2025-10-08 22:16:17 +02:00
|
|
|
//elem.events = ctx.get_elem_events(elem);
|
2025-10-02 23:19:42 +02:00
|
|
|
|
|
|
|
|
// check close condition, mouse release anywhere outside the div bounds
|
2025-10-10 22:31:28 +02:00
|
|
|
if ((ctx.mouse_released() & BTN_ANY) && ctx.input.mouse.pos.outside(elem.bounds)) {
|
2025-10-02 23:19:42 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: check active
|
|
|
|
|
return true;
|
|
|
|
|
}
|