From 811e341cd51ce7e83787620987dad3208aa9788e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 13 Jan 2025 23:39:06 +0100 Subject: [PATCH] wip: hook up more builtins --- output/src/align.rs | 60 +++++++++++++++++++++++++++-------------- output/src/direction.rs | 59 ++++++++++++++++++++++++---------------- output/src/edn_view.rs | 37 ++++++++++++------------- output/src/measure.rs | 2 +- 4 files changed, 94 insertions(+), 64 deletions(-) diff --git a/output/src/align.rs b/output/src/align.rs index acc52fb6..469a8ec9 100644 --- a/output/src/align.rs +++ b/output/src/align.rs @@ -1,27 +1,23 @@ use crate::*; - #[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } - -pub struct Align(Alignment, T); - -impl Align { - pub fn c (a: T) -> Self { Self(Alignment::Center, a) } - pub fn x (a: T) -> Self { Self(Alignment::X, a) } - pub fn y (a: T) -> Self { Self(Alignment::Y, a) } - pub fn n (a: T) -> Self { Self(Alignment::N, a) } - pub fn s (a: T) -> Self { Self(Alignment::S, a) } - pub fn e (a: T) -> Self { Self(Alignment::E, a) } - pub fn w (a: T) -> Self { Self(Alignment::W, a) } - pub fn nw (a: T) -> Self { Self(Alignment::NW, a) } - pub fn sw (a: T) -> Self { Self(Alignment::SW, a) } - pub fn ne (a: T) -> Self { Self(Alignment::NE, a) } - pub fn se (a: T) -> Self { Self(Alignment::SE, a) } +pub struct Align(PhantomData, Alignment, A); +impl Align { + pub fn c (a: A) -> Self { Self(Default::default(), Alignment::Center, a) } + pub fn x (a: A) -> Self { Self(Default::default(), Alignment::X, a) } + pub fn y (a: A) -> Self { Self(Default::default(), Alignment::Y, a) } + pub fn n (a: A) -> Self { Self(Default::default(), Alignment::N, a) } + pub fn s (a: A) -> Self { Self(Default::default(), Alignment::S, a) } + pub fn e (a: A) -> Self { Self(Default::default(), Alignment::E, a) } + pub fn w (a: A) -> Self { Self(Default::default(), Alignment::W, a) } + pub fn nw (a: A) -> Self { Self(Default::default(), Alignment::NW, a) } + pub fn sw (a: A) -> Self { Self(Default::default(), Alignment::SW, a) } + pub fn ne (a: A) -> Self { Self(Default::default(), Alignment::NE, a) } + pub fn se (a: A) -> Self { Self(Default::default(), Alignment::SE, a) } } - -impl> Content for Align { +impl> Content for Align { fn content (&self) -> impl Render { - &self.1 + &self.2 } fn layout (&self, on: E::Area) -> E::Area { use Alignment::*; @@ -30,7 +26,7 @@ impl> Content for Align { let cy = on.y()+(on.h().minus(it.h())/2.into()); let fx = (on.x()+on.w()).minus(it.w()); let fy = (on.y()+on.h()).minus(it.h()); - let [x, y] = match self.0 { + let [x, y] = match self.1 { Center => [cx, cy], X => [cx, it.y()], Y => [it.x(), cy], @@ -49,3 +45,27 @@ impl> Content for Align { to.place(Content::layout(self, to.area()), &self.content()) } } +impl<'a, T, E, A> TryFromEdn<'a, T> for Align +where + T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a, + E: Output, + A: Render + 'a, +{ + fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option { + 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 + }) + } +} diff --git a/output/src/direction.rs b/output/src/direction.rs index 3f6f5dce..2d51ea70 100644 --- a/output/src/direction.rs +++ b/output/src/direction.rs @@ -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 (self, area: impl Area, a: N) -> ([N;4],[N;4]) { let [x, y, w, h] = area.xywh(); @@ -17,10 +15,8 @@ impl Direction { } } } - -pub struct Bsp(Direction, X, Y); - -impl, B: Content> Content for Bsp { +pub struct Bsp(PhantomData, Direction, X, Y); +impl, B: Content> Content for Bsp { fn layout (&self, outer: E::Area) -> E::Area { let [_, _, c] = self.areas(outer); c @@ -28,22 +24,20 @@ impl, B: Content> Content for Bsp { 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 Bsp { - 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 Bsp { + 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, B: Content> { fn direction (&self) -> Direction; fn contents (&self) -> (&A, &B); @@ -51,14 +45,14 @@ pub trait BspAreas, B: Content> { 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, B: Content> { } } } - -impl, B: Content> BspAreas for Bsp { - fn direction (&self) -> Direction { self. 0 } - fn contents (&self) -> (&A, &B) { (&self.1, &self.2) } +impl, B: Content> BspAreas for Bsp { + 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 +where + T: EdnProvide<'a, bool> + EdnProvide<'a, A> + EdnProvide<'a, B> + 'a, + E: Output, + A: Render + 'a, + B: Render + 'a, +{ + fn try_from_edn (s: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option { + 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, diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index af6fbf46..8a4844c3 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -37,32 +37,29 @@ pub trait EdnViewData<'a, E: Output>: match item { Nil => Box::new(()), Exp(e) => if let [head, tail @ ..] = e.as_slice() { - if let Some(builtin) = When::<_, Box>>::try_from_edn(self, head, tail) { + + if let Some(builtin) = When::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) { return builtin.boxed() } - if let Some(builtin) = Either::<_, Box>, Box>>::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() } + + 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:?}"); - 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/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(), diff --git a/output/src/measure.rs b/output/src/measure.rs index aec48204..76b6d0ed 100644 --- a/output/src/measure.rs +++ b/output/src/measure.rs @@ -65,7 +65,7 @@ impl Measure { y: Arc::new(0.into()), } } - pub fn of > (&self, item: T) -> Bsp, T> { + pub fn of > (&self, item: T) -> Bsp, T> { Bsp::b(Fill::xy(self), item) } }