use crate::{Rect, Transform, Vec2};
#[derive(Clone, Copy, Debug)]
pub(super) struct Pose {
pub(super) transform: Transform,
pub(super) opacity: f32,
pub(super) content: f32,
pub(super) dim: f32,
pub(super) blur: f32,
}
fn rest(d: f32) -> f32 {
if d.abs() < 0.002 {
0.0
} else {
d
}
}
pub(super) fn about(center: Vec2, scale: f32, angle: f32, offset: Vec2) -> Transform {
let mut t = Transform {
scale,
translation: Vec2::ZERO,
angle,
};
t.translation = center + offset - t.vector(center);
t
}
pub(super) struct StackGeometry {
pub(super) card: Rect,
pub(super) direction: Vec2,
pub(super) step: f32,
pub(super) layers: usize,
pub(super) scale_step: f32,
pub(super) fade_step: f32,
pub(super) exit: Vec2,
pub(super) exit_rotation: f32,
}
pub(super) fn stack_pose(g: &StackGeometry, d: f32) -> Option<Pose> {
let d = rest(d);
let size = g.card.size();
let center = g.card.center();
if d >= 0.0 {
let layers = g.layers as f32;
if d >= layers {
return None;
}
let scale = (1.0 - g.scale_step * d).max(0.5);
let extent = g.direction.abs().dot(size);
let offset = g.direction * (extent * 0.5 * (1.0 - scale) + g.step * d);
Some(Pose {
transform: about(center, scale, 0.0, offset),
opacity: (1.0 - g.fade_step * d).clamp(0.0, 1.0) * (layers - d).clamp(0.0, 1.0),
content: (1.0 - d).clamp(0.0, 1.0),
dim: 0.0,
blur: 0.0,
})
} else {
let gone = -d;
if gone >= 1.0 {
return None;
}
Some(Pose {
transform: about(center, 1.0, g.exit_rotation * gone, g.exit * size * gone),
opacity: 1.0 - gone,
content: 1.0,
dim: 0.0,
blur: 0.0,
})
}
}
pub(super) struct SlideGeometry {
pub(super) slide: Rect,
pub(super) axis: usize,
pub(super) neighbors: usize,
pub(super) peek: f32,
pub(super) scale_step: f32,
pub(super) drop: f32,
pub(super) dim: f32,
pub(super) blur: f32,
}
pub(super) fn slide_pose(g: &SlideGeometry, d: f32) -> Option<Pose> {
let d = rest(d);
let distance = d.abs();
let reach = g.neighbors as f32 + 1.0;
if distance >= reach {
return None;
}
let scale = (1.0 - g.scale_step * distance).max(0.4);
let main = (g.slide.size()[g.axis] * 0.5 * (1.0 - scale) + g.peek * distance).copysign(d);
let mut offset = Vec2::splat(g.drop * distance);
offset[g.axis] = main;
Some(Pose {
transform: about(g.slide.center(), scale, 0.0, offset),
opacity: (reach - distance).clamp(0.0, 1.0),
content: 1.0,
dim: (g.dim * distance).min(0.85),
blur: g.blur * distance.min(1.0),
})
}
pub(super) fn paint_order(a: f32, b: f32) -> std::cmp::Ordering {
let rank = |d: f32| (d < 0.0, if d < 0.0 { d } else { -d });
let (ra, rb) = (rank(a), rank(b));
ra.0.cmp(&rb.0).then(ra.1.total_cmp(&rb.1))
}
pub(super) fn depth_order(a: f32, b: f32) -> std::cmp::Ordering {
b.abs().total_cmp(&a.abs())
}