1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Common functionality.

#![warn(missing_copy_implementations)]

use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// A two-dimensional vector.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Vector {
    pub x: f32,
    pub y: f32,
}

/// Alias for vectors, used to better state intent in certain places.
pub type Point = Vector;

/// An axis-aligned rectangle.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Rect {
    pub position: Point,
    pub size: Vector,
}

/// An 8-bit RGBA color.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl Vector {
    /// Creates a new vector from X/Y coordinates.
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    /// Returns the length² of a vector.
    pub fn length_sq(self) -> f32 {
        self.x * self.x + self.y * self.y
    }

    /// Returns the length of a vector.
    pub fn length(self) -> f32 {
        self.length_sq().sqrt()
    }

    /// Returns the distance between this point and another point.
    pub fn distance(self, other: Vector) -> f32 {
        (other - self).length()
    }
}

/// Shorthand for `Vector::new(x, y)`.
pub fn vector(x: f32, y: f32) -> Vector {
    Vector::new(x, y)
}

/// Shorthand for `Point::new(x, y)`, which is equivalent to `Vector::new(x, y)`.
pub fn point(x: f32, y: f32) -> Point {
    Point::new(x, y)
}

impl Color {
    /// Solid black.
    pub const BLACK: Self = rgb(0, 0, 0);
    /// Solid white.
    pub const WHITE: Self = rgb(255, 255, 255);
    /// Fully transparent black.
    pub const TRANSPARENT: Self = rgba(0, 0, 0, 0);

    /// Creates a new color from channels.
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    /// Creates a new color from an ARGB hex literal.
    pub const fn argb(hex: u32) -> Self {
        let a = (hex >> 24) & 0xFF;
        let r = (hex >> 16) & 0xFF;
        let g = (hex >> 8) & 0xFF;
        let b = hex & 0xFF;
        Self {
            r: r as u8,
            g: g as u8,
            b: b as u8,
            a: a as u8,
        }
    }

    /// Converts a color to a u32 holding ARGB in its bits, from most significant to least significant.
    pub const fn to_argb(&self) -> u32 {
        ((self.a as u32) << 24) | ((self.r as u32) << 16) | ((self.g as u32) << 8) | (self.b as u32)
    }

    /// Creates a new color from an RGB hex literal.
    pub const fn rgb(hex: u32) -> Self {
        Self::argb(hex | 0xFF000000)
    }

    /// Returns a color with the same RGB channels, but with the alpha channel altered.
    pub const fn with_alpha(self, a: u8) -> Self {
        Self::new(self.r, self.g, self.b, a)
    }
}

/// Creates a new color from RGB channels.
pub const fn rgb(r: u8, g: u8, b: u8) -> Color {
    Color::new(r, g, b, 255)
}

/// Creates a new color from RGBA channels. Shorthand for `Color::new(r, g, b, a)`.
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Color {
    Color::new(r, g, b, a)
}

impl Rect {
    /// Creates a new rectangle from a position and a size.
    pub fn new(position: impl Into<Point>, size: impl Into<Vector>) -> Self {
        Self {
            position: position.into(),
            size: size.into(),
        }
    }

    /// Returns the X coordinate of the rectangle's position.
    pub fn x(&self) -> f32 {
        self.position.x
    }

    /// Returns the Y coordinate of the rectangle's position.
    pub fn y(&self) -> f32 {
        self.position.y
    }

    /// Returns the width of the rectangle.
    pub fn width(&self) -> f32 {
        self.size.x
    }

    /// Returns the width of the rectangle.
    pub fn height(&self) -> f32 {
        self.size.y
    }

    /// Returns the left side of the rectangle.
    pub fn left(&self) -> f32 {
        self.position.x
    }

    /// Returns the top side of the rectangle.
    pub fn top(&self) -> f32 {
        self.position.y
    }

    /// Returns the right side of the rectangle.
    pub fn right(&self) -> f32 {
        self.position.x + self.size.x
    }

    /// Returns the bottom side of the rectangle.
    pub fn bottom(&self) -> f32 {
        self.position.y + self.size.y
    }

    /// Returns the top left corner of the rectangle.
    pub fn top_left(&self) -> Point {
        self.position
    }

