rive_rs/shapes/
triangle.rs
1use std::rc::Rc;
6
7use crate::component::Component;
8use crate::component_dirt::ComponentDirt;
9use crate::core::{Core, Object, ObjectRef, OnAdded};
10use crate::shapes::{ParametricPath, Path, PathVertex, StraightVertex};
11
12#[derive(Debug)]
13pub struct Triangle {
14 parametric_path: ParametricPath,
15 vertex1: Rc<StraightVertex>,
16 vertex2: Rc<StraightVertex>,
17 vertex3: Rc<StraightVertex>,
18}
19
20impl ObjectRef<'_, Triangle> {
21 pub fn update(&self, value: ComponentDirt) {
22 if Component::value_has_dirt(value, ComponentDirt::PATH) {
23 let parametric_path = self.cast::<ParametricPath>();
24
25 let width = parametric_path.width();
26 let height = parametric_path.height();
27
28 let ox = -parametric_path.origin_x() * width;
29 let oy = -parametric_path.origin_y() * height;
30
31 let vertex1_ref = ObjectRef::from(&*self.vertex1);
32 vertex1_ref.cast::<PathVertex>().set_x(ox + width * 0.5);
33 vertex1_ref.cast::<PathVertex>().set_y(oy);
34
35 let vertex2_ref = ObjectRef::from(&*self.vertex2);
36 vertex2_ref.cast::<PathVertex>().set_x(ox + width);
37 vertex2_ref.cast::<PathVertex>().set_y(oy + height);
38
39 let vertex3_ref = ObjectRef::from(&*self.vertex3);
40 vertex3_ref.cast::<PathVertex>().set_x(ox);
41 vertex3_ref.cast::<PathVertex>().set_y(oy + height);
42 }
43
44 self.cast::<Path>().update(value);
45 }
46}
47
48impl Core for Triangle {
49 parent_types![(parametric_path, ParametricPath)];
50
51 properties!(parametric_path);
52}
53
54impl OnAdded for ObjectRef<'_, Triangle> {
55 on_added!(ParametricPath);
56}
57
58impl Default for Triangle {
59 fn default() -> Self {
60 let triangle = Self {
61 parametric_path: ParametricPath::default(),
62 vertex1: Rc::new(StraightVertex::default()),
63 vertex2: Rc::new(StraightVertex::default()),
64 vertex3: Rc::new(StraightVertex::default()),
65 };
66
67 let triangle_ref = ObjectRef::from(&triangle);
68 let path = triangle_ref.cast::<Path>();
69
70 path.push_vertex(Object::new(&(triangle.vertex1.clone() as Rc<dyn Core>)));
71 path.push_vertex(Object::new(&(triangle.vertex2.clone() as Rc<dyn Core>)));
72 path.push_vertex(Object::new(&(triangle.vertex3.clone() as Rc<dyn Core>)));
73
74 triangle
75 }
76}