Skip to main content

euv_engine/tween/
struct.rs

1use super::*;
2
3/// A generic tween interpolating a value of type `T` from a start to an end
4/// value over a fixed duration, driven by an [`Easing`] curve.
5///
6/// Works with any type implementing [`Interpolable`] — the engine provides
7/// implementations for `f64`, [`Vector2D`], [`Vector3D`], and [`Color`].
8///
9/// ## Why hand-written accessors
10///
11/// Lombok's `Data` derive is intentionally **not** applied here for the same
12/// reason as [`EngineCell`]: the derive does not propagate generic bounds, so
13/// deriving on `Tween<T>` would force `T: Default`-style bounds that
14/// `Interpolable` types do not carry. The accessor pairs below follow the
15/// same naming contract as the Lombok-generated ones (`get_*` / `set_*`).
16pub struct Tween<T: Interpolable + Copy> {
17    /// The value at the start of the tween.
18    pub(crate) from: T,
19    /// The value at the end of the tween.
20    pub(crate) to: T,
21    /// The total interpolation duration in seconds.
22    pub(crate) duration: f64,
23    /// The easing curve applied to the normalized time.
24    pub(crate) easing: Easing,
25    /// The start delay in seconds before interpolation begins.
26    pub(crate) delay: f64,
27    /// The time elapsed since creation (including the delay phase).
28    pub(crate) elapsed: f64,
29    /// The current playback state.
30    pub(crate) state: TweenState,
31    /// What happens when the tween reaches the end of its duration.
32    pub(crate) mode: AnimationMode,
33    /// The current playback direction (1.0 = forward, -1.0 = backward) for ping-pong mode.
34    pub(crate) direction: f64,
35    /// An optional callback fired when the tween completes a cycle.
36    pub(crate) on_complete: Option<Rc<dyn Fn()>>,
37}
38
39impl<T: Interpolable + Copy> Clone for Tween<T> {
40    fn clone(&self) -> Tween<T> {
41        Tween {
42            from: self.from,
43            to: self.to,
44            duration: self.duration,
45            easing: self.easing,
46            delay: self.delay,
47            elapsed: self.elapsed,
48            state: self.state,
49            mode: self.mode,
50            direction: self.direction,
51            on_complete: self.on_complete.clone(),
52        }
53    }
54}
55
56impl<T: Interpolable + Copy + Debug> Debug for Tween<T> {
57    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        formatter
59            .debug_struct("Tween")
60            .field("from", &self.from)
61            .field("to", &self.to)
62            .field("duration", &self.duration)
63            .field("easing", &self.easing)
64            .field("delay", &self.delay)
65            .field("elapsed", &self.elapsed)
66            .field("state", &self.state)
67            .field("mode", &self.mode)
68            .field("direction", &self.direction)
69            .finish_non_exhaustive()
70    }
71}