Use World helper methods for sending HierarchyEvents  (#6921)
				
					
				
			A code-quality PR Also cleans up the helper methods by just importing the `Event` type Co-authored-by: devil-ira <justthecooldude@gmail.com>
This commit is contained in:
		
							parent
							
								
									1523c38ce8
								
							
						
					
					
						commit
						0761594dd8
					
				@ -15,6 +15,7 @@ use crate::{
 | 
				
			|||||||
        Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells,
 | 
					        Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
 | 
					    entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
 | 
				
			||||||
 | 
					    event::{Event, Events},
 | 
				
			||||||
    query::{QueryState, ReadOnlyWorldQuery, WorldQuery},
 | 
					    query::{QueryState, ReadOnlyWorldQuery, WorldQuery},
 | 
				
			||||||
    storage::{ResourceData, SparseSet, Storages},
 | 
					    storage::{ResourceData, SparseSet, Storages},
 | 
				
			||||||
    system::Resource,
 | 
					    system::Resource,
 | 
				
			||||||
@ -1268,22 +1269,22 @@ impl World {
 | 
				
			|||||||
        result
 | 
					        result
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Sends an [`Event`](crate::event::Event).
 | 
					    /// Sends an [`Event`].
 | 
				
			||||||
    #[inline]
 | 
					    #[inline]
 | 
				
			||||||
    pub fn send_event<E: crate::event::Event>(&mut self, event: E) {
 | 
					    pub fn send_event<E: Event>(&mut self, event: E) {
 | 
				
			||||||
        self.send_event_batch(std::iter::once(event));
 | 
					        self.send_event_batch(std::iter::once(event));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Sends the default value of the [`Event`](crate::event::Event) of type `E`.
 | 
					    /// Sends the default value of the [`Event`] of type `E`.
 | 
				
			||||||
    #[inline]
 | 
					    #[inline]
 | 
				
			||||||
    pub fn send_event_default<E: crate::event::Event + Default>(&mut self) {
 | 
					    pub fn send_event_default<E: Event + Default>(&mut self) {
 | 
				
			||||||
        self.send_event_batch(std::iter::once(E::default()));
 | 
					        self.send_event_batch(std::iter::once(E::default()));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Sends a batch of [`Event`](crate::event::Event)s from an iterator.
 | 
					    /// Sends a batch of [`Event`]s from an iterator.
 | 
				
			||||||
    #[inline]
 | 
					    #[inline]
 | 
				
			||||||
    pub fn send_event_batch<E: crate::event::Event>(&mut self, events: impl Iterator<Item = E>) {
 | 
					    pub fn send_event_batch<E: Event>(&mut self, events: impl IntoIterator<Item = E>) {
 | 
				
			||||||
        match self.get_resource_mut::<crate::event::Events<E>>() {
 | 
					        match self.get_resource_mut::<Events<E>>() {
 | 
				
			||||||
            Some(mut events_resource) => events_resource.extend(events),
 | 
					            Some(mut events_resource) => events_resource.extend(events),
 | 
				
			||||||
            None => bevy_utils::tracing::error!(
 | 
					            None => bevy_utils::tracing::error!(
 | 
				
			||||||
                    "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ",
 | 
					                    "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ",
 | 
				
			||||||
 | 
				
			|||||||
@ -2,20 +2,11 @@ use crate::{Children, HierarchyEvent, Parent};
 | 
				
			|||||||
use bevy_ecs::{
 | 
					use bevy_ecs::{
 | 
				
			||||||
    bundle::Bundle,
 | 
					    bundle::Bundle,
 | 
				
			||||||
    entity::Entity,
 | 
					    entity::Entity,
 | 
				
			||||||
    event::Events,
 | 
					 | 
				
			||||||
    system::{Command, Commands, EntityCommands},
 | 
					    system::{Command, Commands, EntityCommands},
 | 
				
			||||||
    world::{EntityMut, World},
 | 
					    world::{EntityMut, World},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use smallvec::SmallVec;
 | 
					use smallvec::SmallVec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn push_events(world: &mut World, events: SmallVec<[HierarchyEvent; 8]>) {
 | 
					 | 
				
			||||||
    if let Some(mut moved) = world.get_resource_mut::<Events<HierarchyEvent>>() {
 | 
					 | 
				
			||||||
        for evt in events {
 | 
					 | 
				
			||||||
            moved.send(evt);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
 | 
					fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
 | 
				
			||||||
    let mut parent = world.entity_mut(parent);
 | 
					    let mut parent = world.entity_mut(parent);
 | 
				
			||||||
    if let Some(mut children) = parent.get_mut::<Children>() {
 | 
					    if let Some(mut children) = parent.get_mut::<Children>() {
 | 
				
			||||||
@ -64,7 +55,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    push_events(world, moved);
 | 
					    world.send_event_batch(moved);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
 | 
					fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
 | 
				
			||||||
@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
 | 
				
			|||||||
            world.entity_mut(child).remove::<Parent>();
 | 
					            world.entity_mut(child).remove::<Parent>();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    push_events(world, events);
 | 
					    world.send_event_batch(events);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut parent = world.entity_mut(parent);
 | 
					    let mut parent = world.entity_mut(parent);
 | 
				
			||||||
    if let Some(mut parent_children) = parent.get_mut::<Children>() {
 | 
					    if let Some(mut parent_children) = parent.get_mut::<Children>() {
 | 
				
			||||||
@ -114,19 +105,16 @@ impl Command for AddChild {
 | 
				
			|||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            remove_from_children(world, previous, self.child);
 | 
					            remove_from_children(world, previous, self.child);
 | 
				
			||||||
            if let Some(mut events) = world.get_resource_mut::<Events<HierarchyEvent>>() {
 | 
					            world.send_event(HierarchyEvent::ChildMoved {
 | 
				
			||||||
                events.send(HierarchyEvent::ChildMoved {
 | 
					 | 
				
			||||||
                child: self.child,
 | 
					                child: self.child,
 | 
				
			||||||
                previous_parent: previous,
 | 
					                previous_parent: previous,
 | 
				
			||||||
                new_parent: self.parent,
 | 
					                new_parent: self.parent,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        } else if let Some(mut events) = world.get_resource_mut::<Events<HierarchyEvent>>() {
 | 
					        world.send_event(HierarchyEvent::ChildAdded {
 | 
				
			||||||
            events.send(HierarchyEvent::ChildAdded {
 | 
					 | 
				
			||||||
            child: self.child,
 | 
					            child: self.child,
 | 
				
			||||||
            parent: self.parent,
 | 
					            parent: self.parent,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        let mut parent = world.entity_mut(self.parent);
 | 
					        let mut parent = world.entity_mut(self.parent);
 | 
				
			||||||
        if let Some(mut children) = parent.get_mut::<Children>() {
 | 
					        if let Some(mut children) = parent.get_mut::<Children>() {
 | 
				
			||||||
            if !children.contains(&self.child) {
 | 
					            if !children.contains(&self.child) {
 | 
				
			||||||
@ -202,15 +190,13 @@ impl Command for RemoveParent {
 | 
				
			|||||||
            let parent_entity = parent.get();
 | 
					            let parent_entity = parent.get();
 | 
				
			||||||
            remove_from_children(world, parent_entity, self.child);
 | 
					            remove_from_children(world, parent_entity, self.child);
 | 
				
			||||||
            world.entity_mut(self.child).remove::<Parent>();
 | 
					            world.entity_mut(self.child).remove::<Parent>();
 | 
				
			||||||
            if let Some(mut events) = world.get_resource_mut::<Events<_>>() {
 | 
					            world.send_event(HierarchyEvent::ChildRemoved {
 | 
				
			||||||
                events.send(HierarchyEvent::ChildRemoved {
 | 
					 | 
				
			||||||
                child: self.child,
 | 
					                child: self.child,
 | 
				
			||||||
                parent: parent_entity,
 | 
					                parent: parent_entity,
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Struct for building children onto an entity
 | 
					/// Struct for building children onto an entity
 | 
				
			||||||
pub struct ChildBuilder<'w, 's, 'a> {
 | 
					pub struct ChildBuilder<'w, 's, 'a> {
 | 
				
			||||||
@ -354,12 +340,10 @@ impl<'w> WorldChildBuilder<'w> {
 | 
				
			|||||||
    pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
 | 
					    pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
 | 
				
			||||||
        let entity = self.world.spawn((bundle, Parent(self.parent))).id();
 | 
					        let entity = self.world.spawn((bundle, Parent(self.parent))).id();
 | 
				
			||||||
        push_child_unchecked(self.world, self.parent, entity);
 | 
					        push_child_unchecked(self.world, self.parent, entity);
 | 
				
			||||||
        if let Some(mut added) = self.world.get_resource_mut::<Events<HierarchyEvent>>() {
 | 
					        self.world.send_event(HierarchyEvent::ChildAdded {
 | 
				
			||||||
            added.send(HierarchyEvent::ChildAdded {
 | 
					 | 
				
			||||||
            child: entity,
 | 
					            child: entity,
 | 
				
			||||||
            parent: self.parent,
 | 
					            parent: self.parent,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        self.world.entity_mut(entity)
 | 
					        self.world.entity_mut(entity)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -367,12 +351,10 @@ impl<'w> WorldChildBuilder<'w> {
 | 
				
			|||||||
    pub fn spawn_empty(&mut self) -> EntityMut<'_> {
 | 
					    pub fn spawn_empty(&mut self) -> EntityMut<'_> {
 | 
				
			||||||
        let entity = self.world.spawn(Parent(self.parent)).id();
 | 
					        let entity = self.world.spawn(Parent(self.parent)).id();
 | 
				
			||||||
        push_child_unchecked(self.world, self.parent, entity);
 | 
					        push_child_unchecked(self.world, self.parent, entity);
 | 
				
			||||||
        if let Some(mut added) = self.world.get_resource_mut::<Events<HierarchyEvent>>() {
 | 
					        self.world.send_event(HierarchyEvent::ChildAdded {
 | 
				
			||||||
            added.send(HierarchyEvent::ChildAdded {
 | 
					 | 
				
			||||||
            child: entity,
 | 
					            child: entity,
 | 
				
			||||||
            parent: self.parent,
 | 
					            parent: self.parent,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        self.world.entity_mut(entity)
 | 
					        self.world.entity_mut(entity)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user