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
use {
crate::{
scene::{
game_object::GameObject,
Scene,
},
registration::named::Named
},
std::sync::{Arc, RwLock}
};
#[derive(Clone, Debug)]
pub enum ParentWrapper{
GameObject(Arc<RwLock<dyn GameObject>>),
Scene(Arc<RwLock<Scene>>)
}
pub trait Parent: Named {
fn get_children(&self) -> Vec<Arc<RwLock<dyn GameObject >>>;
fn add_child(&mut self, child: Arc<RwLock<dyn GameObject>>);
unsafe fn replace_child(&mut self, old: Arc<RwLock<dyn GameObject>>, new: Arc<RwLock<dyn GameObject>>) -> Result<(), ()>;
unsafe fn remove_child(&mut self, child: Arc<RwLock<dyn GameObject>>) -> Result<(), ()>;
fn get_child_by_name(&self, name: &str) -> Result<Arc<RwLock<dyn GameObject>>, &str> {
let mut result = Err("No child with that name was found.");
self.get_children().into_iter().for_each( |child|
if child.read().unwrap().get_name() == name {
match result {
Ok(_) => panic!("Two children share the same name"),
Err(_) => { result = Ok(child); }
}
}
);
result
}
}
pub trait Child: Named {
fn get_parent(&self) -> ParentWrapper;
unsafe fn set_parent(&mut self, parent: ParentWrapper);
}