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
use crate::core::{Color, Indices};

use crate::draw::{Drawing, Tessellation};
use lyon::algorithms::path::Path;
use lyon::math::Point;
use lyon::tessellation::{LineJoin, StrokeOptions, StrokeTessellator, VertexBuffers, BuffersBuilder, StrokeVertex, FillTessellator, FillOptions, FillVertex};


pub struct Rect
{
    pub left_up: [f32; 2],
    pub right_down: [f32; 2],
    pub width: f32,
    pub joints: LineJoin,
    pub color: Color,
    pub outline: Color,
}

impl Default for Rect
{
    fn default() -> Self
    {
        Self
        {
            left_up: [-0.1, -0.1],
            right_down: [0.1, 0.1],
            width: 0.01,
            joints: LineJoin::Miter,
            color: [1.0, 1.0, 1.0, 0.0],
            outline: [1.0, 1.0, 1.0, 1.0]
        }
    }
}

impl Drawing for Rect
{
    fn get_tessellation(&self) -> Tessellation
    {
        let mut path_builder = Path::builder();
        path_builder.begin(Point::from(self.left_up));
        path_builder.line_to(Point::new(self.left_up[0], self.right_down[1]));
        path_builder.line_to(Point::from(self.right_down));
        path_builder.line_to(Point::new(self.right_down[0], self.left_up[1]));
        path_builder.end(true);
        let path = path_builder.build();
        let mut buffer: VertexBuffers<[f32; 3], u16> = VertexBuffers::new();
        let mut tesselator = FillTessellator::new();
        tesselator.tessellate_path(
            &path,
            &FillOptions::default(),
            &mut BuffersBuilder::new
                (
                    &mut buffer,
                    |vertex: FillVertex| { [vertex.position().x, vertex.position().y, 0.0_f32] }
                )
        ).unwrap();
        let mut colors= vec![self.color; buffer.vertices.len()];
        if self.width > 0.0
        {
            let mut options = StrokeOptions::default();
            options.line_width = self.width;
            options.tolerance = self.width / 90.0;
            options.line_join = self.joints;
            let mut tessellator = StrokeTessellator::new();
            tessellator.tessellate_path
            (
                &path,
                &options,
                &mut BuffersBuilder::new
                    (
                        &mut buffer,
                        |vertex: StrokeVertex| { [vertex.position().x, vertex.position().y, 0.0_f32] }
                    )
            ).unwrap();
        }
        buffer.indices.reverse();
        colors.append(&mut vec![self.outline; buffer.vertices.len() - colors.len()]);
        Tessellation
        {
            vertices: buffer.vertices,
            indices: Indices::U16(buffer.indices),
            colors
        }
    }
}