wip: hook up more builtins

This commit is contained in:
🪞👃🪞 2025-01-13 23:39:06 +01:00
parent fa70a42bad
commit 811e341cd5
4 changed files with 94 additions and 64 deletions

View file

@ -1,10 +1,8 @@
use crate::*;
pub use self::Direction::*;
/// A cardinal direction.
#[derive(Copy, Clone, PartialEq)]
pub enum Direction { North, South, East, West, Above, Below }
impl Direction {
pub fn split_fixed <N: Coordinate> (self, area: impl Area<N>, a: N) -> ([N;4],[N;4]) {
let [x, y, w, h] = area.xywh();
@ -17,10 +15,8 @@ impl Direction {
}
}
}
pub struct Bsp<X, Y>(Direction, X, Y);
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
pub struct Bsp<E, X, Y>(PhantomData<E>, Direction, X, Y);
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<E, A, B> {
fn layout (&self, outer: E::Area) -> E::Area {
let [_, _, c] = self.areas(outer);
c
@ -28,22 +24,20 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
fn render (&self, to: &mut E) {
let [area_a, area_b, _] = self.areas(to.area());
let (a, b) = self.contents();
match self.0 {
match self.1 {
Below => { to.place(area_a, a); to.place(area_b, b); },
_ => { to.place(area_b, b); to.place(area_a, a); }
}
}
}
impl<A, B> Bsp<A, B> {
pub fn n (a: A, b: B) -> Self { Self(North, a, b) }
pub fn s (a: A, b: B) -> Self { Self(South, a, b) }
pub fn e (a: A, b: B) -> Self { Self(East, a, b) }
pub fn w (a: A, b: B) -> Self { Self(West, a, b) }
pub fn a (a: A, b: B) -> Self { Self(Above, a, b) }
pub fn b (a: A, b: B) -> Self { Self(Below, a, b) }
impl<E, A, B> Bsp<E, A, B> {
pub fn n (a: A, b: B) -> Self { Self(Default::default(), North, a, b) }
pub fn s (a: A, b: B) -> Self { Self(Default::default(), South, a, b) }
pub fn e (a: A, b: B) -> Self { Self(Default::default(), East, a, b) }
pub fn w (a: A, b: B) -> Self { Self(Default::default(), West, a, b) }
pub fn a (a: A, b: B) -> Self { Self(Default::default(), Above, a, b) }
pub fn b (a: A, b: B) -> Self { Self(Default::default(), Below, a, b) }
}
pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
fn direction (&self) -> Direction;
fn contents (&self) -> (&A, &B);
@ -51,14 +45,14 @@ pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
let direction = self.direction();
let [x, y, w, h] = outer.xywh();
let (a, b) = self.contents();
let [ax, ay, aw, ah] = a.layout(outer).xywh();
let [bx, by, bw, bh] = b.layout(match direction {
let [aw, ah] = a.layout(outer).wh();
let [bw, bh] = b.layout(match direction {
Above | Below => outer,
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(),
}).xywh();
}).wh();
match direction {
Above | Below => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]);
@ -93,10 +87,29 @@ pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
}
}
}
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> {
fn direction (&self) -> Direction { self. 0 }
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<E, A, B> {
fn direction (&self) -> Direction { self.1 }
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
}
impl<'a, T, E, A, B> TryFromEdn<'a, T> for Bsp<E, A, B>
where
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + EdnProvide<'a, B> + 'a,
E: Output,
A: Render<E> + 'a,
B: Render<E> + 'a,
{
fn try_from_edn (s: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
use EdnItem::*;
Some(match (head, tail) {
(Key("bsp/n"), [a, b]) => Self::n(s.get(a).expect("no a"), s.get(b).expect("no b")),
(Key("bsp/s"), [a, b]) => Self::s(s.get(a).expect("no a"), s.get(b).expect("no b")),
(Key("bsp/e"), [a, b]) => Self::e(s.get(a).expect("no a"), s.get(b).expect("no b")),
(Key("bsp/w"), [a, b]) => Self::w(s.get(a).expect("no a"), s.get(b).expect("no b")),
(Key("bsp/a"), [a, b]) => Self::a(s.get(a).expect("no a"), s.get(b).expect("no b")),
(Key("bsp/b"), [a, b]) => Self::b(s.get(a).expect("no a"), s.get(b).expect("no b")),
_ => return None
})
}
}
/// Renders multiple things on top of each other,