firefly_rust/
shapes.rs

1//! Structs for working with shapes as values.
2//!
3//! The [graphics] module provides functions for drawing shapes.
4//! This modules provides useful struct for when you need to store
5//! or manipulate a shape before it can be drawn.
6use crate::*;
7
8pub trait Shape {
9    fn draw(&self);
10}
11
12/// A wrapper for [`draw_line`].
13#[derive(Clone, Eq, PartialEq, Hash, Debug)]
14pub struct Line {
15    pub a: Point,
16    pub b: Point,
17    pub style: LineStyle,
18}
19
20impl Shape for Line {
21    fn draw(&self) {
22        draw_line(self.a, self.b, self.style);
23    }
24}
25
26/// A wrapper for [`draw_rect`].
27#[derive(Clone, Eq, PartialEq, Hash, Debug)]
28pub struct Rect {
29    pub point: Point,
30    pub size: Size,
31    pub style: Style,
32}
33
34impl Shape for Rect {
35    fn draw(&self) {
36        draw_rect(self.point, self.size, self.style);
37    }
38}
39
40impl From<RoundedRect> for Rect {
41    fn from(value: RoundedRect) -> Self {
42        Self {
43            point: value.point,
44            size: value.size,
45            style: value.style,
46        }
47    }
48}
49
50/// A wrapper for [`draw_rounded_rect`].
51#[derive(Clone, Eq, PartialEq, Hash, Debug)]
52pub struct RoundedRect {
53    pub point: Point,
54    pub size: Size,
55    pub corner: Size,
56    pub style: Style,
57}
58
59impl Shape for RoundedRect {
60    fn draw(&self) {
61        draw_rounded_rect(self.point, self.size, self.corner, self.style);
62    }
63}
64
65impl From<Rect> for RoundedRect {
66    fn from(value: Rect) -> Self {
67        Self {
68            point: value.point,
69            size: value.size,
70            corner: Size {
71                width: 0,
72                height: 0,
73            },
74            style: value.style,
75        }
76    }
77}
78
79/// A wrapper for [`draw_circle`].
80#[derive(Clone, Eq, PartialEq, Hash, Debug)]
81pub struct Circle {
82    pub point: Point,
83    pub diameter: i32,
84    pub style: Style,
85}
86
87impl Shape for Circle {
88    fn draw(&self) {
89        draw_circle(self.point, self.diameter, self.style);
90    }
91}
92
93/// A wrapper for [`draw_ellipse`].
94#[derive(Clone, Eq, PartialEq, Hash, Debug)]
95pub struct Ellipse {
96    pub point: Point,
97    pub size: Size,
98    pub style: Style,
99}
100
101impl Shape for Ellipse {
102    fn draw(&self) {
103        draw_ellipse(self.point, self.size, self.style);
104    }
105}
106
107impl From<Circle> for Ellipse {
108    fn from(value: Circle) -> Self {
109        Self {
110            point: value.point,
111            size: Size {
112                width: value.diameter,
113                height: value.diameter,
114            },
115            style: value.style,
116        }
117    }
118}
119
120/// A wrapper for [`draw_triangle`].
121#[derive(Clone, Eq, PartialEq, Hash, Debug)]
122pub struct Triangle {
123    pub a: Point,
124    pub b: Point,
125    pub c: Point,
126    pub style: Style,
127}
128
129impl Shape for Triangle {
130    fn draw(&self) {
131        draw_triangle(self.a, self.b, self.c, self.style);
132    }
133}
134
135/// A wrapper for [`draw_arc`].
136#[derive(Clone, Debug)]
137pub struct Arc {
138    pub point: Point,
139    pub diameter: i32,
140    pub start: Angle,
141    pub sweep: Angle,
142    pub style: Style,
143}
144
145impl Shape for Arc {
146    fn draw(&self) {
147        draw_arc(
148            self.point,
149            self.diameter,
150            self.start,
151            self.sweep,
152            self.style,
153        );
154    }
155}
156
157impl From<Sector> for Arc {
158    fn from(value: Sector) -> Self {
159        Self {
160            point: value.point,
161            diameter: value.diameter,
162            start: value.start,
163            sweep: value.sweep,
164            style: value.style,
165        }
166    }
167}
168
169/// A wrapper for [`draw_sector`].
170#[derive(Clone, Debug)]
171pub struct Sector {
172    pub point: Point,
173    pub diameter: i32,
174    pub start: Angle,
175    pub sweep: Angle,
176    pub style: Style,
177}
178
179impl Shape for Sector {
180    fn draw(&self) {
181        draw_sector(
182            self.point,
183            self.diameter,
184            self.start,
185            self.sweep,
186            self.style,
187        );
188    }
189}
190
191impl From<Arc> for Sector {
192    fn from(value: Arc) -> Self {
193        Self {
194            point: value.point,
195            diameter: value.diameter,
196            start: value.start,
197            sweep: value.sweep,
198            style: value.style,
199        }
200    }
201}