1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::ops::Deref;
use std::path::Path;
use std::sync::Weak;
use stardust_xr::values::Transform;
use crate::client::Client;
use crate::node::{Node, NodeError, NodeType};
use crate::spatial::Spatial;
use crate::{HandlerWrapper, WeakNodeRef, WeakWrapped};
use super::{HandledItem, Item};
pub struct EnvironmentItem {
pub spatial: Spatial,
}
#[buildstructor::buildstructor]
impl<'a> EnvironmentItem {
#[builder(entry = "builder")]
pub fn create(
spatial_parent: &'a Spatial,
position: Option<mint::Vector3<f32>>,
rotation: Option<mint::Quaternion<f32>>,
file_path: &'a str,
) -> Result<Self, NodeError> {
let path = Path::new(file_path);
if path.is_relative() || !path.exists() {
return Err(NodeError::InvalidPath);
}
let id = nanoid::nanoid!();
Ok(EnvironmentItem {
spatial: Spatial {
node: Node::new(
spatial_parent.node.client.clone(),
"/item",
"create_environment_item",
"/item/environment/item",
true,
&id.clone(),
(
id,
spatial_parent,
Transform {
position,
rotation,
scale: None,
},
file_path,
),
)?,
},
})
}
}
impl NodeType for EnvironmentItem {
fn node(&self) -> &Node {
&self.spatial.node
}
}
impl Item for EnvironmentItem {
type ItemType = EnvironmentItem;
type InitData = String;
const TYPE_NAME: &'static str = "environment";
}
impl<T: Send + Sync + 'static> HandledItem<T> for EnvironmentItem {
fn from_path<F>(
client: Weak<Client>,
path: &str,
init_data: Self::InitData,
mut ui_init_fn: F,
) -> HandlerWrapper<Self, T>
where
F: FnMut(Self::InitData, WeakWrapped<T>, WeakNodeRef<Self>, &Self) -> T
+ Clone
+ Send
+ Sync
+ 'static,
T: Send + Sync + 'static,
{
let item = EnvironmentItem {
spatial: Spatial {
node: Node::from_path(client, path.to_string(), false).unwrap(),
},
};
HandlerWrapper::new(item, |weak_wrapped, weak_node_ref, f| {
ui_init_fn(init_data, weak_wrapped, weak_node_ref, f)
})
}
}
impl Deref for EnvironmentItem {
type Target = Spatial;
fn deref(&self) -> &Self::Target {
&self.spatial
}
}
#[tokio::test]
async fn fusion_environment_item() {
use manifest_dir_macros::file_relative_path;
let (client, _event_loop) = Client::connect_with_async_loop()
.await
.expect("Couldn't connect");
let _environment_item = EnvironmentItem::builder()
.spatial_parent(client.get_root())
.file_path(file_relative_path!("res/fusion/sky.hdr"))
.build()
.unwrap();
}
#[tokio::test]
async fn fusion_environment_ui() -> anyhow::Result<()> {
let (client, event_loop) = Client::connect_with_async_loop().await?;
struct EnvironmentUI {
path: String,
_item: WeakNodeRef<EnvironmentItem>,
}
impl EnvironmentUI {
pub fn new(path: String, _item: WeakNodeRef<EnvironmentItem>) -> Self {
println!("Environment item with path {path} created");
EnvironmentUI { path, _item }
}
}
impl crate::items::ItemHandler<EnvironmentItem> for EnvironmentUI {
fn captured(&mut self, item: &EnvironmentItem, acceptor_uid: &str) {
println!(
"Acceptor {} captured environment item {}",
acceptor_uid,
item.uid()
);
}
fn released(&mut self, item: &EnvironmentItem, acceptor_uid: &str) {
println!(
"Acceptor {} released environment item {}",
acceptor_uid,
item.uid()
);
}
}
impl Drop for EnvironmentUI {
fn drop(&mut self) {
println!("Environment item with path {} destroyed", self.path)
}
}
let _item_ui = crate::items::ItemUI::register(
&client,
|init_data, _weak_wrapped, weak_node_ref, _item: &EnvironmentItem| {
EnvironmentUI::new(init_data, weak_node_ref)
},
)?;
tokio::select! {
_ = tokio::time::sleep(core::time::Duration::from_secs(60)) => Err(anyhow::anyhow!("Timed Out")),
_ = event_loop => Ok(()),
}
}