rive_rs/shapes/
triangle.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
// 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::rc::Rc;

use crate::component::Component;
use crate::component_dirt::ComponentDirt;
use crate::core::{Core, Object, ObjectRef, OnAdded};
use crate::shapes::{ParametricPath, Path, PathVertex, StraightVertex};

#[derive(Debug)]
pub struct Triangle {
    parametric_path: ParametricPath,
    vertex1: Rc<StraightVertex>,
    vertex2: Rc<StraightVertex>,
    vertex3: Rc<StraightVertex>,
}

impl ObjectRef<'_, Triangle> {
    pub fn update(&self, value: ComponentDirt) {
        if Component::value_has_dirt(value, ComponentDirt::PATH) {
            let parametric_path = self.cast::<ParametricPath>();

            let width = parametric_path.width();
            let height = parametric_path.height();

            let ox = -parametric_path.origin_x() * width;
            let oy = -parametric_path.origin_y() * height;

            let vertex1_ref = ObjectRef::from(&*self.vertex1);
            vertex1_ref.cast::<PathVertex>().set_x(ox + width * 0.5);
            vertex1_ref.cast::<PathVertex>().set_y(oy);

            let vertex2_ref = ObjectRef::from(&*self.vertex2);
            vertex2_ref.cast::<PathVertex>().set_x(ox + width);
            vertex2_ref.cast::<PathVertex>().set_y(oy + height);

            let vertex3_ref = ObjectRef::from(&*self.vertex3);
            vertex3_ref.cast::<PathVertex>().set_x(ox);
            vertex3_ref.cast::<PathVertex>().set_y(oy + height);
        }

        self.cast::<Path>().update(value);
    }
}

impl Core for Triangle {
    parent_types![(parametric_path, ParametricPath)];

    properties!(parametric_path);
}

impl OnAdded for ObjectRef<'_, Triangle> {
    on_added!(ParametricPath);
}

impl Default for Triangle {
    fn default() -> Self {
        let triangle = Self {
            parametric_path: ParametricPath::default(),
            vertex1: Rc::new(StraightVertex::default()),
            vertex2: Rc::new(StraightVertex::default()),
            vertex3: Rc::new(StraightVertex::default()),
        };

        let triangle_ref = ObjectRef::from(&triangle);
        let path = triangle_ref.cast::<Path>();

        path.push_vertex(Object::new(&(triangle.vertex1.clone() as Rc<dyn Core>)));
        path.push_vertex(Object::new(&(triangle.vertex2.clone() as Rc<dyn Core>)));
        path.push_vertex(Object::new(&(triangle.vertex3.clone() as Rc<dyn Core>)));

        triangle
    }
}