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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Draw triangles!
use bytemuck::{Pod, Zeroable};
use std::rc::Rc;

use rootvg_core::color::PackedSrgb;
use rootvg_core::math::{Angle, Point, Transform, Vector};

use super::{Indexed, MeshUniforms};

/// A low-level primitive to render a mesh of triangles with a solid color.
#[derive(Debug, Clone, PartialEq)]
pub struct SolidMesh {
    /// The vertices and indices of the mesh.
    pub buffers: Indexed<SolidVertex2D>,
}

impl SolidMesh {
    pub fn new() -> Self {
        Self {
            buffers: Indexed::new(),
        }
    }
}

/// A two-dimensional vertex with a color.
#[derive(Copy, Clone, Debug, PartialEq, Zeroable, Pod)]
#[repr(C)]
pub struct SolidVertex2D {
    /// The vertex position in 2D space.
    pub position: [f32; 2],

    /// The color of the vertex in __linear__ RGBA.
    pub color: PackedSrgb,
}

impl SolidVertex2D {
    pub fn new(position: impl Into<[f32; 2]>, color: impl Into<PackedSrgb>) -> Self {
        Self {
            position: position.into(),
            color: color.into(),
        }
    }
}

#[derive(Debug)]
pub struct SolidMeshPrimitive {
    pub mesh: Rc<SolidMesh>,
    pub uniform: MeshUniforms,
}

impl SolidMeshPrimitive {
    pub fn new(mesh: &Rc<SolidMesh>) -> Self {
        Self {
            mesh: Rc::clone(mesh),
            uniform: MeshUniforms::default(),
        }
    }

    pub fn new_with_offset(mesh: &Rc<SolidMesh>, offset: Point) -> Self {
        Self {
            mesh: Rc::clone(mesh),
            uniform: MeshUniforms {
                offset: offset.into(),
                ..Default::default()
            },
        }
    }

    pub fn new_with_rotation(
        mesh: &Rc<SolidMesh>,
        angle: Angle,
        rotation_origin: Point,
        offset: Point,
    ) -> Self {
        let transform = Transform::translation(-rotation_origin.x, -rotation_origin.y)
            .then_rotate(angle)
            .then_translate(Vector::new(rotation_origin.x, rotation_origin.y));

        Self::new_with_transform(mesh, offset, transform)
    }

    pub fn new_with_transform(mesh: &Rc<SolidMesh>, offset: Point, transform: Transform) -> Self {
        Self {
            mesh: Rc::clone(mesh),
            uniform: MeshUniforms::new(offset, Some(transform)),
        }
    }
}

impl Clone for SolidMeshPrimitive {
    fn clone(&self) -> Self {
        Self {
            mesh: Rc::clone(&self.mesh),
            uniform: self.uniform,
        }
    }
}

impl PartialEq for SolidMeshPrimitive {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.mesh, &other.mesh) && self.uniform == other.uniform
    }
}