forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.rs
More file actions
129 lines (109 loc) · 2.92 KB
/
Copy pathsize.rs
File metadata and controls
129 lines (109 loc) · 2.92 KB
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
use crate::Vector;
/// An amount of space in 2 dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Size<T = f32> {
/// The width.
pub width: T,
/// The height.
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 }
}
}
impl Size {
/// A [`Size`] with zero width and height.
pub const ZERO: Size = Size::new(0., 0.);
/// A [`Size`] with a width and height of 1 unit.
pub const UNIT: Size = Size::new(1., 1.);
/// A [`Size`] with infinite width and height.
pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY);
/// Returns the minimum of each component of this size and another.
pub fn min(self, other: Self) -> Self {
Size {
width: self.width.min(other.width),
height: self.height.min(other.height),
}
}
/// Returns the maximum of each component of this size and another.
pub fn max(self, other: Self) -> Self {
Size {
width: self.width.max(other.width),
height: self.height.max(other.height),
}
}
/// 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,
}
}
}
impl<T> From<[T; 2]> for Size<T> {
fn from([width, height]: [T; 2]) -> Self {
Size { width, height }
}
}
impl<T> From<(T, T)> for Size<T> {
fn from((width, height): (T, T)) -> Self {
Self { width, height }
}
}
impl<T> From<Vector<T>> for Size<T> {
fn from(vector: Vector<T>) -> Self {
Size {
width: vector.x,
height: vector.y,
}
}
}
impl<T> From<Size<T>> for [T; 2] {
fn from(size: Size<T>) -> Self {
[size.width, size.height]
}
}
impl<T> From<Size<T>> for Vector<T> {
fn from(size: Size<T>) -> Self {
Vector::new(size.width, size.height)
}
}
impl<T> std::ops::Sub for Size<T>
where
T: std::ops::Sub<Output = T>,
{
type Output = Size<T>;
fn sub(self, rhs: Self) -> Self::Output {
Size {
width: self.width - rhs.width,
height: self.height - rhs.height,
}
}
}
impl<T> std::ops::Mul<T> for Size<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Size<T>;
fn mul(self, rhs: T) -> Self::Output {
Size {
width: self.width * rhs,
height: self.height * rhs,
}
}
}
impl<T> std::ops::Mul<Vector<T>> for Size<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Size<T>;
fn mul(self, scale: Vector<T>) -> Self::Output {
Size {
width: self.width * scale.x,
height: self.height * scale.y,
}
}
}