rive_rs/animation/
keyed_object.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::any::TypeId;
6
7use crate::animation::{KeyedProperty, LinearAnimation};
8use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
9use crate::dyn_vec::DynVec;
10use crate::importers::{ImportStack, LinearAnimationImporter};
11use crate::status_code::StatusCode;
12use crate::Artboard;
13
14#[derive(Debug, Default)]
15pub struct KeyedObject {
16    object_id: Property<u64>,
17    keyed_properties: DynVec<Object<KeyedProperty>>,
18}
19
20impl ObjectRef<'_, KeyedObject> {
21    pub fn object_id(&self) -> u64 {
22        self.object_id.get()
23    }
24
25    pub fn set_object_id(&self, object_id: u64) {
26        self.object_id.set(object_id)
27    }
28}
29
30impl ObjectRef<'_, KeyedObject> {
31    pub fn push_keyed_property(&self, keyed_property: Object<KeyedProperty>) {
32        self.keyed_properties.push(keyed_property);
33    }
34
35    pub fn apply(&self, artboard: Object<Artboard>, time: f32, mix: f32) {
36        if let Some(core) = artboard.as_ref().resolve(self.object_id() as usize) {
37            for property in self.keyed_properties.iter() {
38                property.as_ref().apply(core.clone(), time, mix);
39            }
40        }
41    }
42}
43
44impl Core for KeyedObject {
45    properties![(51, object_id, set_object_id)];
46}
47
48impl OnAdded for ObjectRef<'_, KeyedObject> {
49    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
50        if context.resolve(self.object_id() as usize).is_none() {
51            return StatusCode::MissingObject;
52        }
53
54        for property in self.keyed_properties.iter() {
55            property.as_ref().on_added_dirty(context);
56        }
57
58        StatusCode::Ok
59    }
60
61    fn on_added_clean(&self, context: &dyn CoreContext) -> StatusCode {
62        for property in self.keyed_properties.iter() {
63            property.as_ref().on_added_clean(context);
64        }
65
66        StatusCode::Ok
67    }
68
69    fn import(&self, object: Object, import_stack: &ImportStack) -> StatusCode {
70        if let Some(importer) =
71            import_stack.latest::<LinearAnimationImporter>(TypeId::of::<LinearAnimation>())
72        {
73            importer.push_keyed_object(object.as_ref().cast::<KeyedObject>().as_object());
74            StatusCode::Ok
75        } else {
76            StatusCode::MissingObject
77        }
78    }
79}