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
|
use crate::{Alignment, Point, Rectangle, Size, Vector};
/// The bounds of an element and its children.
#[derive(Debug, Clone, Default)]
pub struct Node {
bounds: Rectangle,
children: Vec<Node>,
}
impl Node {
/// Creates a new [`Node`] with the given [`Size`].
pub const fn new(size: Size) -> Self {
Self::with_children(size, Vec::new())
}
/// Creates a new [`Node`] with the given [`Size`] and children.
pub const fn with_children(size: Size, children: Vec<Node>) -> Self {
Node {
bounds: Rectangle {
x: 0.0,
y: 0.0,
width: size.width,
height: size.height,
},
children,
}
}
/// Returns the [`Size`] of the [`Node`].
pub fn size(&self) -> Size {
Size::new(self.bounds.width, self.bounds.height)
}
/// Returns the bounds of the [`Node`].
pub fn bounds(&self) -> Rectangle {
self.bounds
}
/// Returns the children of the [`Node`].
pub fn children(&self) -> &[Node] {
&self.children
}
/// Aligns the [`Node`] in the given space.
pub fn align(
&mut self,
horizontal_alignment: Alignment,
vertical_alignment: Alignment,
space: Size,
) {
match horizontal_alignment {
Alignment::Start => {}
Alignment::Center => {
self.bounds.x += (space.width - self.bounds.width) / 2.0;
}
Alignment::End => {
self.bounds.x += space.width - self.bounds.width;
}
Alignment::Fill => {
self.bounds.width = space.width;
}
}
match vertical_alignment {
Alignment::Start => {}
Alignment::Center => {
self.bounds.y += (space.height - self.bounds.height) / 2.0;
}
Alignment::End => {
self.bounds.y += space.height - self.bounds.height;
}
Alignment::Fill => {
self.bounds.height = space.height;
}
}
}
/// Moves the [`Node`] to the given position.
pub fn move_to(&mut self, position: Point) {
self.bounds.x = position.x;
self.bounds.y = position.y;
}
/// Translates the [`Node`] by the given translation.
pub fn translate(self, translation: Vector) -> Self {
Self {
bounds: self.bounds + translation,
..self
}
}
}
|