rive_rs/bones/
root_bone.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 crate::bones::Bone;
6use crate::core::{Core, CoreContext, ObjectRef, OnAdded, Property};
7use crate::status_code::StatusCode;
8use crate::transform_component::TransformComponent;
9
10#[derive(Debug, Default)]
11pub struct RootBone {
12    bone: Bone,
13    x: Property<f32>,
14    y: Property<f32>,
15}
16
17impl ObjectRef<'_, RootBone> {
18    pub fn x(&self) -> f32 {
19        self.x.get()
20    }
21
22    pub fn set_x(&self, x: f32) {
23        if self.x() == x {
24            return;
25        }
26
27        self.x.set(x);
28        self.cast::<TransformComponent>().mark_transform_dirty();
29    }
30
31    pub fn y(&self) -> f32 {
32        self.y.get()
33    }
34
35    pub fn set_y(&self, y: f32) {
36        if self.y() == y {
37            return;
38        }
39
40        self.y.set(y);
41        self.cast::<TransformComponent>().mark_transform_dirty();
42    }
43}
44
45impl Core for RootBone {
46    parent_types![(bone, Bone)];
47
48    properties![(90, x, set_x), (91, y, set_y), bone];
49}
50
51impl OnAdded for ObjectRef<'_, RootBone> {
52    on_added!([on_added_dirty, import], Bone);
53
54    fn on_added_clean(&self, context: &dyn CoreContext) -> StatusCode {
55        // Intentionally doesn't call Bone::on_added_clean and goes straight to
56        // the super.super TransformComponent as that assumes the parent must be a
57        // Bone while a root bone is a special case Bone that can be parented to
58        // other TransformComponents.
59        self.cast::<TransformComponent>().on_added_clean(context)
60    }
61}