refactor(objects): move into objects module
This commit is contained in:
52
src/main.rs
52
src/main.rs
@@ -14,9 +14,7 @@ use clap::Parser;
|
|||||||
use core::client::Client;
|
use core::client::Client;
|
||||||
use core::task;
|
use core::task;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use nodes::spatial::Spatial;
|
use objects::ServerObjects;
|
||||||
use objects::play_space::PlaySpaceBounds;
|
|
||||||
use objects::{ServerObjects, SpatialRef};
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use session::{launch_start, save_session};
|
use session::{launch_start, save_session};
|
||||||
use stardust_xr::server;
|
use stardust_xr::server;
|
||||||
@@ -26,7 +24,7 @@ use std::time::Duration;
|
|||||||
use stereokit_rust::material::Material;
|
use stereokit_rust::material::Material;
|
||||||
use stereokit_rust::shader::Shader;
|
use stereokit_rust::shader::Shader;
|
||||||
use stereokit_rust::sk::{sk_quit, AppMode, DepthMode, QuitReason, SkSettings};
|
use stereokit_rust::sk::{sk_quit, AppMode, DepthMode, QuitReason, SkSettings};
|
||||||
use stereokit_rust::system::{LogLevel, Renderer, World};
|
use stereokit_rust::system::{LogLevel, Renderer};
|
||||||
use stereokit_rust::tex::{SHCubemap, Tex, TexFormat, TexType};
|
use stereokit_rust::tex::{SHCubemap, Tex, TexFormat, TexType};
|
||||||
use stereokit_rust::ui::Ui;
|
use stereokit_rust::ui::Ui;
|
||||||
use stereokit_rust::util::{Color128, Time};
|
use stereokit_rust::util::{Color128, Time};
|
||||||
@@ -114,15 +112,7 @@ async fn main() {
|
|||||||
error!("Unable to get Stardust project directories, default skybox and startup script will not work.");
|
error!("Unable to get Stardust project directories, default skybox and startup script will not work.");
|
||||||
}
|
}
|
||||||
|
|
||||||
let dbus_connection = Arc::new(Connection::session().await.unwrap());
|
let dbus_connection = Connection::session().await.unwrap();
|
||||||
let hmd = SpatialRef::create(&dbus_connection, "/org/stardustxr/HMD")
|
|
||||||
.await
|
|
||||||
.get_aspect::<Spatial>()
|
|
||||||
.unwrap();
|
|
||||||
let play_space = SpatialRef::create(&dbus_connection, "/org/stardustxr/PlaySpace")
|
|
||||||
.await
|
|
||||||
.get_aspect::<Spatial>()
|
|
||||||
.unwrap();
|
|
||||||
dbus_connection
|
dbus_connection
|
||||||
.request_name("org.stardustxr.HMD")
|
.request_name("org.stardustxr.HMD")
|
||||||
.await
|
.await
|
||||||
@@ -140,16 +130,7 @@ async fn main() {
|
|||||||
let project_dirs = project_dirs.clone();
|
let project_dirs = project_dirs.clone();
|
||||||
let cli_args = cli_args.clone();
|
let cli_args = cli_args.clone();
|
||||||
let dbus_connection = dbus_connection.clone();
|
let dbus_connection = dbus_connection.clone();
|
||||||
move || {
|
move || stereokit_loop(sk_ready_notifier, project_dirs, cli_args, dbus_connection)
|
||||||
stereokit_loop(
|
|
||||||
sk_ready_notifier,
|
|
||||||
project_dirs,
|
|
||||||
cli_args,
|
|
||||||
dbus_connection,
|
|
||||||
hmd,
|
|
||||||
play_space,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
sk_ready_notifier.notified().await;
|
sk_ready_notifier.notified().await;
|
||||||
let mut startup_children = project_dirs
|
let mut startup_children = project_dirs
|
||||||
@@ -176,9 +157,7 @@ fn stereokit_loop(
|
|||||||
sk_ready_notifier: Arc<Notify>,
|
sk_ready_notifier: Arc<Notify>,
|
||||||
project_dirs: Option<ProjectDirs>,
|
project_dirs: Option<ProjectDirs>,
|
||||||
args: CliArgs,
|
args: CliArgs,
|
||||||
dbus_connection: Arc<Connection>,
|
dbus_connection: Connection,
|
||||||
hmd: Arc<Spatial>,
|
|
||||||
play_space: Arc<Spatial>,
|
|
||||||
) {
|
) {
|
||||||
let sk = SkSettings::default()
|
let sk = SkSettings::default()
|
||||||
.app_name("Stardust XR")
|
.app_name("Stardust XR")
|
||||||
@@ -236,26 +215,7 @@ fn stereokit_loop(
|
|||||||
sk_ready_notifier.notify_waiters();
|
sk_ready_notifier.notify_waiters();
|
||||||
info!("Stardust ready!");
|
info!("Stardust ready!");
|
||||||
|
|
||||||
let mut objects = ServerObjects::new(&sk, hmd, World::has_bounds().then_some(play_space));
|
let mut objects = ServerObjects::new(dbus_connection.clone(), &sk);
|
||||||
if World::has_bounds() && World::get_bounds_size().x != 0.0 && World::get_bounds_size().y != 0.0
|
|
||||||
{
|
|
||||||
let dbus_connection = dbus_connection.clone();
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
PlaySpaceBounds::create(&dbus_connection).await;
|
|
||||||
dbus_connection
|
|
||||||
.request_name("org.stardustxr.PlaySpace")
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
dbus_connection
|
|
||||||
.object_server()
|
|
||||||
.remove::<SpatialRef, _>("/org/stardustxr/PlaySpace")
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut last_frame_delta = Duration::ZERO;
|
let mut last_frame_delta = Duration::ZERO;
|
||||||
let mut sleep_duration = Duration::ZERO;
|
let mut sleep_duration = Duration::ZERO;
|
||||||
|
|||||||
@@ -138,16 +138,16 @@ pub struct Field {
|
|||||||
pub shape: Mutex<Shape>,
|
pub shape: Mutex<Shape>,
|
||||||
}
|
}
|
||||||
impl Field {
|
impl Field {
|
||||||
pub fn add_to(node: &Arc<Node>, shape: Shape) -> Result<()> {
|
pub fn add_to(node: &Arc<Node>, shape: Shape) -> Result<Arc<Field>> {
|
||||||
let spatial = node.get_aspect::<Spatial>()?;
|
let spatial = node.get_aspect::<Spatial>()?;
|
||||||
let field = Field {
|
let field = Field {
|
||||||
spatial,
|
spatial,
|
||||||
shape: Mutex::new(shape),
|
shape: Mutex::new(shape),
|
||||||
};
|
};
|
||||||
node.add_aspect(field);
|
let field = node.add_aspect(field);
|
||||||
<Field as FieldRefAspect>::add_node_members(node);
|
<Field as FieldRefAspect>::add_node_members(node);
|
||||||
<Field as FieldAspect>::add_node_members(node);
|
<Field as FieldAspect>::add_node_members(node);
|
||||||
Ok(())
|
Ok(field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Aspect for Field {
|
impl Aspect for Field {
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ use input::{
|
|||||||
eye_pointer::EyePointer, mouse_pointer::MousePointer, sk_controller::SkController,
|
eye_pointer::EyePointer, mouse_pointer::MousePointer, sk_controller::SkController,
|
||||||
sk_hand::SkHand,
|
sk_hand::SkHand,
|
||||||
};
|
};
|
||||||
|
use play_space::PlaySpaceBounds;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use stereokit_rust::{
|
use stereokit_rust::{
|
||||||
sk::{DisplayMode, MainThreadToken, Sk},
|
sk::{DisplayMode, MainThreadToken, Sk},
|
||||||
system::{Handed, Input, Key, World},
|
system::{Handed, Input, Key, World},
|
||||||
util::Device,
|
util::Device,
|
||||||
};
|
};
|
||||||
|
use tokio::task::AbortHandle;
|
||||||
use zbus::{interface, Connection};
|
use zbus::{interface, Connection};
|
||||||
|
|
||||||
pub mod input;
|
pub mod input;
|
||||||
@@ -31,17 +33,35 @@ enum Inputs {
|
|||||||
eye_pointer: Option<EyePointer>,
|
eye_pointer: Option<EyePointer>,
|
||||||
},
|
},
|
||||||
MousePointer(MousePointer),
|
MousePointer(MousePointer),
|
||||||
Controllers((SkController, SkController)),
|
// Controllers((SkController, SkController)),
|
||||||
Hands((SkHand, SkHand)),
|
Hands((SkHand, SkHand)),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ServerObjects {
|
pub struct ServerObjects {
|
||||||
hmd: Arc<Spatial>,
|
connection: Connection,
|
||||||
play_space: Option<Arc<Spatial>>,
|
hmd: (Arc<Spatial>, AbortHandle),
|
||||||
|
play_space: Option<(Arc<Spatial>, AbortHandle)>,
|
||||||
inputs: Inputs,
|
inputs: Inputs,
|
||||||
}
|
}
|
||||||
impl ServerObjects {
|
impl ServerObjects {
|
||||||
pub fn new(sk: &Sk, hmd: Arc<Spatial>, play_space: Option<Arc<Spatial>>) -> ServerObjects {
|
pub fn new(connection: Connection, sk: &Sk) -> ServerObjects {
|
||||||
|
let hmd = SpatialRef::create(&connection, "/org/stardustxr/HMD");
|
||||||
|
|
||||||
|
let play_space = (World::has_bounds()
|
||||||
|
&& World::get_bounds_size().x != 0.0
|
||||||
|
&& World::get_bounds_size().y != 0.0)
|
||||||
|
.then(|| SpatialRef::create(&connection, "/org/stardustxr/PlaySpace"));
|
||||||
|
if play_space.is_some() {
|
||||||
|
let dbus_connection = connection.clone();
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
PlaySpaceBounds::create(&dbus_connection).await;
|
||||||
|
dbus_connection
|
||||||
|
.request_name("org.stardustxr.PlaySpace")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
|
let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
|
||||||
Inputs::XR {
|
Inputs::XR {
|
||||||
controllers: (
|
controllers: (
|
||||||
@@ -62,6 +82,7 @@ impl ServerObjects {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ServerObjects {
|
ServerObjects {
|
||||||
|
connection,
|
||||||
hmd,
|
hmd,
|
||||||
play_space,
|
play_space,
|
||||||
inputs,
|
inputs,
|
||||||
@@ -71,6 +92,7 @@ impl ServerObjects {
|
|||||||
pub fn update(&mut self, sk: &Sk, token: &MainThreadToken) {
|
pub fn update(&mut self, sk: &Sk, token: &MainThreadToken) {
|
||||||
let hmd_pose = Input::get_head();
|
let hmd_pose = Input::get_head();
|
||||||
self.hmd
|
self.hmd
|
||||||
|
.0
|
||||||
.set_local_transform(Mat4::from_scale_rotation_translation(
|
.set_local_transform(Mat4::from_scale_rotation_translation(
|
||||||
vec3(1.0, 1.0, 1.0),
|
vec3(1.0, 1.0, 1.0),
|
||||||
hmd_pose.orientation.into(),
|
hmd_pose.orientation.into(),
|
||||||
@@ -79,22 +101,24 @@ impl ServerObjects {
|
|||||||
|
|
||||||
if let Some(play_space) = self.play_space.as_ref() {
|
if let Some(play_space) = self.play_space.as_ref() {
|
||||||
let pose = World::get_bounds_pose();
|
let pose = World::get_bounds_pose();
|
||||||
play_space.set_local_transform(Mat4::from_rotation_translation(
|
play_space
|
||||||
pose.orientation.into(),
|
.0
|
||||||
pose.position.into(),
|
.set_local_transform(Mat4::from_rotation_translation(
|
||||||
));
|
pose.orientation.into(),
|
||||||
|
pose.position.into(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if sk.get_active_display_mode() != DisplayMode::MixedReality {
|
if sk.get_active_display_mode() != DisplayMode::MixedReality {
|
||||||
if Input::key(Key::F6).is_just_inactive() {
|
if Input::key(Key::F6).is_just_inactive() {
|
||||||
self.inputs = Inputs::MousePointer(MousePointer::new().unwrap());
|
self.inputs = Inputs::MousePointer(MousePointer::new().unwrap());
|
||||||
}
|
}
|
||||||
if Input::key(Key::F7).is_just_inactive() {
|
// if Input::key(Key::F7).is_just_inactive() {
|
||||||
self.inputs = Inputs::Controllers((
|
// self.inputs = Inputs::Controllers((
|
||||||
SkController::new(Handed::Left).unwrap(),
|
// SkController::new(Handed::Left).unwrap(),
|
||||||
SkController::new(Handed::Right).unwrap(),
|
// SkController::new(Handed::Right).unwrap(),
|
||||||
));
|
// ));
|
||||||
}
|
// }
|
||||||
if Input::key(Key::F8).is_just_inactive() {
|
if Input::key(Key::F8).is_just_inactive() {
|
||||||
self.inputs = Inputs::Hands((
|
self.inputs = Inputs::Hands((
|
||||||
SkHand::new(Handed::Left).unwrap(),
|
SkHand::new(Handed::Left).unwrap(),
|
||||||
@@ -118,10 +142,10 @@ impl ServerObjects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Inputs::MousePointer(mouse_pointer) => mouse_pointer.update(),
|
Inputs::MousePointer(mouse_pointer) => mouse_pointer.update(),
|
||||||
Inputs::Controllers((left, right)) => {
|
// Inputs::Controllers((left, right)) => {
|
||||||
left.update(token);
|
// left.update(token);
|
||||||
right.update(token);
|
// right.update(token);
|
||||||
}
|
// }
|
||||||
Inputs::Hands((left, right)) => {
|
Inputs::Hands((left, right)) => {
|
||||||
left.update(sk, token);
|
left.update(sk, token);
|
||||||
right.update(sk, token);
|
right.update(sk, token);
|
||||||
@@ -129,20 +153,36 @@ impl ServerObjects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl Drop for ServerObjects {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.hmd.1.abort();
|
||||||
|
if let Some((_, play_space)) = self.play_space.take() {
|
||||||
|
play_space.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct SpatialRef(u64);
|
pub struct SpatialRef(u64);
|
||||||
impl SpatialRef {
|
impl SpatialRef {
|
||||||
pub async fn create(connection: &Connection, path: &str) -> Arc<Node> {
|
pub fn create(connection: &Connection, path: &str) -> (Arc<Spatial>, AbortHandle) {
|
||||||
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
||||||
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
||||||
let uid: u64 = rand::random();
|
let uid: u64 = rand::random();
|
||||||
EXPORTED_SPATIALS.lock().insert(uid, node.clone());
|
EXPORTED_SPATIALS.lock().insert(uid, node.clone());
|
||||||
connection
|
|
||||||
.object_server()
|
let connection = connection.clone();
|
||||||
.at(path, Self(uid))
|
let path = path.to_string();
|
||||||
.await
|
(
|
||||||
.unwrap();
|
spatial,
|
||||||
node
|
tokio::task::spawn(async move {
|
||||||
|
connection
|
||||||
|
.object_server()
|
||||||
|
.at(path, Self(uid))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
})
|
||||||
|
.abort_handle(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[interface(name = "org.stardustxr.SpatialRef")]
|
#[interface(name = "org.stardustxr.SpatialRef")]
|
||||||
@@ -155,18 +195,26 @@ impl SpatialRef {
|
|||||||
|
|
||||||
pub struct FieldRef(u64);
|
pub struct FieldRef(u64);
|
||||||
impl FieldRef {
|
impl FieldRef {
|
||||||
pub async fn create(connection: &Connection, path: &str, shape: Shape) -> Arc<Node> {
|
pub fn create(connection: &Connection, path: &str, shape: Shape) -> (Arc<Field>, AbortHandle) {
|
||||||
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
||||||
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
||||||
Field::add_to(&node, shape).unwrap();
|
let field = Field::add_to(&node, shape).unwrap();
|
||||||
let uid: u64 = rand::random();
|
let uid: u64 = rand::random();
|
||||||
EXPORTED_FIELDS.lock().insert(uid, node.clone());
|
EXPORTED_FIELDS.lock().insert(uid, node.clone());
|
||||||
connection
|
|
||||||
.object_server()
|
let connection = connection.clone();
|
||||||
.at(path, Self(uid))
|
let path = path.to_string();
|
||||||
.await
|
(
|
||||||
.unwrap();
|
field,
|
||||||
node
|
tokio::task::spawn(async move {
|
||||||
|
connection
|
||||||
|
.object_server()
|
||||||
|
.at(path, Self(uid))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
})
|
||||||
|
.abort_handle(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[interface(name = "org.stardustxr.FieldRef")]
|
#[interface(name = "org.stardustxr.FieldRef")]
|
||||||
|
|||||||
Reference in New Issue
Block a user