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§

source

type WQ<'wq>

source

type Out<Init, Fold>

Required Methods§

source

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.

Implementors§

source§

impl<'a, 'w, 's, D, RS, F, Edge, Starts> FoldBreadth<RS> for TraverseAnd<&'a Query<'w, 's, (D, Relations<RS>), F>, Edge, Starts, SelfTracking>
where D: QueryData, F: QueryFilter, RS: RelationSet,

§

type WQ<'wq> = <<D as QueryData>::ReadOnly as WorldQuery>::Item<'wq>

§

type Out<Init, Fold> = TraverseAnd<&'a Query<'w, 's, (D, Relations<RS>), F>, Edge, Starts, SelfTracking, Init, Fold>

source§

impl<'a, 'w, 's, D, RS, F, Edge, Starts> FoldBreadth<RS> for TraverseAnd<&'a mut Query<'w, 's, (D, Relations<RS>), F>, Edge, Starts, SelfTracking>
where D: QueryData, F: QueryFilter, RS: RelationSet,

§

type WQ<'wq> = <D as WorldQuery>::Item<'wq>

§

type Out<Init, Fold> = TraverseAnd<&'a mut Query<'w, 's, (D, Relations<RS>), F>, Edge, Starts, SelfTracking, Init, Fold>