refactor: use inheritance-based style for nodes

This commit is contained in:
Nova
2022-05-23 21:42:26 -04:00
parent 1709d19da2
commit 618f2d6748
3 changed files with 47 additions and 65 deletions

View File

@@ -1,5 +1,4 @@
use crate::core::client::Client;
use crate::nodes::core::{Node, NodeRef};
use crate::nodes::spatial::Spatial;
use anyhow::Result;
use libstardustxr::scenegraph;
@@ -8,41 +7,26 @@ use rccell::{RcCell, WeakCell};
use std::collections::HashMap;
pub struct Scenegraph<'a> {
nodes: HashMap<String, RcCell<Node<'a>>>,
root: NodeRef<'a>,
hmd: NodeRef<'a>,
pub spatial_nodes: HashMap<String, RcCell<Spatial<'a>>>,
}
impl<'a> Scenegraph<'a> {
pub fn new(client: &mut Client<'a>) -> Self {
// root: Spatial::new(Some(client), "/", Default::default()),
// hmd: Spatial::new(Some(client), "/hmd", Default::default()),
Scenegraph {
nodes: HashMap::new(),
root: Spatial::new_node(Some(client), "/", Default::default()).unwrap(),
hmd: Spatial::new_node(Some(client), "/hmd", Default::default()).unwrap(),
spatial_nodes: HashMap::new(),
}
}
pub fn add_node(&mut self, node: RcCell<Node<'a>>) {
let path = node.borrow().get_path().to_string();
self.nodes.insert(path, node);
}
pub fn remove_node(&mut self, path: &str) {
self.nodes.remove(path);
}
pub fn get_node(&self, path: &str) -> WeakCell<Node<'a>> {
self.nodes
.get(path)
.map_or(WeakCell::new(), RcCell::downgrade)
}
}
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.nodes
self.spatial_nodes
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.node
.send_local_signal(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
@@ -52,10 +36,11 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
self.nodes
self.spatial_nodes
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.node
.execute_local_method(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}