]> git.lizzy.rs Git - nothing.git/commitdiff
(#788) Finish Slider implementation
authorrexim <reximkut@gmail.com>
Sun, 30 Jun 2019 18:43:39 +0000 (01:43 +0700)
committerrexim <reximkut@gmail.com>
Sun, 30 Jun 2019 18:43:39 +0000 (01:43 +0700)
src/ui/slider.c
src/ui/slider.h

index 9dbd6654474657ab77c23464f2067e8f32668579..3a13c9f3c94ede9d27bdfd84b465c61efafb958a 100644 (file)
@@ -13,23 +13,24 @@ int slider_render(const Slider *slider, Camera *camera, Rect boundary)
     }
 
     const Color core_color = rgba(0.0f, 0.0f, 0.0f, 1.0f);
-
     const float core_height = boundary.h * 0.33f;
     const Rect core = rect(
         boundary.x,
         boundary.y + boundary.h * 0.5f - core_height * 0.5f,
         boundary.w,
         core_height);
-
     if (camera_fill_rect_screen(camera, core, core_color) < 0) {
         return -1;
     }
 
+    const float ratio = slider->value / slider->max_value;
+    const float cursor_width = boundary.w * 0.1f;
     const Rect cursor = rect(
-        boundary.x + boundary.w * 0.5f, boundary.y,
-        boundary.w * 0.05f, boundary.h);
-    const Color cursor_color = rgba(0.60f, 0.60f, 0.60f, 1.0f);
-
+        boundary.x + ratio * (boundary.w - cursor_width),
+        boundary.y,
+        cursor_width,
+        boundary.h);
+    const Color cursor_color = rgba(1.0f, 0.0f, 0.0f, 0.5f);
     if (camera_fill_rect_screen(camera, cursor, cursor_color) < 0) {
         return -1;
     }
@@ -41,6 +42,27 @@ int slider_event(Slider *slider, const SDL_Event *event, Rect boundary)
 {
     trace_assert(slider);
     trace_assert(event);
-    (void) boundary;
+
+    switch (event->type) {
+    case SDL_MOUSEBUTTONDOWN: {
+        Point position = vec((float) event->button.x, (float) event->button.y);
+        if (rect_contains_point(boundary, position)) {
+            slider->drag = 1;
+        }
+    } break;
+
+    case SDL_MOUSEBUTTONUP: {
+        slider->drag = 0;
+    } break;
+
+    case SDL_MOUSEMOTION: {
+        if (slider->drag) {
+            const float x = fminf(fmaxf((float) event->button.x - boundary.x, 0.0f), (float) boundary.w);
+            const float ratio = x / (float) boundary.w;
+            slider->value = ratio * slider->max_value;
+        }
+    } break;
+    }
+
     return 0;
 }
index c009f1f5a159d1065f7cf021e8729f7f6c14badd..3c4264feb10cc931c5807700a892093355c61381 100644 (file)
@@ -2,6 +2,7 @@
 #define SLIDER_H_
 
 typedef struct {
+    int drag;
     float value;
     float max_value;
 } Slider;