From 9ba5bea143295b7e740b631b7088dd0e049cfb2f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 25 Apr 2020 11:14:32 +0200 Subject: [PATCH] Use RangeInclusive for clamp and lerp --- emigui/src/math.rs | 20 ++++++++++---------- emigui/src/texture_atlas.rs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/emigui/src/math.rs b/emigui/src/math.rs index f765aa6d..55d9f7b5 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -450,17 +450,17 @@ impl std::fmt::Debug for Rect { // ---------------------------------------------------------------------------- -pub fn lerp(min: T, max: T, t: f32) -> T +pub fn lerp(range: RangeInclusive, t: f32) -> T where f32: Mul, - T: Add, + T: Add + Copy, { - (1.0 - t) * min + t * max + (1.0 - t) * *range.start() + t * *range.end() } 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) + lerp(to, t) } pub fn remap_clamp(x: f32, from: RangeInclusive, to: RangeInclusive) -> f32 { @@ -471,14 +471,14 @@ pub fn remap_clamp(x: f32, from: RangeInclusive, to: RangeInclusive) - } else { (x - from.start()) / (from.end() - from.start()) }; - lerp(*to.start(), *to.end(), t) + lerp(to, t) } -pub fn clamp(x: f32, min: f32, max: f32) -> f32 { - if x <= min { - min - } else if x >= max { - max +pub fn clamp(x: f32, range: RangeInclusive) -> f32 { + if x <= *range.start() { + *range.start() + } else if x >= *range.end() { + *range.end() } else { x } diff --git a/emigui/src/texture_atlas.rs b/emigui/src/texture_atlas.rs index acc73258..d25a9321 100644 --- a/emigui/src/texture_atlas.rs +++ b/emigui/src/texture_atlas.rs @@ -133,8 +133,8 @@ impl Texture { .round(); let texel_radius = 32.0; - let u = clamp(u, texel_radius, self.width as f32 - 1.0 - texel_radius); - let v = clamp(v, texel_radius, self.height as f32 - 1.0 - texel_radius); + let u = clamp(u, texel_radius..=self.width as f32 - 1.0 - texel_radius); + let v = clamp(v, texel_radius..=self.height as f32 - 1.0 - texel_radius); let top_left = Vertex { pos: zoom_rect.min(),