rive_rs/animation/
keyed_object.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
// 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::{KeyedProperty, LinearAnimation};
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
use crate::dyn_vec::DynVec;
use crate::importers::{ImportStack, LinearAnimationImporter};
use crate::status_code::StatusCode;
use crate::Artboard;

#[derive(Debug, Default)]
pub struct KeyedObject {
    object_id: Property<u64>,
    keyed_properties: DynVec<Object<KeyedProperty>>,
}

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

    pub fn set_object_id(&self, object_id: u64) {
        self.object_id.set(object_id)
    }
}

impl ObjectRef<'_, KeyedObject> {
    pub fn push_keyed_property(&self, keyed_property: Object<KeyedProperty>) {
        self.keyed_properties.push(keyed_property);
    }

    pub fn apply(&self, artboard: Object<Artboard>, time: f32, mix: f32) {
        if let Some(core) = artboard.as_ref().resolve(self.object_id() as usize) {
            for property in self.keyed_properties.iter() {
                property.as_ref().apply(core.clone(), time, mix);
            }
        }
    }
}

impl Core for KeyedObject {
    properties![(51, object_id, set_object_id)];
}

impl OnAdded for ObjectRef<'_, KeyedObject> {
    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
        if context.resolve(self.object_id() as usize).is_none() {
            return StatusCode::MissingObject;
        }

        for property in self.keyed_properties.iter() {
            property.as_ref().on_added_dirty(context);
        }

        StatusCode::Ok
    }

    fn on_added_clean(&self, context: &dyn CoreContext) -> StatusCode {
        for property in self.keyed_properties.iter() {
            property.as_ref().on_added_clean(context);
        }

        StatusCode::Ok
    }

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