-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Insert children #17558
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
Insert children #17558
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,33 @@ impl<'w> EntityWorldMut<'w> { | |
self.add_related::<ChildOf>(children) | ||
} | ||
|
||
/// Insert children at specific index | ||
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self { | ||
let parent = self.id(); | ||
if children.contains(&parent) { | ||
panic!("Cannot insert entity as a child of itself."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to change the return to Bevy Result and then return |
||
} | ||
if let Some(mut children_component) = self.get_mut::<Children>() { | ||
children_component | ||
.0 | ||
.retain(|value| !children.contains(value)); | ||
if index >= children_component.len() { | ||
panic!( | ||
"Index {} out of bounds! There are only {} children!", | ||
index, | ||
children.len() | ||
); | ||
} | ||
children_component.0.reserve(children.len()); | ||
let mut v = children_component.0.split_off(index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not update the other side of the relationship ( |
||
children_component.0.extend_from_slice(children); | ||
children_component.0.append(&mut v); | ||
} else { | ||
self.insert(Children(children.to_vec())); | ||
} | ||
self | ||
} | ||
|
||
/// Adds the given child to this entity | ||
pub fn add_child(&mut self, child: Entity) -> &mut Self { | ||
self.add_related::<ChildOf>(&[child]) | ||
|
@@ -416,6 +443,35 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn insert_children() { | ||
let mut world = World::new(); | ||
let child1 = world.spawn_empty().id(); | ||
let child2 = world.spawn_empty().id(); | ||
let child3 = world.spawn_empty().id(); | ||
let child4 = world.spawn_empty().id(); | ||
|
||
let mut entity_world_mut = world.spawn_empty(); | ||
|
||
let first_children = entity_world_mut.add_children(&[child1, child2]); | ||
|
||
let root = first_children.insert_children(1, &[child3, child4]).id(); | ||
|
||
let hierarchy = get_hierarchy(&world, root); | ||
assert_eq!( | ||
hierarchy, | ||
Node::new_with( | ||
root, | ||
vec![ | ||
Node::new(child1), | ||
Node::new(child3), | ||
Node::new(child4), | ||
Node::new(child2) | ||
] | ||
) | ||
); | ||
} | ||
|
||
#[test] | ||
fn self_parenting_invalid() { | ||
let mut world = World::new(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the entity is already a child? As a user, I'd like to know whether that is okay ok or not. For example, does doing so reorder it?