aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-14 15:32:17 -0400
committeriamcheeseman <[email protected]>2026-05-14 15:32:17 -0400
commitbf3ff59277a6950f45244d9ae11dc032c360c0ee (patch)
tree02c31e604d0b18a2653990971e0e82c31acef2db
parent7828f700d0e9205cf3ac3eb7f07db29b99c1408e (diff)
tyui buttons
-rw-r--r--dc/dc.c4
-rw-r--r--teensy/teensy_ui.c28
-rw-r--r--teensy/teensy_ui.h1
3 files changed, 32 insertions, 1 deletions
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);