better checkboxes

This commit is contained in:
Alessandro Mauri 2025-07-01 15:28:08 +02:00
parent 586e81935c
commit 1de4fd78b8
4 changed files with 15 additions and 13 deletions

View File

@ -145,8 +145,6 @@ fn void? Ctx.checkbox_id(&ctx, Id id, String description, Point off, bool* activ
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// FIXME: for now checkboxes 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) {
@ -163,21 +161,19 @@ fn void? Ctx.checkbox_id(&ctx, Id id, String description, Point off, bool* activ
elem.events = ctx.get_elem_events(elem);
if (elem.events.mouse_hover && elem.events.mouse_release) *active = !(*active);
Color col;
Color col = ctx.style.bgcolor;
if (tick_sprite != {}) {
col = ctx.style.bgcolor;
ctx.push_rect(elem.bounds, col, parent.div.z_index, do_border: true, do_radius: true)!;
if (*active) {
ctx.draw_sprite_raw(tick_sprite, elem.bounds)!;
ctx.draw_sprite_raw(tick_sprite, elem.bounds, center: true)!;
}
} else {
if (*active) {
col = 0xff0000ffu.to_rgba();
} else {
col = 0xff00ffffu.to_rgba();
}
// Draw the button
ctx.push_rect(elem.bounds, col, parent.div.z_index, do_border: true, do_radius: true)!;
if (*active) {
ushort x = DEFAULT_CHECKBOX_SIZE / 4;
Rect check = elem.bounds.add({x, x, -x*2, -x*2});
ctx.push_rect(check, 0xff0000ffu.to_rgba(), parent.div.z_index, do_radius: true)!;
}
}
}

View File

@ -124,7 +124,6 @@ fn void? Ctx.push_rect(&ctx, Rect rect, Color color, int z_index, bool do_border
ctx.push_cmd(&cmd, z_index)!;
}
// TODO: add texture id
fn void? Ctx.push_sprite(&ctx, Rect bounds, Rect texture, Id texture_id, int z_index, Color hue = 0xffffffffu.to_rgba(), SpriteType type = SPRITE_NORMAL)
{
Cmd cmd = {

View File

@ -145,10 +145,16 @@ fn void? Ctx.draw_sprite_id(&ctx, Id id, String name, Point off)
return ctx.push_sprite(elem.bounds, uv, tex_id, parent.div.z_index)!;
}
fn void? Ctx.draw_sprite_raw(&ctx, String name, Rect bounds)
fn void? Ctx.draw_sprite_raw(&ctx, String name, Rect bounds, bool center = false)
{
Elem *parent = ctx.get_parent()!;
Sprite* sprite = ctx.sprite_atlas.get(name)!;
Id tex_id = ctx.sprite_atlas.id;
if (center) {
Point off = {.x = (bounds.w - sprite.w) / 2, .y = (bounds.h - sprite.h) / 2};
bounds = bounds.off(off);
}
return ctx.push_sprite(bounds, sprite.uv(), tex_id, parent.div.z_index, type: sprite.type)!;
}

View File

@ -228,6 +228,7 @@ fn int main(String[] args)
ui.layout_next_row()!!;
static bool check;
ui.checkbox("", {}, &check, "tick")!!;
ui.checkbox("", {}, &check)!!;
ui.toggle("", {}, &toggle)!!;
};
ui.draw_sprite("tux")!!;