remove LayoutSplit; merge split and bsp modules

This commit is contained in:
🪞👃🪞 2024-12-30 12:54:19 +01:00
parent 0c9c386a79
commit 35a88cb70f
8 changed files with 117 additions and 154 deletions

View file

@ -1,36 +1,6 @@
use crate::*;
use Direction::*;
impl<E: Engine> LayoutSplit<E> for E {}
pub trait LayoutSplit<E: Engine> {
fn split <A: Render<E>, B: Render<E>> (
flip: bool, direction: Direction, amount: E::Unit, a: A, b: B
) -> Split<E, A, B> {
Split::new(flip, direction, amount, a, b)
}
fn split_n <A: Render<E>, B: Render<E>> (
flip: bool, amount: E::Unit, a: A, b: B
) -> Split<E, A, B> {
Self::split(flip, North, amount, a, b)
}
fn split_s <A: Render<E>, B: Render<E>> (
flip: bool, amount: E::Unit, a: A, b: B
) -> Split<E, A, B> {
Self::split(flip, South, amount, a, b)
}
fn split_w <A: Render<E>, B: Render<E>> (
flip: bool, amount: E::Unit, a: A, b: B
) -> Split<E, A, B> {
Self::split(flip, West, amount, a, b)
}
fn split_e <A: Render<E>, B: Render<E>> (
flip: bool, amount: E::Unit, a: A, b: B
) -> Split<E, A, B> {
Self::split(flip, East, amount, a, b)
}
}
/// A binary split with fixed proportion
pub struct Split<E: Engine, A: Render<E>, B: Render<E>>(
pub bool, pub Direction, pub E::Unit, A, B, PhantomData<E>
@ -40,16 +10,16 @@ impl<E: Engine, A: Render<E>, B: Render<E>> Split<E, A, B> {
pub fn new (flip: bool, direction: Direction, proportion: E::Unit, a: A, b: B) -> Self {
Self(flip, direction, proportion, a, b, Default::default())
}
pub fn up (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
pub fn n (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
Self(flip, North, proportion, a, b, Default::default())
}
pub fn down (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
pub fn s (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
Self(flip, South, proportion, a, b, Default::default())
}
pub fn left (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
pub fn e (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
Self(flip, West, proportion, a, b, Default::default())
}
pub fn right (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
pub fn w (flip: bool, proportion: E::Unit, a: A, b: B) -> Self {
Self(flip, East, proportion, a, b, Default::default())
}
}
@ -69,3 +39,105 @@ impl<E: Engine, A: Render<E>, B: Render<E>> Render<E> for Split<E, A, B> {
})
}
}
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::E(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()
},
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()
},
Self::N(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()
},
_ => todo!()
}))
}
fn render (&self, to: &mut E::Output) -> Usually<()> {
let n = [0.into(), 0.into()].into();
let s = to.area().wh().into();
Ok(match self {
Self::Null(_) => {},
Self::S(a, b) => {
let s_a = a.min_size(s)?.unwrap_or(n);
let _ = b.min_size(s)?.unwrap_or(n);
let h = s_a.h().into();
to.render_in(to.area().clip_h(h).into(), a)?;
to.render_in(to.area().shrink_y(h).push_y(h).into(), b)?;
},
Self::E(a, b) => {
let s_a = a.min_size(s)?.unwrap_or(n);
let _ = b.min_size(s)?.unwrap_or(n);
let w = s_a.w().into();
to.render_in(to.area().clip_w(w).into(), a)?;
to.render_in(to.area().push_x(w).shrink_x(w).into(), b)?;
},
Self::W(a, b) => {
let s_a = a.min_size(s)?.unwrap_or(n);
let _ = b.min_size(s)?.unwrap_or(n);
let w = (to.area().w() - s_a.w()).into();
to.render_in(to.area().push_x(w).into(), a)?;
to.render_in(to.area().shrink_x(w).into(), b)?;
},
Self::N(a, b) => {
let s_a = a.min_size(s)?.unwrap_or(n);
let _ = b.min_size(s)?.unwrap_or(n);
let h = to.area().h() - s_a.h();
to.render_in(to.area().push_y(h).into(), a)?;
to.render_in(to.area().shrink_y(h).into(), b)?;
},
_ => todo!()
})
}
}
#[cfg(test)] #[test] fn test_bsp () {
// TODO
}