rive_rs/shapes/
cubic_mirrored_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 CubicMirroredVertex {
11    cubic_vertex: CubicVertex,
12    rotation: Property<f32>,
13    distance: Property<f32>,
14}
15
16impl ObjectRef<'_, CubicMirroredVertex> {
17    pub fn rotation(&self) -> f32 {
18        self.rotation.get()
19    }
20
21    pub fn set_rotation(&self, rotation: f32) {
22        if self.rotation() == rotation {
23            return;
24        }
25
26        self.rotation.set(rotation);
27
28        self.cast::<CubicVertex>().invalidate_in();
29        self.cast::<CubicVertex>().invalidate_out();
30        self.cast::<PathVertex>().mark_path_dirty();
31    }
32
33    pub fn distance(&self) -> f32 {
34        self.distance.get()
35    }
36
37    pub fn set_distance(&self, distance: f32) {
38        if self.distance() == distance {
39            return;
40        }
41
42        self.distance.set(distance);
43
44        self.cast::<CubicVertex>().invalidate_in();
45        self.cast::<CubicVertex>().invalidate_out();
46        self.cast::<PathVertex>().mark_path_dirty();
47    }
48}
49
50impl ObjectRef<'_, CubicMirroredVertex> {
51    pub fn compute_in(&self) {
52        let pos = math::Vec::new(self.cast::<PathVertex>().x(), self.cast::<PathVertex>().y());
53        let diff = math::Vec::new(
54            self.rotation().cos() * -self.distance(),
55            self.rotation().sin() * -self.distance(),
56        );
57
58        self.cast::<CubicVertex>().set_in_point(pos + diff);
59    }
60
61    pub fn compute_out(&self) {
62        let pos = math::Vec::new(self.cast::<PathVertex>().x(), self.cast::<PathVertex>().y());
63        let diff = math::Vec::new(
64            self.rotation().cos() * self.distance(),
65            self.rotation().sin() * self.distance(),
66        );
67
68        self.cast::<CubicVertex>().set_out_point(pos + diff);
69    }
70}
71
72impl Core for CubicMirroredVertex {
73    parent_types![(cubic_vertex, CubicVertex)];
74
75    properties![(82, rotation, set_rotation), (83, distance, set_distance), cubic_vertex];
76}
77
78impl OnAdded for ObjectRef<'_, CubicMirroredVertex> {
79    on_added!(CubicVertex);
80}