    /// Returns the top right corner of the rectangle.
    pub fn top_right(&self) -> Point {
        self.position + vector(self.size.x, 0.0)
    }

    /// Returns the bottom left corner of the rectangle.
    pub fn bottom_left(&self) -> Point {
        self.position + vector(0.0, self.size.y)
    }

    /// Returns the bottom right corner of the rectangle.
    pub fn bottom_right(&self) -> Point {
        self.position + self.size
    }

    /// Returns the center point of the rectangle.
    pub fn center(&self) -> Point {
        self.position + self.size / 2.0
    }

    /// Returns the horizontal (X) center of the rectangle.
    pub fn center_x(&self) -> f32 {
        self.position.x + self.size.x / 2.0
    }

    /// Returns the vertical (Y) center of the rectangle.
    pub fn center_y(&self) -> f32 {
        self.position.y + self.size.y / 2.0
    }

    /// Returns a _sorted_ rectangle - that is, the same rectangle, but with the width and height
    /// guaranteed to be positive.
    pub fn sort(self) -> Self {
        let left = f32::min(self.left(), self.right());
        let right = f32::max(self.left(), self.right());
        let top = f32::min(self.top(), self.bottom());
        let bottom = f32::max(self.top(), self.bottom());
        Self::new(point(left, top), vector(right - left, bottom - top))
    }
}

impl Default for Vector {
    /// The default vector is `[0.0, 0.0]`.
    fn default() -> Self {
        vector(0.0, 0.0)
    }
}

impl From<(f32, f32)> for Vector {
    fn from(tuple: (f32, f32)) -> Self {
        vector(tuple.0, tuple.1)
    }
}

impl From<[f32; 2]> for Vector {
    fn from(array: [f32; 2]) -> Self {
        vector(array[0], array[1])
    }
}

impl Neg for Vector {
    type Output = Self;

    fn neg(self) -> Self::Output {
        vector(-self.x, -self.y)
    }
}

impl<T: Into<Self>> Add<T> for Vector {
    type Output = Self;

    fn add(self, v: T) -> Self {
        let v = v.into();
        vector(self.x + v.x, self.y + v.y)
    }
}

impl<T: Into<Self>> AddAssign<T> for Vector {
    fn add_assign(&mut self, v: T) {
        let v = v.into();
        self.x += v.x;
        self.y += v.y;
    }
}

impl<T: Into<Self>> Sub<T> for Vector {
    type Output = Self;

    fn sub(self, v: T) -> Self {
        let v = v.into();
        vector(self.x - v.x, self.y - v.y)
    }
}

impl<T: Into<Self>> SubAssign<T> for Vector {
    fn sub_assign(&mut self, v: T) {
        let v = v.into();
        self.x -= v.x;
        self.y -= v.y;
    }
}

impl<T: Into<Self>> Mul<T> for Vector {
    type Output = Self;

    fn mul(self, v: T) -> Self {
        let v = v.into();
        vector(self.x * v.x, self.y * v.y)
    }
}

impl<T: Into<Self>> MulAssign<T> for Vector {
    fn mul_assign(&mut self, v: T) {
        let v = v.into();
        self.x *= v.x;
        self.y *= v.y;
    }
}

impl<T: Into<Self>> Div<T> for Vector {
    type Output = Self;

    fn div(self, v: T) -> Self {
        let v = v.into();
        vector(self.x / v.x, self.y / v.y)
    }
}

impl<T: Into<Self>> DivAssign<T> for Vector {
    fn div_assign(&mut self, v: T) {
        let v = v.into();
        self.x /= v.x;
        self.y /= v.y;
    }
}

impl Mul<f32> for Vector {
    type Output = Self;

    fn mul(self, s: f32) -> Self {
        vector(self.x * s, self.y * s)
    }
}

impl MulAssign<f32> for Vector {
    fn mul_assign(&mut self, s: f32) {
        self.x *= s;
        self.y *= s;
    }
}

impl Div<f32> for Vector {
    type Output = Self;

    fn div(self, s: f32) -> Self {
        vector(self.x / s, self.y / s)
    }
}

impl DivAssign<f32> for Vector {
    fn div_assign(&mut self, s: f32) {
        self.x /= s;
        self.y /= s;
    }
}