Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Use World helper methods for sending HierarchyEvents #6921

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
tim-blackbird marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -393,25 +379,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