ugui.c3l/src/ugui_button.c3

53 lines
1.2 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 {
Color color;
bool active;
}
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;
elem.button.color = uint_to_rgba(0x0000ffff);
} 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-19 15:24:39 +01:00
elem.events = ctx.get_elem_events(elem);
if (elem.events.mouse_hover) {
2024-12-17 11:26:59 +01:00
if (parent.flags.has_focus) {
2024-12-19 15:24:39 +01:00
elem.flags.has_focus = true;
elem.button.color = uint_to_rgba(0x00ff00ff);
2024-10-31 00:04:49 +01:00
}
2024-12-17 11:26:59 +01:00
} else {
2024-12-19 15:24:39 +01:00
elem.button.color = uint_to_rgba(0x0000ffff);
2024-12-17 11:26:59 +01:00
}
if (state) {
2024-12-19 15:24:39 +01:00
elem.button.color = uint_to_rgba(0xff00ffff);
2024-10-31 00:04:49 +01:00
}
// Draw the button
2024-12-19 15:24:39 +01:00
ctx.push_rect(elem.bounds, elem.button.color, 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
}