Skip to content

Commit

Permalink
Use World helper methods for sending HierarchyEvents (#6921)
Browse files Browse the repository at this point in the history
A code-quality PR

Also cleans up the helper methods by just importing the `Event` type

Co-authored-by: devil-ira <justthecooldude@gmail.com>
  • Loading branch information
tim-blackbird and tim-blackbird committed Dec 20, 2022
1 parent f8e4b75 commit 26e9b4d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
15 changes: 8 additions & 7 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1268,22 +1269,22 @@ impl World {
result
}

/// Sends an [`Event`](crate::event::Event).
/// Sends an [`Event`].
#[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));
}

/// 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<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()));
}

/// 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<E: crate::event::Event>(&mut self, events: impl Iterator<Item = E>) {
match self.get_resource_mut::<crate::event::Events<E>>() {
pub fn send_event_batch<E: Event>(&mut self, events: impl IntoIterator<Item = E>) {
match self.get_resource_mut::<Events<E>>() {
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 ",
Expand Down
60 changes: 21 additions & 39 deletions crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Events<HierarchyEvent>>() {
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::<Children>() {
Expand Down Expand Up @@ -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) {
Expand All @@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
world.entity_mut(child).remove::<Parent>();
}
}
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::<Children>() {
Expand Down Expand Up @@ -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<HierarchyEvent>>() {
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<HierarchyEvent>>() {
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::<Children>() {
if !children.contains(&self.child) {
Expand Down Expand Up @@ -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::<Parent>();
if let Some(mut events) = world.get_resource_mut::<Events<_>>() {
events.send(HierarchyEvent::ChildRemoved {
child: self.child,
parent: parent_entity,
});
}
world.send_event(HierarchyEvent::ChildRemoved {
child: self.child,
parent: parent_entity,
});
}
}
}
Expand Down Expand Up @@ -354,25 +340,21 @@ 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::<Events<HierarchyEvent>>() {
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)
}

/// Spawns an [`Entity`] with no components and inserts it into the children defined by the [`WorldChildBuilder`] which adds the [`Parent`] component to it.
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::<Events<HierarchyEvent>>() {
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)
}

Expand Down

0 comments on commit 26e9b4d

Please sign in to comment.