logo
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
use std::{cell::RefCell, fmt, rc::Rc};

use crate::engine::d2::util::{Listener2, Value};

use super::{Behavior, Binding, BindingFunction, EaseFunction, Tween};

struct AnimatedFloatProps {
    pub value: Value<f32>,

    // The behavior that is currently animating the value, or None if the value is not being
    // animated.
    behavior: Option<Rc<dyn Behavior>>,
}

/// A Float value that may be animated over time.

#[derive(Clone)]
pub struct AnimatedFloat {
    props: Rc<RefCell<AnimatedFloatProps>>,
}

impl AnimatedFloat {
    pub fn new(value: f32, listener: Option<Listener2<f32, f32>>) -> Self {
        Self {
            props: Rc::new(RefCell::new(AnimatedFloatProps {
                value: Value::new(value, listener),
                behavior: None,
            })),
        }
    }

    #[inline]
    pub fn get(&self) -> f32 {
        *self.props.borrow().value.get()
    }

    // override
    pub fn set(&self, value: f32) {
        let mut props = self.props.borrow_mut();
        props.behavior = None;
        props.value.set(value);
    }

    pub fn update(&self, dt: f32) {
        let mut props = self.props.borrow_mut();
        let result = props.behavior.as_ref().map(|behavior| {
            let value = behavior.update(dt);
            let is_complete = behavior.is_complete();
            (value, is_complete)
        });

        if let Some((value, is_complete)) = result {
            props.value.set(value);

            if is_complete {
                props.behavior = None;
            }
        }
    }

    /// Animates between the two given values.
    ///  *
    /// @param from The initial value.
    /// @param to The target value.
    /// @param seconds The animation duration, in seconds.
    /// @param easing The easing fn to use, defaults to `Ease.linear`.
    pub fn animate(&self, from: f32, to: f32, seconds: f32, easing: Option<EaseFunction>) {
        self.set(from);
        self.animate_to(to, seconds, easing);
    }

    /// Animates between the current value and the given value.
    ///  *
    /// @param to The target value.
    /// @param seconds The animation duration, in seconds.
    /// @param easing The easing fn to use, defaults to `Ease.linear`.
    pub fn animate_to(&self, to: f32, seconds: f32, easing: Option<EaseFunction>) {
        let mut props = self.props.borrow_mut();
        props.behavior = Some(Rc::new(Tween::new(*props.value.get(), to, seconds, easing)));
    }

    /// Animates the current value by the given delta.
    ///  *
    /// @param by The delta added to the current value to get the target value.
    /// @param seconds The animation duration, in seconds.
    /// @param easing The easing fn to use, defaults to `Ease.linear`.
    pub fn animate_by(&self, by: f32, seconds: f32, easing: Option<EaseFunction>) {
        let mut props = self.props.borrow_mut();
        props.behavior = Some(Rc::new(Tween::new(
            *props.value.get(),
            *props.value.get() + by,
            seconds,
            easing,
        )));
    }

    #[inline]
    pub fn bind_to(&self, to: Value<f32>, function: Option<BindingFunction>) {
        let mut props = self.props.borrow_mut();
        props.behavior = Some(Rc::new(Binding::new(to, function)));
    }

    pub fn set_behavior(&self, behavior: Option<Rc<dyn Behavior>>) {
        let mut props = self.props.borrow_mut();
        props.behavior = behavior;
        self.update(0.0);
    }

    #[inline]
    pub fn behavior(&self) -> Option<Rc<dyn Behavior>> {
        let props = self.props.borrow();
        props.behavior.clone()
    }
}

impl Default for AnimatedFloat {
    fn default() -> Self {
        Self {
            props: Rc::new(RefCell::new(AnimatedFloatProps {
                value: Value::new(0.0, None),
                behavior: None,
            })),
        }
    }
}

// TODO:
impl fmt::Display for AnimatedFloat {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.props.borrow().value.value)
    }
}

impl fmt::Debug for AnimatedFloat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AnimatedFloat")
            //  .field("x", &self.x)
            //  .field("y", &self.y)
            .finish()
    }
}

// TODO: need some way to clone boxed closures
// impl Clone for AnimatedFloat {
//     fn clone(&self) -> Self {
//         Self {
//             inner: Value::new(self.inner.value, None),
//             behavior: None,
//         }
//     }
// }

// impl AsRef<Value<f32>> for AnimatedFloat {
//     fn as_ref(&self) -> &Value<f32> {
//         &self.props.borrow().value
//     }
// }