render_agnostic/renderers/
macroquad.rs

1use 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_line(
37        &mut self,
38        start: ::glam::DVec2,
39        end: ::glam::DVec2,
40        thickness: f64,
41        color: Srgba,
42    ) {
43        draw_line(
44            start.x as f32,
45            start.y as f32,
46            end.x as f32,
47            end.y as f32,
48            thickness as f32,
49            srgba_to_color(color),
50        );
51    }
52
53    fn render_circle(&mut self, position: ::glam::DVec2, radius: f64, color: Srgba) {
54        draw_circle(
55            position.x as f32,
56            position.y as f32,
57            radius as f32,
58            srgba_to_color(color),
59        );
60    }
61
62    fn render_circle_lines(
63        &mut self,
64        position: ::glam::DVec2,
65        radius: f64,
66        thickness: f64,
67        color: Srgba,
68    ) {
69        draw_circle_lines(
70            position.x as f32,
71            position.y as f32,
72            radius as f32,
73            thickness as f32,
74            srgba_to_color(color),
75        );
76    }
77
78    fn render_arc(
79        &mut self,
80        position: ::glam::DVec2,
81        radius: f64,
82        rotation: f64,
83        arc: f64,
84        thickness: f64,
85        color: Srgba,
86    ) {
87        draw_arc(
88            position.x as f32,
89            position.y as f32,
90            64,
91            radius as f32,
92            rotation as f32,
93            thickness as f32,
94            arc as f32,
95            srgba_to_color(color),
96        );
97    }
98
99    fn render_text(
100        &mut self,
101        text: &str,
102        position: ::glam::DVec2,
103        anchor: Anchor2D,
104        size: f64,
105        color: Srgba,
106    ) {
107        let measurement = measure_text(text, self.font.as_ref(), size as u16, 1.0);
108
109        let x = match anchor.get_horizontal() {
110            HorizontalAnchor::Left => position.x,
111            HorizontalAnchor::Center => position.x - measurement.width as f64 / 2.0,
112            HorizontalAnchor::Right => position.x - measurement.width as f64,
113        };
114
115        let vertical_anchor = anchor.get_vertical();
116
117        let y = match (vertical_anchor.get_context(), vertical_anchor.get_value()) {
118            (VerticalAnchorContext::Graphics, VerticalAnchorValue::Bottom) => position.y,
119            (VerticalAnchorContext::Math, VerticalAnchorValue::Bottom) => {
120                position.y + measurement.offset_y as f64
121            }
122            (_, VerticalAnchorValue::Center) => position.y + measurement.offset_y as f64 / 2.0,
123            (VerticalAnchorContext::Graphics, VerticalAnchorValue::Top) => {
124                position.y + measurement.offset_y as f64
125            }
126            (VerticalAnchorContext::Math, VerticalAnchorValue::Top) => position.y,
127        };
128
129        draw_text_ex(
130            text,
131            x as f32,
132            y as f32,
133            TextParams {
134                font: self.font.as_ref(),
135                font_size: size as u16,
136                color: srgba_to_color(color),
137                ..TextParams::default()
138            },
139        );
140    }
141
142    fn render_rectangle(
143        &mut self,
144        position: ::glam::DVec2,
145        width: f64,
146        height: f64,
147        offset: ::glam::DVec2,
148        rotation: f64,
149        color: Srgba,
150    ) {
151        draw_rectangle_ex(
152            position.x as f32,
153            position.y as f32,
154            width as f32,
155            height as f32,
156            DrawRectangleParams {
157                offset: vec2(offset.x as f32, offset.y as f32),
158                rotation: rotation as f32,
159                color: srgba_to_color(color),
160            },
161        );
162    }
163
164    fn render_rectangle_lines(
165        &mut self,
166        position: ::glam::DVec2,
167        width: f64,
168        height: f64,
169        offset: ::glam::DVec2,
170        rotation: f64,
171        thickness: f64,
172        color: Srgba,
173    ) {
174        draw_rectangle_lines_ex(
175            position.x as f32,
176            position.y as f32,
177            width as f32,
178            height as f32,
179            thickness as f32,
180            DrawRectangleParams {
181                offset: vec2(offset.x as f32, offset.y as f32),
182                rotation: rotation as f32,
183                color: srgba_to_color(color),
184            },
185        );
186    }
187
188    fn render_equilateral_triangle(
189        &mut self,
190        position: ::glam::DVec2,
191        radius: f64,
192        rotation: f64,
193        color: Srgba,
194    ) {
195        draw_poly(
196            position.x as f32,
197            position.y as f32,
198            3,
199            radius as f32,
200            rotation.to_degrees() as f32,
201            srgba_to_color(color),
202        );
203    }
204
205    fn render_equilateral_triangle_lines(
206        &mut self,
207        position: ::glam::DVec2,
208        radius: f64,
209        rotation: f64,
210        thickness: f64,
211        color: Srgba,
212    ) {
213        draw_poly_lines(
214            position.x as f32,
215            position.y as f32,
216            3,
217            radius as f32,
218            rotation.to_degrees() as f32,
219            thickness as f32,
220            srgba_to_color(color),
221        );
222    }
223}