summaryrefslogtreecommitdiff
path: root/rust/crates/animation/src/resolve.rs
blob: ccb9dbadb67b93aeea78af6de36c786e1d4f7691 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use scene::{ChannelData, ElementAnimations, ParamValue};
use time::MediaTime;

use crate::color::{LinearRgba, format_linear_rgba, parse_color_to_linear_rgba};
use crate::interpolation::channel_value_at_time;

pub fn get_element_local_time(
    timeline_time: MediaTime,
    element_start_time: MediaTime,
    element_duration: MediaTime,
) -> MediaTime {
    let local_time = timeline_time - element_start_time;
    if local_time <= MediaTime::ZERO {
        return MediaTime::ZERO;
    }
    if local_time >= element_duration {
        return element_duration;
    }
    local_time
}

pub fn resolve_animation_path_value_at_time(
    animations: Option<&ElementAnimations>,
    property_path: &str,
    local_time: MediaTime,
    fallback_value: &ParamValue,
) -> ParamValue {
    let Some(data) = animations.and_then(|a| a.get(property_path)) else {
        return fallback_value.clone();
    };

    match data {
        ChannelData::Single(channel) => match channel_value_at_time(
            Some(channel),
            local_time,
            fallback_value,
        ) {
            value => value,
        },
        ChannelData::Composite(components) => {
            let ParamValue::String(fallback_color) = fallback_value else {
                return fallback_value.clone();
            };
            let (Some(r), Some(g), Some(b), Some(a)) = (
                components.get("r"),
                components.get("g"),
                components.get("b"),
                components.get("a"),
            ) else {
                return fallback_value.clone();
            };
            let Some(fallback_components) = parse_color_to_linear_rgba(fallback_color) else {
                return fallback_value.clone();
            };

            let component_at = |channel: &scene::AnimationChannel, fallback: f64| {
                match channel_value_at_time(
                    Some(channel),
                    local_time,
                    &ParamValue::Number(fallback),
                ) {
                    ParamValue::Number(value) => value,
                    _ => fallback,
                }
            };
            ParamValue::String(format_linear_rgba(&LinearRgba {
                r: component_at(r, fallback_components.r),
                g: component_at(g, fallback_components.g),
                b: component_at(b, fallback_components.b),
                a: component_at(a, fallback_components.a),
            }))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scene::{AnimationChannel, ScalarAnimationKey, ScalarChannel, ScalarSegmentType, TangentMode};
    use std::collections::BTreeMap;

    fn at(time: i64) -> MediaTime {
        MediaTime::from_ticks(time)
    }

    #[test]
    fn local_time_clamps_to_element_range() {
        assert_eq!(get_element_local_time(at(50), at(100), at(200)), at(0));
        assert_eq!(get_element_local_time(at(150), at(100), at(200)), at(50));
        assert_eq!(get_element_local_time(at(400), at(100), at(200)), at(200));
    }

    #[test]
    fn resolves_scalar_path_value() {
        let channel = AnimationChannel::Scalar(ScalarChannel {
            keys: vec![
                ScalarAnimationKey {
                    id: "a".to_string(),
                    time: at(0),
                    value: 0.0,
                    left_handle: None,
                    right_handle: None,
                    segment_to_next: ScalarSegmentType::Linear,
                    tangent_mode: TangentMode::Flat,
                },
                ScalarAnimationKey {
                    id: "b".to_string(),
                    time: at(100),
                    value: 1.0,
                    left_handle: None,
                    right_handle: None,
                    segment_to_next: ScalarSegmentType::Linear,
                    tangent_mode: TangentMode::Flat,
                },
            ],
            extrapolation: None,
        });
        let mut animations = BTreeMap::new();
        animations.insert("opacity".to_string(), ChannelData::Single(channel));
        assert_eq!(
            resolve_animation_path_value_at_time(
                Some(&animations),
                "opacity",
                at(50),
                &ParamValue::Number(1.0),
            ),
            ParamValue::Number(0.5)
        );
        assert_eq!(
            resolve_animation_path_value_at_time(
                Some(&animations),
                "volume",
                at(50),
                &ParamValue::Number(1.0),
            ),
            ParamValue::Number(1.0)
        );
    }

    #[test]
    fn resolves_composite_color_path_value() {
        let component = |value: f64| {
            AnimationChannel::Scalar(ScalarChannel {
                keys: vec![ScalarAnimationKey {
                    id: "k".to_string(),
                    time: at(0),
                    value,
                    left_handle: None,
                    right_handle: None,
                    segment_to_next: ScalarSegmentType::Linear,
                    tangent_mode: TangentMode::Flat,
                }],
                extrapolation: None,
            })
        };
        let mut components = BTreeMap::new();
        components.insert("r".to_string(), component(1.0));
        components.insert("g".to_string(), component(0.0));
        components.insert("b".to_string(), component(0.0));
        components.insert("a".to_string(), component(1.0));
        let mut animations = BTreeMap::new();
        animations.insert("color".to_string(), ChannelData::Composite(components));
        assert_eq!(
            resolve_animation_path_value_at_time(
                Some(&animations),
                "color",
                at(0),
                &ParamValue::String("#ffffff".to_string()),
            ),
            ParamValue::String("#ff0000".to_string())
        );
        assert_eq!(
            resolve_animation_path_value_at_time(
                Some(&animations),
                "color",
                at(0),
                &ParamValue::Number(0.0),
            ),
            ParamValue::Number(0.0)
        );
    }
}