use bevy::prelude::{default, Bundle};
use crate::{Alignment, Distribution, Oriented, Size};
use crate::{Container, Flow, LayoutRect, LeafRule, Node, Root, Rule, ScreenRoot};
#[derive(Debug, Clone, Copy)]
pub struct Layout {
pub flow: Flow,
pub align: Alignment,
pub distrib: Distribution,
pub margin: Oriented<f32>,
pub size: Size<Option<Rule>>,
}
impl Default for Layout {
fn default() -> Self {
Self {
align: Alignment::Center,
distrib: Distribution::FillMain,
margin: Oriented::default(),
size: Size::all(None),
flow: Flow::Horizontal,
}
}
}
impl Layout {
#[must_use]
pub fn container(&self) -> Container {
Container {
flow: self.flow,
align: self.align,
distrib: self.distrib,
rules: self.size.map(|r| r.unwrap_or(Rule::Children(1.5))),
margin: self.flow.absolute(self.margin),
}
}
}
#[derive(Bundle, Default)]
pub struct RootBundle {
pub pos_rect: LayoutRect,
pub root: Root,
pub screen_root: ScreenRoot,
}
impl RootBundle {
#[must_use]
pub fn new(Layout { align, distrib, margin, flow, .. }: Layout) -> Self {
let size = Size::all(f32::MAX);
Self {
pos_rect: default(),
root: Root::new(size, flow, align, distrib, flow.absolute(margin)),
screen_root: ScreenRoot,
}
}
}
#[derive(Bundle, Default, Debug)]
pub struct LayoutBundle {
pub pos_rect: LayoutRect,
pub node: Node,
}
impl LayoutBundle {
#[must_use]
pub fn node(container: Container) -> Self {
Self { node: Node::Container(container), ..default() }
}
#[must_use]
pub fn boxy(size: Size<LeafRule>) -> Self {
Self { node: Node::Box(size), ..default() }
}
}