Skip to main content

argui_animation/
transition.rs

1use crate::{Interpolate, Motion, MotionValue, PhysicsError, SpringConfig, Tween};
2
3#[derive(Clone, Debug, PartialEq)]
4enum TransitionDriver {
5    Tween(Tween),
6    Spring(SpringConfig),
7}
8
9/// A validated driver used when a presented style retargets.
10#[derive(Clone, Debug, PartialEq)]
11pub struct Transition(TransitionDriver);
12
13impl Transition {
14    #[must_use]
15    pub const fn tween(tween: Tween) -> Self {
16        Self(TransitionDriver::Tween(tween))
17    }
18
19    #[must_use]
20    pub fn spring() -> Self {
21        Self(TransitionDriver::Spring(SpringConfig::default()))
22    }
23
24    pub fn try_spring(config: SpringConfig) -> Result<Self, PhysicsError> {
25        Ok(Self(TransitionDriver::Spring(config.validate()?)))
26    }
27
28    #[doc(hidden)]
29    pub fn retarget<T>(&self, motion: &Motion<T>, target: T)
30    where
31        T: MotionValue + Interpolate,
32    {
33        match &self.0 {
34            TransitionDriver::Tween(tween) => motion.animate_to(target, tween.clone()),
35            TransitionDriver::Spring(config) => {
36                let result = motion.spring_to(target, *config);
37                debug_assert!(
38                    result.is_ok(),
39                    "validated spring configuration must stay valid"
40                );
41            }
42        }
43    }
44}