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

View File

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