From 2ac1a56ec508ebba940d5657cc27362f0fb2ce06 Mon Sep 17 00:00:00 2001 From: dbr Date: Tue, 16 Nov 2021 12:59:37 +1100 Subject: [PATCH 1/4] Update slider example #565 Full range of slider is half the ::MIN/MAX for f32/f64 --- imgui-examples/examples/slider.rs | 22 +++++++++++++++++++--- imgui/src/widget/slider.rs | 5 +++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/imgui-examples/examples/slider.rs b/imgui-examples/examples/slider.rs index 07b4072..2bb5de1 100644 --- a/imgui-examples/examples/slider.rs +++ b/imgui-examples/examples/slider.rs @@ -43,17 +43,25 @@ fn example_1(ui: &Ui, state: &mut State) { ui.text("Unsigned: u8 u16 u32 u64"); ui.text("Floats: f32 f64"); - Slider::new("u8 value", 0, 255) + // Full ranges can be specified with Rust's `::MIN/MAX` constants + Slider::new("u8 value", u8::MIN, u8::MAX) .build(ui, &mut state.u8_value); - Slider::new("f32 value", -f32::MIN, f32::MAX) + // However for larger data-types, it's usually best to specify + // a much smaller range. The following slider is hard to use. + Slider::new("Full range f32 value", -f32::MIN/2.0, f32::MAX/2.0) .build(ui, &mut state.f32_value); + // Note the `... / 2.0` - anything larger is not supported by + // the upstream C++ library + ui.text("Note that for 32-bit/64-bit types, sliders are always limited to half of the natural type range!"); + // Most of the time, it's best to specify the range ui.separator(); ui.text("Slider range can be limited:"); Slider::new("i32 value with range", -999, 999) .build(ui, &mut state.i32_value); - ui.text("Note that for 32-bit/64-bit types, sliders are always limited to half of the natural type range!"); + Slider::new("f32 value", -10.0, 10.0) + .build(ui, &mut state.f32_value); ui.separator(); ui.text("Value formatting can be customized with a C-style printf string:"); @@ -61,6 +69,14 @@ fn example_1(ui: &Ui, state: &mut State) { .display_format("%09.0f") .build(ui, &mut state.f64_formatted); + // This formatting impacts the increments the slider operates in: + Slider::new("f32 with %.01f", 0.0, 1.0) + .display_format("%.01f") + .build(ui, &mut state.f32_value); + Slider::new("Same f32 with %.05f", 0.0, 1.0) + .display_format("%.05f") + .build(ui, &mut state.f32_value); + ui.separator(); ui.text("Vertical sliders require a size parameter but otherwise work in a similar way:"); VerticalSlider::new("vertical\nu8 value", [50.0, 50.0], u8::MIN, u8::MAX) diff --git a/imgui/src/widget/slider.rs b/imgui/src/widget/slider.rs index c473730..51e7f84 100644 --- a/imgui/src/widget/slider.rs +++ b/imgui/src/widget/slider.rs @@ -67,6 +67,11 @@ where /// /// It is safe, though up to C++ Dear ImGui, on how to handle when /// `min > max`. + /// + /// Note for f32 and f64 sliders, Dear ImGui limits the available + /// range to half their full range (e.g `-f32::MIN/2.0 .. f32::MAX/2.0`) + /// Specifying a value above this will cause an abort. + /// For large ranged values, consider using [`Ui::input_scalar`] instead #[inline] pub fn range(mut self, min: Data, max: Data) -> Self { self.min = min; From caaae327f6b70aa1c10a587f4143afc3026fa678 Mon Sep 17 00:00:00 2001 From: dbr Date: Sat, 20 Nov 2021 19:19:50 +1100 Subject: [PATCH 2/4] Don't negate f32::MIN! --- imgui-examples/examples/slider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-examples/examples/slider.rs b/imgui-examples/examples/slider.rs index 2bb5de1..278fb8a 100644 --- a/imgui-examples/examples/slider.rs +++ b/imgui-examples/examples/slider.rs @@ -49,7 +49,7 @@ fn example_1(ui: &Ui, state: &mut State) { // However for larger data-types, it's usually best to specify // a much smaller range. The following slider is hard to use. - Slider::new("Full range f32 value", -f32::MIN/2.0, f32::MAX/2.0) + Slider::new("Full range f32 value", f32::MIN/2.0, f32::MAX/2.0) .build(ui, &mut state.f32_value); // Note the `... / 2.0` - anything larger is not supported by // the upstream C++ library From 6f3d385fbefc6ed1f2200e0dbd7d514f3387e44f Mon Sep 17 00:00:00 2001 From: dbr Date: Sun, 21 Nov 2021 14:32:35 +1100 Subject: [PATCH 3/4] Better wording in comment --- imgui-examples/examples/slider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-examples/examples/slider.rs b/imgui-examples/examples/slider.rs index 278fb8a..d91cd2c 100644 --- a/imgui-examples/examples/slider.rs +++ b/imgui-examples/examples/slider.rs @@ -69,7 +69,7 @@ fn example_1(ui: &Ui, state: &mut State) { .display_format("%09.0f") .build(ui, &mut state.f64_formatted); - // This formatting impacts the increments the slider operates in: + // The display format changes the increments the slider operates in: Slider::new("f32 with %.01f", 0.0, 1.0) .display_format("%.01f") .build(ui, &mut state.f32_value); From c4d7bc6c98da4ced880ddf3831dc69ca66a37f9a Mon Sep 17 00:00:00 2001 From: dbr Date: Sat, 27 Nov 2021 12:04:38 +1100 Subject: [PATCH 4/4] No -f32::MIN --- imgui/src/widget/slider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui/src/widget/slider.rs b/imgui/src/widget/slider.rs index 51e7f84..7432390 100644 --- a/imgui/src/widget/slider.rs +++ b/imgui/src/widget/slider.rs @@ -69,7 +69,7 @@ where /// `min > max`. /// /// Note for f32 and f64 sliders, Dear ImGui limits the available - /// range to half their full range (e.g `-f32::MIN/2.0 .. f32::MAX/2.0`) + /// range to half their full range (e.g `f32::MIN/2.0 .. f32::MAX/2.0`) /// Specifying a value above this will cause an abort. /// For large ranged values, consider using [`Ui::input_scalar`] instead #[inline]