From 99f59690da7b867c89319f3d48e97bbfb58502fc Mon Sep 17 00:00:00 2001 From: Varphone Wong Date: Thu, 7 Mar 2024 14:08:51 +0800 Subject: [PATCH] `egui`: Add `easing` option to Slider --- crates/egui/src/widgets/slider.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/widgets/slider.rs b/crates/egui/src/widgets/slider.rs index 590282074..46ecfbaec 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -117,6 +117,7 @@ pub struct Slider<'a> { custom_parser: Option>, trailing_fill: Option, handle_shape: Option, + easing: Option, } impl<'a> Slider<'a> { @@ -167,6 +168,7 @@ impl<'a> Slider<'a> { custom_parser: None, trailing_fill: None, handle_shape: None, + easing: None, } } @@ -397,6 +399,13 @@ impl<'a> Slider<'a> { self } + /// Set the easing of the slider. + #[inline] + pub fn easing(mut self) -> Self { + self.easing = Some(true); + self + } + /// Set custom formatter defining how numbers are converted into text. /// /// A custom formatter takes a `f64` for the numeric value and a `RangeInclusive` representing @@ -634,12 +643,18 @@ impl<'a> Slider<'a> { /// For instance, `position` is the mouse position and `position_range` is the physical location of the slider on the screen. fn value_from_position(&self, position: f32, position_range: Rangef) -> f64 { let normalized = remap_clamp(position, position_range, 0.0..=1.0) as f64; - value_from_normalized(normalized, self.range(), &self.spec) + let eased_normalized = self.easing.map_or(normalized, |_easing| { + emath::easing::quadratic_in(normalized as _) as _ + }); + value_from_normalized(eased_normalized, self.range(), &self.spec) } fn position_from_value(&self, value: f64, position_range: Rangef) -> f32 { let normalized = normalized_from_value(value, self.range(), &self.spec); - lerp(position_range, normalized as f32) + let inversed_ease_normalized = self.easing.map_or(normalized, |_easing| { + emath::easing::quadratic_in_inverse(normalized as _) as _ + }); + lerp(position_range, inversed_ease_normalized as f32) } }