Use ..= range arguments to remap functions
This commit is contained in:
parent
663fbda90c
commit
4cc6ca7236
6 changed files with 38 additions and 45 deletions
|
@ -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,
|
||||
)
|
||||
};
|
||||
|
||||
|
|
|
@ -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<f32> {
|
||||
self.min.x..=self.max.x
|
||||
}
|
||||
|
||||
pub fn range_y(&self) -> RangeInclusive<f32> {
|
||||
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<f32>, to: RangeInclusive<f32>) -> 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<f32>, to: RangeInclusive<f32>) -> 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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue