Replace emath::clamp with f32::clamp (new in rustc 1.50)

This commit is contained in:
Emil Ernerfeldt 2021-03-21 17:47:03 +01:00
parent cdab9d777f
commit f5c372910c
16 changed files with 66 additions and 63 deletions

View file

@ -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
}
});

View file

@ -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},

View file

@ -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();

View file

@ -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>) -> f64 {
x.clamp(
range.start().min(*range.end()),
range.start().max(*range.end()),
)
}

View file

@ -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();

View file

@ -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<f64>, 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))
}
}

View file

@ -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"),

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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<T>(x: T, range: RangeInclusive<T>) -> T
where
T: Copy + PartialOrd,

View file

@ -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>) -> 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),
}
}
}

View file

@ -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) {

View file

@ -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>) -> 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),
}
}
}

View file

@ -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);

View file

@ -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 {