From f9f36dd43a017eaf211270ad6e1039d7fcc3e93a Mon Sep 17 00:00:00 2001 From: Nova Date: Wed, 8 Jun 2022 17:28:27 -0400 Subject: [PATCH] feat(node): destroy method --- src/core/scenegraph.rs | 4 ++++ src/nodes/core.rs | 19 ++++++++++++++++++- src/nodes/spatial.rs | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/core/scenegraph.rs b/src/core/scenegraph.rs index 679602c..0729f1f 100644 --- a/src/core/scenegraph.rs +++ b/src/core/scenegraph.rs @@ -34,6 +34,10 @@ impl<'a> Scenegraph<'a> { pub fn get_node(&self, path: &str) -> Option>> { Some(self.nodes.read().ok()?.get(path)?.clone()) } + + pub fn remove_node(&self, path: &str) -> Option>> { + self.nodes.write().unwrap().remove(path) + } } impl<'a> scenegraph::Scenegraph for Scenegraph<'a> { diff --git a/src/nodes/core.rs b/src/nodes/core.rs index 34b124c..d7bf5cd 100644 --- a/src/nodes/core.rs +++ b/src/nodes/core.rs @@ -28,12 +28,15 @@ impl<'a> Node<'a> { pub fn get_path(&self) -> &str { self.path.as_str() } + pub fn is_destroyable(&self) -> bool { + self.destroyable + } pub fn create(client: Weak>, parent: &str, name: &str, destroyable: bool) -> Self { let mut path = parent.to_string(); path.push('/'); path.push_str(name); - Node { + let mut node = Node { client, path, trailing_slash_pos: parent.len(), @@ -41,9 +44,23 @@ impl<'a> Node<'a> { local_methods: HashMap::new(), destroyable, spatial: None, + }; + node.add_local_signal("destroy", Node::destroy_flex); + node + } + pub fn destroy(&self) { + if let Some(client) = self.get_client() { + let _ = client.get_scenegraph().remove_node(self.get_path()); } } + pub fn destroy_flex(node: &Node, _calling_client: Rc, _data: &[u8]) -> Result<()> { + if node.is_destroyable() { + node.destroy(); + } + Ok(()) + } + pub fn add_local_signal(&mut self, method: &str, signal: Signal) { self.local_signals.insert(method.to_string(), signal); } diff --git a/src/nodes/spatial.rs b/src/nodes/spatial.rs index c660dae..2133bdb 100644 --- a/src/nodes/spatial.rs +++ b/src/nodes/spatial.rs @@ -93,7 +93,7 @@ pub fn create_spatial_flex(_node: &Node, calling_client: Rc, data: &[u8] let flex_vec = root.get_vector()?; let spatial = Node::create( Rc::downgrade(&calling_client), - "/spatial", + "/spatial/spatial", flex_vec.idx(0).get_str()?, true, );