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
104
105
106
use crate::graphics::Color;
use crate::math::{Mat4, Vec2, Vec3};
#[derive(Debug, Clone, PartialEq)]
pub struct DrawParams {
pub position: Vec2<f32>,
pub scale: Vec2<f32>,
pub origin: Vec2<f32>,
pub rotation: f32,
pub color: Color,
}
impl DrawParams {
pub fn new() -> DrawParams {
DrawParams::default()
}
pub fn position(mut self, position: Vec2<f32>) -> DrawParams {
self.position = position;
self
}
pub fn scale(mut self, scale: Vec2<f32>) -> DrawParams {
self.scale = scale;
self
}
pub fn origin(mut self, origin: Vec2<f32>) -> DrawParams {
self.origin = origin;
self
}
pub fn rotation(mut self, rotation: f32) -> DrawParams {
self.rotation = rotation;
self
}
pub fn color(mut self, color: Color) -> DrawParams {
self.color = color;
self
}
pub fn to_matrix(&self) -> Mat4<f32> {
let mut matrix = Mat4::translation_2d(-self.origin);
matrix.scale_3d(Vec3::from(self.scale));
matrix.rotate_z(self.rotation);
matrix.translate_2d(self.position);
matrix
}
}
impl Default for DrawParams {
fn default() -> DrawParams {
DrawParams {
position: Vec2::new(0.0, 0.0),
scale: Vec2::new(1.0, 1.0),
origin: Vec2::new(0.0, 0.0),
rotation: 0.0,
color: Color::WHITE,
}
}
}
impl From<Vec2<f32>> for DrawParams {
fn from(position: Vec2<f32>) -> DrawParams {
DrawParams {
position,
..DrawParams::default()
}
}
}
impl From<DrawParams> for Mat4<f32> {
fn from(params: DrawParams) -> Self {
params.to_matrix()
}
}