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