1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

egui: Add easing option to Slider

This commit is contained in:
Varphone Wong
2024-03-07 14:08:51 +08:00
parent f9df09b53d
commit b1f2cb3b9d

View File

@@ -117,6 +117,7 @@ pub struct Slider<'a> {
custom_parser: Option<NumParser<'a>>, custom_parser: Option<NumParser<'a>>,
trailing_fill: Option<bool>, trailing_fill: Option<bool>,
handle_shape: Option<HandleShape>, handle_shape: Option<HandleShape>,
easing: Option<bool>,
} }
impl<'a> Slider<'a> { impl<'a> Slider<'a> {
@@ -167,6 +168,7 @@ impl<'a> Slider<'a> {
custom_parser: None, custom_parser: None,
trailing_fill: None, trailing_fill: None,
handle_shape: None, handle_shape: None,
easing: None,
} }
} }
@@ -397,6 +399,13 @@ impl<'a> Slider<'a> {
self 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. /// Set custom formatter defining how numbers are converted into text.
/// ///
/// A custom formatter takes a `f64` for the numeric value and a `RangeInclusive<usize>` representing /// A custom formatter takes a `f64` for the numeric value and a `RangeInclusive<usize>` 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. /// 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 { fn value_from_position(&self, position: f32, position_range: Rangef) -> f64 {
let normalized = remap_clamp(position, position_range, 0.0..=1.0) as 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 { fn position_from_value(&self, value: f64, position_range: Rangef) -> f32 {
let normalized = normalized_from_value(value, self.range(), &self.spec); 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)
} }
} }