diff options
| author | iamcheeseman <[email protected]> | 2026-05-22 11:49:49 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-22 11:49:49 -0400 |
| commit | eecfe87f8d23b358c31d6b8289f96ba61ad6de01 (patch) | |
| tree | a0cd932e9114c16466eff4ade1599b84e0c37e5e | |
| parent | 8d23224b9d3cb6bac2b19b44141283c1647bd975 (diff) | |
tyui sliders
| -rw-r--r-- | dc/dc.c | 5 | ||||
| -rw-r--r-- | teensy/teensy_ui.c | 55 | ||||
| -rw-r--r-- | teensy/teensy_ui.h | 10 |
3 files changed, 69 insertions, 1 deletions
@@ -92,7 +92,10 @@ void tick(void) } if (tyui_begin_window("2nd Window", ty_recti(110, 5, 100, 120), &winid2)) { - tyui_text("Test text"); + static float value = 100; + tyui_slider(0, 100, &value); + tyui_slider_ex(0, 100, &value, false, 0); + tyui_slider_ex(0, 100, &value, true, TYUI_ALIGN_LEFT); tyui_end_window(); } diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index a0bb5e3..c58113c 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -88,6 +88,7 @@ tyui_Style default_style = { .padding = 1, .control_padding = 2, .frame_size = 1, + .grabber_size = 4, }; static @@ -450,6 +451,60 @@ bool tyui_button(const char *text) return clicked; } +void tyui_slider_ex( + float min, + float max, + float *value_ptr, + bool show_value, + tyui_Align value_align +) { + assert(value_ptr); + + const int grabber_size = uictx.style.grabber_size; + + ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2); + ty_Color bg_col = uictx.style.bg_normal; + ty_Color fg_col = uictx.style.fg_normal; + + bool clicked = false; + if (is_hovered(rect)) { + clicked = window()->has_lmb; + if (ty_button_down(TY_BTN_DB_LMB)) { + bg_col = uictx.style.bg_pressed; + fg_col = uictx.style.fg_pressed; + } else { + bg_col = uictx.style.bg_hover; + fg_col = uictx.style.fg_hover; + } + } + + if (clicked) { + ty_Vec2i mp = ty_mouse_pos(); + float x = mp.x - rect.x; + float p = x / (rect.w - 1); + *value_ptr = ty_clamp(p * (max - min) + min, min, max); + } + + float value = *value_ptr; + + draw_frame(rect, bg_col); + if (show_value) + text_cmd(ty_format("%d", (int)round(value)), rect, value_align); + + // Grabber + { + float p = (value - min) / max; + int x = (rect.w - grabber_size) * p + rect.x; + + ty_Recti grabber_rect = ty_recti( + x, rect.y, + grabber_size, rect.h + ); + + draw_frame(grabber_rect, fg_col); + } +} + static void draw_window(Window *win) { diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h index da7691f..8efc7bd 100644 --- a/teensy/teensy_ui.h +++ b/teensy/teensy_ui.h @@ -14,6 +14,8 @@ #define tyui_begin_window(title, rect, id) \ tyui_begin_window_ex(title, rect, id, 0, NULL) #define tyui_text(...) tyui_text_ex(TYUI_ALIGN_LEFT, __VA_ARGS__) +#define tyui_slider(min, max, value_ptr) \ + tyui_slider_ex(min, max, value_ptr, true, TYUI_ALIGN_CENTER) typedef uint8_t tyui_Align; enum { @@ -37,6 +39,7 @@ typedef struct { int padding; int control_padding; int frame_size; + int grabber_size; } tyui_Style; typedef uint8_t tyui_Id; @@ -58,6 +61,13 @@ void tyui_end_window(void); bool tyui_button(const char *text); void tyui_text_ex(tyui_Align align, const char *fmt, ...); +void tyui_slider_ex( + float min, + float max, + float *value_ptr, + bool show_value, + tyui_Align value_align +); void tyui_draw(void); |
