2023-03-16 20:23:25 +01:00
|
|
|
use crate::Vector;
|
2019-11-10 06:05:20 +01:00
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// An amount of space in 2 dimensions.
|
2023-03-07 06:09:51 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2020-05-20 20:28:35 +02:00
|
|
|
pub struct Size<T = f32> {
|
2019-11-10 06:05:20 +01:00
|
|
|
/// The width.
|
2020-05-20 20:28:35 +02:00
|
|
|
pub width: T,
|
2019-11-10 06:05:20 +01:00
|
|
|
/// The height.
|
2020-05-20 20:28:35 +02:00
|
|
|
pub height: T,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Size<T> {
|
|
|
|
|
/// Creates a new [`Size`] with the given width and height.
|
|
|
|
|
pub const fn new(width: T, height: T) -> Self {
|
|
|
|
|
Size { width, height }
|
|
|
|
|
}
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Size {
|
2019-11-22 19:36:57 +01:00
|
|
|
/// A [`Size`] with zero width and height.
|
2019-11-10 06:05:20 +01:00
|
|
|
pub const ZERO: Size = Size::new(0., 0.);
|
2019-11-22 19:36:57 +01:00
|
|
|
|
2020-05-01 04:30:54 +02:00
|
|
|
/// A [`Size`] with a width and height of 1 unit.
|
|
|
|
|
pub const UNIT: Size = Size::new(1., 1.);
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// A [`Size`] with infinite width and height.
|
2019-11-10 06:05:20 +01:00
|
|
|
pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY);
|
|
|
|
|
|
2023-03-16 20:23:25 +01:00
|
|
|
/// Returns the minimum of each component of this size and another.
|
2022-10-27 11:48:42 -07:00
|
|
|
pub fn min(self, other: Self) -> Self {
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width.min(other.width),
|
|
|
|
|
height: self.height.min(other.height),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 20:23:25 +01:00
|
|
|
/// Returns the maximum of each component of this size and another.
|
2022-10-27 11:48:42 -07:00
|
|
|
pub fn max(self, other: Self) -> Self {
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width.max(other.width),
|
|
|
|
|
height: self.height.max(other.height),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-16 20:23:25 +01:00
|
|
|
|
|
|
|
|
/// Expands this [`Size`] by the given amount.
|
|
|
|
|
pub fn expand(self, other: impl Into<Size>) -> Self {
|
|
|
|
|
let other = other.into();
|
|
|
|
|
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width + other.width,
|
|
|
|
|
height: self.height + other.height,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|
2019-11-29 20:52:26 -05:00
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> From<[T; 2]> for Size<T> {
|
|
|
|
|
fn from([width, height]: [T; 2]) -> Self {
|
2019-11-29 20:52:26 -05:00
|
|
|
Size { width, height }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> From<(T, T)> for Size<T> {
|
|
|
|
|
fn from((width, height): (T, T)) -> Self {
|
|
|
|
|
Self { width, height }
|
2019-11-29 20:52:26 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-08 09:54:22 +02:00
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> From<Vector<T>> for Size<T> {
|
|
|
|
|
fn from(vector: Vector<T>) -> Self {
|
2020-10-28 02:36:49 +01:00
|
|
|
Size {
|
|
|
|
|
width: vector.x,
|
|
|
|
|
height: vector.y,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> From<Size<T>> for [T; 2] {
|
|
|
|
|
fn from(size: Size<T>) -> Self {
|
2020-10-08 09:54:22 +02:00
|
|
|
[size.width, size.height]
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-29 14:50:17 +01:00
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> From<Size<T>> for Vector<T> {
|
|
|
|
|
fn from(size: Size<T>) -> Self {
|
2020-10-29 14:50:17 +01:00
|
|
|
Vector::new(size.width, size.height)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-27 11:48:42 -07:00
|
|
|
|
2024-03-17 17:57:24 +01:00
|
|
|
impl<T> std::ops::Sub for Size<T>
|
|
|
|
|
where
|
|
|
|
|
T: std::ops::Sub<Output = T>,
|
|
|
|
|
{
|
|
|
|
|
type Output = Size<T>;
|
2022-10-27 11:48:42 -07:00
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width - rhs.width,
|
|
|
|
|
height: self.height - rhs.height,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|