tek/crates/tek/src/layout/bsp.rs

77 lines
2.9 KiB
Rust

use crate::*;
pub enum Bsp<E: Engine, X: Render<E>, Y: Render<E>> {
/// X is north of Y
N(Option<X>, Option<Y>),
/// X is south of Y
S(Option<X>, Option<Y>),
/// X is east of Y
E(Option<X>, Option<Y>),
/// X is west of Y
W(Option<X>, Option<Y>),
/// X is above Y
A(Option<X>, Option<Y>),
/// X is below Y
B(Option<X>, Option<Y>),
/// Should be avoided.
Null(PhantomData<E>),
}
impl<E: Engine, X: Render<E>, Y: Render<E>> Bsp<E, X, Y> {
pub fn new (x: X) -> Self { Self::A(Some(x), None) }
pub fn n (x: X, y: Y) -> Self { Self::N(Some(x), Some(y)) }
pub fn s (x: X, y: Y) -> Self { Self::S(Some(x), Some(y)) }
pub fn e (x: X, y: Y) -> Self { Self::E(Some(x), Some(y)) }
pub fn w (x: X, y: Y) -> Self { Self::W(Some(x), Some(y)) }
pub fn a (x: X, y: Y) -> Self { Self::A(Some(x), Some(y)) }
pub fn b (x: X, y: Y) -> Self { Self::B(Some(x), Some(y)) }
}
impl<E: Engine, X: Render<E>, Y: Render<E>> Default for Bsp<E, X, Y> {
fn default () -> Self {
Self::Null(Default::default())
}
}
impl<E: Engine, X: Render<E>, Y: Render<E>> Render<E> for Bsp<E, X, Y> {
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
Ok(Some(match self {
Self::Null(_) => [0.into(), 0.into()].into(),
Self::S(a, b) => {
let a = a.min_size(to)?.unwrap_or([0.into(), 0.into()].into());
let b = b.min_size(to)?.unwrap_or([0.into(), 0.into()].into());
[a.w().max(b.w()), a.h() + b.h()].into()
},
Self::W(a, b) => {
let a = a.min_size(to)?.unwrap_or([0.into(), 0.into()].into());
let b = b.min_size(to)?.unwrap_or([0.into(), 0.into()].into());
[a.w() + b.w(), a.h().max(b.h())].into()
},
_ => todo!()
}))
}
fn render (&self, to: &mut E::Output) -> Usually<()> {
Ok(match self {
Self::Null(_) => {},
Self::S(a, b) => {
let n = [0.into(), 0.into()].into();
let s = to.area().wh().into();
let s_a = a.min_size(s)?.unwrap_or(n);
let s_b = b.min_size(s)?.unwrap_or(n);
let s_y = s_a.h().into();
to.render_in(to.area().clip_h(s_y).into(), a)?;
to.render_in(to.area().push_y(s_y).shrink_y(s_y).into(), b)?;
},
Self::W(a, b) => {
let n = [0.into(), 0.into()].into();
let s = to.area().wh().into();
let s_a = a.min_size(s)?.unwrap_or(n);
let s_b = b.min_size(s)?.unwrap_or(n);
let s_x = (to.area().w() - s_a.w()).into();
to.render_in(to.area().push_x(s_x).into(), a)?;
to.render_in(to.area().shrink_x(s_x).into(), b)?;
},
_ => todo!()
})
}
}