solstice_2d/d2/
vertex.rs

1use bytemuck::{Pod, Zeroable};
2
3#[repr(C)]
4#[derive(Zeroable, Pod, solstice::vertex::Vertex, Copy, Clone, Debug, PartialEq)]
5pub struct Vertex2D {
6    pub position: [f32; 2],
7    pub color: [f32; 4],
8    pub uv: [f32; 2],
9}
10
11impl Default for Vertex2D {
12    fn default() -> Self {
13        Self {
14            position: [0., 0.],
15            color: [1., 1., 1., 1.],
16            uv: [0.5, 0.5],
17        }
18    }
19}
20
21impl Vertex2D {
22    pub fn new(position: [f32; 2], color: [f32; 4], uv: [f32; 2]) -> Self {
23        Self {
24            position,
25            color,
26            uv,
27        }
28    }
29
30    pub fn position(&self) -> &[f32; 2] {
31        &self.position
32    }
33}
34
35impl From<(f32, f32)> for Vertex2D {
36    fn from((x, y): (f32, f32)) -> Self {
37        Self {
38            position: [x, y],
39            ..Default::default()
40        }
41    }
42}
43
44impl From<(f64, f64)> for Vertex2D {
45    fn from((x, y): (f64, f64)) -> Self {
46        Self {
47            position: [x as _, y as _],
48            ..Default::default()
49        }
50    }
51}
52
53impl From<Point> for Vertex2D {
54    fn from(p: Point) -> Self {
55        Self {
56            position: [p.x, p.y],
57            ..Default::default()
58        }
59    }
60}
61
62#[derive(Copy, Clone, Debug, PartialEq)]
63pub struct Point {
64    pub x: f32,
65    pub y: f32,
66}
67
68impl Point {
69    pub fn new(x: f32, y: f32) -> Self {
70        Self { x, y }
71    }
72
73    pub fn x(&self) -> f32 {
74        self.x
75    }
76
77    pub fn y(&self) -> f32 {
78        self.y
79    }
80}
81
82impl From<(f32, f32)> for Point {
83    fn from((x, y): (f32, f32)) -> Self {
84        Self { x, y }
85    }
86}
87
88impl From<[f32; 2]> for Point {
89    fn from([x, y]: [f32; 2]) -> Self {
90        Self { x, y }
91    }
92}
93
94impl<T> From<&T> for Point
95where
96    Self: From<T>,
97    T: Copy,
98{
99    fn from(p: &T) -> Self {
100        Into::into(*p)
101    }
102}
103
104impl From<Point> for mint::Point2<f32> {
105    fn from(p: Point) -> Self {
106        Self { x: p.x, y: p.y }
107    }
108}
109
110impl From<mint::Point2<f32>> for Point {
111    fn from(p: mint::Point2<f32>) -> Self {
112        Self { x: p.x, y: p.y }
113    }
114}