ugui.c3l/src/ugui_button.c3

61 lines
1.4 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;
}
const Elem BUTTON_DEFAULTS = {
.type = ETYPE_BUTTON,
.button = {
.color = uint_to_rgba(0x0000ffff),
},
};
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 = label.hash();
2024-10-31 00:04:49 +01:00
Elem *parent = ctx.get_parent()!;
Elem *c_elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
2024-12-17 11:26:59 +01:00
bool needs_layout = c_elem.flags.is_new || parent.flags.updated;
if (c_elem.flags.is_new) {
*c_elem = BUTTON_DEFAULTS;
} else if (c_elem.type != ETYPE_BUTTON) {
return UgError.WRONG_ELEMENT_TYPE?;
}
2024-10-31 00:04:49 +01:00
2024-12-18 14:58:40 +01:00
c_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
if (c_elem.bounds.is_null()) { return ElemEvents{}; }
2024-10-31 00:04:49 +01:00
c_elem.events = ctx.get_elem_events(c_elem);
2024-12-17 11:26:59 +01:00
if (c_elem.events.mouse_hover) {
if (parent.flags.has_focus) {
2024-10-31 00:04:49 +01:00
c_elem.flags.has_focus = true;
2024-12-17 11:26:59 +01:00
c_elem.button.color = uint_to_rgba(0x00ff00ff);
2024-10-31 00:04:49 +01:00
}
2024-12-17 11:26:59 +01:00
} else {
c_elem.button.color = uint_to_rgba(0x0000ffff);
}
if (state) {
c_elem.button.color = uint_to_rgba(0xff00ffff);
2024-10-31 00:04:49 +01:00
}
// Draw the button
2024-12-17 11:26:59 +01:00
ctx.push_rect(c_elem.bounds, c_elem.button.color, do_border: true, do_radius: true)!;
2024-10-31 00:04:49 +01:00
return c_elem.events;
}