bevy_ecs/reflect/
visit_entities.rs

1use crate::entity::{Entity, VisitEntities, VisitEntitiesMut};
2use bevy_reflect::{FromReflect, FromType, PartialReflect};
3
4/// For a reflected value, apply an operation to all contained entities.
5///
6/// See [`VisitEntities`] for more details.
7#[derive(Clone)]
8pub struct ReflectVisitEntities {
9    visit_entities: fn(&dyn PartialReflect, &mut dyn FnMut(Entity)),
10}
11
12impl ReflectVisitEntities {
13    /// A general method for applying an operation to all entities in a
14    /// reflected component.
15    pub fn visit_entities(&self, component: &dyn PartialReflect, f: &mut dyn FnMut(Entity)) {
16        (self.visit_entities)(component, f);
17    }
18}
19
20impl<C: FromReflect + VisitEntities> FromType<C> for ReflectVisitEntities {
21    fn from_type() -> Self {
22        ReflectVisitEntities {
23            visit_entities: |component, f| {
24                let concrete = C::from_reflect(component).unwrap();
25                concrete.visit_entities(f);
26            },
27        }
28    }
29}
30
31/// For a reflected value, apply an operation to mutable references to all
32/// contained entities.
33///
34/// See [`VisitEntitiesMut`] for more details.
35#[derive(Clone)]
36pub struct ReflectVisitEntitiesMut {
37    visit_entities_mut: fn(&mut dyn PartialReflect, &mut dyn FnMut(&mut Entity)),
38}
39
40impl ReflectVisitEntitiesMut {
41    /// A general method for applying an operation to all entities in a
42    /// reflected component.
43    pub fn visit_entities(
44        &self,
45        component: &mut dyn PartialReflect,
46        f: &mut dyn FnMut(&mut Entity),
47    ) {
48        (self.visit_entities_mut)(component, f);
49    }
50}
51
52impl<C: FromReflect + VisitEntitiesMut> FromType<C> for ReflectVisitEntitiesMut {
53    fn from_type() -> Self {
54        ReflectVisitEntitiesMut {
55            visit_entities_mut: |component, f| {
56                let mut concrete = C::from_reflect(component).unwrap();
57                concrete.visit_entities_mut(f);
58                component.apply(&concrete);
59            },
60        }
61    }
62}