refactor(node): return weak reference and store Rc in scenegraph

This commit is contained in:
Nova
2022-05-17 02:35:44 -04:00
parent a736fb503e
commit 52565cd381
4 changed files with 45 additions and 38 deletions

View File

@@ -12,3 +12,4 @@ ctrlc = "3.2.2"
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]} mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
slab = "0.4.6" slab = "0.4.6"
thiserror = "1.0.31" thiserror = "1.0.31"
vek = "0.15.8"

View File

@@ -2,11 +2,11 @@ use crate::nodes::core::Node;
use anyhow::Result; use anyhow::Result;
use libstardustxr::scenegraph; use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError; use libstardustxr::scenegraph::ScenegraphError;
use std::{cell::RefCell, collections::HashMap, rc::Weak}; use std::{cell::RefCell, collections::HashMap, rc::Rc, rc::Weak};
#[derive(Default)] #[derive(Default)]
pub struct Scenegraph<'a> { pub struct Scenegraph<'a> {
nodes: RefCell<HashMap<String, Weak<Node<'a>>>>, nodes: RefCell<HashMap<String, Rc<RefCell<Node<'a>>>>>,
} }
impl<'a> Scenegraph<'a> { impl<'a> Scenegraph<'a> {
@@ -14,26 +14,20 @@ impl<'a> Scenegraph<'a> {
Default::default() Default::default()
} }
pub fn add_node(&self, node: Weak<Node<'a>>) { pub fn add_node(&self, node: Rc<RefCell<Node<'a>>>) {
let node_ref = node.upgrade(); let path = node.borrow().get_path().to_string();
if node_ref.is_none() { self.nodes.borrow_mut().insert(path, node);
return; }
}
pub fn remove_node(&self, path: &str) {
self.nodes.borrow_mut().remove(path);
}
pub fn get_node(&self, path: &str) -> Weak<RefCell<Node<'a>>> {
self.nodes self.nodes
.borrow_mut() .borrow()
.insert(String::from(node_ref.unwrap().get_path()), node); .get(path)
} .map_or(Weak::default(), |node| Rc::downgrade(node))
pub fn remove_node(&self, node: Weak<Node<'a>>) {
let node_ref = node.upgrade();
if node_ref.is_none() {
return;
}
self.nodes.borrow_mut().remove(node_ref.unwrap().get_path());
}
pub fn get_node(&self, path: &str) -> Weak<Node<'a>> {
self.nodes.borrow().get(path).cloned().unwrap_or_default()
} }
} }
@@ -43,8 +37,7 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
.borrow() .borrow()
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.upgrade() .borrow()
.ok_or(ScenegraphError::NodeNotFound)?
.send_local_signal(method, data) .send_local_signal(method, data)
.map_err(|_| ScenegraphError::MethodNotFound) .map_err(|_| ScenegraphError::MethodNotFound)
} }
@@ -58,8 +51,7 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
.borrow() .borrow()
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.upgrade() .borrow()
.ok_or(ScenegraphError::NodeNotFound)?
.execute_local_method(method, data) .execute_local_method(method, data)
.map_err(|_| ScenegraphError::MethodNotFound) .map_err(|_| ScenegraphError::MethodNotFound)
} }

View File

@@ -2,6 +2,7 @@ use crate::core::client::Client;
use anyhow::{anyhow, ensure, Result}; use anyhow::{anyhow, ensure, Result};
use libstardustxr::messenger::Messenger; use libstardustxr::messenger::Messenger;
use std::{ use std::{
cell::RefCell,
collections::HashMap, collections::HashMap,
rc::{Rc, Weak}, rc::{Rc, Weak},
vec::Vec, vec::Vec,
@@ -19,7 +20,7 @@ pub struct Node<'a> {
local_signals: HashMap<String, Box<Signal<'a>>>, local_signals: HashMap<String, Box<Signal<'a>>>,
local_methods: HashMap<String, Box<Method<'a>>>, local_methods: HashMap<String, Box<Method<'a>>>,
pub spatial: Option<Spatial<'a>>, pub spatial: Option<Spatial>,
} }
impl<'a> Node<'a> { impl<'a> Node<'a> {
@@ -30,7 +31,7 @@ impl<'a> Node<'a> {
self.path.as_str() self.path.as_str()
} }
pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result<Rc<Self>> { pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result<Weak<RefCell<Self>>> {
ensure!(path.starts_with('/'), "Invalid path {}", path); ensure!(path.starts_with('/'), "Invalid path {}", path);
let mut weak_messenger = Weak::default(); let mut weak_messenger = Weak::default();
if client.is_some() { if client.is_some() {
@@ -45,14 +46,12 @@ impl<'a> Node<'a> {
spatial: None, spatial: None,
}; };
let node_ref = Rc::new(node); let node_ref = Rc::new(RefCell::new(node));
let weak_node = Rc::downgrade(&node_ref);
if client.is_some() { if client.is_some() {
client client.unwrap().scenegraph.add_node(node_ref);
.unwrap()
.scenegraph
.add_node(Rc::downgrade(&node_ref));
} }
Ok(node_ref) Ok(weak_node)
} }
pub fn send_local_signal(&self, method: &str, data: &[u8]) -> Result<()> { pub fn send_local_signal(&self, method: &str, data: &[u8]) -> Result<()> {

View File

@@ -1,11 +1,26 @@
use super::core::Node; use super::core::Node;
use crate::core::client::Client;
use anyhow::Result;
use std::cell::RefCell;
use std::rc::Weak;
use vek::mat::repr_c::row_major::Mat4;
pub struct Spatial<'a> { pub struct Spatial {
node: &'a Node<'a>, transform: Mat4<f32>,
} }
impl<'a> Spatial<'a> { impl<'a> Spatial {
pub fn new(node: &'a Node<'a>) -> Self { pub fn new(transform: Mat4<f32>) -> Self {
Spatial { node } Spatial { transform }
}
pub fn new_node(
client: Option<&'a Client<'a>>,
path: &str,
transform: Mat4<f32>,
) -> Result<Weak<RefCell<Node<'a>>>> {
let node = Node::from_path(client, path)?;
node.upgrade().unwrap().borrow_mut().spatial = Some(Spatial::new(transform));
Ok(node)
} }
} }