ugui.c3l/src/ugui_button.c3

45 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-10-31 00:04:49 +01:00
module ugui;
2024-11-17 21:36:46 +01:00
import std::io;
2024-12-17 11:26:59 +01:00
// button element
struct ElemButton {
2024-12-20 01:45:10 +01:00
int filler;
2024-12-17 11:26:59 +01:00
}
2024-10-31 00:04:49 +01:00
// draw a button, return the events on that button
2024-12-17 11:26:59 +01:00
fn ElemEvents! Ctx.button(&ctx, String label, Rect size, bool state = false)
2024-10-31 00:04:49 +01:00
{
Id id = ctx.gen_id(label)!;
2024-10-31 00:04:49 +01:00
Elem *parent = ctx.get_parent()!;
2024-12-19 15:24:39 +01:00
Elem *elem = ctx.get_elem(id)!;
2024-10-31 00:04:49 +01:00
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
2024-12-19 15:24:39 +01:00
if (elem.flags.is_new) {
elem.type = ETYPE_BUTTON;
} else if (elem.type != ETYPE_BUTTON) {
2024-12-17 11:26:59 +01:00
return UgError.WRONG_ELEMENT_TYPE?;
}
2024-10-31 00:04:49 +01:00
2024-12-19 15:24:39 +01:00
elem.bounds = ctx.position_element(parent, size, true);
2024-10-31 00:04:49 +01:00
2024-12-17 11:26:59 +01:00
// if the bounds are null the element is outside the div view,
// no interaction should occur so just return
2024-12-19 15:24:39 +01:00
if (elem.bounds.is_null()) { return ElemEvents{}; }
2024-12-17 11:26:59 +01:00
2024-12-20 01:45:10 +01:00
Color col = uint_to_rgba(0x0000ffff);
2024-12-19 15:24:39 +01:00
elem.events = ctx.get_elem_events(elem);
2024-12-17 11:26:59 +01:00
if (state) {
2024-12-20 01:45:10 +01:00
col = uint_to_rgba(0xff0000ff);
} else if (ctx.elem_focus(elem) || elem.events.mouse_hover) {
col = uint_to_rgba(0xff00ffff);
2024-10-31 00:04:49 +01:00
}
// Draw the button
2024-12-20 01:45:10 +01:00
ctx.push_rect(elem.bounds, col, do_border: true, do_radius: true)!;
2024-10-31 00:04:49 +01:00
2024-12-19 15:24:39 +01:00
return elem.events;
2024-10-31 00:04:49 +01:00
}