ugui.c3l/src/ugui_button.c3

86 lines
2.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 {
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
}
2024-12-20 19:50:58 +01:00
fn ElemEvents! Ctx.button_label(&ctx, String label, Rect size = Rect{0,0,short.max,short.max}, bool state = false)
{
Id id = ctx.gen_id(label)!;
Elem *parent = ctx.get_parent()!;
Elem *elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
// this resets the flags
elem.type = ETYPE_BUTTON;
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
Rect text_size = ctx.get_text_bounds(label)!;
2024-12-26 22:58:24 +01:00
Rect btn_size = text_size.add(Rect{0,0,10,10});
2024-12-20 19:50:58 +01:00
// 2. Layout
elem.bounds = ctx.position_element(parent, btn_size, true);
if (elem.bounds.is_null()) { return ElemEvents{}; }
Color col = uint_to_rgba(0x0000ffff);
elem.events = ctx.get_elem_events(elem);
if (state) {
col = uint_to_rgba(0xff0000ff);
} else if (ctx.elem_focus(elem) || elem.events.mouse_hover) {
col = uint_to_rgba(0xff00ffff);
}
// Draw the button
text_size.x = elem.bounds.x;
text_size.y = elem.bounds.y;
Point off = ctx.center_text(text_size, elem.bounds);
text_size.x += off.x;
text_size.y += off.y;
ctx.push_rect(elem.bounds, col, do_border: true, do_radius: true)!;
ctx.push_string(text_size, label)!;
return elem.events;
}