vizia_core/animation/
animation_state.rs

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
use crate::animation::Interpolator;
use hashbrown::HashSet;

use crate::prelude::*;

use super::TimingFunction;

/// A keyframe in an animation state.
#[derive(Debug, Clone)]
pub(crate) struct Keyframe<T: Interpolator> {
    pub time: f32,
    pub value: T,
    pub timing_function: TimingFunction,
}

/// Represents an animation of a property with type `T`.
#[derive(Clone, Debug)]
pub(crate) struct AnimationState<T: Interpolator> {
    /// ID of the animation description.
    pub id: Animation,
    /// The start time of the animation.
    pub start_time: Instant,
    /// The duration of the animation.
    pub duration: Duration,
    /// The delay before the animation starts.
    pub delay: Duration,
    /// List of animation keyframes as (normalized time, value).
    pub keyframes: Vec<Keyframe<T>>,
    /// The output of value of the animation.
    pub output: Option<T>,
    /// Whether the animation should persist after finishing.
    pub persistent: bool,
    /// How far through the animation between 0.0 and 1.0.
    pub t: f32,

    pub dt: f32,

    pub active: bool,

    /// For transitions. The starting rule for this transition.
    pub from_rule: usize,
    /// For tansitions. The ending rule for this transition.
    pub to_rule: usize,

    /// List of entities connected to this animation (used when animation is removed from active list)
    pub entities: HashSet<Entity>,
}

impl<T> AnimationState<T>
where
    T: Interpolator,
{
    /// Create a new animation state with the given [Animation] id.
    pub(crate) fn new(id: Animation) -> Self {
        AnimationState {
            id,
            start_time: Instant::now(),
            duration: Duration::new(0, 0),
            delay: Duration::new(0, 0),
            keyframes: Vec::new(),
            output: None,
            persistent: false,
            t: 0.0,
            dt: 0.0,
            active: false,
            entities: HashSet::new(),
            from_rule: usize::MAX,
            to_rule: usize::MAX,
        }
    }

    pub(crate) fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;

        self
    }

    pub(crate) fn with_delay(mut self, delay: Duration) -> Self {
        self.delay = delay;

        self
    }

    pub(crate) fn with_keyframe(mut self, key: Keyframe<T>) -> Self {
        self.keyframes.push(key);

        self
    }

    pub(crate) fn get_output(&self) -> Option<&T> {
        self.output.as_ref()
    }

    pub(crate) fn play(&mut self, entity: Entity) {
        self.active = true;
        self.t = 0.0;
        self.start_time = Instant::now();
        self.entities.insert(entity);
    }

    pub(crate) fn is_transition(&self) -> bool {
        !(self.from_rule == usize::MAX && self.to_rule == usize::MAX)
    }
}

impl<Prop> Default for AnimationState<Prop>
where
    Prop: Interpolator,
{
    fn default() -> Self {
        AnimationState {
            id: Animation::null(),
            start_time: Instant::now(),
            duration: Duration::new(0, 0),
            delay: Duration::new(0, 0),
            keyframes: Vec::new(),
            output: None,
            persistent: true,
            t: 0.0,
            dt: 0.0,
            active: false,
            entities: HashSet::new(),
            from_rule: usize::MAX,
            to_rule: usize::MAX,
        }
    }
}