summaryrefslogtreecommitdiff
path: root/rust/crates/animation/src/values.rs
blob: d16bf16a73d6235c31fa8d4617db73a4c4199429 (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
use scene::ElementAnimations;
use time::MediaTime;

use crate::resolve::resolve_animation_path_value_at_time;
use scene::ParamValue;

pub fn resolve_opacity_at_time(
    base_opacity: f64,
    animations: Option<&ElementAnimations>,
    local_time: MediaTime,
) -> f64 {
    match resolve_animation_path_value_at_time(
        animations,
        "opacity",
        local_time.max(MediaTime::ZERO),
        &ParamValue::Number(base_opacity),
    ) {
        ParamValue::Number(value) => value,
        _ => base_opacity,
    }
}

pub fn resolve_number_at_time(
    base_value: f64,
    animations: Option<&ElementAnimations>,
    property_path: &str,
    local_time: MediaTime,
) -> f64 {
    match resolve_animation_path_value_at_time(
        animations,
        property_path,
        local_time.max(MediaTime::ZERO),
        &ParamValue::Number(base_value),
    ) {
        ParamValue::Number(value) => value,
        _ => base_value,
    }
}

pub fn resolve_color_at_time(
    base_color: &str,
    animations: Option<&ElementAnimations>,
    property_path: &str,
    local_time: MediaTime,
) -> String {
    match resolve_animation_path_value_at_time(
        animations,
        property_path,
        local_time.max(MediaTime::ZERO),
        &ParamValue::String(base_color.to_string()),
    ) {
        ParamValue::String(value) => value,
        _ => base_color.to_string(),
    }
}

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

    #[test]
    fn resolves_opacity_with_animation_and_fallback() {
        let channel = AnimationChannel::Scalar(ScalarChannel {
            keys: vec![ScalarAnimationKey {
                id: "a".to_string(),
                time: MediaTime::from_ticks(0),
                value: 0.25,
                left_handle: None,
                right_handle: None,
                segment_to_next: ScalarSegmentType::Linear,
                tangent_mode: TangentMode::Flat,
            }],
            extrapolation: None,
        });
        let mut animations: ElementAnimations = BTreeMap::new();
        animations.insert("opacity".to_string(), ChannelData::Single(channel));
        assert_eq!(
            resolve_opacity_at_time(1.0, Some(&animations), MediaTime::from_ticks(0)),
            0.25
        );
        assert_eq!(
            resolve_opacity_at_time(1.0, Some(&animations), MediaTime::from_ticks(-5)),
            0.25
        );
        assert_eq!(resolve_opacity_at_time(1.0, None, MediaTime::from_ticks(0)), 1.0);
    }
}