From d5d8eeb1723aa77688e8b0af15d0fbce8896ed02 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 6 Oct 2020 21:03:47 +0200 Subject: [PATCH] [slider] use f64 internally for increased precision --- egui/src/math/mod.rs | 8 --- egui/src/math/smart_aim.rs | 4 -- egui/src/widgets/slider.rs | 111 ++++++++++++++++++++----------------- 3 files changed, 59 insertions(+), 64 deletions(-) diff --git a/egui/src/math/mod.rs b/egui/src/math/mod.rs index 4fdd179e..ac74f4b4 100644 --- a/egui/src/math/mod.rs +++ b/egui/src/math/mod.rs @@ -120,14 +120,6 @@ pub fn ease_in_ease_out(t: f32) -> f32 { /// See pub const TAU: f32 = 2.0 * std::f32::consts::PI; -/// Round a value to the given number of decimal places. -pub fn round_to_precision_f32(value: f32, decimal_places: usize) -> f32 { - // This is a stupid way of doing this, but stupid works. - format!("{:.*}", decimal_places, value) - .parse() - .unwrap_or_else(|_| value) -} - /// Round a value to the given number of decimal places. pub fn round_to_precision(value: f64, decimal_places: usize) -> f64 { // This is a stupid way of doing this, but stupid works. diff --git a/egui/src/math/smart_aim.rs b/egui/src/math/smart_aim.rs index 4f857cbb..96dbbf49 100644 --- a/egui/src/math/smart_aim.rs +++ b/egui/src/math/smart_aim.rs @@ -2,10 +2,6 @@ const NUM_DECIMALS: usize = 15; -pub fn best_in_range_f32(min: f32, max: f32) -> f32 { - best_in_range_f64(min as f64, max as f64) as f32 -} - /// Find the "simplest" number in a closed range [min, max], i.e. the one with the fewest decimal digits. /// /// So in the range `[0.83, 1.354]` you will get `1.0`, and for `[0.37, 0.48]` you will get `0.4`. diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 99e0d21b..2f74adfe 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -2,15 +2,29 @@ use std::ops::RangeInclusive; use crate::{paint::*, widgets::Label, *}; -// TODO: switch to f64 internally so we can handle integers larger than 2^24. /// Combined into one function (rather than two) to make it easier /// for the borrow checker. -type SliderGetSet<'a> = Box) -> f32>; +type GetSetValue<'a> = Box) -> f64>; + +fn get(value_function: &mut GetSetValue<'_>) -> f64 { + (value_function)(None) +} + +fn set(value_function: &mut GetSetValue<'_>, value: f64) { + (value_function)(Some(value)); +} + +fn to_f64_range(r: RangeInclusive) -> RangeInclusive +where + f64: From, +{ + f64::from(*r.start())..=f64::from(*r.end()) +} /// Control a number by a horizontal slider. pub struct Slider<'a> { - get_set_value: SliderGetSet<'a>, - range: RangeInclusive, + get_set_value: GetSetValue<'a>, + range: RangeInclusive, // TODO: label: Option