feat: basic spatial closure
This commit is contained in:
@@ -9,8 +9,10 @@ version = "0.9.0"
|
||||
libstardustxr = {path = "../libstardustxr-rs"}
|
||||
anyhow = "1.0.57"
|
||||
ctrlc = "3.2.2"
|
||||
euler = {version = "0.4.0", features = ["mint"]}
|
||||
flexbuffers = "2.0.0"
|
||||
mint = "0.5.9"
|
||||
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
|
||||
rccell = "0.1.3"
|
||||
slab = "0.4.6"
|
||||
thiserror = "1.0.31"
|
||||
vek = "0.15.8"
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
use crate::core::client::Client;
|
||||
use crate::core::scenegraph::Scenegraph;
|
||||
use anyhow::{anyhow, ensure, Result};
|
||||
use libstardustxr::messenger::Messenger;
|
||||
use std::{collections::HashMap, rc::Weak, vec::Vec};
|
||||
|
||||
pub type Signal<'a> = dyn Fn(&[u8]) + 'a;
|
||||
pub type Method<'a> = dyn Fn(&[u8]) -> Vec<u8> + 'a;
|
||||
pub type Signal<'a> = Box<dyn Fn(&[u8]) -> Result<()> + 'a>;
|
||||
pub type Method<'a> = Box<dyn Fn(&[u8]) -> Result<Vec<u8>> + 'a>;
|
||||
|
||||
pub struct Node<'a> {
|
||||
path: String,
|
||||
trailing_slash_pos: usize,
|
||||
messenger: Weak<Messenger<'a>>,
|
||||
local_signals: HashMap<String, Box<Signal<'a>>>,
|
||||
local_methods: HashMap<String, Box<Method<'a>>>,
|
||||
scenegraph: Option<&'a Scenegraph<'a>>,
|
||||
local_signals: HashMap<String, Signal<'a>>,
|
||||
local_methods: HashMap<String, Method<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Node<'a> {
|
||||
@@ -22,7 +24,11 @@ impl<'a> Node<'a> {
|
||||
self.path.as_str()
|
||||
}
|
||||
|
||||
pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result<Node<'a>> {
|
||||
pub fn from_path(
|
||||
client: Option<&Client<'a>>,
|
||||
path: &str,
|
||||
destroyable: bool,
|
||||
) -> Result<Node<'a>> {
|
||||
ensure!(path.starts_with('/'), "Invalid path {}", path);
|
||||
let mut weak_messenger = Weak::default();
|
||||
if let Some(c) = client.as_ref() {
|
||||
@@ -34,22 +40,25 @@ impl<'a> Node<'a> {
|
||||
.rfind('/')
|
||||
.ok_or_else(|| anyhow!("Invalid path {}", path))?,
|
||||
messenger: weak_messenger,
|
||||
scenegraph: client.as_ref().map(|f| f.scenegraph.as_ref().unwrap()),
|
||||
local_signals: HashMap::new(),
|
||||
local_methods: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn add_local_signal(&mut self, method: &str, signal: Signal<'a>) {
|
||||
self.local_signals.insert(method.to_string(), signal);
|
||||
}
|
||||
|
||||
pub fn send_local_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
||||
self.local_signals
|
||||
.get(method)
|
||||
.ok_or_else(|| anyhow!("Signal {} not found", method))?(data);
|
||||
Ok(())
|
||||
.ok_or_else(|| anyhow!("Signal {} not found", method))?(data)
|
||||
}
|
||||
pub fn execute_local_method(&self, method: &str, data: &[u8]) -> Result<Vec<u8>> {
|
||||
Ok(self
|
||||
.local_methods
|
||||
self.local_methods
|
||||
.get(method)
|
||||
.ok_or_else(|| anyhow!("Method {} not found", method))?(data))
|
||||
.ok_or_else(|| anyhow!("Method {} not found", method))?(data)
|
||||
}
|
||||
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
||||
self.messenger
|
||||
|
||||
@@ -1,44 +1,73 @@
|
||||
use super::core::Node;
|
||||
use crate::core::client::Client;
|
||||
use anyhow::{anyhow, Result};
|
||||
use euler::Mat4;
|
||||
use libstardustxr::{flex_to_quat, flex_to_vec3};
|
||||
use mint::RowMatrix4;
|
||||
use rccell::{RcCell, WeakCell};
|
||||
use vek::mat::repr_c::row_major::Mat4;
|
||||
|
||||
pub struct Spatial<'a> {
|
||||
pub node: Node<'a>,
|
||||
parent: WeakCell<Spatial<'a>>,
|
||||
transform: Mat4<f32>,
|
||||
transform: RowMatrix4<f32>,
|
||||
}
|
||||
|
||||
impl<'a> Spatial<'a> {
|
||||
pub fn new(
|
||||
client: Option<&mut Client<'a>>,
|
||||
path: &str,
|
||||
transform: Mat4<f32>,
|
||||
) -> WeakCell<Self> {
|
||||
let spatial = RcCell::new(Spatial {
|
||||
node: Node::from_path(client.as_deref(), path).unwrap(),
|
||||
transform: RowMatrix4<f32>,
|
||||
) -> Result<WeakCell<Self>> {
|
||||
let mut spatial = Spatial {
|
||||
node: Node::from_path(client.as_deref(), path, true).unwrap(),
|
||||
parent: WeakCell::new(),
|
||||
transform,
|
||||
});
|
||||
let weak_spatial = spatial.downgrade();
|
||||
client
|
||||
.unwrap()
|
||||
.scenegraph
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.spatial_nodes
|
||||
.insert(path.to_string(), spatial);
|
||||
weak_spatial
|
||||
};
|
||||
let spatial_cell = RcCell::new(spatial);
|
||||
let weak_spatial = spatial_cell.downgrade();
|
||||
if let Some(client) = client {
|
||||
let weak_spatial = weak_spatial.clone();
|
||||
spatial.node.add_local_signal(
|
||||
"setTransform",
|
||||
Box::new(|data| {
|
||||
let root = flexbuffers::Reader::get_root(data).unwrap();
|
||||
let flex_vec = root.get_vector().unwrap();
|
||||
let spatial = client
|
||||
.scenegraph
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.spatial_nodes
|
||||
.get(flex_vec.idx(1).as_str())
|
||||
.ok_or(anyhow!("Spatial not found"))?;
|
||||
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));
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
client
|
||||
.scenegraph
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.spatial_nodes
|
||||
.insert(path.to_string(), spatial_cell);
|
||||
}
|
||||
Ok(weak_spatial)
|
||||
}
|
||||
|
||||
pub fn local_transform(&self) -> Mat4<f32> {
|
||||
pub fn local_transform(&self) -> RowMatrix4<f32> {
|
||||
self.transform
|
||||
}
|
||||
pub fn global_transform(&self) -> Mat4<f32> {
|
||||
match self.parent.upgrade() {
|
||||
Some(value) => value.borrow().global_transform() * self.transform,
|
||||
None => self.transform,
|
||||
}
|
||||
pub fn global_transform(&self) -> RowMatrix4<f32> {
|
||||
todo!()
|
||||
// match self.parent.upgrade() {
|
||||
// Some(value) => Mat4::from(value.borrow().global_transform()) * self.transform,
|
||||
// None => self.transform,
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn set_transform_components(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// pub fn relative_transform(&self, space: WeakCell<Spatial>) {}
|
||||
|
||||
Reference in New Issue
Block a user