feat(spatial): basic interface

This commit is contained in:
Nova
2022-06-05 05:04:16 -04:00
parent fedbe17b7a
commit 1d93046d5e
4 changed files with 74 additions and 19 deletions

View File

@@ -29,23 +29,24 @@ impl<'a> Node<'a> {
self.path.as_str()
}
pub fn from_path(
pub fn create(
client: WeakCell<Client<'a>>,
path: &str,
parent: &str,
name: &str,
destroyable: bool,
) -> Result<Node<'a>> {
ensure!(path.starts_with('/'), "Invalid path {}", path);
Ok(Node {
) -> Self {
let mut path = parent.to_string();
path.push('/');
path.push_str(name);
Node {
client,
path: path.to_string(),
trailing_slash_pos: path
.rfind('/')
.ok_or_else(|| anyhow!("Invalid path {}", path))?,
path: path,
trailing_slash_pos: parent.len(),
local_signals: HashMap::new(),
local_methods: HashMap::new(),
destroyable,
spatial: None,
})
}
}
pub fn add_local_signal(&mut self, method: &str, signal: Signal<'a>) {

View File

@@ -1,6 +1,6 @@
use super::core::Node;
use crate::core::client::Client;
use anyhow::{anyhow, ensure, Result};
use anyhow::{anyhow, bail, ensure, Result};
use glam::{Mat4, Quat, Vec3};
use libstardustxr::{flex_to_quat, flex_to_vec3};
use rccell::{RcCell, WeakCell};
@@ -12,10 +12,17 @@ pub struct Spatial<'a> {
}
impl<'a> Spatial<'a> {
pub fn new(node: RcCell<Node<'a>>, transform: Mat4) -> Self {
pub fn add_to(
node: RcCell<Node<'a>>,
parent: WeakCell<Node<'a>>,
transform: Mat4,
) -> Result<()> {
if node.borrow_mut().spatial.is_none() {
bail!("Node already has a Spatial aspect!");
}
let spatial = Spatial {
node: node.downgrade(),
parent: WeakCell::new(),
parent,
transform,
};
let node_captured = node.clone();
@@ -24,7 +31,6 @@ impl<'a> Spatial<'a> {
Box::new(move |calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
// let node = node.
let client = node_captured
.borrow()
.get_client()
@@ -52,7 +58,8 @@ impl<'a> Spatial<'a> {
Ok(())
}),
);
spatial
node.borrow_mut().spatial = Some(spatial);
Ok(())
}
pub fn local_transform(&self) -> Mat4 {
@@ -80,3 +87,34 @@ impl<'a> Spatial<'a> {
// pub fn relative_transform(&self, space: WeakCell<Spatial>) {}
}
pub fn create_interface(client: RcCell<Client>) {
let mut node = Node::create(client.downgrade(), "", "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(
calling_client.downgrade(),
"/spatial",
flex_vec.idx(0).get_str()?,
true,
);
let pos = flex_to_vec3!(flex_vec.idx(2)).ok_or(anyhow!("Position not found"))?;
let rot = flex_to_quat!(flex_vec.idx(3)).ok_or(anyhow!("Rotation not found"))?;
let scl = flex_to_vec3!(flex_vec.idx(4)).ok_or(anyhow!("Scale not found"))?;
let node_rc = calling_client
.borrow_mut()
.get_scenegraph_mut()
.add_node(node);
Spatial::add_to(
node_rc,
WeakCell::new(),
Mat4::from_scale_rotation_translation(scl.into(), rot.into(), pos.into()),
)?;
Ok(())
}),
);
client.borrow_mut().get_scenegraph_mut().add_node(node);
}