Start refining RbxTree operations, going to be a new crate

This commit is contained in:
Lucien Greathouse
2018-10-31 18:07:02 -07:00
parent dbad0a16c4
commit 31e1f61548

View File

@@ -7,7 +7,19 @@ use id::{Id, get_id};
pub enum RbxValue { pub enum RbxValue {
String { String {
value: String, value: String,
} },
Number {
value: f64,
},
Bool {
value: bool,
},
Vector3 {
value: [f64; 3],
},
Color3 {
value: [u8; 3],
},
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -71,6 +83,7 @@ impl<'a> Iterator for Descendants<'a> {
pub struct RbxTree { pub struct RbxTree {
instances: HashMap<Id, RbxInstance>, instances: HashMap<Id, RbxInstance>,
// TODO: Make private
pub root_instance_id: Id, pub root_instance_id: Id,
} }
@@ -103,6 +116,30 @@ impl RbxTree {
&self.instances &self.instances
} }
// TODO: Test this function!
pub fn insert_tree(&mut self, parent_id: Id, tree: &RbxTree) {
let mut to_visit = vec![tree.root_instance_id];
loop {
let id = match to_visit.pop() {
Some(id) => id,
None => break,
};
let mut new_child = tree.get_instance(id).unwrap().clone();
for child in &new_child.children {
to_visit.push(*child);
}
if id == tree.root_instance_id {
new_child.parent = Some(parent_id);
}
self.insert_instance(new_child);
}
}
pub fn insert_instance(&mut self, mut instance: RbxInstance) { pub fn insert_instance(&mut self, mut instance: RbxInstance) {
match instance.parent { match instance.parent {
Some(parent_id) => { Some(parent_id) => {