still dark; refactor and document layout crate

This commit is contained in:
🪞👃🪞 2024-12-31 19:57:03 +01:00
parent ed72ab1635
commit 675d376100
12 changed files with 510 additions and 659 deletions

View file

@ -50,146 +50,3 @@ impl Direction {
}
}
}
/// Renders multiple things on top of each other,
#[macro_export] macro_rules! lay {
($($expr:expr),* $(,)?) => {{
let bsp = ();
$(let bsp = Bsp::b(bsp, $expr);)*;
bsp
}}
}
/// Stack southward.
#[macro_export] macro_rules! col {
($($expr:expr),* $(,)?) => {{
let bsp = ();
$(let bsp = Bsp::s(bsp, $expr);)*;
bsp
}};
}
/// Stack northward.
#[macro_export] macro_rules! col_up {
($($expr:expr),* $(,)?) => {{
let bsp = ();
$(let bsp = Bsp::n(bsp, $expr);)*;
bsp
}}
}
/// Stack eastward.
#[macro_export] macro_rules! row {
($($expr:expr),* $(,)?) => {{
let bsp = ();
$(let bsp = Bsp::e(bsp, $expr);)*;
bsp
}};
}
pub enum Bsp<E: Engine, X: Content<E>, Y: Content<E>> {
/// X is north of Y
North(Option<f64>, Option<X>, Option<Y>),
/// X is south of Y
South(Option<f64>, Option<X>, Option<Y>),
/// X is east of Y
East(Option<f64>, Option<X>, Option<Y>),
/// X is west of Y
West(Option<f64>, Option<X>, Option<Y>),
/// X is above Y
Above(Option<X>, Option<Y>),
/// X is below Y
Below(Option<X>, Option<Y>),
/// Should be avoided.
Null(PhantomData<E>),
}
impl<E: Engine, X: Content<E>, Y: Content<E>> Bsp<E, X, Y> {
pub fn n (x: X, y: Y) -> Self { Self::North(None, Some(x), Some(y)) }
pub fn s (x: X, y: Y) -> Self { Self::South(None, Some(x), Some(y)) }
pub fn e (x: X, y: Y) -> Self { Self::East(None, Some(x), Some(y)) }
pub fn w (x: X, y: Y) -> Self { Self::West(None, Some(x), Some(y)) }
pub fn a (x: X, y: Y) -> Self { Self::Above(Some(x), Some(y)) }
pub fn b (x: X, y: Y) -> Self { Self::Below(Some(x), Some(y)) }
}
impl<E: Engine, X: Content<E>, Y: Content<E>> Default for Bsp<E, X, Y> {
fn default () -> Self {
Self::Null(Default::default())
}
}
impl<E: Engine, X: Content<E>, Y: Content<E>> Content<E> for Bsp<E, X, Y> {
fn area (&self, outer: E::Area) -> E::Area {
match self {
Self::Null(_) => [0.into(), 0.into(), 0.into(), 0.into()].into(),
Self::North(_, a, b) => {
let a = a.area(outer);
let b = b.area(North.split_fixed(outer, a.y() + a.h()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h() + b.h()].into()
}
Self::South(_, a, b) => {
let a = a.area(outer);
let b = b.area(South.split_fixed(outer, a.y() + a.h()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h() + b.h()].into()
},
Self::East(_, a, b) => {
let a = a.area(outer);
let b = b.area(East.split_fixed(outer, a.x() + a.w()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w() + b.w(), a.h().max(b.h())].into()
},
Self::West(_, a, b) => {
let a = a.area(outer);
let b = b.area(West.split_fixed(outer, a.x() + a.w()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w() + b.w(), a.h().max(b.h())].into()
},
Self::Above(a, b) | Self::Below(a, b) => {
let a = a.area(outer);
let b = b.area(outer);
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h().max(b.h())].into()
}
}
}
fn render (&self, to: &mut E::Output) {
let area = to.area().clone();
match self {
Self::North(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(North.split_fixed(area, area_a.y() + area_a.h()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
Self::South(_, a, b) => {
let area_a = a.area(area).clone();
let area_b = b.area(South.split_fixed(area, area_a.y() + area_a.h()).1.into()).clone();
to.place(area_a, a);
to.place(area_b, b);
},
Self::East(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(East.split_fixed(area, area_a.x() + area_a.w()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
Self::West(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(West.split_fixed(area, area_a.x() + area_a.w()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
Self::Above(a, b) => {
let area_a = a.area(area);
let area_b = b.area(area);
to.place(area_b, b);
to.place(area_a, a);
},
Self::Below(a, b) => {
let area_a = a.area(area);
let area_b = b.area(area);
to.place(area_a, a);
to.place(area_b, b);
},
Self::Null(_) => {}
}
}
}