aboutsummaryrefslogtreecommitdiff
path: root/teensy
diff options
context:
space:
mode:
Diffstat (limited to 'teensy')
-rw-r--r--teensy/teensy_ui.c75
-rw-r--r--teensy/teensy_ui.h1
2 files changed, 71 insertions, 5 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c
index 6ab0198..e8d71eb 100644
--- a/teensy/teensy_ui.c
+++ b/teensy/teensy_ui.c
@@ -7,6 +7,7 @@
#define TITLE_BAR_HEIGHT (8)
#define TEXT_HEIGHT (8)
+#define PADDING (1)
enum {
CMD_RECT,
@@ -109,10 +110,12 @@ ty_Vec2i next_pos(int height)
{
ty_Vec2i next = uictx.active->next_pos;
+ int win_width = uictx.active->rect.w - PADDING * 2;
+
float x_perc = 0;
for (int i = 0; i < uictx.layout_idx; i++)
x_perc += uictx.layout[i];
- next.x = uictx.active->rect.w * x_perc;
+ next.x = win_width * x_perc + PADDING;
uictx.layout_idx++;
if (uictx.layout_idx >= uictx.layout_size) {
@@ -123,6 +126,26 @@ ty_Vec2i next_pos(int height)
return next;
}
+// Must be called before next_pos()
+static
+int get_element_width()
+{
+ float start_x_perc = 0;
+ for (int i = 0; i < uictx.layout_idx; i++)
+ start_x_perc += uictx.layout[i];
+
+ float end_x_perc = 0;
+ if (uictx.layout_idx + 1 >= uictx.layout_size) {
+ end_x_perc = 1;
+ } else {
+ for (int i = 0; i < uictx.layout_idx + 1; i++)
+ end_x_perc += uictx.layout[i];
+ }
+
+ int win_width = uictx.active->rect.w - 2;
+ return win_width * (end_x_perc - start_x_perc);
+}
+
static
ty_Vec2i mouse_delta(void)
{
@@ -250,7 +273,7 @@ bool tyui_begin_window_ex(
Window *win = &uictx.windows[*id - 1];
uictx.active = win;
- win->next_pos = ty_vec2i(2, 1);
+ win->next_pos = ty_vec2i(10, PADDING);
if (win->flags & TYUI_WIN_INVISIBLE)
goto done;
@@ -330,12 +353,11 @@ void tyui_text(const char *fmt, ...)
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_Vec2i pos = next_pos(TEXT_HEIGHT + PADDING + 1);
ty_Recti rect = ty_recti(
pos.x, pos.y,
- width + padding, TEXT_HEIGHT + padding
+ width + PADDING, TEXT_HEIGHT + PADDING
);
ty_Color bg_col = uictx.style.bg_normal;
ty_Vec2i mouse_pos = ty_mouse_pos();
@@ -355,3 +377,46 @@ bool tyui_button(const char *text)
return clicked;
}
+
+void tyui_slider(float min, float max, float step, float *value)
+{
+ assert(value);
+
+ ty_Vec2i mouse_pos = ty_mouse_pos();
+ mouse_pos.x -= uictx.active->rect.x;
+ mouse_pos.y -= uictx.active->rect.y + TEXT_HEIGHT;
+
+ int width = get_element_width();
+ ty_Vec2i pos = next_pos(TEXT_HEIGHT + PADDING + 1);
+
+ float p = (*value - min) / (max - min);
+
+ ty_Recti bar_rect = ty_recti(
+ pos.x, pos.y,
+ width, TEXT_HEIGHT
+ );
+
+ rect_cmd(bar_rect, uictx.style.bg_normal);
+ bar_rect.x += 1;
+ bar_rect.y += 1;
+ bar_rect.w -= 2;
+ bar_rect.h -= 2;
+ rect_cmd(bar_rect, uictx.style.win_bg);
+
+ bool hovered = ty_pointi_in_recti(mouse_pos, bar_rect);
+
+ int grabber_x = width * p + pos.x;
+ ty_Recti grabber_rect = ty_recti(
+ grabber_x, pos.y + 1,
+ 6, TEXT_HEIGHT - 2
+ );
+
+ ty_Color color = hovered ? uictx.style.bg_hover : uictx.style.bg_normal;
+
+ rect_cmd(grabber_rect, color);
+
+ if (hovered && ty_button_down(TY_BTN_DB_LMB)) {
+ float new_val = (float)(mouse_pos.x - bar_rect.x) / bar_rect.w * (max - min) + min;
+ *value = new_val;
+ }
+}
diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h
index 0a1837a..ce35ec1 100644
--- a/teensy/teensy_ui.h
+++ b/teensy/teensy_ui.h
@@ -39,6 +39,7 @@ bool tyui_begin_window_ex(
);
void tyui_end_window(void);
bool tyui_button(const char *text);
+void tyui_slider(float min, float max, float step, float *value);
void tyui_text(const char *fmt, ...);