wip: and sweeps right through the codebase

This commit is contained in:
🪞👃🪞 2024-12-31 02:03:16 +01:00
parent d37bd3e0c5
commit c9b09b7dea
16 changed files with 370 additions and 625 deletions

View file

@ -1,139 +1,76 @@
use crate::*;
////////////////////////////////////////////////////////////////////////////////////////////////////
//! Groupings of elements.
mod bsp; pub use self::bsp::*;
mod layers; pub use self::layers::*;
mod split; pub use self::split::*;
mod stack; pub use self::stack::*;
////////////////////////////////////////////////////////////////////////////////////////////////////
use crate::*;
/// A function or closure that emits renderables.
pub trait CollectCallback<E: Engine>: Send
+ Sync
+ Fn(&mut dyn FnMut(&dyn Render<E>)->Usually<()>)->Usually<()>
{}
pub trait Collector<E: Engine>: Send + Sync + Fn(&mut dyn FnMut(&dyn Content<E>)) {}
/// Any function or closure that emits renderables for the given engine matches [CollectCallback].
impl<E, F> CollectCallback<E> for F
where
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)->Usually<()>)->Usually<()>
{}
impl<E, F> Collector<E> for F
where E: Engine, F: Send + Sync + Fn(&mut dyn FnMut(&dyn Content<E>)) {}
////////////////////////////////////////////////////////////////////////////////////////////////////
pub struct Map<E, T, I, R, F>(PhantomData<E>, I, F)
where E: Engine, I: Iterator<Item = T>, R: Content<E>, F: Fn(T)->R;
pub enum Collect<'a, E: Engine, const N: usize> {
Callback(CallbackCollection<'a, E>),
//Iterator(IteratorCollection<'a, E>),
Array(ArrayCollection<'a, E, N>),
Slice(SliceCollection<'a, E>),
}
pub struct Reduce<E, T, I, R, F>(PhantomData<(E, R)>, I, F)
where E: Engine, I: Iterator<Item = T>, R: Content<E>, F: Fn(&dyn Content<E>, T)->R;
impl<'a, E: Engine, const N: usize> Collect<'a, E, N> {
pub fn iter (&'a self) -> CollectIterator<'a, E, N> {
CollectIterator(0, &self)
impl<E: Engine, T, I: Iterator<Item=T>+Send+Sync, R: Content<E>, F: Fn(&dyn Content<E>, T)->R+Send+Sync> Content<E> for Reduce<E, T, I, R, F> {
fn render (&self, _to: &mut E::Output) {
todo!()
}
}
impl<'a, E: Engine, const N: usize> From<CallbackCollection<'a, E>> for Collect<'a, E, N> {
fn from (callback: CallbackCollection<'a, E>) -> Self {
Self::Callback(callback)
/// Conditional rendering, in unary and binary forms.
pub struct Cond;
impl Cond {
/// Content `item` when `cond` is true.
pub fn when <E: Engine, A: Content<E>> (cond: bool, item: A) -> When<E, A> {
When(cond, item, Default::default())
}
/// Content `item` if `cond` is true, otherwise render `other`.
pub fn either <E: Engine, A: Content<E>, B: Content<E>> (cond: bool, a: A, b: B) -> Either<E, A, B> {
Either(cond, a, b, Default::default())
}
}
impl<'a, E: Engine, const N: usize> From<SliceCollection<'a, E>> for Collect<'a, E, N> {
fn from (slice: SliceCollection<'a, E>) -> Self {
Self::Slice(slice)
}
}
/// Contents `self.1` when `self.0` is true.
pub struct When<E: Engine, A>(bool, A, PhantomData<E>);
impl<'a, E: Engine, const N: usize> From<ArrayCollection<'a, E, N>> for Collect<'a, E, N>{
fn from (array: ArrayCollection<'a, E, N>) -> Self {
Self::Array(array)
}
}
type CallbackCollection<'a, E> =
&'a dyn Fn(&'a mut dyn FnMut(&dyn Render<E>)->Usually<()>);
//type IteratorCollection<'a, E> =
//&'a mut dyn Iterator<Item = dyn Render<E>>;
type SliceCollection<'a, E> =
&'a [&'a dyn Render<E>];
type ArrayCollection<'a, E, const N: usize> =
[&'a dyn Render<E>; N];
pub struct CollectIterator<'a, E: Engine, const N: usize>(usize, &'a Collect<'a, E, N>);
impl<'a, E: Engine, const N: usize> Iterator for CollectIterator<'a, E, N> {
type Item = &'a dyn Render<E>;
fn next (&mut self) -> Option<Self::Item> {
match self.1 {
Collect::Callback(_callback) => {
todo!()
},
//Collection::Iterator(iterator) => {
//iterator.next()
//},
Collect::Array(array) => {
if let Some(_item) = array.get(self.0) {
self.0 += 1;
//Some(item)
None
} else {
None
}
}
Collect::Slice(slice) => {
if let Some(_item) = slice.get(self.0) {
self.0 += 1;
//Some(item)
None
} else {
None
}
}
impl<E: Engine, A: Content<E>> Content<E> for When<E, A> {
fn area (&self, to: E::Area) -> E::Area {
let Self(cond, item, ..) = self;
let mut area = E::Area::zero();
if *cond {
let item_area = item.area(to);
area[0] = item_area.x();
area[1] = item_area.y();
area[2] = item_area.w();
area[3] = item_area.h();
}
area.into()
}
fn render (&self, to: &mut E::Output) {
let Self(cond, item, ..) = self;
if *cond { item.render(to) }
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Contents `self.1` when `self.0` is true, otherwise renders `self.2`
pub struct Either<E: Engine, A, B>(bool, A, B, PhantomData<E>);
pub struct Map<E: Engine, T, I: Iterator<Item=T>, R: Render<E>, F: Fn(T)->R>(
PhantomData<E>,
I,
F
);
////////////////////////////////////////////////////////////////////////////////////////////////////
pub struct Reduce<E: Engine, T, I: Iterator<Item=T>, R: Render<E>, F: Fn(&dyn Render<E>, T)->R>(
PhantomData<(E, R)>,
I,
F
);
impl<E: Engine, T, I: Iterator<Item=T>+Send+Sync, R: Render<E>, F: Fn(&dyn Render<E>, T)->R+Send+Sync> Render<E> for Reduce<E, T, I, R, F> {
fn min_size (&self, _to: E::Size) -> Perhaps<E::Size> {
todo!()
impl<E: Engine, A: Content<E>, B: Content<E>> Content<E> for Either<E, A, B> {
fn area (&self, to: E::Area) -> E::Area {
let Self(cond, a, b, ..) = self;
if *cond { a.area(to) } else { b.area(to) }
}
fn render (&self, _to: &mut E::Output) -> Usually<()> {
todo!()
fn render (&self, to: &mut E::Output) {
let Self(cond, a, b, ..) = self;
if *cond { a.render(to) } else { b.render(to) }
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)] #[test] fn test_bsp () {
// TODO
}