2023-03-16 20:23:25 +01:00
|
|
|
use crate::{Alignment, Padding, Point, Rectangle, Size, Vector};
|
2019-11-10 06:05:20 +01:00
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// The bounds of an element and its children.
|
2019-11-10 06:05:20 +01:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
|
pub struct Node {
|
2020-02-14 21:41:35 +01:00
|
|
|
bounds: Rectangle,
|
2019-11-10 06:05:20 +01:00
|
|
|
children: Vec<Node>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Node {
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Creates a new [`Node`] with the given [`Size`].
|
2019-11-29 21:24:52 -05:00
|
|
|
pub const fn new(size: Size) -> Self {
|
2020-02-14 21:41:35 +01:00
|
|
|
Self::with_children(size, Vec::new())
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Creates a new [`Node`] with the given [`Size`] and children.
|
2019-11-29 21:24:52 -05:00
|
|
|
pub const fn with_children(size: Size, children: Vec<Node>) -> Self {
|
2019-11-10 06:05:20 +01:00
|
|
|
Node {
|
|
|
|
|
bounds: Rectangle {
|
2020-02-14 21:41:35 +01:00
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
2019-11-10 06:05:20 +01:00
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
|
|
|
|
},
|
|
|
|
|
children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 20:23:25 +01:00
|
|
|
/// Creates a new [`Node`] that wraps a single child with some [`Padding`].
|
|
|
|
|
pub fn container(child: Self, padding: Padding) -> Self {
|
|
|
|
|
Self::with_children(
|
|
|
|
|
child.bounds.size().expand(padding),
|
|
|
|
|
vec![child.move_to(Point::new(padding.left, padding.top))],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the [`Size`] of the [`Node`].
|
2019-11-10 06:05:20 +01:00
|
|
|
pub fn size(&self) -> Size {
|
|
|
|
|
Size::new(self.bounds.width, self.bounds.height)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the bounds of the [`Node`].
|
2019-11-10 06:05:20 +01:00
|
|
|
pub fn bounds(&self) -> Rectangle {
|
|
|
|
|
self.bounds
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the children of the [`Node`].
|
2019-11-10 06:05:20 +01:00
|
|
|
pub fn children(&self) -> &[Node] {
|
|
|
|
|
&self.children
|
|
|
|
|
}
|
2019-11-11 05:26:08 +01:00
|
|
|
|
2020-02-14 23:23:45 +01:00
|
|
|
/// Aligns the [`Node`] in the given space.
|
2020-02-14 15:36:33 +01:00
|
|
|
pub fn align(
|
2023-03-16 20:23:25 +01:00
|
|
|
mut self,
|
|
|
|
|
horizontal_alignment: Alignment,
|
|
|
|
|
vertical_alignment: Alignment,
|
|
|
|
|
space: Size,
|
|
|
|
|
) -> Self {
|
|
|
|
|
self.align_mut(horizontal_alignment, vertical_alignment, space);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Mutable reference version of [`align`].
|
|
|
|
|
pub fn align_mut(
|
2019-11-11 05:26:08 +01:00
|
|
|
&mut self,
|
2021-09-20 15:09:55 +07:00
|
|
|
horizontal_alignment: Alignment,
|
|
|
|
|
vertical_alignment: Alignment,
|
2019-11-11 05:26:08 +01:00
|
|
|
space: Size,
|
|
|
|
|
) {
|
|
|
|
|
match horizontal_alignment {
|
2021-09-20 15:09:55 +07:00
|
|
|
Alignment::Start => {}
|
|
|
|
|
Alignment::Center => {
|
2019-11-11 05:26:08 +01:00
|
|
|
self.bounds.x += (space.width - self.bounds.width) / 2.0;
|
|
|
|
|
}
|
2021-09-20 15:09:55 +07:00
|
|
|
Alignment::End => {
|
2019-11-11 05:37:51 +01:00
|
|
|
self.bounds.x += space.width - self.bounds.width;
|
|
|
|
|
}
|
2019-11-11 05:26:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match vertical_alignment {
|
2021-09-20 15:09:55 +07:00
|
|
|
Alignment::Start => {}
|
|
|
|
|
Alignment::Center => {
|
2019-11-11 05:26:08 +01:00
|
|
|
self.bounds.y += (space.height - self.bounds.height) / 2.0;
|
|
|
|
|
}
|
2021-09-20 15:09:55 +07:00
|
|
|
Alignment::End => {
|
2019-11-11 05:37:51 +01:00
|
|
|
self.bounds.y += space.height - self.bounds.height;
|
|
|
|
|
}
|
2019-11-11 05:26:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-14 21:41:35 +01:00
|
|
|
|
2020-02-14 23:23:45 +01:00
|
|
|
/// Moves the [`Node`] to the given position.
|
2024-01-09 06:35:33 +01:00
|
|
|
pub fn move_to(mut self, position: impl Into<Point>) -> Self {
|
2023-03-16 20:23:25 +01:00
|
|
|
self.move_to_mut(position);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Mutable reference version of [`move_to`].
|
2024-01-09 06:35:33 +01:00
|
|
|
pub fn move_to_mut(&mut self, position: impl Into<Point>) {
|
|
|
|
|
let position = position.into();
|
|
|
|
|
|
2020-02-14 21:41:35 +01:00
|
|
|
self.bounds.x = position.x;
|
|
|
|
|
self.bounds.y = position.y;
|
|
|
|
|
}
|
2021-12-13 17:46:39 +07:00
|
|
|
|
|
|
|
|
/// Translates the [`Node`] by the given translation.
|
2024-01-09 06:35:33 +01:00
|
|
|
pub fn translate(self, translation: impl Into<Vector>) -> Self {
|
|
|
|
|
let translation = translation.into();
|
|
|
|
|
|
2021-12-13 17:46:39 +07:00
|
|
|
Self {
|
|
|
|
|
bounds: self.bounds + translation,
|
|
|
|
|
..self
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|