summaryrefslogtreecommitdiff
path: root/rust/crates/animation/src/property_groups.rs
blob: fd639905ab76c7c818c5386a948ce5cc3adc8ad6 (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
use scene::{ANIMATION_PROPERTY_PATHS, ElementAnimations};
use time::MediaTime;

use crate::keyframe_query::get_keyframe_at_time;

pub const ANIMATION_PROPERTY_GROUPS: &[(&str, &[&str])] = &[(
    "transform.scale",
    &["transform.scaleX", "transform.scaleY"],
)];

#[derive(Clone, Debug, PartialEq)]
pub struct GroupKeyframeRef {
    pub property_path: &'static str,
    pub keyframe_id: String,
}

pub fn animation_property_group_paths(group: &str) -> Option<&'static [&'static str]> {
    ANIMATION_PROPERTY_GROUPS
        .iter()
        .find(|(name, _)| *name == group)
        .map(|(_, paths)| *paths)
}

pub fn get_group_keyframes_at_time(
    animations: Option<&ElementAnimations>,
    group: &str,
    time: MediaTime,
) -> Vec<GroupKeyframeRef> {
    let Some(paths) = animation_property_group_paths(group) else {
        return Vec::new();
    };
    paths
        .iter()
        .filter_map(|property_path| {
            get_keyframe_at_time(animations, property_path, time).map(|keyframe| {
                GroupKeyframeRef {
                    property_path,
                    keyframe_id: keyframe.id,
                }
            })
        })
        .collect()
}

pub fn has_group_keyframe_at_time(
    animations: Option<&ElementAnimations>,
    group: &str,
    time: MediaTime,
) -> bool {
    !get_group_keyframes_at_time(animations, group, time).is_empty()
}

pub fn is_animation_property_path(property_path: &str) -> bool {
    ANIMATION_PROPERTY_PATHS.contains(&property_path)
}

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

    #[test]
    fn finds_group_keyframes_across_group_paths() {
        let key = |id: &str| ScalarAnimationKey {
            id: id.to_string(),
            time: MediaTime::from_ticks(0),
            value: 1.0,
            left_handle: None,
            right_handle: None,
            segment_to_next: ScalarSegmentType::Linear,
            tangent_mode: TangentMode::Flat,
        };
        let mut animations: ElementAnimations = BTreeMap::new();
        animations.insert(
            "transform.scaleX".to_string(),
            ChannelData::Single(AnimationChannel::Scalar(ScalarChannel {
                keys: vec![key("kx")],
                extrapolation: None,
            })),
        );
        let refs = get_group_keyframes_at_time(
            Some(&animations),
            "transform.scale",
            MediaTime::from_ticks(0),
        );
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].property_path, "transform.scaleX");
        assert!(has_group_keyframe_at_time(
            Some(&animations),
            "transform.scale",
            MediaTime::from_ticks(0)
        ));
        assert!(!has_group_keyframe_at_time(
            Some(&animations),
            "transform.scale",
            MediaTime::from_ticks(10)
        ));
    }

    #[test]
    fn recognizes_animation_property_paths() {
        assert!(is_animation_property_path("opacity"));
        assert!(is_animation_property_path("transform.rotate"));
        assert!(!is_animation_property_path("params.blur"));
    }
}