1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

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