rive_rs/shapes/
cubic_asymmetric_vertex.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::core::{Core, ObjectRef, OnAdded, Property};
6use crate::math;
7use crate::shapes::{CubicVertex, PathVertex};
8
9#[derive(Debug, Default)]
10pub struct CubicAsymmetricVertex {
11    cubic_vertex: CubicVertex,
12    rotation: Property<f32>,
13    in_distance: Property<f32>,
14    out_distance: Property<f32>,
15}
16
17impl ObjectRef<'_, CubicAsymmetricVertex> {
18    pub fn rotation(&self) -> f32 {
19        self.rotation.get()
20    }
21
22    pub fn set_rotation(&self, rotation: f32) {
23        if self.rotation() == rotation {
24            return;
25        }
26
27        self.rotation.set(rotation);
28
29        self.cast::<CubicVertex>().invalidate_in();
30        self.cast::<CubicVertex>().invalidate_out();
31        self.cast::<PathVertex>().mark_path_dirty();
32    }
33
34    pub fn in_distance(&self) -> f32 {
35        self.in_distance.get()
36    }
37
38    pub fn set_in_distance(&self, in_distance: f32) {
39        if self.in_distance() == in_distance {
40            return;
41        }
42
43        self.in_distance.set(in_distance);
44
45        self.cast::<CubicVertex>().invalidate_in();
46        self.cast::<PathVertex>().mark_path_dirty();
47    }
48
49    pub fn out_distance(&self) -> f32 {
50        self.out_distance.get()
51    }
52
53    pub fn set_out_distance(&self, out_distance: f32) {
54        if self.out_distance() == out_distance {
55            return;
56        }
57
58        self.out_distance.set(out_distance);
59
60        self.cast::<CubicVertex>().invalidate_out();
61        self.cast::<PathVertex>().mark_path_dirty();
62    }
63}
64
65impl ObjectRef<'_, CubicAsymmetricVertex> {
66    pub fn compute_in(&self) {
67        let pos = math::Vec::new(self.cast::<PathVertex>().x(), self.cast::<PathVertex>().y());
68        let diff = math::Vec::new(
69            self.rotation().cos() * -self.in_distance(),
70            self.rotation().sin() * -self.in_distance(),
71        );
72
73        self.cast::<CubicVertex>().set_in_point(pos + diff);
74    }
75
76    pub fn compute_out(&self) {
77        let pos = math::Vec::new(self.cast::<PathVertex>().x(), self.cast::<PathVertex>().y());
78        let diff = math::Vec::new(
79            self.rotation().cos() * self.out_distance(),
80            self.rotation().sin() * self.out_distance(),
81        );
82
83        self.cast::<CubicVertex>().set_out_point(pos + diff);
84    }
85}
86
87impl Core for CubicAsymmetricVertex {
88    parent_types![(cubic_vertex, CubicVertex)];
89
90    properties![
91        (79, rotation, set_rotation),
92        (80, in_distance, set_in_distance),
93        (81, out_distance, set_out_distance),
94        cubic_vertex,
95    ];
96}
97
98impl OnAdded for ObjectRef<'_, CubicAsymmetricVertex> {
99    on_added!(CubicVertex);
100}