mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
139 lines
6 KiB
Rust
139 lines
6 KiB
Rust
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();
|
|
match self {
|
|
North => ([x, (y+h).minus(a), w, a], [x, y, w, h.minus(a)]),
|
|
South => ([x, y, w, a], [x, y + a, w, h.minus(a)]),
|
|
East => ([x, y, a, h], [x + a, y, w.minus(a), h]),
|
|
West => ([(x+w).minus(a), y, a, h], [x, y, w - a, h]),
|
|
Above | Below => (area.xywh(), area.xywh())
|
|
}
|
|
}
|
|
}
|
|
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
|
|
}
|
|
fn render (&self, to: &mut E) {
|
|
let [area_a, area_b, _] = self.areas(to.area());
|
|
let (a, b) = self.contents();
|
|
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<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);
|
|
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<E, A, B> {
|
|
fn direction (&self) -> Direction { self.1 }
|
|
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
|
|
}
|
|
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
|
fn try_from_edn (s: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
|
use EdnItem::*;
|
|
Some(match (head, tail) {
|
|
(Key("bsp/n"), [a, b]) => Self::n(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
(Key("bsp/s"), [a, b]) => Self::s(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
(Key("bsp/e"), [a, b]) => Self::e(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
(Key("bsp/w"), [a, b]) => Self::w(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
(Key("bsp/a"), [a, b]) => Self::a(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
(Key("bsp/b"), [a, b]) => Self::b(
|
|
s.get_content(a).expect("no a"),
|
|
s.get_content(b).expect("no b")),
|
|
_ => return None
|
|
})
|
|
}
|
|
}
|
|
|
|
/// 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 }};
|
|
}
|