tabula rasa

This commit is contained in:
🪞👃🪞 2025-03-02 14:31:04 +02:00
commit 47b3413d7d
76 changed files with 7000 additions and 0 deletions

143
output/src/op_bsp.rs Normal file
View file

@ -0,0 +1,143 @@
use crate::*;
pub use Direction::*;
/// A split or layer.
pub struct Bsp<X, Y>(Direction, X, Y);
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
fn layout (&self, outer: E::Area) -> E::Area {
let [_, _, c] = self.areas(outer);
c
}
fn render (&self, to: &mut E) {
let [area_a, area_b, _] = self.areas(to.area());
let (a, b) = self.contents();
match self.0 {
Below => { to.place(area_a, a); to.place(area_b, b); },
_ => { to.place(area_b, b); to.place(area_a, a); }
}
}
}
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b" => {
let _ = iter.next().unwrap();
let c1 = iter.next().expect("no content1 specified");
let c2 = iter.next().expect("no content2 specified");
let c1 = state.get_content(&c1.value).expect("no content1 provided");
let c2 = state.get_content(&c2.value).expect("no content2 provided");
return Some(match key {
"bsp/n" => Self::n(c1, c2),
"bsp/s" => Self::s(c1, c2),
"bsp/e" => Self::e(c1, c2),
"bsp/w" => Self::w(c1, c2),
"bsp/a" => Self::a(c1, c2),
"bsp/b" => Self::b(c1, c2),
_ => unreachable!(),
})
},
_ => return None
}
}
});
impl<A, B> Bsp<A, B> {
#[inline] pub fn n (a: A, b: B) -> Self { Self(North, a, b) }
#[inline] pub fn s (a: A, b: B) -> Self { Self(South, a, b) }
#[inline] pub fn e (a: A, b: B) -> Self { Self(East, a, b) }
#[inline] pub fn w (a: A, b: B) -> Self { Self(West, a, b) }
#[inline] pub fn a (a: A, b: B) -> Self { Self(Above, a, b) }
#[inline] pub fn b (a: A, b: B) -> Self { Self(Below, a, b) }
}
pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
fn direction (&self) -> Direction;
fn contents (&self) -> (&A, &B);
fn areas (&self, outer: E::Area) -> [E::Area;3] {
let direction = self.direction();
let [x, y, w, h] = outer.xywh();
let (a, b) = self.contents();
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(),
}).wh();
match direction {
Above | Below => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]);
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
South => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
North => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
East => {
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
West => {
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
}
}
}
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) }
}
/// Renders multiple things on top of each other,
#[macro_export] macro_rules! lay {
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }}
}
/// Stack southward.
#[macro_export] macro_rules! col {
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }};
}
/// Stack northward.
#[macro_export] macro_rules! col_up {
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }}
}
/// Stack eastward.
#[macro_export] macro_rules! row {
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }};
}
#[cfg(test)] mod test {
use super::*;
use proptest::prelude::*;
proptest! {
#[test] fn proptest_op_bsp (
d in prop_oneof![
Just(North), Just(South),
Just(East), Just(West),
Just(Above), Just(Below)
],
a in "\\PC*",
b in "\\PC*",
x in u16::MIN..u16::MAX,
y in u16::MIN..u16::MAX,
w in u16::MIN..u16::MAX,
h in u16::MIN..u16::MAX,
) {
let bsp = Bsp(d, a, b);
assert_eq!(
Content::layout(&bsp, [x, y, w, h]),
Render::layout(&bsp, [x, y, w, h]),
);
}
}
}