rive_rs/shapes/
path_vertex.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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::cell::Cell;

use crate::bones::Weight;
use crate::container_component::ContainerComponent;
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
use crate::math::{self, Mat};
use crate::option_cell::OptionCell;
use crate::shapes::Path;
use crate::status_code::StatusCode;
use crate::Component;

use super::CubicVertex;

#[derive(Debug, Default)]
pub struct PathVertex {
    container_component: ContainerComponent,
    x: Property<f32>,
    y: Property<f32>,
    weight: OptionCell<Object<Weight>>,
}

impl ObjectRef<'_, PathVertex> {
    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.mark_path_dirty();

        if let Some(cubic_vertex) = self.try_cast::<CubicVertex>() {
            cubic_vertex.invalidate_in();
            cubic_vertex.invalidate_out();
        }
    }

    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.mark_path_dirty();

        if let Some(cubic_vertex) = self.try_cast::<CubicVertex>() {
            cubic_vertex.invalidate_in();
            cubic_vertex.invalidate_out();
        }
    }
}

impl ObjectRef<'_, PathVertex> {
    fn parent_path(&self) -> Option<Object<Path>> {
        self.cast::<Component>().parent().and_then(|parent| parent.try_cast::<Path>())
    }

    pub(crate) fn mark_path_dirty(&self) {
        if let Some(path) = self.parent_path() {
            path.as_ref().mark_path_dirty();
        }
    }

    pub fn weight(&self) -> Option<Object<Weight>> {
        self.weight.get()
    }

    pub(crate) fn set_weight(&self, weight: Object<Weight>) {
        self.weight.set(Some(weight));
    }

    pub fn deform(&self, world_transform: Mat, bone_transforms: &[Cell<Mat>]) {
        if let Some(weight) = self.weight() {
            let weight = weight.as_ref();
            weight.set_translation(Weight::deform(
                self.x(),
                self.y(),
                weight.indices() as usize,
                weight.values() as usize,
                world_transform,
                bone_transforms,
            ));
        }

        if let Some(cubic_vertex) = self.try_cast::<CubicVertex>() {
            cubic_vertex.deform(world_transform, bone_transforms);
        }
    }

    pub fn render_translation(&self) -> math::Vec {
        self.weight()
            .map(|weight| weight.as_ref().translation())
            .unwrap_or_else(|| math::Vec::new(self.x(), self.y()))
    }
}

impl Core for PathVertex {
    parent_types![(container_component, ContainerComponent)];

    properties![(24, x, set_x), (25, y, set_y), container_component];
}

impl OnAdded for ObjectRef<'_, PathVertex> {
    on_added!([on_added_clean, import], ContainerComponent);

    fn on_added_dirty(&self, context: &dyn CoreContext) -> StatusCode {
        let code = self.cast::<ContainerComponent>().on_added_dirty(context);
        if code != StatusCode::Ok {
            return code;
        }

        if let Some(path) = self.parent_path() {
            path.as_ref().push_vertex(self.as_object());
            StatusCode::Ok
        } else {
            StatusCode::MissingObject
        }
    }
}