Trait aery::operations::FoldBreadth
source · pub trait FoldBreadth<RS: RelationSet> {
type WQ<'wq>;
type Out<Init, Fold>;
// Required method
fn fold_breadth<Acc, E, Init, Fold>(
self,
init: Init,
fold: Fold
) -> Self::Out<Init, Fold>
where Init: FnMut(&mut Self::WQ<'_>, &RelationsItem<'_, RS>) -> Acc,
Fold: FnMut(Acc, &mut Self::WQ<'_>, &RelationsItem<'_, RS>) -> Result<Acc, E>;
}
Expand description
Passes each breadth twice doing a fold operation the first time for traversals that
TrackSelf
.
- The init closure recieves the components of the entity who’s breadth we’re currently passing & returns an inital value.
- The fold closure recieves the componetns from each entity in the breadth, the accumulated value so far & returns a result.
- Returning an
Ok
from the fold closure advances it to the next item in the breadth. - Returning an
Err
from the fold closure ends the fold & starts the second pass in the.for_each
immediately.
The result of the fold operation will be available in the .for_each
call as the first
parameter.
use bevy::prelude::*;
use aery::prelude::*;
#[derive(Component)]
struct ChildDir {
// ..
}
#[derive(Component)]
struct Size {
// ..
}
#[derive(Component)]
struct Spacing {
// ..
}
#[derive(Relation)]
struct Ui;
fn tiling_layout_algo(
roots: Query<Entity, Root<Ui>>,
mut tree: Query<((&mut Size, &ChildDir, &Spacing), Relations<Ui>)>
) {
tree.traverse_mut::<Ui>(roots.iter())
.track_self()
.fold_breadth(
|(size, dir, _), _| {
// get available space for on child layout direction
},
|available, (size, _, spacing), _| {
// calculate size of children that take up a proportion of the free space
}
)
.for_each(|res, parent, _, child, _| {
let Ok(res) = res else { return };
// subdivide parent space between children
});
}
Required Associated Types§
Required Methods§
fn fold_breadth<Acc, E, Init, Fold>(
self,
init: Init,
fold: Fold
) -> Self::Out<Init, Fold>where
Init: FnMut(&mut Self::WQ<'_>, &RelationsItem<'_, RS>) -> Acc,
Fold: FnMut(Acc, &mut Self::WQ<'_>, &RelationsItem<'_, RS>) -> Result<Acc, E>,
Object Safety§
This trait is not object safe.