making use of the first class types
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Jun 2021 02:46:30 +0000 (21:46 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Jun 2021 02:46:30 +0000 (21:46 -0500)
modules/ui/components/slider.onyx

index f17c1ac024281230cfdf621c55ef4c669836f17b..4c1251a18cbe9913ce7300a49a5a80e1a8252684 100644 (file)
@@ -29,7 +29,22 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, text: str,
             result = true;
 
             // Animate this?
-            adjust_slider_value(value, mouse_state.x - x0, width, min_value, max_value);
+            // adjust_slider_value(value, mouse_state.x - x0, width, min_value, max_value);
+
+            x := mouse_state.x - x0;
+
+            if T == i32 || T == i64 || T == u32 || T == u64 {
+                step_width := width / ~~math.abs(max_value - min_value);
+                percent := (x + step_width / 2) / width;
+                *value = math.lerp(percent, min_value, max_value);
+                *value = math.clamp(*value, min_value, max_value);
+
+            } else {
+                percent := x / width;
+                *value = math.lerp(percent, min_value, max_value);
+                *value = math.clamp(*value, min_value, max_value);
+            }
+
         } else {
             set_active_item(0);
         }
@@ -72,19 +87,3 @@ slider :: (use r: Rectangle, value: ^$T, min_value: T, max_value: T, text: str,
 
     return result;
 }
-
-#private_file
-adjust_slider_value :: #match {
-    (value: ^i32, x: f32, width: f32, min_value: i32, max_value: i32) {
-        step_width := width / ~~math.abs(max_value - min_value);
-        percent := (x + step_width / 2) / width;
-        *value = math.lerp(percent, min_value, max_value);
-        *value = math.clamp(*value, min_value, max_value);
-    }, 
-
-    (value: ^$T, x: f32, width: f32, min_value: T, max_value: T) {
-        percent := x / width;
-        *value = math.lerp(percent, min_value, max_value);
-        *value = math.clamp(*value, min_value, max_value);
-    }, 
-}