From 4cc6ca7236346b48646c501cecfb7bca4073764b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 25 Apr 2020 11:11:44 +0200 Subject: [PATCH] Use ..= range arguments to remap functions --- emigui/src/collapsing_header.rs | 14 ++++++-------- emigui/src/math.rs | 26 +++++++++++++++++--------- emigui/src/mesher.rs | 8 +++----- emigui/src/scroll_area.rs | 5 +++-- emigui/src/texture_atlas.rs | 21 +++++---------------- emigui/src/widgets.rs | 9 ++++----- 6 files changed, 38 insertions(+), 45 deletions(-) diff --git a/emigui/src/collapsing_header.rs b/emigui/src/collapsing_header.rs index d50f48c8..a65f8d6a 100644 --- a/emigui/src/collapsing_header.rs +++ b/emigui/src/collapsing_header.rs @@ -99,18 +99,16 @@ impl CollapsingHeader { let max_height = if state.open { remap( time_since_toggle, - 0.0, - animation_time, - 50.0, // Get instant feedback - 1500.0, // We don't expect to get bigger than this + 0.0..=animation_time, + // Get instant feedback, and we don't expect to get bigger than this + 50.0..=1500.0, ) } else { remap_clamp( time_since_toggle, - 0.0, - animation_time, - 50.0, // TODO: state.open_height - 0.0, + 0.0..=animation_time, + // TODO: state.open_height + 50.0..=0.0, ) }; diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 2d60a34c..f765aa6d 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, RangeInclusive, Sub, SubAssign}; #[derive(Clone, Copy, Default, Deserialize, Serialize)] pub struct Vec2 { @@ -387,6 +387,14 @@ impl Rect { self.max.y - self.min.y } + pub fn range_x(&self) -> RangeInclusive { + self.min.x..=self.max.x + } + + pub fn range_y(&self) -> RangeInclusive { + self.min.y..=self.max.y + } + pub fn is_empty(&self) -> bool { self.max.x < self.min.x || self.max.y < self.min.y } @@ -450,20 +458,20 @@ where (1.0 - t) * min + t * max } -pub fn remap(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 { - let t = (from - from_min) / (from_max - from_min); - lerp(to_min, to_max, t) +pub fn remap(x: f32, from: RangeInclusive, to: RangeInclusive) -> f32 { + let t = (x - from.start()) / (from.end() - from.start()); + lerp(*to.start(), *to.end(), t) } -pub fn remap_clamp(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 { - let t = if from <= from_min { +pub fn remap_clamp(x: f32, from: RangeInclusive, to: RangeInclusive) -> f32 { + let t = if x <= *from.start() { 0.0 - } else if from >= from_max { + } else if x >= *from.end() { 1.0 } else { - (from - from_min) / (from_max - from_min) + (x - from.start()) / (from.end() - from.start()) }; - lerp(to_min, to_max, t) + lerp(*to.start(), *to.end(), t) } pub fn clamp(x: f32, min: f32, max: f32) -> f32 { diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index bb371f64..ffcc8533 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -154,7 +154,7 @@ impl Path { pub fn add_circle(&mut self, center: Pos2, radius: f32) { let n = 32; // TODO: parameter for i in 0..n { - let angle = remap(i as f32, 0.0, n as f32, 0.0, TAU); + let angle = remap(i as f32, 0.0..=n as f32, 0.0..=TAU); let normal = vec2(angle.cos(), angle.sin()); self.add_point(center + radius * normal, normal); } @@ -222,10 +222,8 @@ impl Path { for i in 0..=n { let angle = remap( i as f32, - 0.0, - n as f32, - quadrant * RIGHT_ANGLE, - (quadrant + 1.0) * RIGHT_ANGLE, + 0.0..=n as f32, + quadrant * RIGHT_ANGLE..=(quadrant + 1.0) * RIGHT_ANGLE, ); let normal = vec2(angle.cos(), angle.sin()); self.add_point(center + radius * normal, normal); diff --git a/emigui/src/scroll_area.rs b/emigui/src/scroll_area.rs index e161c40d..cc46d5f9 100644 --- a/emigui/src/scroll_area.rs +++ b/emigui/src/scroll_area.rs @@ -85,7 +85,8 @@ impl ScrollArea { pos2(right, inner_rect.bottom()), ); - let from_content = |content_y| remap_clamp(content_y, 0.0, content_size.y, top, bottom); + let from_content = + |content_y| remap_clamp(content_y, 0.0..=content_size.y, top..=bottom); let handle_rect = Rect::from_min_max( pos2(left, from_content(state.offset.y)), @@ -110,7 +111,7 @@ impl ScrollArea { if scroll_bg_interact.active { // Center scroll at mouse pos: let mpos_top = mouse_pos.y - handle_rect.height() / 2.0; - state.offset.y = remap(mpos_top, top, bottom, 0.0, content_size.y); + state.offset.y = remap(mpos_top, top..=bottom, 0.0..=content_size.y); } } } diff --git a/emigui/src/texture_atlas.rs b/emigui/src/texture_atlas.rs index 83c79984..acc73258 100644 --- a/emigui/src/texture_atlas.rs +++ b/emigui/src/texture_atlas.rs @@ -126,22 +126,11 @@ impl Texture { if interact.hovered { show_popup(region.ctx(), mouse_pos, |region| { let zoom_rect = region.reserve_space(vec2(128.0, 128.0), None).rect; - let u = remap_clamp( - mouse_pos.x, - rect.min().x, - rect.max().x, - 0.0, - self.width as f32 - 1.0, - ) - .round(); - let v = remap_clamp( - mouse_pos.y, - rect.min().y, - rect.max().y, - 0.0, - self.height as f32 - 1.0, - ) - .round(); + let u = remap_clamp(mouse_pos.x, rect.range_x(), 0.0..=self.width as f32 - 1.0) + .round(); + let v = + remap_clamp(mouse_pos.y, rect.range_y(), 0.0..=self.height as f32 - 1.0) + .round(); let texel_radius = 32.0; let u = clamp(u, texel_radius, self.width as f32 - 1.0 - texel_radius); diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 428171c2..de0f8ba3 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -457,13 +457,12 @@ impl<'a> Widget for Slider<'a> { let left = interact.rect.left() + handle_radius; let right = interact.rect.right() - handle_radius; - let min = *self.range.start(); - let max = *self.range.end(); - debug_assert!(min <= max); + let range = self.range.clone(); + debug_assert!(range.start() <= range.end()); if let Some(mouse_pos) = region.input().mouse_pos { if interact.active { - self.set_value_f32(remap_clamp(mouse_pos.x, left, right, min, max)); + self.set_value_f32(remap_clamp(mouse_pos.x, left..=right, range.clone())); } } @@ -477,7 +476,7 @@ impl<'a> Widget for Slider<'a> { pos2(interact.rect.left(), rect.center().y - rail_radius), pos2(interact.rect.right(), rect.center().y + rail_radius), ); - let marker_center_x = remap_clamp(value, min, max, left, right); + let marker_center_x = remap_clamp(value, range, left..=right); region.add_paint_cmd(PaintCmd::Rect { rect: rail_rect,