render_agnostic/renderers/
macroquad.rs1use anchor2d::{Anchor2D, HorizontalAnchor, VerticalAnchorContext, VerticalAnchorValue};
2use macroquad::prelude::*;
3use palette::Srgba;
4
5use crate::Renderer;
6
7fn srgba_to_color(srgba: Srgba) -> Color {
8 Color {
9 r: srgba.red,
10 g: srgba.green,
11 b: srgba.blue,
12 a: srgba.alpha,
13 }
14}
15
16#[derive(Debug, Default, Clone)]
17pub struct MacroquadRenderer {
18 font: Option<Font>,
19}
20
21impl MacroquadRenderer {
22 pub fn new(font: Option<Font>) -> Self {
23 Self { font }
24 }
25
26 pub fn get_font(&self) -> Option<&Font> {
27 self.font.as_ref()
28 }
29
30 pub fn set_font(&mut self, font: Option<Font>) {
31 self.font = font;
32 }
33}
34
35impl Renderer for MacroquadRenderer {
36 fn render_point(&mut self, position: ::glam::DVec2, color: Srgba) {
37 draw_rectangle(
38 position.x as f32,
39 position.y as f32,
40 1.0,
41 1.0,
42 srgba_to_color(color),
43 );
44 }
45
46 fn render_line(
47 &mut self,
48 start: ::glam::DVec2,
49 end: ::glam::DVec2,
50 thickness: f64,
51 color: Srgba,
52 ) {
53 draw_line(
54 start.x as f32,
55 start.y as f32,
56 end.x as f32,
57 end.y as f32,
58 thickness as f32,
59 srgba_to_color(color),
60 );
61 }
62
63 fn render_circle(&mut self, position: ::glam::DVec2, radius: f64, color: Srgba) {
64 draw_circle(
65 position.x as f32,
66 position.y as f32,
67 radius as f32,
68 srgba_to_color(color),
69 );
70 }
71
72 fn render_circle_lines(
73 &mut self,
74 position: ::glam::DVec2,
75 radius: f64,
76 thickness: f64,
77 color: Srgba,
78 ) {
79 draw_circle_lines(
80 position.x as f32,
81 position.y as f32,
82 radius as f32,
83 thickness as f32,
84 srgba_to_color(color),
85 );
86 }
87
88 fn render_arc(
89 &mut self,
90 position: ::glam::DVec2,
91 radius: f64,
92 rotation: f64,
93 arc: f64,
94 color: Srgba,
95 ) {
96 draw_arc(
98 position.x as f32,
99 position.y as f32,
100 64,
101 radius as f32,
102 rotation as f32,
103 1.0,
104 arc as f32,
105 srgba_to_color(color),
106 );
107 }
108
109 fn render_arc_lines(
110 &mut self,
111 position: ::glam::DVec2,
112 radius: f64,
113 rotation: f64,
114 arc: f64,
115 thickness: f64,
116 color: Srgba,
117 ) {
118 draw_arc(
119 position.x as f32,
120 position.y as f32,
121 64,
122 radius as f32,
123 rotation as f32,
124 thickness as f32,
125 arc as f32,
126 srgba_to_color(color),
127 );
128 }
129
130 fn render_text(
131 &mut self,
132 text: &str,
133 position: ::glam::DVec2,
134 anchor: Anchor2D,
135 size: f64,
136 color: Srgba,
137 ) {
138 let measurement = measure_text(text, self.font.as_ref(), size as u16, 1.0);
139
140 let x = match anchor.get_horizontal() {
141 HorizontalAnchor::Left => position.x,
142 HorizontalAnchor::Center => position.x - measurement.width as f64 / 2.0,
143 HorizontalAnchor::Right => position.x - measurement.width as f64,
144 };
145
146 let vertical_anchor = anchor.get_vertical();
147
148 let y = match (vertical_anchor.get_context(), vertical_anchor.get_value()) {
149 (VerticalAnchorContext::Graphics, VerticalAnchorValue::Bottom) => position.y,
150 (VerticalAnchorContext::Math, VerticalAnchorValue::Bottom) => {
151 position.y + measurement.offset_y as f64
152 }
153 (_, VerticalAnchorValue::Center) => position.y + measurement.offset_y as f64 / 2.0,
154 (VerticalAnchorContext::Graphics, VerticalAnchorValue::Top) => {
155 position.y + measurement.offset_y as f64
156 }
157 (VerticalAnchorContext::Math, VerticalAnchorValue::Top) => position.y,
158 };
159
160 draw_text_ex(
161 text,
162 x as f32,
163 y as f32,
164 TextParams {
165 font: self.font.as_ref(),
166 font_size: size as u16,
167 color: srgba_to_color(color),
168 ..TextParams::default()
169 },
170 );
171 }
172
173 fn render_rectangle(
174 &mut self,
175 position: ::glam::DVec2,
176 width: f64,
177 height: f64,
178 offset: ::glam::DVec2,
179 rotation: f64,
180 color: Srgba,
181 ) {
182 draw_rectangle_ex(
183 position.x as f32,
184 position.y as f32,
185 width as f32,
186 height as f32,
187 DrawRectangleParams {
188 offset: vec2(offset.x as f32, offset.y as f32),
189 rotation: rotation as f32,
190 color: srgba_to_color(color),
191 },
192 );
193 }
194
195 fn render_rectangle_lines(
196 &mut self,
197 position: ::glam::DVec2,
198 width: f64,
199 height: f64,
200 offset: ::glam::DVec2,
201 rotation: f64,
202 thickness: f64,
203 color: Srgba,
204 ) {
205 draw_rectangle_lines_ex(
206 position.x as f32,
207 position.y as f32,
208 width as f32,
209 height as f32,
210 thickness as f32,
211 DrawRectangleParams {
212 offset: vec2(offset.x as f32, offset.y as f32),
213 rotation: rotation as f32,
214 color: srgba_to_color(color),
215 },
216 );
217 }
218
219 fn render_equilateral_triangle(
220 &mut self,
221 position: ::glam::DVec2,
222 radius: f64,
223 rotation: f64,
224 color: Srgba,
225 ) {
226 draw_poly(
227 position.x as f32,
228 position.y as f32,
229 3,
230 radius as f32,
231 rotation.to_degrees() as f32,
232 srgba_to_color(color),
233 );
234 }
235
236 fn render_equilateral_triangle_lines(
237 &mut self,
238 position: ::glam::DVec2,
239 radius: f64,
240 rotation: f64,
241 thickness: f64,
242 color: Srgba,
243 ) {
244 draw_poly_lines(
245 position.x as f32,
246 position.y as f32,
247 3,
248 radius as f32,
249 rotation.to_degrees() as f32,
250 thickness as f32,
251 srgba_to_color(color),
252 );
253 }
254}