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

Filter entities sent for deletion #243

Merged
merged 6 commits into from
Nov 15, 2024
Merged
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
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 62 additions & 3 deletions rmf_site_editor/src/site/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use crate::{
},
AppState, Issue,
};
use bevy::{ecs::system::SystemParam, prelude::*};
use bevy::{
ecs::system::{BoxedSystem, SystemParam, SystemState},
prelude::*,
};
use rmf_site_format::{ConstraintDependents, Edge, MeshConstraint, Path, Point};
use std::collections::HashSet;

Expand All @@ -48,7 +51,7 @@ impl PreventDeletion {

/// This is an event used to delete site elements. Deleting the element is
/// recursive, so all its children will be deleted along with it.
#[derive(Debug, Clone, Copy, Event)]
#[derive(Debug, Clone, Copy, Eq, Event, Hash, PartialEq)]
pub struct Delete {
pub element: Entity,
/// If this is true, all dependents of the element or any of its children
Expand Down Expand Up @@ -105,6 +108,7 @@ impl Plugin for DeletionPlugin {
)
.add_systems(First, apply_deferred.in_set(SiteUpdateSet::DeletionFlush))
.add_event::<Delete>()
.init_resource::<DeletionFilters>()
.add_systems(
First,
handle_deletion_requests
Expand All @@ -114,14 +118,69 @@ impl Plugin for DeletionPlugin {
}
}

fn handle_deletion_requests(mut deletions: EventReader<Delete>, mut params: DeletionParams) {
#[derive(Deref, DerefMut)]
pub struct DeletionBox(pub BoxedSystem<HashSet<Delete>, HashSet<Delete>>);

#[derive(Default, Resource)]
pub struct DeletionFilters {
boxed_systems: Vec<DeletionBox>,
pending_insertion: Vec<DeletionBox>,
}

impl DeletionFilters {
pub fn insert(&mut self, filter: DeletionBox) {
self.pending_insertion.push(filter);
}

fn insert_boxes(&mut self, world: &mut World) {
for mut inserted in self.pending_insertion.drain(..) {
inserted.initialize(world);
self.boxed_systems.push(inserted);
}
}

fn run_boxes(
&mut self,
mut pending_delete: HashSet<Delete>,
world: &mut World,
) -> HashSet<Delete> {
for boxed_system in self.boxed_systems.iter_mut() {
pending_delete = boxed_system.0.run(pending_delete, world);
}
pending_delete
}
}

fn handle_deletion_requests(
world: &mut World,
state: &mut SystemState<(EventReader<Delete>, DeletionParams)>,
) {
let (mut deletions, _) = state.get_mut(world);
if deletions.is_empty() {
return;
}
let mut pending_delete: HashSet<Delete> = HashSet::new();
for delete in deletions.read() {
pending_delete.insert(*delete);
}

pending_delete =
world.resource_scope::<DeletionFilters, _>(move |world, mut deletion_filters| {
deletion_filters.insert_boxes(world);
// Run through all boxed systems to filter out entities that should not
// be sent to delete
deletion_filters.run_boxes(pending_delete, world)
});

let (_, mut params) = state.get_mut(world);
for delete in pending_delete.iter() {
if delete.and_dependents {
recursive_dependent_delete(delete.element, &mut params);
} else {
cautious_delete(delete.element, &mut params);
}
}
state.apply(world);
}

fn cautious_delete(element: Entity, params: &mut DeletionParams) {
Expand Down
Loading