summaryrefslogtreecommitdiff
path: root/rust/crates/scene/src/animation.rs
blob: d09dd3de6158d5e3b0e057a7f3b9167d382113bb (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
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use time::MediaTime;

use crate::params::ParamValue;

pub const ANIMATION_PROPERTY_PATHS: &[&str] = &[
    "transform.positionX",
    "transform.positionY",
    "transform.scaleX",
    "transform.scaleY",
    "transform.rotate",
    "opacity",
    "volume",
    "color",
    "background.color",
    "background.paddingX",
    "background.paddingY",
    "background.offsetX",
    "background.offsetY",
    "background.cornerRadius",
];

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ScalarSegmentType {
    Step,
    Linear,
    Bezier,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum TangentMode {
    Auto,
    Aligned,
    Broken,
    Flat,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ChannelExtrapolationMode {
    Hold,
    Linear,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
pub struct CurveHandle {
    pub dt: MediaTime,
    pub dv: f64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ScalarAnimationKey {
    pub id: String,
    pub time: MediaTime,
    pub value: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub left_handle: Option<CurveHandle>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub right_handle: Option<CurveHandle>,
    pub segment_to_next: ScalarSegmentType,
    pub tangent_mode: TangentMode,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum DiscreteValue {
    Bool(bool),
    String(String),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DiscreteAnimationKey {
    pub id: String,
    pub time: MediaTime,
    pub value: DiscreteValue,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ChannelExtrapolation {
    pub before: ChannelExtrapolationMode,
    pub after: ChannelExtrapolationMode,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ScalarChannel {
    pub keys: Vec<ScalarAnimationKey>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extrapolation: Option<ChannelExtrapolation>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct DiscreteChannel {
    pub keys: Vec<DiscreteAnimationKey>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum AnimationChannel {
    Scalar(ScalarChannel),
    Discrete(DiscreteChannel),
}

pub type CompositeChannelData = BTreeMap<String, AnimationChannel>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum ChannelData {
    Single(AnimationChannel),
    Composite(CompositeChannelData),
}

pub type ElementAnimations = BTreeMap<String, ChannelData>;

impl From<DiscreteValue> for ParamValue {
    fn from(value: DiscreteValue) -> Self {
        match value {
            DiscreteValue::Bool(b) => ParamValue::Bool(b),
            DiscreteValue::String(s) => ParamValue::String(s),
        }
    }
}