feat: basic scenegraph, node, and spatial

This commit is contained in:
Nova
2022-05-16 14:19:08 -04:00
parent 888e60b8a2
commit cbfd4c13c2
10 changed files with 197 additions and 7 deletions

66
src/core/scenegraph.rs Normal file
View File

@@ -0,0 +1,66 @@
use crate::nodes::core::Node;
use anyhow::Result;
use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError;
use std::{cell::RefCell, collections::HashMap, rc::Weak};
#[derive(Default)]
pub struct Scenegraph<'a> {
nodes: RefCell<HashMap<String, Weak<Node<'a>>>>,
}
impl<'a> Scenegraph<'a> {
pub fn new() -> Self {
Default::default()
}
pub fn add_node(&self, node: Weak<Node<'a>>) {
let node_ref = node.upgrade();
if node_ref.is_none() {
return;
}
self.nodes
.borrow_mut()
.insert(String::from(node_ref.unwrap().get_path()), 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()
}
}
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.nodes
.borrow()
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.upgrade()
.ok_or(ScenegraphError::NodeNotFound)?
.send_local_signal(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
fn execute_method(
&self,
path: &str,
method: &str,
data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> {
self.nodes
.borrow()
.get(path)
.ok_or(ScenegraphError::NodeNotFound)?
.upgrade()
.ok_or(ScenegraphError::NodeNotFound)?
.execute_local_method(method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
}