Skip to content

Commit

Permalink
refactor(items): codegen prototocol
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed May 30, 2024
1 parent eda50b7 commit 5f9d9d4
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 582 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ optional = true

[dependencies.stardust-xr]
git = "https://github.com/StardustXR/core.git"
branch = "dev"

[dependencies.stardust-xr-server-codegen]
path = "codegen"

# [patch.crates-io.stereokit]
# path = "../stereokit-rs"
# [patch.crates-io.stereokit-sys]
# path = "../stereokit-sys"
1 change: 1 addition & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ split-iter = "0.1.0"

[dependencies.stardust-xr-schemas]
git = "https://github.com/StardustXR/core.git"
branch = "dev"
36 changes: 29 additions & 7 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ pub fn codegen_drawable_protocol(_input: proc_macro::TokenStream) -> proc_macro:
pub fn codegen_input_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
codegen_protocol(INPUT_PROTOCOL)
}
#[proc_macro]
pub fn codegen_item_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
codegen_protocol(ITEM_PROTOCOL)
}
#[proc_macro]
pub fn codegen_item_camera_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
codegen_protocol(ITEM_CAMERA_PROTOCOL)
}
#[proc_macro]
pub fn codegen_item_panel_protocol(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
codegen_protocol(ITEM_PANEL_PROTOCOL)
}

fn codegen_protocol(protocol: &'static str) -> proc_macro::TokenStream {
let protocol = Protocol::parse(protocol).unwrap();
Expand Down Expand Up @@ -457,13 +469,15 @@ fn generate_argument_decl(argument: &Argument, owned_values: bool) -> TokenStrea
}
fn argument_type_option_name(argument_type: &ArgumentType) -> String {
match argument_type {
ArgumentType::Empty => "Empty".to_string(),
ArgumentType::Bool => "Bool".to_string(),
ArgumentType::Int => "Int".to_string(),
ArgumentType::UInt => "UInt".to_string(),
ArgumentType::Float => "Float".to_string(),
ArgumentType::Vec2 => "Vec2".to_string(),
ArgumentType::Vec3 => "Vec3".to_string(),
ArgumentType::Vec2(_) => "Vec2".to_string(),
ArgumentType::Vec3(_) => "Vec3".to_string(),
ArgumentType::Quat => "Quat".to_string(),
ArgumentType::Mat4 => "Mat4".to_string(),
ArgumentType::Color => "Color".to_string(),
ArgumentType::String => "String".to_string(),
ArgumentType::Bytes => "Bytes".to_string(),
Expand All @@ -483,13 +497,21 @@ fn generate_argument_type(
owned: bool,
) -> TokenStream {
let _type = match argument_type {
ArgumentType::Empty => quote!(()),
ArgumentType::Bool => quote!(bool),
ArgumentType::Int => quote!(i32),
ArgumentType::UInt => quote!(u32),
ArgumentType::Float => quote!(f32),
ArgumentType::Vec2 => quote!(mint::Vector2<f32>),
ArgumentType::Vec3 => quote!(mint::Vector3<f32>),
ArgumentType::Quat => quote!(mint::Quaternion<f32>),
ArgumentType::Vec2(t) => {
let t = generate_argument_type(&t, false, true);
quote!(stardust_xr::values::Vector2<#t>)
}
ArgumentType::Vec3(t) => {
let t = generate_argument_type(&t, false, true);
quote!(stardust_xr::values::Vector3<#t>)
}
ArgumentType::Quat => quote!(stardust_xr::values::Quaternion),
ArgumentType::Mat4 => quote!(stardust_xr::values::Mat4),
ArgumentType::Color => quote!(stardust_xr::values::Color),
ArgumentType::Bytes => {
if !owned {
Expand Down Expand Up @@ -517,9 +539,9 @@ fn generate_argument_type(
let t = generate_argument_type(&t, false, true);

if !owned {
quote!(&rustc_hash::FxHashMap<String, #t>)
quote!(&stardust_xr::values::Map<String, #t>)
} else {
quote!(rustc_hash::FxHashMap<String, #t>)
quote!(stardust_xr::values::Map<String, #t>)
}
}
ArgumentType::Datamap => {
Expand Down
3 changes: 2 additions & 1 deletion src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ impl Client {
drawable::create_interface(&client)?;
audio::create_interface(&client)?;
data::create_interface(&client)?;
items::create_interface(&client)?;
input::create_interface(&client)?;
items::camera::create_interface(&client)?;
items::panel::create_interface(&client)?;

client
.root
Expand Down
4 changes: 0 additions & 4 deletions src/nodes/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,3 @@ impl FieldInterfaceAspect for FieldInterface {
Ok(())
}
}

pub fn find_field(client: &Client, path: &str) -> Result<Arc<Field>> {
client.get_node("Field", path)?.get_aspect::<Field>()
}
92 changes: 60 additions & 32 deletions src/nodes/items/camera.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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",
Expand All @@ -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);
}
};
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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>,
) -> 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, &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(())
}
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);

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(())
#[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,
)
}
}
97 changes: 0 additions & 97 deletions src/nodes/items/environment.rs

This file was deleted.

Loading

0 comments on commit 5f9d9d4

Please sign in to comment.