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] - Add a wrapper around Entity for RemovedComponents #7503

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
23 changes: 15 additions & 8 deletions crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ use std::{
option,
};

/// Wrapper around a [`ManualEventReader<Entity>`] so that we
/// Wrapper around [`Entity`] for [`RemovedComponents`].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve these docs? Maybe try to explain how they're used in events?

#[derive(Debug, Clone)]
pub struct RemovedComponentEntity(pub Entity);

/// Wrapper around a [`ManualEventReader<RemovedComponentEntity>`] so that we
/// can differentiate events between components.
#[derive(Debug)]
pub struct RemovedComponentReader<T>
where
T: Component,
{
reader: ManualEventReader<Entity>,
reader: ManualEventReader<RemovedComponentEntity>,
marker: PhantomData<T>,
}

Expand All @@ -40,7 +44,7 @@ impl<T: Component> Default for RemovedComponentReader<T> {
}

impl<T: Component> Deref for RemovedComponentReader<T> {
type Target = ManualEventReader<Entity>;
type Target = ManualEventReader<RemovedComponentEntity>;
fn deref(&self) -> &Self::Target {
&self.reader
}
Expand All @@ -56,7 +60,7 @@ impl<T: Component> DerefMut for RemovedComponentReader<T> {
/// So that we can find the events without naming the type directly.
#[derive(Default, Debug)]
pub struct RemovedComponentEvents {
event_sets: SparseSet<ComponentId, Events<Entity>>,
event_sets: SparseSet<ComponentId, Events<RemovedComponentEntity>>,
}

impl RemovedComponentEvents {
Expand All @@ -70,14 +74,17 @@ impl RemovedComponentEvents {
}
}

pub fn get(&self, component_id: impl Into<ComponentId>) -> Option<&Events<Entity>> {
pub fn get(
&self,
component_id: impl Into<ComponentId>,
) -> Option<&Events<RemovedComponentEntity>> {
self.event_sets.get(component_id.into())
}

pub fn send(&mut self, component_id: impl Into<ComponentId>, entity: Entity) {
self.event_sets
.get_or_insert_with(component_id.into(), Default::default)
.send(entity);
.send(RemovedComponentEntity(entity));
}
}

Expand Down Expand Up @@ -123,7 +130,7 @@ pub struct RemovedComponents<'w, 's, T: Component> {
///
/// See [`RemovedComponents`].
pub type RemovedIter<'a> =
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, Entity>>>>;
iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, RemovedComponentEntity>>>>;

impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> {
pub fn iter(&mut self) -> RemovedIter<'_> {
Expand All @@ -139,7 +146,7 @@ impl<'a, 'w, 's: 'a, T> IntoIterator for &'a mut RemovedComponents<'w, 's, T>
where
T: Component,
{
type Item = Entity;
type Item = RemovedComponentEntity;
CatThingy marked this conversation as resolved.
Show resolved Hide resolved
type IntoIter = RemovedIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ mod tests {
mut n_systems: ResMut<NSystems>,
) {
assert_eq!(
removed_i32.iter().collect::<Vec<_>>(),
removed_i32.iter().map(|e| e.0).collect::<Vec<_>>(),
&[despawned.0],
"despawning causes the correct entity to show up in the 'RemovedComponent' system parameter."
);
Expand Down Expand Up @@ -636,7 +636,7 @@ mod tests {
// The despawned entity from the previous frame was
// double buffered so we now have it in this system as well.
assert_eq!(
removed_i32.iter().collect::<Vec<_>>(),
removed_i32.iter().map(|e| e.0).collect::<Vec<_>>(),
&[despawned.0, removed.0],
"removing a component causes the correct entity to show up in the 'RemovedComponent' system parameter."
);
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ impl World {
.map(|removed| removed.iter_current_update_events().cloned())
.into_iter()
.flatten()
.map(|e| e.0)
}

/// Initializes a new resource and returns the [`ComponentId`] created for it.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ pub fn flex_node_system(
}

// clean up removed nodes
flex_surface.remove_entities(removed_nodes.iter());
flex_surface.remove_entities(removed_nodes.iter().map(|e| e.0));

// update window children (for now assuming all Nodes live in the primary window)
flex_surface.set_window_children(primary_window_entity, root_node_query.iter());

// update and remove children
for entity in removed_children.iter() {
flex_surface.try_remove_children(entity);
flex_surface.try_remove_children(entity.0);
}
for (entity, children) in &children_query {
flex_surface.update_children(entity, children);
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ pub(crate) fn despawn_window(
mut winit_windows: NonSendMut<WinitWindows>,
) {
for window in closed.iter() {
info!("Closing window {:?}", window);
let entity = window.0;
info!("Closing window {:?}", entity);
// Guard to verify that the window is in fact actually gone,
// rather than having the component added and removed in the same frame.
if !window_entities.contains(window) {
winit_windows.remove_window(window);
close_events.send(WindowClosed { window });
if !window_entities.contains(entity) {
winit_windows.remove_window(entity);
close_events.send(WindowClosed { window: entity });
}
}
}
Expand Down