From bbf5f09b21b2f47470c1a660912c5039a52e20d6 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 017270fc7..dbb1316b5 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -118,6 +118,7 @@ pub struct Slider<'a> { trailing_fill: Option, handle_shape: Option, update_while_editing: bool, + easing: Option, } impl<'a> Slider<'a> { @@ -169,6 +170,7 @@ impl<'a> Slider<'a> { trailing_fill: None, handle_shape: None, update_while_editing: true, + easing: None, } } @@ -399,6 +401,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 @@ -636,12 +645,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) } /// Update the value on each key press when text-editing the value.