From bf3ff59277a6950f45244d9ae11dc032c360c0ee Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Thu, 14 May 2026 15:32:17 -0400 Subject: tyui buttons --- dc/dc.c | 4 +++- teensy/teensy_ui.c | 28 ++++++++++++++++++++++++++++ teensy/teensy_ui.h | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dc/dc.c b/dc/dc.c index 1ecc7d9..fb640bb 100644 --- a/dc/dc.c +++ b/dc/dc.c @@ -119,8 +119,10 @@ void tick(void) y += 8; if (tyui_begin_window("Test Window", ty_recti(5, 5, 100, 100), &winid)) { - for (int i = 0; i < 20; i++) + for (int i = 0; i < 3; i++) tyui_text("example text"); + if (tyui_button("press me")) + toggled = !toggled; tyui_end_window(); } diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index c8369a5..c5bd6af 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -291,3 +291,31 @@ void tyui_text(const char *text) { text_cmd(text, next_pos(TEXT_HEIGHT)); } + +bool tyui_button(const char *text) +{ + const int padding = 2; + int width = ty_font_width(uictx.font, text); + ty_Vec2i pos = next_pos(TEXT_HEIGHT + padding + 1); + ty_Recti rect = ty_recti( + pos.x, pos.y, + width + padding, TEXT_HEIGHT + padding + ); + ty_Color bg_col = uictx.style.bg_normal; + ty_Vec2i mouse_pos = ty_mouse_pos(); + + mouse_pos.x -= uictx.active->rect.x; + mouse_pos.y -= uictx.active->rect.y + TEXT_HEIGHT; + + bool clicked = false; + + if (ty_pointi_in_recti(mouse_pos, rect)) { + bg_col = uictx.style.bg_hover; + clicked = ty_button_pressed(TY_BTN_DB_LMB); + } + + rect_cmd(rect, bg_col); + text_cmd(text, ty_vec2i(pos.x+1, pos.y+1)); + + return clicked; +} diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h index 9a1e33a..aae066f 100644 --- a/teensy/teensy_ui.h +++ b/teensy/teensy_ui.h @@ -36,6 +36,7 @@ bool tyui_begin_window_ex( bool *closed ); void tyui_end_window(void); +bool tyui_button(const char *text); void tyui_text(const char *text); -- cgit v1.3-2-g0d8e