diff --git a/Source/Maker/Widgets/slider.c b/Source/Maker/Widgets/slider.c index b305105..744ebf8 100644 --- a/Source/Maker/Widgets/slider.c +++ b/Source/Maker/Widgets/slider.c @@ -1,3 +1,8 @@ +#if defined(STFU_MAIN) && !defined(STFU_SLIDER) +#define STFU_SLIDER + +#include + #include "../config.h" #include "border.c" @@ -8,6 +13,7 @@ struct stfu_slider { float width, height; float r, g, b; float min, max, cur; + bool integeric; }; static void stfu_draw_slider(struct stfu_slider slider) { @@ -36,3 +42,21 @@ static void stfu_draw_slider(struct stfu_slider slider) { sgl_end(); } } + +static struct stfu_slider stfu_press_slider(struct stfu_slider slider, float x, + float y) { + if (x < slider.x || x > slider.x + slider.width) + return slider; + if (y < slider.y || y > slider.y + slider.height) + return slider; + + slider.cur = slider.min + + ((x - slider.x) / (slider.width)) * (slider.max - slider.min); + + if (slider.integeric) + slider.cur = floorf(slider.cur); + + return slider; +} + +#endif diff --git a/Source/Maker/main.c b/Source/Maker/main.c index e6fc64b..0ac4509 100644 --- a/Source/Maker/main.c +++ b/Source/Maker/main.c @@ -32,6 +32,14 @@ static unsigned int crossfade_frames_left = 0; // 1.0 is with A at 440 static float octave = 0.5; +static struct stfu_slider feedback_slider = {.x = 16, + .y = 16, + .width = 120, + .height = 16, + .min = 0, + .max = 1, + .cur = 0.25}; + static void stream(float *buffer, int num_frames, int num_channels) { /* Fade away */ for (size_t i = 0; crossfade_frames_left > 0; @@ -95,6 +103,16 @@ static void cleanup(void) { saudio_shutdown(); } static void event(const sapp_event *e) { switch (e->type) { + case SAPP_EVENTTYPE_MOUSE_DOWN: { + if (e->mouse_button != SAPP_MOUSEBUTTON_LEFT) + break; + + feedback_slider = + stfu_press_slider(feedback_slider, e->mouse_x, e->mouse_y); + synth.feedback_gain = feedback_slider.cur; + + break; + } case SAPP_EVENTTYPE_KEY_DOWN: { if (e->key_repeat) break; @@ -221,13 +239,7 @@ static void frame(void) { sgl_load_default_pipeline(); sgl_load_identity(); sgl_ortho(0, STFU_MAKER_WINDOW_WIDTH, STFU_MAKER_WINDOW_HEIGHT, 0, -1, 1); - stfu_draw_slider((struct stfu_slider){.x = 16, - .y = 16, - .width = 120, - .height = 16, - .min = 10, - .max = 100, - .cur = 55}); + stfu_draw_slider(feedback_slider); // sgl_pop_matrix(); sgl_draw(); sg_end_pass();