ugui.c3l/src/ugui_div.c3

153 lines
4.7 KiB
Plaintext
Raw Normal View History

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-19 00:29:30 +01:00
const short SCROLLBAR_DIM = 5;
2024-12-17 11:26:59 +01:00
// div element
struct ElemDiv {
2024-12-19 00:29:30 +01:00
Layout layout;
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
}
2024-12-18 14:58:40 +01:00
Rect children_bounds; // current frame children bounds
Rect pcb; // previous frame children bounds
2024-12-17 11:26:59 +01:00
Point origin_r, origin_c;
}
2024-12-26 22:58:24 +01:00
// begin a widget container, or div, the size determines the offset (x,y) width and height.
// if the width or height are zero the width or height are set to the maximum available.
// if the width or height are negative the width or height will be calculated based on the children size
// sort similar to a flexbox, and the minimum size is set by the negative of the width or height
// FIXME: there is a bug if the size.w or size.h == -0
2024-12-17 11:26:59 +01:00
fn void! Ctx.div_begin(&ctx, String label, Rect size, bool scroll_x = false, bool scroll_y = false)
2024-11-07 18:35:20 +01:00
{
Id id = ctx.gen_id(label)!;
2024-11-07 18:35:20 +01:00
2024-12-18 14:58:40 +01:00
Elem* parent = ctx.get_parent()!;
2024-12-19 15:24:39 +01:00
Elem* elem = ctx.get_elem(id)!;
2024-11-07 18:35:20 +01:00
isz div_node = ctx.tree.add(id, ctx.active_div)!;
ctx.active_div = div_node;
2024-12-19 15:24:39 +01:00
bool is_new = elem.flags.is_new;
2024-12-17 11:26:59 +01:00
2024-12-19 15:24:39 +01:00
if (elem.flags.is_new) {
elem.type = ETYPE_DIV;
} else if (elem.type != ETYPE_DIV) {
2024-12-17 11:26:59 +01:00
return UgError.WRONG_ELEMENT_TYPE?;
}
2024-12-19 15:24:39 +01:00
elem.div.scroll_x.enabled = scroll_x;
elem.div.scroll_y.enabled = scroll_y;
2024-11-07 18:35:20 +01:00
2024-12-18 14:58:40 +01:00
// 2. layout the element
2024-12-26 22:58:24 +01:00
Rect wanted_size = {
.x = size.x,
.y = size.y,
.w = size.w < 0 ? max(elem.div.pcb.w, (short)-size.w) : size.w,
.h = size.h < 0 ? max(elem.div.pcb.h, (short)-size.h) : size.h,
};
elem.bounds = ctx.position_element(parent, wanted_size);
2024-12-19 15:24:39 +01:00
elem.div.children_bounds = elem.bounds;
2024-12-18 14:58:40 +01:00
// update the ctx scissor
2024-12-19 15:24:39 +01:00
ctx.div_scissor = elem.bounds;
ctx.push_scissor(elem.bounds)!;
2024-12-18 14:58:40 +01:00
// 4. Fill the div fields
2024-12-19 15:24:39 +01:00
elem.div.origin_c = Point{
.x = elem.bounds.x,
.y = elem.bounds.y,
2024-12-18 14:58:40 +01:00
};
2024-12-19 15:24:39 +01:00
elem.div.origin_r = elem.div.origin_c;
elem.div.layout = parent.div.layout;
2024-11-07 18:35:20 +01:00
// Add the background to the draw stack
2024-12-17 11:26:59 +01:00
bool do_border = parent.div.layout == LAYOUT_FLOATING;
2024-12-19 15:24:39 +01:00
ctx.push_rect(elem.bounds, ctx.style.bgcolor, do_border: do_border)!;
2024-11-07 18:35:20 +01:00
2024-12-19 15:24:39 +01: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
}
fn void! Ctx.div_end(&ctx)
{
// swap the children bounds
Elem* parent = ctx.get_parent()!;
2024-12-19 15:24:39 +01:00
Elem* elem = ctx.get_elem_by_tree_idx(ctx.active_div)!;
elem.div.pcb = elem.div.children_bounds;
2024-12-20 01:45:10 +01:00
// FIXME: this causes all elements inside the div to loose focus since the mouse press happens
// both inside the element and inside the div bounds
//elem.events = ctx.get_elem_events(elem);
2024-12-19 15:24:39 +01:00
Rect cb = elem.div.pcb;
2024-12-18 14:58:40 +01:00
// children bounds bottom-right corner
Point cbc = {
.x = cb.x + cb.w,
.y = cb.y + cb.h,
};
// div bounds bottom-right corner
Point bc = {
2024-12-19 15:24:39 +01:00
.x = elem.bounds.x + elem.bounds.w,
.y = elem.bounds.y + elem.bounds.h,
2024-12-18 14:58:40 +01:00
};
// set the scrollbar flag, is used in layout
// 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
2024-12-20 02:17:41 +01:00
Id hsid = ctx.gen_id("div_scrollbar_horizontal")!;
Id vsid = ctx.gen_id("div_scrollbar_vertical")!;
2024-12-25 12:30:35 +01:00
short wdim = elem.div.scroll_y.on ? (ctx.focus_id == vsid || ctx.is_hovered(ctx.find_elem(vsid)) ? SCROLLBAR_DIM*3 : SCROLLBAR_DIM) : 0;
short hdim = elem.div.scroll_x.on ? (ctx.focus_id == hsid || ctx.is_hovered(ctx.find_elem(hsid)) ? SCROLLBAR_DIM*3 : SCROLLBAR_DIM) : 0;
2024-12-18 14:58:40 +01:00
2024-12-19 15:24:39 +01:00
if (elem.div.scroll_y.on) {
2024-12-25 12:30:35 +01:00
if (ctx.input.events.mouse_scroll && ctx.hover_id == elem.id) {
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);
}
2024-12-18 14:58:40 +01:00
Rect vslider = {
2024-12-19 15:24:39 +01:00
.x = elem.bounds.x + elem.bounds.w - wdim,
.y = elem.bounds.y,
2024-12-19 00:29:30 +01:00
.w = wdim,
2024-12-19 15:24:39 +01:00
.h = elem.bounds.h - hdim,
2024-11-17 21:36:46 +01:00
};
2024-12-19 15:24:39 +01:00
Layout prev_l = elem.div.layout;
elem.div.layout = LAYOUT_ABSOLUTE;
ctx.slider_ver("div_scrollbar_vertical", vslider, &elem.div.scroll_y.value, max((float)bc.y / cbc.y, (float)0.15))!;
elem.div.layout = prev_l;
2024-11-17 21:36:46 +01: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) {
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-18 14:58:40 +01:00
Rect hslider = {
2024-12-19 15:24:39 +01:00
.x = elem.bounds.x,
.y = elem.bounds.y + elem.bounds.h - hdim,
.w = elem.bounds.w - wdim,
2024-12-19 00:29:30 +01:00
.h = hdim,
2024-12-18 14:58:40 +01:00
};
2024-12-19 15:24:39 +01:00
Layout prev_l = elem.div.layout;
elem.div.layout = LAYOUT_ABSOLUTE;
ctx.slider_hor("div_scrollbar_horizontal", hslider, &elem.div.scroll_x.value, max((float)bc.x / cbc.x, (float)0.15))!;
elem.div.layout = prev_l;
2024-12-18 14:58:40 +01:00
}
2024-11-07 18:35:20 +01:00
// the active_div returns to the parent of the current one
ctx.active_div = ctx.tree.parentof(ctx.active_div)!;
}