fltk/draw/
types.rs

1/// Defines a pair of `x, y` coordinates
2#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
3pub struct Coordinates<T: Copy> {
4    /// Horizontal X coordinate
5    pub x: T,
6    /// Vertical Y coordinate
7    pub y: T,
8}
9
10/// `i32` Coordinates
11#[derive(Debug, Copy, Clone)]
12pub struct Coord<T: Copy>(pub T, pub T);
13// pub type Coord = Coordinates<i32>; // TODO for 2.0
14
15/// `f64` Coordinates
16pub type Coordf = Coordinates<f64>;
17
18impl<T: Copy> Coordinates<T> {
19    /// Returns a new pair of `x, y` coordinates
20    pub fn new(x: T, y: T) -> Self {
21        Coordinates { x, y }
22    }
23
24    /// Returns a tuple of the values
25    pub fn tup(&self) -> (T, T) {
26        (self.x, self.y)
27    }
28}
29
30// Conversions From/Into array and tuple
31
32impl<T: Copy> From<[T; 2]> for Coordinates<T> {
33    fn from(array: [T; 2]) -> Self {
34        Self {
35            x: array[0],
36            y: array[1],
37        }
38    }
39}
40
41impl<T: Copy> From<Coordinates<T>> for [T; 2] {
42    fn from(c: Coordinates<T>) -> Self {
43        [c.x, c.y]
44    }
45}
46
47impl<T: Copy> From<(T, T)> for Coordinates<T> {
48    fn from(tuple: (T, T)) -> Self {
49        Self {
50            x: tuple.0,
51            y: tuple.1,
52        }
53    }
54}
55
56impl<T: Copy> From<Coordinates<T>> for (T, T) {
57    fn from(c: Coordinates<T>) -> Self {
58        (c.x, c.y)
59    }
60}
61
62/// Defines a pair of `w, h` (width, height) values representing size
63#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
64pub struct Size {
65    /// Width
66    pub w: i32,
67    /// Height
68    pub h: i32,
69}
70
71impl Size {
72    /// Returns a new pair of `w, h` (width, height) values
73    pub fn new(w: i32, h: i32) -> Self {
74        Size { w, h }
75    }
76
77    /// Returns a tuple of the values
78    pub fn tup(self) -> (i32, i32) {
79        (self.w, self.h)
80    }
81}
82
83// Conversions From/Into array and tuple
84
85impl From<[i32; 2]> for Size {
86    fn from(array: [i32; 2]) -> Self {
87        Self {
88            w: array[0],
89            h: array[1],
90        }
91    }
92}
93
94impl From<Size> for [i32; 2] {
95    fn from(c: Size) -> Self {
96        [c.w, c.h]
97    }
98}
99
100impl From<(i32, i32)> for Size {
101    fn from(tuple: (i32, i32)) -> Self {
102        Self {
103            w: tuple.0,
104            h: tuple.1,
105        }
106    }
107}
108impl From<Size> for (i32, i32) {
109    fn from(c: Size) -> Self {
110        (c.w, c.h)
111    }
112}
113
114/// Defines a pair of `r, c` (row, column) representing a Cell
115#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
116pub struct Cell {
117    /// Horizontal X coordinate
118    pub r: i32,
119    /// Vertical Y coordinate
120    pub c: i32,
121}
122
123impl Cell {
124    /// Returns a new pair of `r, c` (row, column) cell
125    pub fn new(r: i32, c: i32) -> Self {
126        Cell { r, c }
127    }
128
129    /// Returns a tuple of the values
130    pub fn tup(self) -> (i32, i32) {
131        (self.r, self.c)
132    }
133}
134
135// Conversions From/Into array and tuple
136
137impl From<[i32; 2]> for Cell {
138    fn from(array: [i32; 2]) -> Self {
139        Cell {
140            r: array[0],
141            c: array[1],
142        }
143    }
144}
145
146impl From<Cell> for [i32; 2] {
147    fn from(c: Cell) -> Self {
148        [c.r, c.c]
149    }
150}
151
152impl From<(i32, i32)> for Cell {
153    fn from(tuple: (i32, i32)) -> Self {
154        Self {
155            r: tuple.0,
156            c: tuple.1,
157        }
158    }
159}
160impl From<Cell> for (i32, i32) {
161    fn from(c: Cell) -> Self {
162        (c.r, c.c)
163    }
164}
165
166use std::ops::{Add, Sub};
167
168/// Defines a rectangular bounding box
169#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
170pub struct Rectangle<T: Copy + Add<Output = T> + Sub<Output = T>> {
171    /// Leftmost corner
172    pub x: T,
173    /// Topmost corner
174    pub y: T,
175    /// Width
176    pub w: T,
177    /// Height
178    pub h: T,
179}
180
181/// `i32` Coordinates
182pub type Rect = Rectangle<i32>;
183
184impl<T: Copy + Add<Output = T> + Sub<Output = T>> Rectangle<T> {
185    /// Returns a new `Rectangle` from its top-left corner position,
186    /// and the length of its sides.
187    pub fn new(x: T, y: T, width: T, height: T) -> Self {
188        Self {
189            x,
190            y,
191            w: width,
192            h: height,
193        }
194    }
195
196    /// Sets the Rectangle to be padded
197    pub fn with_padding(self, pad: T) -> Self {
198        Self {
199            x: self.x + pad,
200            y: self.y + pad,
201            w: self.w - (pad + pad),
202            h: self.h - (pad + pad),
203        }
204    }
205
206    /// Returns a new `Rectangle` from the position of its `top_left`
207    /// and `bottom_right` corners.
208    pub fn from_coords(top_left: Coordinates<T>, bottom_right: Coordinates<T>) -> Self {
209        Self {
210            x: top_left.x,
211            y: top_left.y,
212            w: bottom_right.x - top_left.x,
213            h: bottom_right.y - top_left.y,
214        }
215    }
216
217    /// Returns the coordinates of the top-left corner
218    pub fn top_left(&self) -> Coordinates<T> {
219        Coordinates::new(self.x, self.y)
220    }
221
222    /// Returns the coordinates of the bottom-right corner
223    pub fn bottom_right(&self) -> Coordinates<T> {
224        Coordinates::new(self.x + self.w, self.y + self.h)
225    }
226
227    /// Returns a tuple of the values
228    pub fn tup(&self) -> (T, T, T, T) {
229        (self.x, self.y, self.w, self.h)
230    }
231}
232
233// Conversions From/Into array and tuple
234
235impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<[T; 4]> for Rectangle<T> {
236    fn from(array: [T; 4]) -> Self {
237        Self {
238            x: array[0],
239            y: array[1],
240            w: array[2],
241            h: array[3],
242        }
243    }
244}
245
246impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for [T; 4] {
247    fn from(c: Rectangle<T>) -> Self {
248        [c.x, c.y, c.w, c.h]
249    }
250}
251
252impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<(T, T, T, T)> for Rectangle<T> {
253    fn from(tuple: (T, T, T, T)) -> Self {
254        Self {
255            x: tuple.0,
256            y: tuple.1,
257            w: tuple.2,
258            h: tuple.3,
259        }
260    }
261}
262
263impl<T: Copy + Add<Output = T> + Sub<Output = T>> From<Rectangle<T>> for (T, T, T, T) {
264    fn from(c: Rectangle<T>) -> Self {
265        (c.x, c.y, c.w, c.h)
266    }
267}