Skip to content

Commit

Permalink
refactor(objects): move into objects module
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Jul 18, 2024
1 parent 71ca32a commit a3bcff0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 83 deletions.
52 changes: 6 additions & 46 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use clap::Parser;
use core::client::Client;
use core::task;
use directories::ProjectDirs;
use nodes::spatial::Spatial;
use objects::play_space::PlaySpaceBounds;
use objects::{ServerObjects, SpatialRef};
use objects::ServerObjects;
use once_cell::sync::OnceCell;
use session::{launch_start, save_session};
use stardust_xr::server;
Expand All @@ -26,7 +24,7 @@ use std::time::Duration;
use stereokit_rust::material::Material;
use stereokit_rust::shader::Shader;
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::ui::Ui;
use stereokit_rust::util::{Color128, Time};
Expand Down Expand Up @@ -114,15 +112,7 @@ async fn main() {
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 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();
let dbus_connection = Connection::session().await.unwrap();
dbus_connection
.request_name("org.stardustxr.HMD")
.await
Expand All @@ -140,16 +130,7 @@ async fn main() {
let project_dirs = project_dirs.clone();
let cli_args = cli_args.clone();
let dbus_connection = dbus_connection.clone();
move || {
stereokit_loop(
sk_ready_notifier,
project_dirs,
cli_args,
dbus_connection,
hmd,
play_space,
)
}
move || stereokit_loop(sk_ready_notifier, project_dirs, cli_args, dbus_connection)
});
sk_ready_notifier.notified().await;
let mut startup_children = project_dirs
Expand All @@ -176,9 +157,7 @@ fn stereokit_loop(
sk_ready_notifier: Arc<Notify>,
project_dirs: Option<ProjectDirs>,
args: CliArgs,
dbus_connection: Arc<Connection>,
hmd: Arc<Spatial>,
play_space: Arc<Spatial>,
dbus_connection: Connection,
) {
let sk = SkSettings::default()
.app_name("Stardust XR")
Expand Down Expand Up @@ -236,26 +215,7 @@ fn stereokit_loop(
sk_ready_notifier.notify_waiters();
info!("Stardust ready!");

let mut objects = ServerObjects::new(&sk, hmd, World::has_bounds().then_some(play_space));
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 objects = ServerObjects::new(dbus_connection.clone(), &sk);

let mut last_frame_delta = Duration::ZERO;
let mut sleep_duration = Duration::ZERO;
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ pub struct Field {
pub shape: Mutex<Shape>,
}
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 field = Field {
spatial,
shape: Mutex::new(shape),
};
node.add_aspect(field);
let field = node.add_aspect(field);
<Field as FieldRefAspect>::add_node_members(node);
<Field as FieldAspect>::add_node_members(node);
Ok(())
Ok(field)
}
}
impl Aspect for Field {
Expand Down
116 changes: 82 additions & 34 deletions src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use input::{
eye_pointer::EyePointer, mouse_pointer::MousePointer, sk_controller::SkController,
sk_hand::SkHand,
};
use play_space::PlaySpaceBounds;
use std::sync::Arc;
use stereokit_rust::{
sk::{DisplayMode, MainThreadToken, Sk},
system::{Handed, Input, Key, World},
util::Device,
};
use tokio::task::AbortHandle;
use zbus::{interface, Connection};

pub mod input;
Expand All @@ -31,17 +33,35 @@ enum Inputs {
eye_pointer: Option<EyePointer>,
},
MousePointer(MousePointer),
Controllers((SkController, SkController)),
// Controllers((SkController, SkController)),
Hands((SkHand, SkHand)),
}

pub struct ServerObjects {
hmd: Arc<Spatial>,
play_space: Option<Arc<Spatial>>,
connection: Connection,
hmd: (Arc<Spatial>, AbortHandle),
play_space: Option<(Arc<Spatial>, AbortHandle)>,
inputs: Inputs,
}
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 {
Inputs::XR {
controllers: (
Expand All @@ -62,6 +82,7 @@ impl ServerObjects {
};

ServerObjects {
connection,
hmd,
play_space,
inputs,
Expand All @@ -71,6 +92,7 @@ impl ServerObjects {
pub fn update(&mut self, sk: &Sk, token: &MainThreadToken) {
let hmd_pose = Input::get_head();
self.hmd
.0
.set_local_transform(Mat4::from_scale_rotation_translation(
vec3(1.0, 1.0, 1.0),
hmd_pose.orientation.into(),
Expand All @@ -79,22 +101,24 @@ impl ServerObjects {

if let Some(play_space) = self.play_space.as_ref() {
let pose = World::get_bounds_pose();
play_space.set_local_transform(Mat4::from_rotation_translation(
pose.orientation.into(),
pose.position.into(),
));
play_space
.0
.set_local_transform(Mat4::from_rotation_translation(
pose.orientation.into(),
pose.position.into(),
));
}

if sk.get_active_display_mode() != DisplayMode::MixedReality {
if Input::key(Key::F6).is_just_inactive() {
self.inputs = Inputs::MousePointer(MousePointer::new().unwrap());
}
if Input::key(Key::F7).is_just_inactive() {
self.inputs = Inputs::Controllers((
SkController::new(Handed::Left).unwrap(),
SkController::new(Handed::Right).unwrap(),
));
}
// if Input::key(Key::F7).is_just_inactive() {
// self.inputs = Inputs::Controllers((
// SkController::new(Handed::Left).unwrap(),
// SkController::new(Handed::Right).unwrap(),
// ));
// }
if Input::key(Key::F8).is_just_inactive() {
self.inputs = Inputs::Hands((
SkHand::new(Handed::Left).unwrap(),
Expand All @@ -118,31 +142,47 @@ impl ServerObjects {
}
}
Inputs::MousePointer(mouse_pointer) => mouse_pointer.update(),
Inputs::Controllers((left, right)) => {
left.update(token);
right.update(token);
}
// Inputs::Controllers((left, right)) => {
// left.update(token);
// right.update(token);
// }
Inputs::Hands((left, right)) => {
left.update(sk, token);
right.update(sk, token);
}
}
}
}
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);
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));
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
let uid: u64 = rand::random();
EXPORTED_SPATIALS.lock().insert(uid, node.clone());
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
node

let connection = connection.clone();
let path = path.to_string();
(
spatial,
tokio::task::spawn(async move {
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
})
.abort_handle(),
)
}
}
#[interface(name = "org.stardustxr.SpatialRef")]
Expand All @@ -155,18 +195,26 @@ impl SpatialRef {

pub struct FieldRef(u64);
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));
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();
EXPORTED_FIELDS.lock().insert(uid, node.clone());
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
node

let connection = connection.clone();
let path = path.to_string();
(
field,
tokio::task::spawn(async move {
connection
.object_server()
.at(path, Self(uid))
.await
.unwrap();
})
.abort_handle(),
)
}
}
#[interface(name = "org.stardustxr.FieldRef")]
Expand Down

0 comments on commit a3bcff0

Please sign in to comment.