From 7c6f7d31d2e8101c69b62e82901001457a16a148 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Fri, 7 Feb 2025 23:46:21 +0100 Subject: [PATCH] quick and dirty checkbox --- TODO | 2 +- src/main.c3 | 1 + src/ugui_button.c3 | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 0caf07d..9b412a2 100644 --- a/TODO +++ b/TODO @@ -77,5 +77,5 @@ _ border radius [x] Button with label [ ] Text Input box [ ] Icon Buttons -[ ] Switch +[x] Switch [x] Checkbox diff --git a/src/main.c3 b/src/main.c3 index 7382ebc..7b710fd 100644 --- a/src/main.c3 +++ b/src/main.c3 @@ -236,6 +236,7 @@ fn int main(String[] args) ui.layout_next_row()!!; static bool check; ui.checkbox("check1", "", Point{}, &check, "tick")!!; + ui.toggle("toggle1", "", Point{}, &toggle)!!; /* ui.layout_set_column()!!; diff --git a/src/ugui_button.c3 b/src/ugui_button.c3 index c7089f4..cd76206 100644 --- a/src/ugui_button.c3 +++ b/src/ugui_button.c3 @@ -132,3 +132,45 @@ fn void! Ctx.checkbox(&ctx, String label, String description, Point off, bool* s ctx.push_rect(elem.bounds, col, do_border: true, do_radius: true)!; } } + +// FIXME: this should be inside the style +const short DEFAULT_SWITCH_SIZE = 16; +fn void! Ctx.toggle(&ctx, String label, String description, Point off, bool* state) +{ + 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)!; + + // FIXME: for now switches and buttons have no members so the element types + // can be the same + if (elem.flags.is_new) { + elem.type = ETYPE_BUTTON; + } else if (elem.type != ETYPE_BUTTON) { + return UgError.WRONG_ELEMENT_TYPE?; + } + + Rect size = {off.x, off.y, DEFAULT_SWITCH_SIZE*2, DEFAULT_SWITCH_SIZE}; + elem.bounds = ctx.position_element(parent, size, true); + + // if the bounds are null the element is outside the div view, + // no interaction should occur so just return + if (elem.bounds.is_null()) return; + + elem.events = ctx.get_elem_events(elem); + if (elem.events.mouse_hover && elem.events.mouse_release) *state = !(*state); + + Color col; + if (*state) { + col = 0xff0000ffu.to_rgba(); + } else { + col = 0xff00ffffu.to_rgba(); + } + // Draw the button + // FIXME: THIS IS SHIT + ctx.push_rect(elem.bounds, ctx.style.bgcolor, do_border: true, do_radius: true)!; + Rect t = elem.bounds.add(Rect{*state ? (DEFAULT_SWITCH_SIZE+3) : +3, +3, -DEFAULT_SWITCH_SIZE-6, -6}); + ctx.push_rect(t, col, do_border: false, do_radius: true)!; +}