refactor(node): use sized fn instead of dyn Fn for signals/methods

This commit is contained in:
Nova
2022-06-06 23:34:44 -04:00
parent 2c27e5728c
commit bb356f6cb1
2 changed files with 70 additions and 73 deletions

View File

@@ -4,18 +4,18 @@ use anyhow::{anyhow, Result};
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
use std::{collections::HashMap, vec::Vec}; use std::{collections::HashMap, vec::Vec};
pub type Signal<'a> = Box<dyn Fn(Rc<Client>, &[u8]) -> Result<()> + 'a>; pub type Signal = fn(&Node, Rc<Client>, &[u8]) -> Result<()>;
pub type Method<'a> = Box<dyn Fn(Rc<Client>, &[u8]) -> Result<Vec<u8>> + 'a>; pub type Method = fn(&Node, Rc<Client>, &[u8]) -> Result<Vec<u8>>;
pub struct Node<'a> { pub struct Node<'a> {
client: Weak<Client<'a>>, client: Weak<Client<'a>>,
path: String, path: String,
trailing_slash_pos: usize, trailing_slash_pos: usize,
local_signals: HashMap<String, Signal<'a>>, local_signals: HashMap<String, Signal>,
local_methods: HashMap<String, Method<'a>>, local_methods: HashMap<String, Method>,
destroyable: bool, destroyable: bool,
pub spatial: Option<Spatial<'a>>, pub spatial: Option<Rc<Spatial<'a>>>,
} }
impl<'a> Node<'a> { impl<'a> Node<'a> {
@@ -44,7 +44,7 @@ impl<'a> Node<'a> {
} }
} }
pub fn add_local_signal(&mut self, method: &str, signal: Signal<'a>) { pub fn add_local_signal(&mut self, method: &str, signal: Signal) {
self.local_signals.insert(method.to_string(), signal); self.local_signals.insert(method.to_string(), signal);
} }
@@ -58,7 +58,7 @@ impl<'a> Node<'a> {
.local_signals .local_signals
.get(method) .get(method)
.ok_or_else(|| anyhow!("Signal {} not found", method))?; .ok_or_else(|| anyhow!("Signal {} not found", method))?;
signal(calling_client, data) signal(self, calling_client, data)
} }
pub fn execute_local_method( pub fn execute_local_method(
&self, &self,
@@ -66,9 +66,11 @@ impl<'a> Node<'a> {
method: &str, method: &str,
data: &[u8], data: &[u8],
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
self.local_methods let method = self
.local_methods
.get(method) .get(method)
.ok_or_else(|| anyhow!("Method {} not found", method))?(calling_client, data) .ok_or_else(|| anyhow!("Method {} not found", method))?;
method(self, calling_client, data)
} }
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> { pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
self.get_client() self.get_client()

View File

@@ -4,12 +4,13 @@ use anyhow::{anyhow, bail, ensure, Result};
use glam::Mat4; use glam::Mat4;
use libstardustxr::{flex_to_quat, flex_to_vec3}; use libstardustxr::{flex_to_quat, flex_to_vec3};
use rccell::{RcCell, WeakCell}; use rccell::{RcCell, WeakCell};
use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
pub struct Spatial<'a> { pub struct Spatial<'a> {
node: WeakCell<Node<'a>>, node: WeakCell<Node<'a>>,
parent: WeakCell<Node<'a>>, parent: WeakCell<Node<'a>>,
transform: Mat4, transform: Cell<Mat4>,
} }
impl<'a> Spatial<'a> { impl<'a> Spatial<'a> {
@@ -24,57 +25,52 @@ impl<'a> Spatial<'a> {
let spatial = Spatial { let spatial = Spatial {
node: node.downgrade(), node: node.downgrade(),
parent, parent,
transform, transform: Cell::new(transform),
}; };
let node_captured = node.clone(); node.borrow_mut()
node.borrow_mut().add_local_signal( .add_local_signal("setTransform", Spatial::set_transform_flex);
"setTransform", node.borrow_mut().spatial = Some(Rc::new(spatial));
Box::new(move |calling_client, data| { Ok(())
let root = flexbuffers::Reader::get_root(data)?; }
let flex_vec = root.get_vector()?;
let client = node_captured pub fn set_transform_flex(node: &Node, calling_client: Rc<Client>, data: &[u8]) -> Result<()> {
.borrow() let root = flexbuffers::Reader::get_root(data)?;
.get_client() let flex_vec = root.get_vector()?;
.ok_or_else(|| anyhow!("Node somehow has no client"))?; let client = node
let other_spatial = calling_client .get_client()
.get_scenegraph() .ok_or_else(|| anyhow!("Node somehow has no client"))?;
.get_node(flex_vec.idx(0).as_str()) let other_spatial = calling_client
.ok_or_else(|| anyhow!("Other spatial node not found"))?; .get_scenegraph()
ensure!( .get_node(flex_vec.idx(0).as_str())
other_spatial.borrow().spatial.is_some(), .ok_or_else(|| anyhow!("Other spatial node not found"))?;
"Node is not a Spatial!" ensure!(
); other_spatial.borrow().spatial.is_some(),
let pos = flex_to_vec3!(flex_vec.idx(1)); "Node is not a Spatial!"
let rot = flex_to_quat!(flex_vec.idx(2));
let scl = flex_to_vec3!(flex_vec.idx(3));
node_captured
.borrow_mut()
.spatial
.as_mut()
.unwrap()
.set_transform_components(client, other_spatial, pos, rot, scl);
Ok(())
}),
); );
node.borrow_mut().spatial = Some(spatial); let pos = flex_to_vec3!(flex_vec.idx(1));
let rot = flex_to_quat!(flex_vec.idx(2));
let scl = flex_to_vec3!(flex_vec.idx(3));
node.spatial
.as_ref()
.unwrap()
.set_transform_components(other_spatial, pos, rot, scl);
Ok(()) Ok(())
} }
pub fn local_transform(&self) -> Mat4 { pub fn local_transform(&self) -> Mat4 {
self.transform self.transform.get()
} }
pub fn global_transform(&self) -> Mat4 { pub fn global_transform(&self) -> Mat4 {
match self.parent.upgrade() { match self.parent.upgrade() {
Some(value) => { Some(value) => {
value.borrow().spatial.as_ref().unwrap().global_transform() * self.transform value.borrow().spatial.as_ref().unwrap().global_transform() * self.transform.get()
} }
None => self.transform, None => self.transform.get(),
} }
} }
pub fn set_transform_components( pub fn set_transform_components(
&mut self, &self,
calling_client: Rc<Client>,
relative_space: RcCell<Node>, relative_space: RcCell<Node>,
pos: Option<mint::Vector3<f32>>, pos: Option<mint::Vector3<f32>>,
rot: Option<mint::Quaternion<f32>>, rot: Option<mint::Quaternion<f32>>,
@@ -88,32 +84,31 @@ impl<'a> Spatial<'a> {
pub fn create_interface(client: Rc<Client>) { pub fn create_interface(client: Rc<Client>) {
let mut node = Node::create(Rc::downgrade(&client), "", "spatial", false); let mut node = Node::create(Rc::downgrade(&client), "", "spatial", false);
node.add_local_signal( node.add_local_signal("createSpatial", create_spatial_flex);
"createSpatial",
Box::new(move |calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let node = Node::create(
Rc::downgrade(&calling_client),
"/spatial",
flex_vec.idx(0).get_str()?,
true,
);
let transform = Mat4::from_scale_rotation_translation(
flex_to_vec3!(flex_vec.idx(4))
.ok_or_else(|| anyhow!("Scale not found"))?
.into(),
flex_to_quat!(flex_vec.idx(3))
.ok_or_else(|| anyhow!("Rotation not found"))?
.into(),
flex_to_vec3!(flex_vec.idx(2))
.ok_or_else(|| anyhow!("Position not found"))?
.into(),
);
let node_rc = calling_client.get_scenegraph().add_node(node);
Spatial::add_to(node_rc, WeakCell::new(), transform)?;
Ok(())
}),
);
client.get_scenegraph().add_node(node); client.get_scenegraph().add_node(node);
} }
pub fn create_spatial_flex(_node: &Node, calling_client: Rc<Client>, data: &[u8]) -> Result<()> {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let spatial = Node::create(
Rc::downgrade(&calling_client),
"/spatial",
flex_vec.idx(0).get_str()?,
true,
);
let transform = Mat4::from_scale_rotation_translation(
flex_to_vec3!(flex_vec.idx(4))
.ok_or_else(|| anyhow!("Scale not found"))?
.into(),
flex_to_quat!(flex_vec.idx(3))
.ok_or_else(|| anyhow!("Rotation not found"))?
.into(),
flex_to_vec3!(flex_vec.idx(2))
.ok_or_else(|| anyhow!("Position not found"))?
.into(),
);
let spatial_rc = calling_client.get_scenegraph().add_node(spatial);
Spatial::add_to(spatial_rc, WeakCell::new(), transform)?;
Ok(())
}