Skip to content

Commit

Permalink
Replace emath::clamp with f32::clamp (new in rustc 1.50)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Mar 21, 2021
1 parent cdab9d7 commit f5c3729
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 63 deletions.
6 changes: 2 additions & 4 deletions egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand Down
4 changes: 1 addition & 3 deletions egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 14 additions & 7 deletions egui/src/widgets/drag_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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()),
)
}
2 changes: 1 addition & 1 deletion egui/src/widgets/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 7 additions & 3 deletions egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,19 @@ 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
}
}

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

Expand Down
16 changes: 8 additions & 8 deletions egui_demo_lib/src/apps/demo/sliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down
10 changes: 5 additions & 5 deletions egui_glium/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use {
egui::{
emath::{clamp, Rect},
emath::Rect,
epaint::{Color32, Mesh},
},
glium::{
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions egui_web/src/webgl1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
};

use egui::{
emath::{clamp, vec2},
emath::vec2,
epaint::{Color32, Texture},
};

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions egui_web/src/webgl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
};

use egui::{
emath::{clamp, vec2},
emath::vec2,
epaint::{Color32, Texture},
};

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions emath/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions emath/src/pos2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
use std::ops::{Add, AddAssign, Sub, SubAssign};

use crate::*;

Expand Down Expand Up @@ -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),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions emath/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions emath/src/vec2.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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),
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions epaint/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f5c3729

Please sign in to comment.