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
|
//! Geometrical types.
use std::fmt::{self, Debug, Formatter};
use std::ops::*;
use crate::layout::prelude::*;
/// A value in two dimensions.
#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub struct Value2<T> {
/// The horizontal component.
pub x: T,
/// The vertical component.
pub y: T,
}
impl<T: Clone> Value2<T> {
/// Create a new 2D-value from two values.
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
/// Create a new 2D-value with `x` set to a value and `y` to default.
pub fn with_x(x: T) -> Self where T: Default {
Self { x, y: T::default() }
}
/// Create a new 2D-value with `y` set to a value and `x` to default.
pub fn with_y(y: T) -> Self where T: Default {
Self { x: T::default(), y }
}
/// Create a 2D-value with `x` and `y` set to the same value `s`.
pub fn with_all(s: T) -> Self {
Self { x: s.clone(), y: s }
}
/// Get the specificed component.
pub fn get(self, axis: SpecAxis) -> T {
match axis {
Horizontal => self.x,
Vertical => self.y,
}
}
/// Borrow the specificed component mutably.
pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
match axis {
Horizontal => &mut self.x,
Vertical => &mut self.y,
}
}
/// Return the primary value of this specialized 2D-value.
pub fn primary(self, axes: LayoutAxes) -> T {
if axes.primary.axis() == Horizontal { self.x } else { self.y }
}
/// Borrow the primary value of this specialized 2D-value mutably.
pub fn primary_mut(&mut self, axes: LayoutAxes) -> &mut T {
if axes.primary.axis() == Horizontal { &mut self.x } else { &mut self.y }
}
/// Return the secondary value of this specialized 2D-value.
pub fn secondary(self, axes: LayoutAxes) -> T {
if axes.primary.axis() == Horizontal { self.y } else { self.x }
}
/// Borrow the secondary value of this specialized 2D-value mutably.
pub fn secondary_mut(&mut self, axes: LayoutAxes) -> &mut T {
if axes.primary.axis() == Horizontal { &mut self.y } else { &mut self.x }
}
/// Returns the generalized version of a `Size2D` dependent on the layouting
/// axes, that is:
/// - `x` describes the primary axis instead of the horizontal one.
/// - `y` describes the secondary axis instead of the vertical one.
pub fn generalized(self, axes: LayoutAxes) -> Self {
match axes.primary.axis() {
Horizontal => self,
Vertical => Self { x: self.y, y: self.x },
}
}
/// Returns the specialized version of this generalized Size2D (inverse to
/// `generalized`).
pub fn specialized(self, axes: LayoutAxes) -> Self {
// In fact, generalized is its own inverse. For reasons of clarity
// at the call site, we still have this second function.
self.generalized(axes)
}
/// Swap the `x` and `y` values.
pub fn swap(&mut self) {
std::mem::swap(&mut self.x, &mut self.y);
}
}
impl<T: Debug> Debug for Value2<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_list()
.entry(&self.x)
.entry(&self.y)
.finish()
}
}
/// A position or extent in 2-dimensional space.
pub type Size = Value2<f64>;
impl Size {
/// The zeroed size.
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
/// Whether the given size fits into this one, that is, both coordinate
/// values are smaller or equal.
pub fn fits(self, other: Self) -> bool {
self.x >= other.x && self.y >= other.y
}
/// Return a size padded by the paddings of the given box.
pub fn padded(self, padding: Margins) -> Self {
Size {
x: self.x + padding.left + padding.right,
y: self.y + padding.top + padding.bottom,
}
}
/// Return a size reduced by the paddings of the given box.
pub fn unpadded(self, padding: Margins) -> Self {
Size {
x: self.x - padding.left - padding.right,
y: self.y - padding.top - padding.bottom,
}
}
/// The anchor position along the given axis for an item with the given
/// alignment in a container with this size.
///
/// This assumes the size to be generalized such that `x` corresponds to the
/// primary axis.
pub fn anchor(self, align: LayoutAlign, axes: LayoutAxes) -> Self {
Size {
x: anchor(self.x, align.primary, axes.primary),
y: anchor(self.y, align.secondary, axes.secondary),
}
}
}
fn anchor(length: f64, align: GenAlign, dir: Dir) -> f64 {
match (dir.is_positive(), align) {
(true, Start) | (false, End) => 0.0,
(_, Center) => length / 2.0,
(true, End) | (false, Start) => length,
}
}
impl Neg for Size {
type Output = Size;
fn neg(self) -> Size {
Size {
x: -self.x,
y: -self.y,
}
}
}
/// A value in four dimensions.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Value4<T> {
/// The left extent.
pub left: T,
/// The top extent.
pub top: T,
/// The right extent.
pub right: T,
/// The bottom extent.
pub bottom: T,
}
impl<T: Clone> Value4<T> {
/// Create a new box from four sizes.
pub fn new(left: T, top: T, right: T, bottom: T) -> Self {
Value4 { left, top, right, bottom }
}
/// Create a box with all four fields set to the same value `s`.
pub fn with_all(value: T) -> Self {
Value4 {
left: value.clone(),
top: value.clone(),
right: value.clone(),
bottom: value,
}
}
/// Get a mutable reference to the value for the specified direction at the
/// alignment.
///
/// Center alignment is treated the same as origin alignment.
pub fn get_mut(&mut self, mut dir: Dir, align: GenAlign) -> &mut T {
if align == End {
dir = dir.inv();
}
match dir {
LTR => &mut self.left,
RTL => &mut self.right,
TTB => &mut self.top,
BTT => &mut self.bottom,
}
}
/// Set all values to the given value.
pub fn set_all(&mut self, value: T) {
*self = Value4::with_all(value);
}
}
/// A length in four dimensions.
pub type Margins = Value4<f64>;
impl Margins {
/// The zero margins.
pub const ZERO: Margins = Margins {
left: 0.0,
top: 0.0,
right: 0.0,
bottom: 0.0,
};
}
macro_rules! implement_traits {
($ty:ident, $t:ident, $o:ident
reflexive {$(
($tr:ident($tf:ident), $at:ident($af:ident), [$($f:ident),*])
)*}
numbers { $(($w:ident: $($rest:tt)*))* }
) => {
$(impl $tr for $ty {
type Output = $ty;
fn $tf($t, $o: $ty) -> $ty {
$ty { $($f: $tr::$tf($t.$f, $o.$f),)* }
}
}
impl $at for $ty {
fn $af(&mut $t, $o: $ty) { $($at::$af(&mut $t.$f, $o.$f);)* }
})*
$(implement_traits!(@$w f64, $ty $t $o $($rest)*);)*
};
(@front $num:ty, $ty:ident $t:ident $o:ident
$tr:ident($tf:ident),
[$($f:ident),*]
) => {
impl $tr<$ty> for $num {
type Output = $ty;
fn $tf($t, $o: $ty) -> $ty {
$ty { $($f: $tr::$tf($t as f64, $o.$f),)* }
}
}
};
(@back $num:ty, $ty:ident $t:ident $o:ident
$tr:ident($tf:ident), $at:ident($af:ident),
[$($f:ident),*]
) => {
impl $tr<$num> for $ty {
type Output = $ty;
fn $tf($t, $o: $num) -> $ty {
$ty { $($f: $tr::$tf($t.$f, $o as f64),)* }
}
}
impl $at<$num> for $ty {
fn $af(&mut $t, $o: $num) { $($at::$af(&mut $t.$f, $o as f64);)* }
}
};
}
macro_rules! implement_size {
($ty:ident($t:ident, $o:ident) [$($f:ident),*]) => {
implement_traits! {
$ty, $t, $o
reflexive {
(Add(add), AddAssign(add_assign), [$($f),*])
(Sub(sub), SubAssign(sub_assign), [$($f),*])
}
numbers {
(front: Mul(mul), [$($f),*])
(back: Mul(mul), MulAssign(mul_assign), [$($f),*])
(back: Div(div), DivAssign(div_assign), [$($f),*])
}
}
};
}
implement_size! { Size(self, other) [x, y] }
|