rive_rs/shapes/
points_path.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::{Skin, Skinnable};
6use crate::component::Component;
7use crate::component_dirt::ComponentDirt;
8use crate::core::{Core, Object, ObjectRef, OnAdded, Property};
9use crate::math::Mat;
10use crate::option_cell::OptionCell;
11use crate::shapes::path::Path;
12use crate::transform_component::TransformComponent;
13
14#[derive(Debug, Default)]
15pub struct PointsPath {
16    path: Path,
17    is_closed: Property<bool>,
18    skin: OptionCell<Object<Skin>>,
19}
20
21impl ObjectRef<'_, PointsPath> {
22    pub fn is_closed(&self) -> bool {
23        self.is_closed.get()
24    }
25
26    pub fn set_is_closed(&self, is_closed: bool) {
27        self.is_closed.set(is_closed);
28    }
29}
30
31impl ObjectRef<'_, PointsPath> {
32    pub fn transform(&self) -> Mat {
33        if self.skin.get().is_some() {
34            Mat::default()
35        } else {
36            self.cast::<TransformComponent>().world_transform()
37        }
38    }
39
40    pub fn mark_path_dirty(&self) {
41        if let Some(skin) = self.skin.get() {
42            skin.as_ref().cast::<Component>().add_dirt(ComponentDirt::PATH, false);
43        }
44    }
45
46    pub fn is_path_closed(&self) -> bool {
47        self.is_closed()
48    }
49
50    pub fn build_dependencies(&self) {
51        self.cast::<Path>().build_dependencies();
52
53        if let Some(skin) = self.skin.get() {
54            skin.as_ref().cast::<Component>().push_dependent(self.as_object().cast());
55        }
56    }
57
58    pub fn update(&self, value: ComponentDirt) {
59        let path = self.cast::<Path>();
60
61        if Component::value_has_dirt(value, ComponentDirt::PATH) {
62            if let Some(skin) = self.skin.get() {
63                skin.as_ref().deform(path.vertices());
64            }
65        }
66
67        path.update(value);
68    }
69}
70
71impl Skinnable for ObjectRef<'_, PointsPath> {
72    fn skin(&self) -> Option<Object<Skin>> {
73        self.skin.get()
74    }
75
76    fn set_skin(&self, skin: Object<Skin>) {
77        self.skin.set(Some(skin));
78    }
79
80    fn mark_skin_dirty(&self) {
81        self.cast::<Path>().mark_path_dirty();
82    }
83}
84
85impl Core for PointsPath {
86    parent_types![(path, Path)];
87
88    properties![(32, is_closed, set_is_closed), path];
89}
90
91impl OnAdded for ObjectRef<'_, PointsPath> {
92    on_added!(Path);
93}