use std::panic::{AssertUnwindSafe, catch_unwind};
use gpui::{Bounds, Pixels, px};
use gpui_material_theme::PolygonRole;
use polymorpher::{Cubic, Morph, geometry::Point as PolygonPoint};
use super::polygon::{ShapePolygon, cubic_center};
use super::{Outline, Shape, ShapeFit};
#[derive(Clone)]
pub struct ShapeMorph {
kind: MorphKind,
}
#[derive(Clone)]
enum MorphKind {
FeatureMatched(Morph),
PerimeterAligned(Vec<(Cubic, Cubic)>),
}
impl ShapeMorph {
pub fn new(start: impl Into<ShapePolygon>, end: impl Into<ShapePolygon>) -> Self {
let start = start.into();
let end = end.into();
let kind = match (start.as_rounded(), end.as_rounded()) {
(Some(from), Some(to)) => {
match catch_unwind(AssertUnwindSafe(|| Morph::new(from.clone(), to.clone()))) {
Ok(morph) => MorphKind::FeatureMatched(morph),
Err(_) => {
MorphKind::PerimeterAligned(align_perimeter(&from.cubics, &to.cubics))
}
}
}
_ => MorphKind::PerimeterAligned(align_perimeter(start.cubics(), end.cubics())),
};
Self { kind }
}
pub fn perimeter_aligned(start: impl Into<ShapePolygon>, end: impl Into<ShapePolygon>) -> Self {
let start = start.into();
let end = end.into();
Self {
kind: MorphKind::PerimeterAligned(align_perimeter(start.cubics(), end.cubics())),
}
}
pub fn sample_polygon(&self, progress: f32) -> ShapePolygon {
ShapePolygon::from_cubics(self.as_cubics(clamp_progress(progress)))
}
pub fn sample(&self, bounds: Bounds<Pixels>, progress: f32) -> Option<Shape> {
self.sample_with(bounds, progress, ShapeFit::Contain, 0.)
}
pub fn sample_with(
&self,
bounds: Bounds<Pixels>,
progress: f32,
fit: ShapeFit,
rotation_radians: f32,
) -> Option<Shape> {
Shape::from_cubics(
self.as_cubics(clamp_progress(progress)),
bounds,
fit,
rotation_radians,
)
}
fn as_cubics(&self, progress: f32) -> Vec<Cubic> {
match &self.kind {
MorphKind::FeatureMatched(morph) => morph.as_cubics(progress),
MorphKind::PerimeterAligned(pairs) => pairs
.iter()
.map(|(a, b)| {
Cubic::new(
a.anchor0().lerp(b.anchor0(), progress),
a.control0().lerp(b.control0(), progress),
a.control1().lerp(b.control1(), progress),
a.anchor1().lerp(b.anchor1(), progress),
)
})
.collect(),
}
}
}
#[derive(Clone)]
pub struct ShapeMorphSequence {
steps: Vec<ShapeMorph>,
wrap: Option<ShapeMorph>,
rotation_scale: f32,
control_radius: Option<f32>,
}
impl ShapeMorphSequence {
pub fn new(
shapes: impl IntoIterator<Item = impl Into<ShapePolygon>>,
looping: bool,
) -> Option<Self> {
Self::build(shapes, looping, false)
}
pub fn perimeter_aligned(
shapes: impl IntoIterator<Item = impl Into<ShapePolygon>>,
looping: bool,
) -> Option<Self> {
Self::build(shapes, looping, true)
}
fn build(
shapes: impl IntoIterator<Item = impl Into<ShapePolygon>>,
looping: bool,
perimeter_aligned: bool,
) -> Option<Self> {
let polygons: Vec<ShapePolygon> = shapes.into_iter().map(Into::into).collect();
if polygons.len() < 2 {
return None;
}
let make = |a: &ShapePolygon, b: &ShapePolygon| {
if perimeter_aligned {
ShapeMorph::perimeter_aligned(a.clone(), b.clone())
} else {
ShapeMorph::new(a.clone(), b.clone())
}
};
let steps = polygons
.windows(2)
.map(|pair| make(&pair[0], &pair[1]))
.collect();
let wrap = looping.then(|| make(polygons.last().unwrap(), &polygons[0]));
let rotation_scale = polygons
.iter()
.fold(1_f32, |scale, polygon| scale.min(polygon.rotation_scale()));
let control_radius = perimeter_aligned.then(|| {
polygons
.iter()
.map(ShapePolygon::control_point_radius)
.fold(0_f32, f32::max)
});
Some(Self {
steps,
wrap,
rotation_scale,
control_radius,
})
}
pub fn len(&self) -> usize {
self.steps.len() + 1
}
pub fn is_empty(&self) -> bool {
false
}
pub fn step_count(&self) -> usize {
self.steps.len() + usize::from(self.wrap.is_some())
}
pub fn is_looping(&self) -> bool {
self.wrap.is_some()
}
pub fn rotation_scale(&self) -> f32 {
self.rotation_scale
}
pub fn sample(&self, bounds: Bounds<Pixels>, progress: f32) -> Option<Shape> {
let value = clamp_progress(progress);
let phase = value * self.steps.len() as f32;
let index = (phase as usize).min(self.steps.len() - 1);
self.sample_step(index, phase - index as f32, bounds, ShapeFit::Contain, 0.)
}
pub fn sample_step(
&self,
step: usize,
progress: f32,
bounds: Bounds<Pixels>,
fit: ShapeFit,
rotation_radians: f32,
) -> Option<Shape> {
let morph = self.morph_for_step(step)?;
morph.sample_with(bounds, progress, fit, rotation_radians)
}
pub fn sample_step_polygon(&self, step: usize, progress: f32) -> Option<ShapePolygon> {
let morph = self.morph_for_step(step)?;
Some(morph.sample_polygon(progress))
}
pub fn sample_step_in_envelope(
&self,
step: usize,
progress: f32,
bounds: Bounds<Pixels>,
extent: Pixels,
rotation_radians: f32,
) -> Option<Shape> {
if !rotation_radians.is_finite() {
return None;
}
let morph = self.morph_for_step(step)?;
let cubics = morph.as_cubics(clamp_progress(progress));
if cubics.is_empty() {
return None;
}
let pivot = cubic_center(&cubics);
let side = bounds.size.width.min(bounds.size.height);
let extent = side.min(extent);
if !(extent > px(0.) && extent < px(f32::INFINITY)) {
return None;
}
let mut target_side = extent * self.rotation_scale;
if let Some(radius) = self.control_radius {
let radius = radius + (pivot - PolygonPoint::splat(0.5)).length();
if radius > f32::EPSILON {
target_side = target_side.min(extent / (2. * radius));
}
}
let scale = f32::from(target_side) / f32::from(side);
Shape::new(Outline {
cubics,
bounds,
fit: ShapeFit::Contain,
rotation: rotation_radians,
pivot,
scale,
})
}
fn morph_for_step(&self, step: usize) -> Option<&ShapeMorph> {
if self.wrap.is_some() {
let count = self.steps.len() + 1;
let index = step % count;
self.steps.get(index).or(self.wrap.as_ref())
} else {
self.steps.get(step)
}
}
}
#[derive(Clone)]
pub struct PolygonMorph(ShapeMorph);
impl PolygonMorph {
pub fn new(start: PolygonRole, end: PolygonRole) -> Self {
Self(ShapeMorph::new(start, end))
}
pub fn sample(&self, bounds: Bounds<Pixels>, progress: f32) -> Option<Shape> {
self.0.sample(bounds, progress)
}
}
fn clamp_progress(progress: f32) -> f32 {
if progress.is_finite() {
progress.clamp(0., 1.)
} else {
0.
}
}
fn align_perimeter(start: &[Cubic], end: &[Cubic]) -> Vec<(Cubic, Cubic)> {
let mut starts = weighted_segments(start).into_iter();
let mut ends = weighted_segments(end).into_iter();
let (mut from, mut to) = (starts.next(), ends.next());
let mut position = 0.;
let mut pairs = Vec::new();
while let (Some(mut a), Some(mut b)) = (from, to) {
let boundary = a.0.min(b.0);
let first = split_until(&mut a, position, boundary);
let last = split_until(&mut b, position, boundary);
pairs.push((first, last));
from = if boundary == a.0 {
starts.next()
} else {
Some(a)
};
to = if boundary == b.0 {
ends.next()
} else {
Some(b)
};
position = boundary;
}
pairs
}
fn weighted_segments(cubics: &[Cubic]) -> Vec<(f64, Cubic)> {
let mut cumulative = 0.;
let mut segments: Vec<_> = cubics
.iter()
.map(|cubic| {
let samples = [
cubic.anchor0(),
cubic.point_on_curve(0.25),
cubic.point_on_curve(0.5),
cubic.point_on_curve(0.75),
cubic.anchor1(),
];
cumulative += samples
.windows(2)
.map(|points| f64::from((points[1] - points[0]).length()))
.sum::<f64>();
(cumulative, *cubic)
})
.collect();
if cumulative > 0. {
for (end, _) in &mut segments {
*end /= cumulative;
}
}
segments
}
fn split_until(segment: &mut (f64, Cubic), from: f64, until: f64) -> Cubic {
if until == segment.0 {
return segment.1;
}
let (head, tail) = segment
.1
.split(((until - from) / (segment.0 - from)) as f32);
segment.1 = tail;
head
}