use crate::*;
/// A binary split or layer.
pub struct Bsp
(
pub(crate) Direction,
/// First element.
pub(crate) Head,
/// Second element.
pub(crate) Tail,
);
impl Bsp {
#[inline] pub const fn n (a: Head, b: Tail) -> Self { Self(North, a, b) }
#[inline] pub const fn s (a: Head, b: Tail) -> Self { Self(South, a, b) }
#[inline] pub const fn e (a: Head, b: Tail) -> Self { Self(East, a, b) }
#[inline] pub const fn w (a: Head, b: Tail) -> Self { Self(West, a, b) }
#[inline] pub const fn a (a: Head, b: Tail) -> Self { Self(Above, a, b) }
#[inline] pub const fn b (a: Head, b: Tail) -> Self { Self(Below, a, b) }
}
impl, Tail: Content> Draw for Bsp {
fn draw (&self, to: &mut O) {
match self.0 {
South => {
panic!("{}", self.1.h(to.area()));
let area_1 = self.1.layout(to.area());
let area_2 = self.2.layout([
to.area().x(),
to.area().y().plus(area_1.h()),
to.area().w(),
to.area().h().minus(area_1.h())
].into());
panic!("{area_1:?} {area_2:?}");
to.place_at(area_1, &self.1);
to.place_at(area_2, &self.2);
},
_ => todo!("{:?}", self.0)
}
//let [a, b, _] = bsp_areas(to.area(), self.0, &self.1, &self.2);
//panic!("{a:?} {b:?}");
//if self.0 == Below {
//to.place_at(a, &self.1);
//to.place_at(b, &self.2);
//} else {
//to.place_at(b, &self.2);
//to.place_at(a, &self.1);
//}
}
}
impl, Tail: Layout> Layout for Bsp {
fn w (&self, area: O::Area) -> O::Unit {
match self.0 {
North | South | Above | Below => self.1.w(area).max(self.2.w(area)),
East | West => self.1.min_w(area).plus(self.2.w(area)),
}
}
fn min_w (&self, area: O::Area) -> O::Unit {
match self.0 {
North | South | Above | Below => self.1.min_w(area).max(self.2.min_w(area)),
East | West => self.1.min_w(area).plus(self.2.min_w(area)),
}
}
fn max_w (&self, area: O::Area) -> O::Unit {
match self.0 {
North | South | Above | Below => self.1.max_w(area).max(self.2.max_w(area)),
East | West => self.1.max_w(area).plus(self.2.max_w(area)),
}
}
fn h (&self, area: O::Area) -> O::Unit {
match self.0 {
East | West | Above | Below => self.1.h(area).max(self.2.h(area)),
North | South => self.1.h(area).plus(self.2.h(area)),
}
}
fn min_h (&self, area: O::Area) -> O::Unit {
match self.0 {
East | West | Above | Below => self.1.min_h(area).max(self.2.min_h(area)),
North | South => self.1.min_h(area).plus(self.2.min_h(area)),
}
}
fn max_h (&self, area: O::Area) -> O::Unit {
match self.0 {
North | South | Above | Below => self.1.max_h(area).max(self.2.max_h(area)),
East | West => self.1.max_h(area).plus(self.2.max_h(area)),
}
}
fn layout (&self, area: O::Area) -> O::Area {
bsp_areas(area, self.0, &self.1, &self.2)[2]
}
}
fn bsp_areas , B: Layout> (
area: O::Area, direction: Direction, a: &A, b: &B,
) -> [O::Area;3] {
let [x, y, w, h] = area.xywh();
let [aw, ah] = a.layout(area).wh();
let [bw, bh] = b.layout(match direction {
Above | Below => area,
South => [x, y + ah, w, h.minus(ah)].into(),
North => [x, y, w, h.minus(ah)].into(),
East => [x + aw, y, w.minus(aw), h].into(),
West => [x, y, w.minus(aw), h].into(),
}).wh();
match direction {
Above | Below => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah.max(bh)]);
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
South => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
North => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
East => {
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
West => {
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
}
}
/// Stack 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 }});