1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use bevy_ecs::{
component::Component,
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
prelude::FromWorld,
reflect::{ReflectComponent, ReflectMapEntities},
world::World,
};
use bevy_reflect::Reflect;
use core::slice;
use smallvec::SmallVec;
use std::ops::Deref;
#[derive(Component, Debug, Reflect)]
#[reflect(Component, MapEntities)]
pub struct Children(pub(crate) SmallVec<[Entity; 8]>);
impl MapEntities for Children {
fn map_entities(&mut self, entity_map: &EntityMap) -> Result<(), MapEntitiesError> {
for entity in &mut self.0 {
*entity = entity_map.get(*entity)?;
}
Ok(())
}
}
impl FromWorld for Children {
fn from_world(_world: &mut World) -> Self {
Children(SmallVec::new())
}
}
impl Children {
pub(crate) fn from_entities(entities: &[Entity]) -> Self {
Self(SmallVec::from_slice(entities))
}
pub fn swap(&mut self, a_index: usize, b_index: usize) {
self.0.swap(a_index, b_index);
}
}
impl Deref for Children {
type Target = [Entity];
fn deref(&self) -> &Self::Target {
&self.0[..]
}
}
impl<'a> IntoIterator for &'a Children {
type Item = <Self::IntoIter as Iterator>::Item;
type IntoIter = slice::Iter<'a, Entity>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}