Skip to content

Commit 2b50bc8

Browse files
committed
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 <[email protected]>
1 parent 15dc0eb commit 2b50bc8

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells,
1616
},
1717
entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
18+
event::{Event, Events},
1819
query::{QueryState, ReadOnlyWorldQuery, WorldQuery},
1920
storage::{ResourceData, SparseSet, Storages},
2021
system::Resource,
@@ -1268,22 +1269,22 @@ impl World {
12681269
result
12691270
}
12701271

1271-
/// Sends an [`Event`](crate::event::Event).
1272+
/// Sends an [`Event`].
12721273
#[inline]
1273-
pub fn send_event<E: crate::event::Event>(&mut self, event: E) {
1274+
pub fn send_event<E: Event>(&mut self, event: E) {
12741275
self.send_event_batch(std::iter::once(event));
12751276
}
12761277

1277-
/// Sends the default value of the [`Event`](crate::event::Event) of type `E`.
1278+
/// Sends the default value of the [`Event`] of type `E`.
12781279
#[inline]
1279-
pub fn send_event_default<E: crate::event::Event + Default>(&mut self) {
1280+
pub fn send_event_default<E: Event + Default>(&mut self) {
12801281
self.send_event_batch(std::iter::once(E::default()));
12811282
}
12821283

1283-
/// Sends a batch of [`Event`](crate::event::Event)s from an iterator.
1284+
/// Sends a batch of [`Event`]s from an iterator.
12841285
#[inline]
1285-
pub fn send_event_batch<E: crate::event::Event>(&mut self, events: impl Iterator<Item = E>) {
1286-
match self.get_resource_mut::<crate::event::Events<E>>() {
1286+
pub fn send_event_batch<E: Event>(&mut self, events: impl IntoIterator<Item = E>) {
1287+
match self.get_resource_mut::<Events<E>>() {
12871288
Some(mut events_resource) => events_resource.extend(events),
12881289
None => bevy_utils::tracing::error!(
12891290
"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 ",

crates/bevy_hierarchy/src/child_builder.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@ use crate::{Children, HierarchyEvent, Parent};
22
use bevy_ecs::{
33
bundle::Bundle,
44
entity::Entity,
5-
event::Events,
65
system::{Command, Commands, EntityCommands},
76
world::{EntityMut, World},
87
};
98
use smallvec::SmallVec;
109

11-
fn push_events(world: &mut World, events: SmallVec<[HierarchyEvent; 8]>) {
12-
if let Some(mut moved) = world.get_resource_mut::<Events<HierarchyEvent>>() {
13-
for evt in events {
14-
moved.send(evt);
15-
}
16-
}
17-
}
18-
1910
fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
2011
let mut parent = world.entity_mut(parent);
2112
if let Some(mut children) = parent.get_mut::<Children>() {
@@ -64,7 +55,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
6455
});
6556
}
6657
}
67-
push_events(world, moved);
58+
world.send_event_batch(moved);
6859
}
6960

7061
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
@@ -83,7 +74,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
8374
world.entity_mut(child).remove::<Parent>();
8475
}
8576
}
86-
push_events(world, events);
77+
world.send_event_batch(events);
8778

8879
let mut parent = world.entity_mut(parent);
8980
if let Some(mut parent_children) = parent.get_mut::<Children>() {
@@ -114,19 +105,16 @@ impl Command for AddChild {
114105
return;
115106
}
116107
remove_from_children(world, previous, self.child);
117-
if let Some(mut events) = world.get_resource_mut::<Events<HierarchyEvent>>() {
118-
events.send(HierarchyEvent::ChildMoved {
119-
child: self.child,
120-
previous_parent: previous,
121-
new_parent: self.parent,
122-
});
123-
}
124-
} else if let Some(mut events) = world.get_resource_mut::<Events<HierarchyEvent>>() {
125-
events.send(HierarchyEvent::ChildAdded {
108+
world.send_event(HierarchyEvent::ChildMoved {
126109
child: self.child,
127-
parent: self.parent,
110+
previous_parent: previous,
111+
new_parent: self.parent,
128112
});
129113
}
114+
world.send_event(HierarchyEvent::ChildAdded {
115+
child: self.child,
116+
parent: self.parent,
117+
});
130118
let mut parent = world.entity_mut(self.parent);
131119
if let Some(mut children) = parent.get_mut::<Children>() {
132120
if !children.contains(&self.child) {
@@ -202,12 +190,10 @@ impl Command for RemoveParent {
202190
let parent_entity = parent.get();
203191
remove_from_children(world, parent_entity, self.child);
204192
world.entity_mut(self.child).remove::<Parent>();
205-
if let Some(mut events) = world.get_resource_mut::<Events<_>>() {
206-
events.send(HierarchyEvent::ChildRemoved {
207-
child: self.child,
208-
parent: parent_entity,
209-
});
210-
}
193+
world.send_event(HierarchyEvent::ChildRemoved {
194+
child: self.child,
195+
parent: parent_entity,
196+
});
211197
}
212198
}
213199
}
@@ -354,25 +340,21 @@ impl<'w> WorldChildBuilder<'w> {
354340
pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
355341
let entity = self.world.spawn((bundle, Parent(self.parent))).id();
356342
push_child_unchecked(self.world, self.parent, entity);
357-
if let Some(mut added) = self.world.get_resource_mut::<Events<HierarchyEvent>>() {
358-
added.send(HierarchyEvent::ChildAdded {
359-
child: entity,
360-
parent: self.parent,
361-
});
362-
}
343+
self.world.send_event(HierarchyEvent::ChildAdded {
344+
child: entity,
345+
parent: self.parent,
346+
});
363347
self.world.entity_mut(entity)
364348
}
365349

366350
/// Spawns an [`Entity`] with no components and inserts it into the children defined by the [`WorldChildBuilder`] which adds the [`Parent`] component to it.
367351
pub fn spawn_empty(&mut self) -> EntityMut<'_> {
368352
let entity = self.world.spawn(Parent(self.parent)).id();
369353
push_child_unchecked(self.world, self.parent, entity);
370-
if let Some(mut added) = self.world.get_resource_mut::<Events<HierarchyEvent>>() {
371-
added.send(HierarchyEvent::ChildAdded {
372-
child: entity,
373-
parent: self.parent,
374-
});
375-
}
354+
self.world.send_event(HierarchyEvent::ChildAdded {
355+
child: entity,
356+
parent: self.parent,
357+
});
376358
self.world.entity_mut(entity)
377359
}
378360

0 commit comments

Comments
 (0)