rive_rs/bones/
root_bone.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
// 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 crate::bones::Bone;
use crate::core::{Core, CoreContext, ObjectRef, OnAdded, Property};
use crate::status_code::StatusCode;
use crate::transform_component::TransformComponent;

#[derive(Debug, Default)]
pub struct RootBone {
    bone: Bone,
    x: Property<f32>,
    y: Property<f32>,
}

impl ObjectRef<'_, RootBone> {
    pub fn x(&self) -> f32 {
        self.x.get()
    }

    pub fn set_x(&self, x: f32) {
        if self.x() == x {
            return;
        }

        self.x.set(x);
        self.cast::<TransformComponent>().mark_transform_dirty();
    }

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

    pub fn set_y(&self, y: f32) {
        if self.y() == y {
            return;
        }

        self.y.set(y);
        self.cast::<TransformComponent>().mark_transform_dirty();
    }
}

impl Core for RootBone {
    parent_types![(bone, Bone)];

    properties![(90, x, set_x), (91, y, set_y), bone];
}

impl OnAdded for ObjectRef<'_, RootBone> {
    on_added!([on_added_dirty, import], Bone);

    fn on_added_clean(&self, context: &dyn CoreContext) -> StatusCode {
        // Intentionally doesn't call Bone::on_added_clean and goes straight to
        // the super.super TransformComponent as that assumes the parent must be a
        // Bone while a root bone is a special case Bone that can be parented to
        // other TransformComponents.
        self.cast::<TransformComponent>().on_added_clean(context)
    }
}