diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 0d280e9bb71..1ed40941e52 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -291,10 +291,8 @@ impl Prepared { } else { let handle_top_pos_at_bottom = bottom - handle_rect.height(); // Calculate the new handle top position, centering the handle on the mouse. - let new_handle_top_pos = clamp( - pointer_pos.y - handle_rect.height() / 2.0, - top..=handle_top_pos_at_bottom, - ); + let new_handle_top_pos = (pointer_pos.y - handle_rect.height() / 2.0) + .clamp(top, handle_top_pos_at_bottom); pointer_pos.y - new_handle_top_pos } }); diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 5c2fa144e9e..bf4d0e83ca5 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -303,9 +303,7 @@ pub use epaint as paint; // historical reasons // Can't add deprecation notice due to https://github.com/rust-lang/rust/issues/30827 pub use emath as math; // historical reasons -pub use emath::{ - clamp, lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2, -}; +pub use emath::{lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2}; pub use epaint::{ color, mutex, text::{FontDefinitions, FontFamily, TextStyle}, diff --git a/egui/src/response.rs b/egui/src/response.rs index 068b5fae410..78b0b8f6566 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -162,7 +162,7 @@ impl Response { /// The widget had keyboard focus and lost it, /// either because the user pressed tab or clicked somewhere else, - /// or (in case of a [`TextEdit`]) because the user pressed enter. + /// or (in case of a [`crate::TextEdit`]) because the user pressed enter. /// /// ``` /// # let mut ui = egui::Ui::__test(); diff --git a/egui/src/widgets/drag_value.rs b/egui/src/widgets/drag_value.rs index 1eb00ff2fb7..cbd30d75cec 100644 --- a/egui/src/widgets/drag_value.rs +++ b/egui/src/widgets/drag_value.rs @@ -196,11 +196,11 @@ impl<'a> Widget for DragValue<'a> { } = self; let value = get(&mut get_set_value); - let value = clamp(value, clamp_range.clone()); + let value = clamp_to_range(value, clamp_range.clone()); let aim_rad = ui.input().aim_radius() as f64; - let auto_decimals = clamp((aim_rad / speed.abs()).log10().ceil(), 0.0..=15.0) as usize; + let auto_decimals = (aim_rad / speed.abs()).log10().ceil().clamp(0.0, 15.0) as usize; let max_decimals = max_decimals.unwrap_or(auto_decimals + 2); - let auto_decimals = clamp(auto_decimals, min_decimals..=max_decimals); + let auto_decimals = auto_decimals.clamp(min_decimals, max_decimals); let value_text = if value == 0.0 { "0".to_owned() } else { @@ -225,7 +225,7 @@ impl<'a> Widget for DragValue<'a> { .text_style(TextStyle::Monospace), ); if let Ok(parsed_value) = value_text.parse() { - let parsed_value = clamp(parsed_value, clamp_range); + let parsed_value = clamp_to_range(parsed_value, clamp_range); set(&mut get_set_value, parsed_value) } if ui.input().key_pressed(Key::Enter) { @@ -263,7 +263,7 @@ impl<'a> Widget for DragValue<'a> { .flatten(); let stored_value = stored_value.unwrap_or(value); let stored_value = stored_value + delta_value as f64; - let stored_value = clamp(stored_value, clamp_range.clone()); + let stored_value = clamp_to_range(stored_value, clamp_range.clone()); let rounded_new_value = stored_value; @@ -274,7 +274,7 @@ impl<'a> Widget for DragValue<'a> { ); let rounded_new_value = emath::round_to_decimals(rounded_new_value, auto_decimals); - let rounded_new_value = clamp(rounded_new_value, clamp_range); + let rounded_new_value = clamp_to_range(rounded_new_value, clamp_range); set(&mut get_set_value, rounded_new_value); drag_state.last_dragged_id = Some(response.id); @@ -290,7 +290,7 @@ impl<'a> Widget for DragValue<'a> { if change != 0.0 { let new_value = value + speed * change; let new_value = emath::round_to_decimals(new_value, auto_decimals); - let new_value = clamp(new_value, clamp_range); + let new_value = clamp_to_range(new_value, clamp_range); set(&mut get_set_value, new_value); } } @@ -307,3 +307,10 @@ impl<'a> Widget for DragValue<'a> { response } } + +fn clamp_to_range(x: f64, range: RangeInclusive) -> f64 { + x.clamp( + range.start().min(*range.end()), + range.start().max(*range.end()), + ) +} diff --git a/egui/src/widgets/plot.rs b/egui/src/widgets/plot.rs index 277f3c8da88..ac92b0bc522 100644 --- a/egui/src/widgets/plot.rs +++ b/egui/src/widgets/plot.rs @@ -602,7 +602,7 @@ impl Prepared { let step_size_in_points = (self.dpos_dvalue()[axis] * step_size) as f32; // Where on the cross-dimension to show the label values - let value_cross = clamp(0.0, bounds.min[1 - axis]..=bounds.max[1 - axis]); + let value_cross = 0.0_f64.clamp(bounds.min[1 - axis], bounds.max[1 - axis]); for i in 0.. { let value_main = step_size * (bounds.min[axis] / step_size + i as f64).floor(); diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 8ca160ecbb1..1f7edbda4dd 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -248,7 +248,9 @@ impl<'a> Slider<'a> { fn get_value(&mut self) -> f64 { let value = get(&mut self.get_set_value); if self.clamp_to_range { - clamp(value, self.range.clone()) + let start = *self.range.start(); + let end = *self.range.end(); + value.clamp(start.min(end), start.max(end)) } else { value } @@ -256,7 +258,9 @@ impl<'a> Slider<'a> { fn set_value(&mut self, mut value: f64) { if self.clamp_to_range { - value = clamp(value, self.range.clone()); + let start = *self.range.start(); + let end = *self.range.end(); + value = value.clamp(start.min(end), start.max(end)); } if let Some(max_decimals) = self.max_decimals { value = emath::round_to_decimals(value, max_decimals); @@ -500,7 +504,7 @@ fn value_from_normalized(normalized: f64, range: RangeInclusive, spec: &Sli min.is_finite() && max.is_finite(), "You should use a logarithmic range" ); - lerp(range, clamp(normalized, 0.0..=1.0)) + lerp(range, normalized.clamp(0.0, 1.0)) } } diff --git a/egui_demo_lib/src/apps/demo/sliders.rs b/egui_demo_lib/src/apps/demo/sliders.rs index 21e049ac08d..9c7cd311229 100644 --- a/egui_demo_lib/src/apps/demo/sliders.rs +++ b/egui_demo_lib/src/apps/demo/sliders.rs @@ -59,16 +59,16 @@ impl super::View for Sliders { ui.label("You can click a slider value to edit it with the keyboard."); - let full_range = if *integer { - (i32::MIN as f64)..=(i32::MAX as f64) + let (type_min, type_max) = if *integer { + ((i32::MIN as f64), (i32::MAX as f64)) } else if *logarithmic { - -INFINITY..=INFINITY + (-INFINITY, INFINITY) } else { - -1e5..=1e5 // linear sliders make little sense with huge numbers + (-1e5, 1e5) // linear sliders make little sense with huge numbers }; - *min = clamp(*min, full_range.clone()); - *max = clamp(*max, full_range.clone()); + *min = min.clamp(type_min, type_max); + *max = max.clamp(type_min, type_max); if *integer { let mut value_i32 = *value as i32; @@ -102,13 +102,13 @@ impl super::View for Sliders { ui.separator(); ui.label("Slider range:"); ui.add( - Slider::f64(min, full_range.clone()) + Slider::f64(min, type_min..=type_max) .logarithmic(true) .smart_aim(*smart_aim) .text("left"), ); ui.add( - Slider::f64(max, full_range) + Slider::f64(max, type_min..=type_max) .logarithmic(true) .smart_aim(*smart_aim) .text("right"), diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index b6876cb108b..4f0513a1891 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -2,7 +2,7 @@ use { egui::{ - emath::{clamp, Rect}, + emath::Rect, epaint::{Color32, Mesh}, }, glium::{ @@ -201,10 +201,10 @@ impl Painter { let clip_max_y = pixels_per_point * clip_rect.max.y; // Make sure clip rect can fit within a `u32`: - let clip_min_x = clamp(clip_min_x, 0.0..=width_in_pixels as f32); - let clip_min_y = clamp(clip_min_y, 0.0..=height_in_pixels as f32); - let clip_max_x = clamp(clip_max_x, clip_min_x..=width_in_pixels as f32); - let clip_max_y = clamp(clip_max_y, clip_min_y..=height_in_pixels as f32); + let clip_min_x = clip_min_x.clamp(0.0, width_in_pixels as f32); + let clip_min_y = clip_min_y.clamp(0.0, height_in_pixels as f32); + let clip_max_x = clip_max_x.clamp(clip_min_x, width_in_pixels as f32); + let clip_max_y = clip_max_y.clamp(clip_min_y, height_in_pixels as f32); let clip_min_x = clip_min_x.round() as u32; let clip_min_y = clip_min_y.round() as u32; diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 994bacf24fe..770b35680e3 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -5,7 +5,7 @@ use { }; use egui::{ - emath::{clamp, vec2}, + emath::vec2, epaint::{Color32, Texture}, }; @@ -455,10 +455,10 @@ impl crate::Painter for WebGlPainter { let clip_min_y = pixels_per_point * clip_rect.min.y; let clip_max_x = pixels_per_point * clip_rect.max.x; let clip_max_y = pixels_per_point * clip_rect.max.y; - let clip_min_x = clamp(clip_min_x, 0.0..=screen_size_pixels.x); - let clip_min_y = clamp(clip_min_y, 0.0..=screen_size_pixels.y); - let clip_max_x = clamp(clip_max_x, clip_min_x..=screen_size_pixels.x); - let clip_max_y = clamp(clip_max_y, clip_min_y..=screen_size_pixels.y); + let clip_min_x = clip_min_x.clamp(0.0, screen_size_pixels.x); + let clip_min_y = clip_min_y.clamp(0.0, screen_size_pixels.y); + let clip_max_x = clip_max_x.clamp(clip_min_x, screen_size_pixels.x); + let clip_max_y = clip_max_y.clamp(clip_min_y, screen_size_pixels.y); let clip_min_x = clip_min_x.round() as i32; let clip_min_y = clip_min_y.round() as i32; let clip_max_x = clip_max_x.round() as i32; diff --git a/egui_web/src/webgl2.rs b/egui_web/src/webgl2.rs index f0457c5c2be..b0553c09553 100644 --- a/egui_web/src/webgl2.rs +++ b/egui_web/src/webgl2.rs @@ -7,7 +7,7 @@ use { }; use egui::{ - emath::{clamp, vec2}, + emath::vec2, epaint::{Color32, Texture}, }; @@ -457,10 +457,10 @@ impl crate::Painter for WebGl2Painter { let clip_min_y = pixels_per_point * clip_rect.min.y; let clip_max_x = pixels_per_point * clip_rect.max.x; let clip_max_y = pixels_per_point * clip_rect.max.y; - let clip_min_x = clamp(clip_min_x, 0.0..=screen_size_pixels.x); - let clip_min_y = clamp(clip_min_y, 0.0..=screen_size_pixels.y); - let clip_max_x = clamp(clip_max_x, clip_min_x..=screen_size_pixels.x); - let clip_max_y = clamp(clip_max_y, clip_min_y..=screen_size_pixels.y); + let clip_min_x = clip_min_x.clamp(0.0, screen_size_pixels.x); + let clip_min_y = clip_min_y.clamp(0.0, screen_size_pixels.y); + let clip_max_x = clip_max_x.clamp(clip_min_x, screen_size_pixels.x); + let clip_max_y = clip_max_y.clamp(clip_min_y, screen_size_pixels.y); let clip_min_x = clip_min_x.round() as i32; let clip_min_y = clip_min_y.round() as i32; let clip_max_x = clip_max_x.round() as i32; diff --git a/emath/src/lib.rs b/emath/src/lib.rs index 23f121b004c..571009a4b19 100644 --- a/emath/src/lib.rs +++ b/emath/src/lib.rs @@ -159,6 +159,7 @@ where /// Returns `range.start()` if `x <= range.start()`, /// returns `range.end()` if `x >= range.end()` /// and returns `x` elsewhen. +#[deprecated = "Use f32::clamp instead"] pub fn clamp(x: T, range: RangeInclusive) -> T where T: Copy + PartialOrd, diff --git a/emath/src/pos2.rs b/emath/src/pos2.rs index 7fde33cbd55..1c59e0f8267 100644 --- a/emath/src/pos2.rs +++ b/emath/src/pos2.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Sub, SubAssign}; use crate::*; @@ -143,10 +143,10 @@ impl Pos2 { } #[must_use] - pub fn clamp(self, range: RangeInclusive) -> Self { + pub fn clamp(self, min: Self, max: Self) -> Self { Self { - x: clamp(self.x, range.start().x..=range.end().x), - y: clamp(self.y, range.start().y..=range.end().y), + x: self.x.clamp(min.x, max.x), + y: self.y.clamp(min.y, max.y), } } } diff --git a/emath/src/rect.rs b/emath/src/rect.rs index a1513a1d19c..87f47567147 100644 --- a/emath/src/rect.rs +++ b/emath/src/rect.rs @@ -204,11 +204,10 @@ impl Rect { } /// Return the given points clamped to be inside the rectangle + /// Panics if [`Self::is_negative`]. #[must_use] - pub fn clamp(&self, mut p: Pos2) -> Pos2 { - p.x = clamp(p.x, self.x_range()); - p.y = clamp(p.y, self.y_range()); - p + pub fn clamp(&self, p: Pos2) -> Pos2 { + p.clamp(self.min, self.max) } pub fn extend_with(&mut self, p: Pos2) { diff --git a/emath/src/vec2.rs b/emath/src/vec2.rs index 73450c2437b..7d956863664 100644 --- a/emath/src/vec2.rs +++ b/emath/src/vec2.rs @@ -1,11 +1,9 @@ -use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, RangeInclusive, Sub, SubAssign}; - -use crate::*; +use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; /// A vector has a direction and length. /// A [`Vec2`] is often used to represent a size. /// -/// emath represents positions using [`Pos2`]. +/// emath represents positions using [`crate::Pos2`]. /// /// Normally the units are points (logical pixels). #[derive(Clone, Copy, Default, PartialEq)] @@ -183,10 +181,10 @@ impl Vec2 { } #[must_use] - pub fn clamp(self, range: RangeInclusive) -> Self { + pub fn clamp(self, min: Self, max: Self) -> Self { Self { - x: clamp(self.x, range.start().x..=range.end().x), - y: clamp(self.y, range.start().y..=range.end().y), + x: self.x.clamp(min.x, max.x), + y: self.y.clamp(min.y, max.y), } } } diff --git a/epaint/src/color.rs b/epaint/src/color.rs index 7332a4105d4..b42b4c0a1b4 100644 --- a/epaint/src/color.rs +++ b/epaint/src/color.rs @@ -4,8 +4,6 @@ //! If you want to manipulate RGBA colors use [`Rgba`]. //! If you want to manipulate colors in a way closer to how humans think about colors, use [`HsvaGamma`]. -use emath::clamp; - /// This format is used for space-efficient color representation (32 bits). /// /// Instead of manipulating this directly it is often better @@ -363,7 +361,7 @@ pub fn gamma_u8_from_linear_f32(l: f32) -> u8 { /// linear [0, 1] -> linear [0, 255] (clamped). /// Useful for alpha-channel. pub fn linear_u8_from_linear_f32(a: f32) -> u8 { - clamp(a * 255.0, 0.0..=255.0).round() as u8 + (a * 255.0).round() as u8 // rust does a saturating cast since 1.45 } #[test] @@ -593,7 +591,7 @@ pub fn hsv_from_rgb([r, g, b]: [f32; 3]) -> (f32, f32, f32) { pub fn rgb_from_hsv((h, s, v): (f32, f32, f32)) -> [f32; 3] { #![allow(clippy::many_single_char_names)] let h = (h.fract() + 1.0).fract(); // wrap - let s = clamp(s, 0.0..=1.0); + let s = s.clamp(0.0, 1.0); let f = h * 6.0 - (h * 6.0).floor(); let p = v * (1.0 - s); diff --git a/epaint/src/tessellator.rs b/epaint/src/tessellator.rs index d57f5c9c10f..cd79dbef097 100644 --- a/epaint/src/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -49,7 +49,7 @@ impl Path { pub fn add_circle(&mut self, center: Pos2, radius: f32) { let n = (radius * 4.0).round() as i32; // TODO: tweak a bit more - let n = clamp(n, 4..=64); + let n = n.clamp(4, 64); self.reserve(n as usize); for i in 0..n { let angle = remap(i as f32, 0.0..=n as f32, 0.0..=TAU); @@ -199,7 +199,7 @@ pub mod path { // TODO: optimize with precalculated vertices for some radii ranges let n = (radius * 0.75).round() as i32; // TODO: tweak a bit more - let n = clamp(n, 2..=32); + let n = n.clamp(2, 32); const RIGHT_ANGLE: f32 = TAU / 4.0; path.reserve(n as usize + 1); for i in 0..=n {