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,27 +1,23 @@
use crate::*; use crate::*;
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone, Default)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<E, A>(PhantomData<E>, Alignment, A);
pub struct Align<T>(Alignment, T); impl<E, A> Align<E, A> {
pub fn c (a: A) -> Self { Self(Default::default(), Alignment::Center, a) }
impl<T> Align<T> { pub fn x (a: A) -> Self { Self(Default::default(), Alignment::X, a) }
pub fn c (a: T) -> Self { Self(Alignment::Center, a) } pub fn y (a: A) -> Self { Self(Default::default(), Alignment::Y, a) }
pub fn x (a: T) -> Self { Self(Alignment::X, a) } pub fn n (a: A) -> Self { Self(Default::default(), Alignment::N, a) }
pub fn y (a: T) -> Self { Self(Alignment::Y, a) } pub fn s (a: A) -> Self { Self(Default::default(), Alignment::S, a) }
pub fn n (a: T) -> Self { Self(Alignment::N, a) } pub fn e (a: A) -> Self { Self(Default::default(), Alignment::E, a) }
pub fn s (a: T) -> Self { Self(Alignment::S, a) } pub fn w (a: A) -> Self { Self(Default::default(), Alignment::W, a) }
pub fn e (a: T) -> Self { Self(Alignment::E, a) } pub fn nw (a: A) -> Self { Self(Default::default(), Alignment::NW, a) }
pub fn w (a: T) -> Self { Self(Alignment::W, a) } pub fn sw (a: A) -> Self { Self(Default::default(), Alignment::SW, a) }
pub fn nw (a: T) -> Self { Self(Alignment::NW, a) } pub fn ne (a: A) -> Self { Self(Default::default(), Alignment::NE, a) }
pub fn sw (a: T) -> Self { Self(Alignment::SW, a) } pub fn se (a: A) -> Self { Self(Default::default(), Alignment::SE, a) }
pub fn ne (a: T) -> Self { Self(Alignment::NE, a) }
pub fn se (a: T) -> Self { Self(Alignment::SE, a) }
} }
impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
impl<E: Output, T: Content<E>> Content<E> for Align<T> {
fn content (&self) -> impl Render<E> { fn content (&self) -> impl Render<E> {
&self.1 &self.2
} }
fn layout (&self, on: E::Area) -> E::Area { fn layout (&self, on: E::Area) -> E::Area {
use Alignment::*; use Alignment::*;
@ -30,7 +26,7 @@ impl<E: Output, T: Content<E>> Content<E> for Align<T> {
let cy = on.y()+(on.h().minus(it.h())/2.into()); let cy = on.y()+(on.h().minus(it.h())/2.into());
let fx = (on.x()+on.w()).minus(it.w()); let fx = (on.x()+on.w()).minus(it.w());
let fy = (on.y()+on.h()).minus(it.h()); let fy = (on.y()+on.h()).minus(it.h());
let [x, y] = match self.0 { let [x, y] = match self.1 {
Center => [cx, cy], Center => [cx, cy],
X => [cx, it.y()], X => [cx, it.y()],
Y => [it.x(), cy], Y => [it.x(), cy],
@ -49,3 +45,27 @@ impl<E: Output, T: Content<E>> Content<E> for Align<T> {
to.place(Content::layout(self, to.area()), &self.content()) to.place(Content::layout(self, to.area()), &self.content())
} }
} }
impl<'a, T, E, A> TryFromEdn<'a, T> for Align<E, A>
where
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a,
E: Output,
A: Render<E> + 'a,
{
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
use EdnItem::*;
Some(match (head, tail) {
(Key("align/c"), [a]) => Self::c(state.get(a).expect("no content")),
(Key("align/x"), [a]) => Self::x(state.get(a).expect("no content")),
(Key("align/y"), [a]) => Self::y(state.get(a).expect("no content")),
(Key("align/n"), [a]) => Self::n(state.get(a).expect("no content")),
(Key("align/s"), [a]) => Self::s(state.get(a).expect("no content")),
(Key("align/e"), [a]) => Self::e(state.get(a).expect("no content")),
(Key("align/w"), [a]) => Self::w(state.get(a).expect("no content")),
(Key("align/nw"), [a]) => Self::nw(state.get(a).expect("no content")),
(Key("align/ne"), [a]) => Self::ne(state.get(a).expect("no content")),
(Key("align/sw"), [a]) => Self::sw(state.get(a).expect("no content")),
(Key("align/se"), [a]) => Self::se(state.get(a).expect("no content")),
_ => return None
})
}
}

View file

@ -1,10 +1,8 @@
use crate::*; use crate::*;
pub use self::Direction::*; pub use self::Direction::*;
/// A cardinal direction. /// A cardinal direction.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum Direction { North, South, East, West, Above, Below } pub enum Direction { North, South, East, West, Above, Below }
impl Direction { impl Direction {
pub fn split_fixed <N: Coordinate> (self, area: impl Area<N>, a: N) -> ([N;4],[N;4]) { pub fn split_fixed <N: Coordinate> (self, area: impl Area<N>, a: N) -> ([N;4],[N;4]) {
let [x, y, w, h] = area.xywh(); let [x, y, w, h] = area.xywh();
@ -17,10 +15,8 @@ impl Direction {
} }
} }
} }
pub struct Bsp<E, X, Y>(PhantomData<E>, Direction, X, Y);
pub struct Bsp<X, Y>(Direction, X, Y); impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<E, A, B> {
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
fn layout (&self, outer: E::Area) -> E::Area { fn layout (&self, outer: E::Area) -> E::Area {
let [_, _, c] = self.areas(outer); let [_, _, c] = self.areas(outer);
c 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) { fn render (&self, to: &mut E) {
let [area_a, area_b, _] = self.areas(to.area()); let [area_a, area_b, _] = self.areas(to.area());
let (a, b) = self.contents(); let (a, b) = self.contents();
match self.0 { match self.1 {
Below => { to.place(area_a, a); to.place(area_b, b); }, Below => { to.place(area_a, a); to.place(area_b, b); },
_ => { to.place(area_b, b); to.place(area_a, a); } _ => { to.place(area_b, b); to.place(area_a, a); }
} }
} }
} }
impl<E, A, B> Bsp<E, A, B> {
impl<A, B> Bsp<A, B> { pub fn n (a: A, b: B) -> Self { Self(Default::default(), North, a, b) }
pub fn n (a: A, b: B) -> Self { Self(North, a, b) } pub fn s (a: A, b: B) -> Self { Self(Default::default(), South, a, b) }
pub fn s (a: A, b: B) -> Self { Self(South, a, b) } pub fn e (a: A, b: B) -> Self { Self(Default::default(), East, a, b) }
pub fn e (a: A, b: B) -> Self { Self(East, a, b) } pub fn w (a: A, b: B) -> Self { Self(Default::default(), West, a, b) }
pub fn w (a: A, b: B) -> Self { Self(West, a, b) } pub fn a (a: A, b: B) -> Self { Self(Default::default(), Above, a, b) }
pub fn a (a: A, b: B) -> Self { Self(Above, a, b) } pub fn b (a: A, b: B) -> Self { Self(Default::default(), Below, a, b) }
pub fn b (a: A, b: B) -> Self { Self(Below, a, b) }
} }
pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> { pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
fn direction (&self) -> Direction; fn direction (&self) -> Direction;
fn contents (&self) -> (&A, &B); 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 direction = self.direction();
let [x, y, w, h] = outer.xywh(); let [x, y, w, h] = outer.xywh();
let (a, b) = self.contents(); let (a, b) = self.contents();
let [ax, ay, aw, ah] = a.layout(outer).xywh(); let [aw, ah] = a.layout(outer).wh();
let [bx, by, bw, bh] = b.layout(match direction { let [bw, bh] = b.layout(match direction {
Above | Below => outer, Above | Below => outer,
South => [x, y + ah, w, h.minus(ah)].into(), South => [x, y + ah, w, h.minus(ah)].into(),
North => [x, y, w, h.minus(ah)].into(), North => [x, y, w, h.minus(ah)].into(),
East => [x + aw, y, w.minus(aw), h].into(), East => [x + aw, y, w.minus(aw), h].into(),
West => [x, y, w.minus(aw), h].into(), West => [x, y, w.minus(aw), h].into(),
}).xywh(); }).wh();
match direction { match direction {
Above | Below => { Above | Below => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]); 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<E, A, B> {
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> { fn direction (&self) -> Direction { self.1 }
fn direction (&self) -> Direction { self. 0 } fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) } }
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, /// Renders multiple things on top of each other,

View file

@ -37,32 +37,29 @@ pub trait EdnViewData<'a, E: Output>:
match item { match item {
Nil => Box::new(()), Nil => Box::new(()),
Exp(e) => if let [head, tail @ ..] = e.as_slice() { Exp(e) => if let [head, tail @ ..] = e.as_slice() {
if let Some(builtin) = When::<_, Box<dyn Render<E>>>::try_from_edn(self, head, tail) {
if let Some(builtin) = When::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
return builtin.boxed() return builtin.boxed()
} }
if let Some(builtin) = Either::<_, Box<dyn Render<E>>, Box<dyn Render<E>>>::try_from_edn(self, head, tail) {
if let Some(builtin) = Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(
self, head, tail
) {
return builtin.boxed() return builtin.boxed()
} }
if let Some(builtin) = Align::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
return builtin.boxed()
}
if let Some(builtin) = Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(
self, head, tail
) {
return builtin.boxed()
}
panic!("{item:?}"); panic!("{item:?}");
match (head, tail) { match (head, tail) {
//(Key("when"), [c, a]) =>
//When(self.arg(c).unwrap(), self.get_content(a)).boxed(),
//(Key("either"), [c, a, b]) =>
//Either(self.arg(c).unwrap(), self.get_content(a), self.get_content(b)).boxed(),
(Key("align/c"), [a]) => Align::c(self.get_content(a)).boxed(),
(Key("align/x"), [a]) => Align::x(self.get_content(a)).boxed(),
(Key("align/y"), [a]) => Align::y(self.get_content(a)).boxed(),
(Key("align/n"), [a]) => Align::n(self.get_content(a)).boxed(),
(Key("align/s"), [a]) => Align::s(self.get_content(a)).boxed(),
(Key("align/e"), [a]) => Align::e(self.get_content(a)).boxed(),
(Key("align/w"), [a]) => Align::w(self.get_content(a)).boxed(),
(Key("align/nw"), [a]) => Align::nw(self.get_content(a)).boxed(),
(Key("align/ne"), [a]) => Align::ne(self.get_content(a)).boxed(),
(Key("align/sw"), [a]) => Align::sw(self.get_content(a)).boxed(),
(Key("align/se"), [a]) => Align::se(self.get_content(a)).boxed(),
(Key("bsp/a"), [a, b]) => Bsp::a(self.get_content(a), self.get_content(b),).boxed(), (Key("bsp/a"), [a, b]) => Bsp::a(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/b"), [a, b]) => Bsp::b(self.get_content(a), self.get_content(b),).boxed(), (Key("bsp/b"), [a, b]) => Bsp::b(self.get_content(a), self.get_content(b),).boxed(),
(Key("bsp/e"), [a, b]) => Bsp::e(self.get_content(a), self.get_content(b),).boxed(), (Key("bsp/e"), [a, b]) => Bsp::e(self.get_content(a), self.get_content(b),).boxed(),

View file

@ -65,7 +65,7 @@ impl<E: Output> Measure<E> {
y: Arc::new(0.into()), y: Arc::new(0.into()),
} }
} }
pub fn of <T: Content<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> { pub fn of <T: Content<E>> (&self, item: T) -> Bsp<E, Fill<&Self>, T> {
Bsp::b(Fill::xy(self), item) Bsp::b(Fill::xy(self), item)
} }
} }