refactor(spatial): store weak reference to parent node
This commit is contained in:
@@ -1,23 +1,24 @@
|
|||||||
use super::scenegraph::Scenegraph;
|
use super::scenegraph::Scenegraph;
|
||||||
use crate::nodes::core::{Node, NodeRef};
|
|
||||||
use libstardustxr::messenger::Messenger;
|
use libstardustxr::messenger::Messenger;
|
||||||
use mio::net::UnixStream;
|
use mio::net::UnixStream;
|
||||||
use std::rc::{Rc, Weak};
|
use std::rc::{Rc, Weak};
|
||||||
|
|
||||||
pub struct Client<'a> {
|
pub struct Client<'a> {
|
||||||
messenger: Rc<Messenger<'a>>,
|
messenger: Rc<Messenger<'a>>,
|
||||||
pub scenegraph: Scenegraph<'a>,
|
pub scenegraph: Option<Scenegraph<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Client<'a> {
|
impl<'a> Client<'a> {
|
||||||
pub fn from_connection(connection: UnixStream) -> Self {
|
pub fn from_connection(connection: UnixStream) -> Self {
|
||||||
Client {
|
let mut client = Client {
|
||||||
scenegraph: Default::default(),
|
scenegraph: None,
|
||||||
messenger: Rc::new(Messenger::new(connection)),
|
messenger: Rc::new(Messenger::new(connection)),
|
||||||
}
|
};
|
||||||
|
client.scenegraph = Some(Scenegraph::new(&mut client));
|
||||||
|
client
|
||||||
}
|
}
|
||||||
pub fn dispatch(&self) -> Result<(), std::io::Error> {
|
pub fn dispatch(&self) -> Result<(), std::io::Error> {
|
||||||
self.messenger.dispatch(&self.scenegraph)
|
self.messenger.dispatch(self.scenegraph.as_ref().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_weak_messenger(&self) -> Weak<Messenger<'a>> {
|
pub fn get_weak_messenger(&self) -> Weak<Messenger<'a>> {
|
||||||
|
|||||||
@@ -14,6 +14,13 @@ pub struct Scenegraph<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Scenegraph<'a> {
|
impl<'a> Scenegraph<'a> {
|
||||||
|
pub fn new(client: &mut Client<'a>) -> Self {
|
||||||
|
Scenegraph {
|
||||||
|
nodes: HashMap::new(),
|
||||||
|
root: Spatial::new_node(Some(client), "/", Default::default()).unwrap(),
|
||||||
|
hmd: Spatial::new_node(Some(client), "/hmd", Default::default()).unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn add_node(&mut self, node: RcCell<Node<'a>>) {
|
pub fn add_node(&mut self, node: RcCell<Node<'a>>) {
|
||||||
let path = node.borrow().get_path().to_string();
|
let path = node.borrow().get_path().to_string();
|
||||||
self.nodes.insert(path, node);
|
self.nodes.insert(path, node);
|
||||||
@@ -28,22 +35,6 @@ impl<'a> Scenegraph<'a> {
|
|||||||
.get(path)
|
.get(path)
|
||||||
.map_or(WeakCell::new(), RcCell::downgrade)
|
.map_or(WeakCell::new(), RcCell::downgrade)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_interfaces(&mut self, client: &mut Client<'a>) -> Result<()> {
|
|
||||||
self.root = Spatial::new_node(Some(client), "/", Default::default())?;
|
|
||||||
self.hmd = Spatial::new_node(Some(client), "/hmd", Default::default())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Default for Scenegraph<'a> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Scenegraph {
|
|
||||||
nodes: HashMap::new(),
|
|
||||||
root: WeakCell::new(),
|
|
||||||
hmd: WeakCell::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
|
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
|
||||||
|
|||||||
@@ -2,12 +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 rccell::{RcCell, WeakCell};
|
use rccell::{RcCell, WeakCell};
|
||||||
use std::{
|
use std::{collections::HashMap, rc::Weak, vec::Vec};
|
||||||
cell::RefCell,
|
|
||||||
collections::HashMap,
|
|
||||||
rc::{Rc, Weak},
|
|
||||||
vec::Vec,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::spatial::Spatial;
|
use super::spatial::Spatial;
|
||||||
|
|
||||||
@@ -39,12 +34,16 @@ impl<'a> Node<'a> {
|
|||||||
self.path.as_str()
|
self.path.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_path(client: Option<&mut Client<'a>>, path: &str) -> Result<NodeRef<'a>> {
|
pub fn from_path(
|
||||||
|
client: Option<&mut Client<'a>>,
|
||||||
|
path: &str,
|
||||||
|
data_closure: impl FnOnce(NodeRef<'a>) -> NodeData<'a>,
|
||||||
|
) -> Result<NodeRef<'a>> {
|
||||||
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() {
|
client
|
||||||
weak_messenger = client.as_ref().unwrap().get_weak_messenger();
|
.as_ref()
|
||||||
}
|
.map(|c| weak_messenger = c.get_weak_messenger());
|
||||||
let node = Node {
|
let node = Node {
|
||||||
path: path.to_string(),
|
path: path.to_string(),
|
||||||
trailing_slash_pos: path
|
trailing_slash_pos: path
|
||||||
@@ -58,10 +57,8 @@ impl<'a> Node<'a> {
|
|||||||
};
|
};
|
||||||
let node_ref = RcCell::new(node);
|
let node_ref = RcCell::new(node);
|
||||||
let weak_node = node_ref.downgrade();
|
let weak_node = node_ref.downgrade();
|
||||||
match client {
|
node_ref.borrow_mut().data = data_closure(weak_node.clone());
|
||||||
Some(client_) => client_.scenegraph.add_node(node_ref),
|
client.map(|c| c.scenegraph.as_mut().unwrap().add_node(node_ref));
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
Ok(weak_node)
|
Ok(weak_node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ use anyhow::Result;
|
|||||||
use vek::mat::repr_c::row_major::Mat4;
|
use vek::mat::repr_c::row_major::Mat4;
|
||||||
|
|
||||||
pub struct Spatial<'a> {
|
pub struct Spatial<'a> {
|
||||||
node: &'a Node<'a>,
|
node: NodeRef<'a>,
|
||||||
transform: Mat4<f32>,
|
transform: Mat4<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Spatial<'a> {
|
impl<'a> Spatial<'a> {
|
||||||
pub fn new(node: &'a Node<'a>, transform: Mat4<f32>) -> Self {
|
pub fn new(node: NodeRef<'a>, transform: Mat4<f32>) -> Self {
|
||||||
Spatial { node, transform }
|
Spatial { node, transform }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,8 +18,8 @@ impl<'a> Spatial<'a> {
|
|||||||
path: &str,
|
path: &str,
|
||||||
transform: Mat4<f32>,
|
transform: Mat4<f32>,
|
||||||
) -> Result<NodeRef<'a>> {
|
) -> Result<NodeRef<'a>> {
|
||||||
let node = Node::from_path(client, path)?;
|
Node::from_path(client, path, move |node| {
|
||||||
node.upgrade().unwrap().borrow_mut().spatial = Some(Spatial::new(transform));
|
NodeData::Spatial(Spatial::new(node, transform))
|
||||||
Ok(node)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user