refactor(items): codegen prototocol
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
use super::{Item, ItemType};
|
||||
use super::{create_item_acceptor_flex, register_item_ui_flex, Item, ItemInterface, ItemType};
|
||||
use crate::{
|
||||
core::{
|
||||
client::{Client, INTERNAL_CLIENT},
|
||||
registry::Registry,
|
||||
scenegraph::MethodResponseSender,
|
||||
},
|
||||
create_interface,
|
||||
nodes::{
|
||||
drawable::{model::ModelPart, shaders::UNLIT_SHADER_BYTES},
|
||||
items::TypeInfo,
|
||||
spatial::{parse_transform, Spatial, Transform},
|
||||
spatial::{Spatial, Transform},
|
||||
Message, Node,
|
||||
},
|
||||
};
|
||||
use color_eyre::eyre::{bail, eyre, Result};
|
||||
use glam::Mat4;
|
||||
use lazy_static::lazy_static;
|
||||
use mint::{RowMatrix4, Vector2};
|
||||
use mint::{ColumnMatrix4, Vector2};
|
||||
use nanoid::nanoid;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::Mutex;
|
||||
use send_wrapper::SendWrapper;
|
||||
use serde::Deserialize;
|
||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||
use std::sync::Arc;
|
||||
use stereokit_rust::{
|
||||
@@ -33,6 +33,7 @@ use stereokit_rust::{
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
stardust_xr_server_codegen::codegen_item_camera_protocol!();
|
||||
lazy_static! {
|
||||
pub(super) static ref ITEM_TYPE_INFO_CAMERA: TypeInfo = TypeInfo {
|
||||
type_name: "camera",
|
||||
@@ -42,6 +43,9 @@ lazy_static! {
|
||||
ui: Default::default(),
|
||||
items: Registry::new(),
|
||||
acceptors: Registry::new(),
|
||||
new_acceptor_fn: |node, uid, acceptor, acceptor_field| {
|
||||
let _ = camera_item_ui_client::create_acceptor(node, uid, acceptor, acceptor_field);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -114,8 +118,11 @@ impl CameraItem {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn serialize_start_data(&self, id: &str) -> Result<Message> {
|
||||
Ok(serialize(id)?.into())
|
||||
pub fn send_ui_item_created(&self, node: &Node, uid: &str, item: &Arc<Node>) {
|
||||
let _ = camera_item_ui_client::create_item(node, uid, item);
|
||||
}
|
||||
pub fn send_acceptor_item_created(&self, node: &Node, uid: &str, item: &Arc<Node>) {
|
||||
let _ = camera_item_acceptor_client::capture_item(node, uid, item);
|
||||
}
|
||||
|
||||
pub fn update(&self, token: &MainThreadToken) {
|
||||
@@ -169,34 +176,55 @@ pub fn update(token: &MainThreadToken) {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn create_camera_item_flex(
|
||||
_node: Arc<Node>,
|
||||
calling_client: Arc<Client>,
|
||||
message: Message,
|
||||
) -> Result<()> {
|
||||
#[derive(Deserialize)]
|
||||
struct CreateCameraItemInfo<'a> {
|
||||
name: &'a str,
|
||||
parent_path: &'a str,
|
||||
create_interface!(ItemInterface, ItemCameraInterfaceAspect, "/item/camera");
|
||||
impl ItemCameraInterfaceAspect for ItemInterface {
|
||||
#[doc = "Create a camera item at a specific location"]
|
||||
fn create_camera_item(
|
||||
_node: Arc<Node>,
|
||||
calling_client: Arc<Client>,
|
||||
name: String,
|
||||
parent: Arc<Node>,
|
||||
transform: Transform,
|
||||
proj_matrix: RowMatrix4<f32>,
|
||||
proj_matrix: ColumnMatrix4<f32>,
|
||||
px_size: Vector2<u32>,
|
||||
}
|
||||
let info: CreateCameraItemInfo = deserialize(message.as_ref())?;
|
||||
let parent_name = format!("/item/{}/item", ITEM_TYPE_INFO_CAMERA.type_name);
|
||||
let space = calling_client
|
||||
.get_node("Spatial parent", info.parent_path)?
|
||||
.get_aspect::<Spatial>()?;
|
||||
let transform = parse_transform(info.transform, true, true, false);
|
||||
) -> Result<()> {
|
||||
let parent_name = format!("/item/{}/item", ITEM_TYPE_INFO_CAMERA.type_name);
|
||||
let space = parent.get_aspect::<Spatial>()?;
|
||||
let transform = transform.to_mat4(true, true, false);
|
||||
|
||||
let node = Node::create_parent_name(&INTERNAL_CLIENT, &parent_name, info.name, false)
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(&node, None, transform * space.global_transform(), false);
|
||||
CameraItem::add_to(&node, info.proj_matrix.into(), info.px_size);
|
||||
node.get_aspect::<Item>().unwrap().make_alias_named(
|
||||
&calling_client,
|
||||
&parent_name,
|
||||
info.name,
|
||||
)?;
|
||||
Ok(())
|
||||
let node = Node::create_parent_name(&INTERNAL_CLIENT, &parent_name, &name, false)
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(&node, None, transform * space.global_transform(), false);
|
||||
CameraItem::add_to(&node, proj_matrix.into(), px_size);
|
||||
node.get_aspect::<Item>().unwrap().make_alias_named(
|
||||
&calling_client,
|
||||
&parent_name,
|
||||
&name,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[doc = "Register this client to manage camera items and create default 3D UI for them."]
|
||||
fn register_camera_item_ui(_node: Arc<Node>, calling_client: Arc<Client>) -> Result<()> {
|
||||
register_item_ui_flex(calling_client, &ITEM_TYPE_INFO_CAMERA)
|
||||
}
|
||||
|
||||
#[doc = "Create an item acceptor to allow temporary ownership of a given type of item. Creates a node at `/item/camera/acceptor/<name>`."]
|
||||
fn create_camera_item_acceptor(
|
||||
_node: Arc<Node>,
|
||||
calling_client: Arc<Client>,
|
||||
name: String,
|
||||
parent: Arc<Node>,
|
||||
transform: Transform,
|
||||
field: Arc<Node>,
|
||||
) -> Result<()> {
|
||||
create_item_acceptor_flex(
|
||||
calling_client,
|
||||
name,
|
||||
parent,
|
||||
transform,
|
||||
&ITEM_TYPE_INFO_CAMERA,
|
||||
field,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user