1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! # Timing functions for animations:
//! * `Easing`: a quadratic bezier curve timing defined by two points and a duration
//! * `Spring`: a physics-based spring timing, emulating a dumped harmonic oscillator
//!
//! # Helper functions:
//! * `Transition::ease_in(duration: f32) -> Transition`
//! * `Transition::ease_out(duration: f32) -> Transition`
//! * `Transition::ease_in_out(duration: f32) -> Transition`
//! * `Transition::ease_out_quad(duration: f32) -> Transition`
//! * `Transition::ease_in_quad(duration: f32) -> Transition`
//! * `Transition::ease_in_out_quad(duration: f32) -> Transition`
//! * `Transition::spring(duration: f32, bounce: f32) -> Transition`
//! * `Transition::spring_with_velocity(duration: f32, bounce: f32, velocity: f32) -> Transition`
//!   ...
//!
//! # Usage
//! ```rust
//! // use default easing function
//! layer.set_position((100.0, 100.0), Transition::ease_in(0.3));
//!
//! // spring helper with perceptual duration and bounce
//! layer.set_position((100.0, 100.0), Transition::spring(0.3, 0.3));
//!
//! // spring helper with perceptual duration and initial velocity
//! layer.set_position((100.0, 100.0), Transition::spring_with_velocity(0.3, 0.3, 0.3));
//! ```
//!
//! # Advanced Easing usage
//! ```rust
//! // use custom set a delay and easing function
//! layer.set_position((100.0, 100.0), Transition {
//!     delay: 0.1,
//!     timing: TimingFunction::ease_in_out_quad(1.0)
//! });
//! // use predefined easing functions
//! layer.set_position((100.0, 100.0),  Transition {
//!     delay: 0.0,
//!     timing: TimingFunction::Easing(Easing::ease_in_out_quad(), 0.3)
//! });
//!
//! // access the easing parameters directly
//! layer.set_position((100.0, 100.0), Transition {
//!    delay: 0.0,
//!    timing: TimingFunction::Easing(Easing{x1: 0.0, y1: 0.0, x2: 1.0, y2: 1.0}, 0.3)
//! });
//! ```
//!

use core::fmt;

mod easing;
mod spring;
mod timing;

pub use self::timing::TimingFunction;

pub use easing::Easing;
pub use spring::Spring;

/// Transition is a data structure that contains the information needed to
/// create an animation that can start at a later time.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Transition {
    // pub duration: f32,
    pub delay: f32,
    // easing
    pub timing: TimingFunction,
}
/// Default Transition, 0.3 seconds, no delay, ease out
impl Default for Transition {
    fn default() -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::Easing(Easing::default(), 0.3),
        }
    }
}

impl Transition {
    pub fn linear(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::linear(duration),
        }
    }
    pub fn ease_in(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_in(duration),
        }
    }
    pub fn ease_out(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_out(duration),
        }
    }
    pub fn ease_in_out(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_in_out(duration),
        }
    }
    pub fn ease_out_quad(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_out_quad(duration),
        }
    }
    pub fn ease_in_quad(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_in_quad(duration),
        }
    }
    pub fn ease_in_out_quad(duration: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::ease_in_out_quad(duration),
        }
    }
    pub fn spring(duration: f32, bounce: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::spring(duration, bounce),
        }
    }
    pub fn spring_with_velocity(duration: f32, bounce: f32, velocity: f32) -> Self {
        Transition {
            delay: 0.0,
            timing: TimingFunction::spring_with_initial_velocity(duration, bounce, velocity),
        }
    }
}
/// Animation is a data structure that contains the information needed to
/// animate a property.
#[derive(Clone, Default)]
pub struct Animation {
    pub start: f32,
    // pub duration: f32,
    pub timing: TimingFunction,
}

// getter for Animation value
impl Animation {
    pub fn update_at(&mut self, current_time: f32) -> (f32, f32) {
        let Animation {
            start,
            // duration,
            timing,
        } = self;
        let elapsed = current_time - *start;
        // t = t.clamp(0.0, 1.0);
        // println!("[{} / {}] ({}, {})", time, duration, t, timing.value_at(t));
        timing.update_at(elapsed)
    }
    pub fn done(&self, current_time: f32) -> bool {
        self.timing.done(self.start, current_time)
    }
}

impl fmt::Debug for Animation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "([{:.5}]->{:?})", self.start, self.timing)
    }
}