Newer
Older
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
use std::ops::{Mul, MulAssign};
use bevy::reflect::Reflect;
/// A struct for defining properties related to the edges of widgets
///
/// This is useful for things like borders, padding, etc.
#[derive(Debug, Default, Reflect, Copy, Clone, PartialEq, Eq)]
pub struct Edge<T>
where
T: Copy + Default + PartialEq + Reflect,
{
/// The value of the top edge
pub top: T,
/// The value of the right edge
pub right: T,
/// The value of the bottom edge
pub bottom: T,
/// The value of the left edge
pub left: T,
}
impl<T> Edge<T>
where
T: Copy + Default + PartialEq + Reflect,
{
/// Creates a new `Edge` with values individually specified for each edge
///
/// # Arguments
///
/// * `top`: The top edge value
/// * `right`: The right edge value
/// * `bottom`: The bottom edge value
/// * `left`: The left edge value
///
pub fn new(top: T, right: T, bottom: T, left: T) -> Self {
Self {
top,
right,
bottom,
left,
}
}
/// Creates a new `Edge` with matching vertical edges and matching horizontal edges
///
/// # Arguments
///
/// * `vertical`: The value of the vertical edges
/// * `horizontal`: The value of the horizontal edges
///
pub fn axis(vertical: T, horizontal: T) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
/// Creates a new `Edge` with all edges having the same value
///
/// # Arguments
///
/// * `value`: The value of all edges
///
pub fn all(value: T) -> Self {
Self {
top: value,
right: value,
bottom: value,
left: value,
}
}
/// Converts this `Edge` into a tuple matching `(Top, Right, Bottom, Left)`
pub fn into_tuple(self) -> (T, T, T, T) {
(self.top, self.right, self.bottom, self.left)
}
}
impl<T> From<Edge<T>> for (T, T, T, T)
where
T: Copy + Default + PartialEq + Reflect,
{
fn from(edge: Edge<T>) -> Self {
edge.into_tuple()
}
}
impl<T> From<T> for Edge<T>
where
T: Copy + Default + PartialEq + Reflect,
{
fn from(value: T) -> Self {
Edge::all(value)
}
}
impl<T> From<(T, T)> for Edge<T>
where
T: Copy + Default + PartialEq + Reflect,
{
fn from(value: (T, T)) -> Self {
Edge::axis(value.0, value.1)
}
}
impl<T> From<(T, T, T, T)> for Edge<T>
where
T: Copy + Default + PartialEq + Reflect,
{
fn from(value: (T, T, T, T)) -> Self {
Edge::new(value.0, value.1, value.2, value.3)
}
}
impl<T> Mul<T> for Edge<T>
where
T: Copy + Default + PartialEq + Mul<Output = T> + Reflect,
{
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
Self {
top: self.top * rhs,
right: self.right * rhs,
bottom: self.bottom * rhs,
left: self.left * rhs,
}
}
}
impl<T> Mul<Edge<T>> for Edge<T>
where
T: Copy + Default + PartialEq + Mul<Output = T> + Reflect,
{
type Output = Self;
fn mul(self, rhs: Edge<T>) -> Self::Output {
Self {
top: rhs.top * self.top,
right: rhs.right * self.right,
bottom: rhs.bottom * self.bottom,
left: rhs.left * self.left,
}
}
}
impl<T> MulAssign<T> for Edge<T>
where
T: Copy + Default + PartialEq + MulAssign + Reflect,
{
fn mul_assign(&mut self, rhs: T) {
self.top *= rhs;
self.right *= rhs;
self.bottom *= rhs;
self.left *= rhs;
}
}
impl<T> MulAssign<Edge<T>> for Edge<T>
where
T: Copy + Default + PartialEq + MulAssign + Reflect,
{
fn mul_assign(&mut self, rhs: Edge<T>) {
self.top *= rhs.top;
self.right *= rhs.right;
self.bottom *= rhs.bottom;
self.left *= rhs.left;
}
}
#[cfg(test)]
mod tests {
use super::Edge;
#[test]
fn tuples_should_convert_to_edge() {
let expected = (1.0, 2.0, 3.0, 4.0);
let edge: Edge<f32> = expected.into();
assert_eq!(expected, edge.into_tuple());
let expected = (1.0, 2.0, 1.0, 2.0);
let edge: Edge<f32> = (expected.0, expected.1).into();
assert_eq!(expected, edge.into_tuple());
let expected = (1.0, 1.0, 1.0, 1.0);
let edge: Edge<f32> = (expected.0).into();
assert_eq!(expected, edge.into_tuple());
let expected = (1.0, 1.0, 1.0, 1.0);
let edge: Edge<f32> = expected.0.into();
assert_eq!(expected, edge.into_tuple());
}
#[test]
fn multiplication_should_work_on_edges() {
let expected = (10.0, 20.0, 30.0, 40.0);
let mut corner = Edge::new(1.0, 2.0, 3.0, 4.0);
// Basic multiplication
let multiplied = corner * 10.0;
assert_eq!(expected, multiplied.into_tuple());
// Multiply and assign
corner *= 10.0;
assert_eq!(expected, corner.into_tuple());
}
}