rive_rs/animation/
linear_animation.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
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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::any::TypeId;

use crate::animation::{Animation, KeyedObject, Loop};
use crate::artboard::Artboard;
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
use crate::dyn_vec::DynVec;
use crate::importers::{ArtboardImporter, ImportStack};
use crate::status_code::StatusCode;

#[derive(Debug)]
pub struct LinearAnimation {
    animation: Animation,
    fps: Property<u64>,
    duration: Property<u64>,
    speed: Property<f32>,
    r#loop: Property<Loop>,
    work_start: Property<u64>,
    work_end: Property<u64>,
    enable_work_area: Property<bool>,
    keyed_objects: DynVec<Object<KeyedObject>>,
}

impl ObjectRef<'_, LinearAnimation> {
    pub fn fps(&self) -> u64 {
        self.fps.get()
    }

    pub fn set_fps(&self, fps: u64) {
        self.fps.set(fps);
    }

    pub fn duration(&self) -> u64 {
        self.duration.get()
    }

    pub fn set_duration(&self, duration: u64) {
        self.duration.set(duration);
    }

    pub fn speed(&self) -> f32 {
        self.speed.get()
    }

    pub fn set_speed(&self, speed: f32) {
        self.speed.set(speed);
    }

    pub fn r#loop(&self) -> Loop {
        self.r#loop.get()
    }

    pub fn set_loop(&self, r#loop: Loop) {
        self.r#loop.set(r#loop);
    }

    pub fn work_start(&self) -> u64 {
        self.work_start.get()
    }

    pub fn set_work_start(&self, work_start: u64) {
        self.work_start.set(work_start);
    }

    pub fn work_end(&self) -> u64 {
        self.work_end.get()
    }

    pub fn set_work_end(&self, work_end: u64) {
        self.work_end.set(work_end);
    }

    pub fn enable_work_area(&self) -> bool {
        self.enable_work_area.get()
    }

    pub fn set_enable_work_area(&self, enable_work_area: bool) {
        self.enable_work_area.set(enable_work_area);
    }
}

impl ObjectRef<'_, LinearAnimation> {
    pub fn push_keyed_object(&self, keyed_object: Object<KeyedObject>) {
        self.keyed_objects.push(keyed_object);
    }

    pub fn apply(&self, artboard: Object<Artboard>, time: f32, mix: f32) {
        for object in self.keyed_objects.iter() {
            object.as_ref().apply(artboard.clone(), time, mix);
        }
    }
}

impl Core for LinearAnimation {
    parent_types![(animation, Animation)];

    properties![
        (56, fps, set_fps),
        (57, duration, set_duration),
        (58, speed, set_speed),
        (59, r#loop, set_loop),
        (60, work_start, set_work_start),
        (61, work_end, set_work_end),
        (62, enable_work_area, set_enable_work_area),
        animation,
    ];
}

impl OnAdded for ObjectRef<'_, LinearAnimation> {
    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
        for object in self.keyed_objects.iter() {
            let code = object.as_ref().on_added_dirty(context);
            if code != StatusCode::Ok {
                return code;
            }
        }

        StatusCode::Ok
    }

    fn on_added_clean(&self, context: &dyn CoreContext) -> StatusCode {
        for object in self.keyed_objects.iter() {
            let code = object.as_ref().on_added_dirty(context);
            if code != StatusCode::Ok {
                return code;
            }
        }

        StatusCode::Ok
    }

    fn import(&self, object: Object, import_stack: &ImportStack) -> StatusCode {
        if let Some(importer) = import_stack.latest::<ArtboardImporter>(TypeId::of::<Artboard>()) {
            importer.push_animation(object.as_ref().cast::<LinearAnimation>().as_object());
            self.cast::<Animation>().import(object, import_stack)
        } else {
            StatusCode::MissingObject
        }
    }
}

impl Default for LinearAnimation {
    fn default() -> Self {
        Self {
            animation: Animation::default(),
            fps: Property::new(60),
            duration: Property::new(60),
            speed: Property::new(1.0),
            r#loop: Property::new(Loop::default()),
            work_start: Property::new(0),
            work_end: Property::new(0),
            enable_work_area: Property::new(false),
            keyed_objects: DynVec::new(),
        }
    }
}