Skip to main content

glory_render/
graphic_transition.rs

1use crate::{Error, GraphicPoint, MAX_GRAPHIC_COMMANDS, MAX_GRAPHIC_COORDINATE, PathCommand};
2
3/// A finite transition between compatible paths. Creation paints the endpoint;
4/// only a later accepted path replacement supplies a previous geometry.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub struct GraphicTransition {
7    pub duration_millis: u16,
8}
9impl GraphicTransition {
10    pub fn validate(self) -> Result<(), Error> {
11        if self.duration_millis > 500 {
12            return Err(Error::InvalidProps(
13                "graphic transition duration exceeds 500 milliseconds",
14            ));
15        }
16        Ok(())
17    }
18    pub fn compatible(from: &[PathCommand], to: &[PathCommand]) -> bool {
19        from.len() == to.len()
20            && from.len() <= MAX_GRAPHIC_COMMANDS
21            && from
22                .iter()
23                .zip(to)
24                .all(|(a, b)| std::mem::discriminant(a) == std::mem::discriminant(b))
25    }
26    /// One allocation is useful when interrupting a transition, never per paint.
27    pub fn sample(
28        from: &[PathCommand],
29        to: &[PathCommand],
30        progress: f64,
31    ) -> Result<Vec<PathCommand>, Error> {
32        if !Self::compatible(from, to) {
33            return Err(Error::InvalidProps(
34                "graphic transition requires compatible command topology",
35            ));
36        }
37        from.iter()
38            .zip(to)
39            .map(|(a, b)| Self::command(*a, *b, progress))
40            .collect()
41    }
42    pub fn command(
43        from: PathCommand,
44        to: PathCommand,
45        progress: f64,
46    ) -> Result<PathCommand, Error> {
47        if !progress.is_finite() || !(0.0..=1.0).contains(&progress) {
48            return Err(Error::InvalidProps("invalid graphic transition progress"));
49        }
50        let point = |a: GraphicPoint, b: GraphicPoint| -> Result<GraphicPoint, Error> {
51            if [a.x, a.y, b.x, b.y]
52                .iter()
53                .any(|v| !v.is_finite() || v.abs() > MAX_GRAPHIC_COORDINATE)
54            {
55                return Err(Error::InvalidProps("invalid graphic transition coordinate"));
56            }
57            Ok(GraphicPoint::new(
58                a.x * (1.0 - progress) + b.x * progress,
59                a.y * (1.0 - progress) + b.y * progress,
60            ))
61        };
62        Ok(match (from, to) {
63            (PathCommand::Move(a), PathCommand::Move(b)) => PathCommand::Move(point(a, b)?),
64            (PathCommand::Line(a), PathCommand::Line(b)) => PathCommand::Line(point(a, b)?),
65            (
66                PathCommand::Cubic {
67                    first: a,
68                    second: b,
69                    end: c,
70                },
71                PathCommand::Cubic {
72                    first: x,
73                    second: y,
74                    end: z,
75                },
76            ) => PathCommand::Cubic {
77                first: point(a, x)?,
78                second: point(b, y)?,
79                end: point(c, z)?,
80            },
81            (PathCommand::Close, PathCommand::Close) => PathCommand::Close,
82            _ => {
83                return Err(Error::InvalidProps(
84                    "graphic transition requires compatible command topology",
85                ));
86            }
87        })
88    }
89}