From 26e9b4d4d34320a310bafa45fcb1a6bc38b1f091 Mon Sep 17 00:00:00 2001 From: ira Date: Tue, 20 Dec 2022 01:59:06 +0000 Subject: [PATCH] Use `World` helper methods for sending `HierarchyEvent`s (#6921) A code-quality PR Also cleans up the helper methods by just importing the `Event` type Co-authored-by: devil-ira --- crates/bevy_ecs/src/world/mod.rs | 15 +++--- crates/bevy_hierarchy/src/child_builder.rs | 60 ++++++++-------------- 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 9e4b5757214367..0e8009916715cb 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -15,6 +15,7 @@ use crate::{ Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells, }, entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation}, + event::{Event, Events}, query::{QueryState, ReadOnlyWorldQuery, WorldQuery}, storage::{ResourceData, SparseSet, Storages}, system::Resource, @@ -1268,22 +1269,22 @@ impl World { result } - /// Sends an [`Event`](crate::event::Event). + /// Sends an [`Event`]. #[inline] - pub fn send_event(&mut self, event: E) { + pub fn send_event(&mut self, event: E) { 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] - pub fn send_event_default(&mut self) { + pub fn send_event_default(&mut self) { 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] - pub fn send_event_batch(&mut self, events: impl Iterator) { - match self.get_resource_mut::>() { + pub fn send_event_batch(&mut self, events: impl IntoIterator) { + match self.get_resource_mut::>() { Some(mut events_resource) => events_resource.extend(events), 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 ", diff --git a/crates/bevy_hierarchy/src/child_builder.rs b/crates/bevy_hierarchy/src/child_builder.rs index 0bb8b29f627533..c1c7029ff84c7d 100644 --- a/crates/bevy_hierarchy/src/child_builder.rs +++ b/crates/bevy_hierarchy/src/child_builder.rs @@ -2,20 +2,11 @@ use crate::{Children, HierarchyEvent, Parent}; use bevy_ecs::{ bundle::Bundle, entity::Entity, - event::Events, system::{Command, Commands, EntityCommands}, world::{EntityMut, World}, }; use smallvec::SmallVec; -fn push_events(world: &mut World, events: SmallVec<[HierarchyEvent; 8]>) { - if let Some(mut moved) = world.get_resource_mut::>() { - for evt in events { - moved.send(evt); - } - } -} - fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) { let mut parent = world.entity_mut(parent); if let Some(mut children) = parent.get_mut::() { @@ -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) { @@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) { world.entity_mut(child).remove::(); } } - push_events(world, events); + world.send_event_batch(events); let mut parent = world.entity_mut(parent); if let Some(mut parent_children) = parent.get_mut::() { @@ -114,19 +105,16 @@ impl Command for AddChild { return; } remove_from_children(world, previous, self.child); - if let Some(mut events) = world.get_resource_mut::>() { - events.send(HierarchyEvent::ChildMoved { - child: self.child, - previous_parent: previous, - new_parent: self.parent, - }); - } - } else if let Some(mut events) = world.get_resource_mut::>() { - events.send(HierarchyEvent::ChildAdded { + world.send_event(HierarchyEvent::ChildMoved { child: self.child, - parent: self.parent, + previous_parent: previous, + new_parent: self.parent, }); } + world.send_event(HierarchyEvent::ChildAdded { + child: self.child, + parent: self.parent, + }); let mut parent = world.entity_mut(self.parent); if let Some(mut children) = parent.get_mut::() { if !children.contains(&self.child) { @@ -202,12 +190,10 @@ impl Command for RemoveParent { let parent_entity = parent.get(); remove_from_children(world, parent_entity, self.child); world.entity_mut(self.child).remove::(); - if let Some(mut events) = world.get_resource_mut::>() { - events.send(HierarchyEvent::ChildRemoved { - child: self.child, - parent: parent_entity, - }); - } + world.send_event(HierarchyEvent::ChildRemoved { + child: self.child, + parent: parent_entity, + }); } } } @@ -354,12 +340,10 @@ impl<'w> WorldChildBuilder<'w> { pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> { let entity = self.world.spawn((bundle, Parent(self.parent))).id(); push_child_unchecked(self.world, self.parent, entity); - if let Some(mut added) = self.world.get_resource_mut::>() { - added.send(HierarchyEvent::ChildAdded { - child: entity, - parent: self.parent, - }); - } + self.world.send_event(HierarchyEvent::ChildAdded { + child: entity, + parent: self.parent, + }); self.world.entity_mut(entity) } @@ -367,12 +351,10 @@ impl<'w> WorldChildBuilder<'w> { pub fn spawn_empty(&mut self) -> EntityMut<'_> { let entity = self.world.spawn(Parent(self.parent)).id(); push_child_unchecked(self.world, self.parent, entity); - if let Some(mut added) = self.world.get_resource_mut::>() { - added.send(HierarchyEvent::ChildAdded { - child: entity, - parent: self.parent, - }); - } + self.world.send_event(HierarchyEvent::ChildAdded { + child: entity, + parent: self.parent, + }); self.world.entity_mut(entity) }