stfu/Source/Maker/Widgets/slider.c

63 lines
1.4 KiB
C
Raw Normal View History

2024-06-29 15:33:59 +00:00
#if defined(STFU_MAIN) && !defined(STFU_SLIDER)
#define STFU_SLIDER
#include <stdbool.h>
2024-06-29 15:15:06 +00:00
#include "../config.h"
#include "border.c"
#define STFU_SLIDER_WIDTH 16
struct stfu_slider {
float x, y;
float width, height;
float r, g, b;
float min, max, cur;
2024-06-29 15:33:59 +00:00
bool integeric;
2024-06-29 15:15:06 +00:00
};
static void stfu_draw_slider(struct stfu_slider slider) {
/* Border */
stfu_draw_border((struct stfu_border){
.x = slider.x,
.y = slider.y,
.width = slider.width,
.height = slider.height,
.r = slider.r,
.g = slider.g,
.b = slider.b,
});
/* Slider */
{
float progress = (slider.cur - slider.min) / (slider.max - slider.min);
float x = slider.x + (slider.width - STFU_SLIDER_WIDTH) * progress;
sgl_begin_quads();
sgl_c3f(slider.r, slider.g, slider.b);
sgl_v2f(x, slider.y);
sgl_v2f(x + STFU_SLIDER_WIDTH, slider.y);
sgl_v2f(x + STFU_SLIDER_WIDTH, slider.y + slider.height);
sgl_v2f(x, slider.y + slider.height);
sgl_end();
}
}
2024-06-29 15:33:59 +00:00
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