rive_rs/shapes/
path.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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::component_dirt::ComponentDirt;
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
use crate::dyn_vec::DynVec;
use crate::math::{self, Mat};
use crate::node::Node;
use crate::option_cell::OptionCell;
use crate::shapes::command_path::CommandPathBuilder;
use crate::shapes::{CommandPath, CubicVertex, PathVertex, PointsPath, Shape, StraightVertex};
use crate::status_code::StatusCode;
use crate::{Component, TransformComponent};

#[derive(Debug, Default)]
pub struct Path {
    node: Node,
    path_flags: Property<u64>,
    shape: OptionCell<Object<Shape>>,
    pub(crate) vertices: DynVec<Object<PathVertex>>,
    command_path: OptionCell<CommandPath>,
}

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

    pub fn set_path_flags(&self, path_flags: u64) {
        self.path_flags.set(path_flags);
    }
}

impl ObjectRef<'_, Path> {
    pub fn shape(&self) -> Option<Object<Shape>> {
        self.shape.get()
    }

    pub fn transform(&self) -> Mat {
        if let Some(points_path) = self.try_cast::<PointsPath>() {
            return points_path.transform();
        }

        self.cast::<TransformComponent>().world_transform()
    }

    pub fn push_vertex(&self, path_vertex: Object<PathVertex>) {
        self.vertices.push(path_vertex);
    }

    pub(crate) fn vertices(&self) -> impl Iterator<Item = Object<PathVertex>> + '_ {
        self.vertices.iter()
    }

    pub(crate) fn with_command_path(&self, f: impl FnMut(Option<&CommandPath>)) {
        self.command_path.with(f);
    }

    pub fn mark_path_dirty(&self) {
        if let Some(points_path) = self.try_cast::<PointsPath>() {
            points_path.mark_path_dirty();
        }

        self.cast::<Component>().add_dirt(ComponentDirt::PATH, false);

        if let Some(shape) = self.shape() {
            shape.as_ref().path_changed();
        }
    }

    pub fn is_path_closed(&self) -> bool {
        if let Some(points_path) = self.try_cast::<PointsPath>() {
            return points_path.is_path_closed();
        }

        true
    }

    pub fn build_dependencies(&self) {
        self.cast::<TransformComponent>().build_dependencies();
    }

    pub fn on_dirty(&self, dirt: ComponentDirt) {
        if let Some(shape) = self.shape() {
            if Component::value_has_dirt(dirt, ComponentDirt::WORLD_TRANSFORM) {
                shape.as_ref().path_changed();
            }
        }
    }

    pub fn update(&self, value: ComponentDirt) {
        self.cast::<TransformComponent>().update(value);

        if Component::value_has_dirt(value, ComponentDirt::PATH) {
            self.command_path.set(Some(self.build_path()));
        }
    }

    fn build_path(&self) -> CommandPath {
        let mut builder = CommandPathBuilder::new();

        if self.vertices.len() < 2 {
            return builder.build();
        }

        enum Dir {
            In,
            Out,
        }

        fn path_vertex_get_or_translation(
            path_vertex: ObjectRef<'_, PathVertex>,
            dir: Dir,
        ) -> math::Vec {
            path_vertex
                .try_cast::<CubicVertex>()
                .map(|cubic| match dir {
                    Dir::In => cubic.render_in(),
                    Dir::Out => cubic.render_out(),
                })
                .unwrap_or_else(|| path_vertex.render_translation())
        }

        let first_point = self.vertices.index(0);

        let mut out;
        let mut prev_is_cubic;

        let start;
        let start_in;
        let start_is_cubic;

        if let Some(cubic) = first_point.try_cast::<CubicVertex>() {
            let cubic = cubic.as_ref();
            prev_is_cubic = true;
            start_is_cubic = true;

            start_in = cubic.render_in();
            out = cubic.render_out();
            start = cubic.cast::<PathVertex>().render_translation();

            builder.move_to(start);
        } else {
            prev_is_cubic = false;
            start_is_cubic = false;

            let point = first_point.cast::<StraightVertex>();
            let point = point.as_ref();
            let radius = point.radius();

            if radius > 0.0 {
                let prev = self.vertices.index(self.vertices.len() - 1);

                let pos = point.cast::<PathVertex>().render_translation();
                let to_prev = path_vertex_get_or_translation(prev.as_ref(), Dir::Out) - pos;
                let to_prev_length = to_prev.length();
                let to_prev = to_prev * to_prev_length.recip();

                let next = self.vertices.index(1);
                let to_next = path_vertex_get_or_translation(next.as_ref(), Dir::In) - pos;
                let to_next_length = to_next.length();
                let to_next = to_next * to_next_length.recip();

                let render_radius = to_prev_length.min(to_next_length.min(radius));

                let translation = pos + to_prev * render_radius;

                start = translation;
                start_in = translation;

                builder.move_to(translation);

                let out_point = pos + to_prev * (1.0 - math::CIRCLE_CONSTANT) * render_radius;
                let in_point = pos + to_next * (1.0 - math::CIRCLE_CONSTANT) * render_radius;

                out = pos + to_next * render_radius;

                builder.cubic_to(out_point, in_point, out);
            } else {
                let translation = point.cast::<PathVertex>().render_translation();

                start = translation;
                start_in = translation;
                out = translation;

                builder.move_to(translation);
            }
        }

        for (i, vertex) in self.vertices.iter().enumerate().skip(1) {
            let vertex = vertex.as_ref();

            if let Some(cubic) = vertex.try_cast::<CubicVertex>() {
                let in_point = cubic.render_in();
                let translation = vertex.render_translation();

                builder.cubic_to(out, in_point, translation);

                prev_is_cubic = true;
                out = cubic.render_out();
            } else {
                let point = vertex.cast::<StraightVertex>();
                let pos = vertex.render_translation();
                let radius = point.radius();

                if radius > 0.0 {
                    let to_prev = out - pos;
                    let to_prev_length = to_prev.length();
                    let to_prev = to_prev * to_prev_length.recip();

                    let next = self.vertices.index((i + 1) % self.vertices.len());
                    let to_next = path_vertex_get_or_translation(next.as_ref(), Dir::In) - pos;
                    let to_next_length = to_next.length();
                    let to_next = to_next * to_next_length.recip();

                    let render_radius = to_prev_length.min(to_next_length.min(radius));

                    let translation = pos + to_prev * render_radius;

                    if prev_is_cubic {
                        builder.cubic_to(out, translation, translation);
                    } else {
                        builder.line_to(translation);
                    }

                    let out_point = pos + to_prev * (1.0 - math::CIRCLE_CONSTANT) * render_radius;
                    let in_point = pos + to_next * (1.0 - math::CIRCLE_CONSTANT) * render_radius;

                    out = pos + to_next * render_radius;

                    builder.cubic_to(out_point, in_point, out);

                    prev_is_cubic = false;
                } else if prev_is_cubic {
                    builder.cubic_to(out, pos, pos);

                    prev_is_cubic = false;
                    out = pos;
                } else {
                    builder.line_to(pos);

                    out = pos;
                }
            }
        }

        if self.is_path_closed() {
            if prev_is_cubic || start_is_cubic {
                builder.cubic_to(out, start_in, start);
            }

            builder.close();
        }

        builder.build()
    }
}

impl Core for Path {
    parent_types![(node, Node)];

    properties![(128, path_flags, set_path_flags), node];
}

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

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

        for parent in self.cast::<Component>().parents() {
            if let Some(shape) = parent.try_cast::<Shape>() {
                self.shape.set(Some(shape.clone()));
                shape.as_ref().push_path(self.as_object());
                return StatusCode::Ok;
            }
        }

        StatusCode::MissingObject
    }
}