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
use crate::vector::Vector; #[cfg(feature = "euclid")] use euclid::Size2D; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign}; #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Size<T> { pub width: T, pub height: T, } impl<T> Size<T> { pub fn new(width: T, height: T) -> Self { Size { width, height } } pub fn to_vector(self) -> Vector<T> { Vector::new(self.width, self.height) } } impl<T: Mul> Size<T> { pub fn area(self) -> T::Output { self.width * self.height } } impl<T: Copy + Mul<Output = T> + Div<Output = T>> Size<T> { pub fn scaled_to_width(self, rhs: T) -> Size<T> { Size::new(rhs, self.height * rhs / self.width) } pub fn scaled_to_height(self, rhs: T) -> Size<T> { Size::new(self.width * rhs / self.height, rhs) } } impl<T: Copy + Mul<Output = T> + Div<Output = T> + Ord> Size<T> { pub fn scaled_to_fill(self, rhs: Size<T>) -> Size<T> { self * std::cmp::max(rhs.width / self.width, rhs.height / self.height) } } impl<T: Ord> Size<T> { pub fn is_landscape(&self) -> bool { self.width > self.height } pub fn is_portrait(&self) -> bool { self.width < self.height } pub fn is_square(&self) -> bool { self.width == self.height } } impl<T: Add<RHS, Output = Output>, RHS, Output> Add<Size<RHS>> for Size<T> { type Output = Size<Output>; fn add(self, rhs: Size<RHS>) -> Self::Output { Size { width: self.width + rhs.width, height: self.height + rhs.height, } } } impl<T: AddAssign<RHS>, RHS> AddAssign<Size<RHS>> for Size<T> { fn add_assign(&mut self, rhs: Size<RHS>) { self.width += rhs.width } } impl<T: Mul<RHS>, RHS: Copy> Mul<RHS> for Size<T> { type Output = Size<T::Output>; fn mul(self, rhs: RHS) -> Self::Output { Size { width: self.width * rhs, height: self.height * rhs, } } } impl<T: MulAssign<RHS>, RHS: Copy> MulAssign<RHS> for Size<T> { fn mul_assign(&mut self, rhs: RHS) { self.width *= rhs; self.height *= rhs } } impl<T: Div<RHS>, RHS: Copy> Div<RHS> for Size<T> { type Output = Size<T::Output>; fn div(self, rhs: RHS) -> Self::Output { Size { width: self.width / rhs, height: self.height / rhs, } } } impl<T: DivAssign<RHS>, RHS: Copy> DivAssign<RHS> for Size<T> { fn div_assign(&mut self, rhs: RHS) { self.width /= rhs; self.height /= rhs } } impl<T: Rem<RHS>, RHS: Copy> Rem<RHS> for Size<T> { type Output = Size<T::Output>; fn rem(self, rhs: RHS) -> Self::Output { Size { width: self.width % rhs, height: self.height % rhs, } } } impl<T: RemAssign<RHS>, RHS: Copy> RemAssign<RHS> for Size<T> { fn rem_assign(&mut self, rhs: RHS) { self.width %= rhs; self.height %= rhs } } #[cfg(feature = "euclid")] impl<T> From<Size2D<T>> for Size<T> { fn from(size: Size2D<T>) -> Self { Size::new(size.width, size.height) } } #[cfg(feature = "euclid")] impl<T: Copy> Into<Size2D<T>> for Size<T> { fn into(self) -> Size2D<T> { Size2D::new(self.width, self.height) } }