@@ -19,6 +19,7 @@ impl Command for InsertChildren {
19
19
for child in self . children . iter ( ) {
20
20
world
21
21
. entity_mut ( * child)
22
+ // FIXME: don't erase the previous parent (see #1545)
22
23
. insert_bundle ( ( Parent ( self . parent ) , PreviousParent ( self . parent ) ) ) ;
23
24
}
24
25
{
@@ -55,6 +56,7 @@ impl Command for PushChildren {
55
56
for child in self . children . iter ( ) {
56
57
world
57
58
. entity_mut ( * child)
59
+ // FIXME: don't erase the previous parent (see #1545)
58
60
. insert_bundle ( ( Parent ( self . parent ) , PreviousParent ( self . parent ) ) ) ;
59
61
}
60
62
{
@@ -198,6 +200,8 @@ impl<'w> WorldChildBuilder<'w> {
198
200
199
201
pub trait BuildWorldChildren {
200
202
fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut WorldChildBuilder ) ) -> & mut Self ;
203
+ fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
204
+ fn insert_children ( & mut self , index : usize , children : & [ Entity ] ) -> & mut Self ;
201
205
}
202
206
203
207
impl < ' w > BuildWorldChildren for EntityMut < ' w > {
@@ -217,6 +221,47 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {
217
221
self . update_location ( ) ;
218
222
self
219
223
}
224
+
225
+ fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self {
226
+ let parent = self . id ( ) ;
227
+ {
228
+ // SAFE: parent entity is not modified
229
+ let world = unsafe { self . world_mut ( ) } ;
230
+ for child in children. iter ( ) {
231
+ world
232
+ . entity_mut ( * child)
233
+ // FIXME: don't erase the previous parent (see #1545)
234
+ . insert_bundle ( ( Parent ( parent) , PreviousParent ( parent) ) ) ;
235
+ }
236
+ }
237
+ if let Some ( mut children_component) = self . get_mut :: < Children > ( ) {
238
+ children_component. 0 . extend ( children. iter ( ) . cloned ( ) ) ;
239
+ } else {
240
+ self . insert ( Children :: with ( children) ) ;
241
+ }
242
+ self
243
+ }
244
+
245
+ fn insert_children ( & mut self , index : usize , children : & [ Entity ] ) -> & mut Self {
246
+ let parent = self . id ( ) ;
247
+ {
248
+ // SAFE: parent entity is not modified
249
+ let world = unsafe { self . world_mut ( ) } ;
250
+ for child in children. iter ( ) {
251
+ world
252
+ . entity_mut ( * child)
253
+ // FIXME: don't erase the previous parent (see #1545)
254
+ . insert_bundle ( ( Parent ( parent) , PreviousParent ( parent) ) ) ;
255
+ }
256
+ }
257
+
258
+ if let Some ( mut children_component) = self . get_mut :: < Children > ( ) {
259
+ children_component. 0 . insert_from_slice ( index, children) ;
260
+ } else {
261
+ self . insert ( Children :: with ( children) ) ;
262
+ }
263
+ self
264
+ }
220
265
}
221
266
222
267
impl < ' w > BuildWorldChildren for WorldChildBuilder < ' w > {
@@ -235,11 +280,52 @@ impl<'w> BuildWorldChildren for WorldChildBuilder<'w> {
235
280
self . current_entity = self . parent_entities . pop ( ) ;
236
281
self
237
282
}
283
+
284
+ fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self {
285
+ let parent = self
286
+ . current_entity
287
+ . expect ( "Cannot add children without a parent. Try creating an entity first." ) ;
288
+ for child in children. iter ( ) {
289
+ self . world
290
+ . entity_mut ( * child)
291
+ // FIXME: don't erase the previous parent (see #1545)
292
+ . insert_bundle ( ( Parent ( parent) , PreviousParent ( parent) ) ) ;
293
+ }
294
+ if let Some ( mut children_component) = self . world . get_mut :: < Children > ( parent) {
295
+ children_component. 0 . extend ( children. iter ( ) . cloned ( ) ) ;
296
+ } else {
297
+ self . world
298
+ . entity_mut ( parent)
299
+ . insert ( Children :: with ( children) ) ;
300
+ }
301
+ self
302
+ }
303
+
304
+ fn insert_children ( & mut self , index : usize , children : & [ Entity ] ) -> & mut Self {
305
+ let parent = self
306
+ . current_entity
307
+ . expect ( "Cannot add children without a parent. Try creating an entity first." ) ;
308
+
309
+ for child in children. iter ( ) {
310
+ self . world
311
+ . entity_mut ( * child)
312
+ // FIXME: don't erase the previous parent (see #1545)
313
+ . insert_bundle ( ( Parent ( parent) , PreviousParent ( parent) ) ) ;
314
+ }
315
+ if let Some ( mut children_component) = self . world . get_mut :: < Children > ( parent) {
316
+ children_component. 0 . insert_from_slice ( index, children) ;
317
+ } else {
318
+ self . world
319
+ . entity_mut ( parent)
320
+ . insert ( Children :: with ( children) ) ;
321
+ }
322
+ self
323
+ }
238
324
}
239
325
240
326
#[ cfg( test) ]
241
327
mod tests {
242
- use super :: BuildChildren ;
328
+ use super :: { BuildChildren , BuildWorldChildren } ;
243
329
use crate :: prelude:: { Children , Parent , PreviousParent } ;
244
330
use bevy_ecs:: {
245
331
entity:: Entity ,
@@ -281,7 +367,7 @@ mod tests {
281
367
}
282
368
283
369
#[ test]
284
- fn push_and_insert_children ( ) {
370
+ fn push_and_insert_children_commands ( ) {
285
371
let mut world = World :: default ( ) ;
286
372
287
373
let entities = world
@@ -340,4 +426,55 @@ mod tests {
340
426
PreviousParent ( parent)
341
427
) ;
342
428
}
429
+
430
+ #[ test]
431
+ fn push_and_insert_children_world ( ) {
432
+ let mut world = World :: default ( ) ;
433
+
434
+ let entities = world
435
+ . spawn_batch ( vec ! [ ( 1 , ) , ( 2 , ) , ( 3 , ) , ( 4 , ) , ( 5 , ) ] )
436
+ . collect :: < Vec < Entity > > ( ) ;
437
+
438
+ world. entity_mut ( entities[ 0 ] ) . push_children ( & entities[ 1 ..3 ] ) ;
439
+
440
+ let parent = entities[ 0 ] ;
441
+ let child1 = entities[ 1 ] ;
442
+ let child2 = entities[ 2 ] ;
443
+ let child3 = entities[ 3 ] ;
444
+ let child4 = entities[ 4 ] ;
445
+
446
+ let expected_children: SmallVec < [ Entity ; 8 ] > = smallvec ! [ child1, child2] ;
447
+ assert_eq ! (
448
+ world. get:: <Children >( parent) . unwrap( ) . 0 . clone( ) ,
449
+ expected_children
450
+ ) ;
451
+ assert_eq ! ( * world. get:: <Parent >( child1) . unwrap( ) , Parent ( parent) ) ;
452
+ assert_eq ! ( * world. get:: <Parent >( child2) . unwrap( ) , Parent ( parent) ) ;
453
+
454
+ assert_eq ! (
455
+ * world. get:: <PreviousParent >( child1) . unwrap( ) ,
456
+ PreviousParent ( parent)
457
+ ) ;
458
+ assert_eq ! (
459
+ * world. get:: <PreviousParent >( child2) . unwrap( ) ,
460
+ PreviousParent ( parent)
461
+ ) ;
462
+
463
+ world. entity_mut ( parent) . insert_children ( 1 , & entities[ 3 ..] ) ;
464
+ let expected_children: SmallVec < [ Entity ; 8 ] > = smallvec ! [ child1, child3, child4, child2] ;
465
+ assert_eq ! (
466
+ world. get:: <Children >( parent) . unwrap( ) . 0 . clone( ) ,
467
+ expected_children
468
+ ) ;
469
+ assert_eq ! ( * world. get:: <Parent >( child3) . unwrap( ) , Parent ( parent) ) ;
470
+ assert_eq ! ( * world. get:: <Parent >( child4) . unwrap( ) , Parent ( parent) ) ;
471
+ assert_eq ! (
472
+ * world. get:: <PreviousParent >( child3) . unwrap( ) ,
473
+ PreviousParent ( parent)
474
+ ) ;
475
+ assert_eq ! (
476
+ * world. get:: <PreviousParent >( child4) . unwrap( ) ,
477
+ PreviousParent ( parent)
478
+ ) ;
479
+ }
343
480
}
0 commit comments