feat(objects): controllers
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::{
|
|||||||
spatial::Spatial,
|
spatial::Spatial,
|
||||||
Node, OwnedNode,
|
Node, OwnedNode,
|
||||||
},
|
},
|
||||||
|
objects::{ObjectHandle, SpatialRef},
|
||||||
};
|
};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
use glam::{Mat4, Vec2, Vec3};
|
use glam::{Mat4, Vec2, Vec3};
|
||||||
@@ -19,6 +20,7 @@ use stereokit_rust::{
|
|||||||
system::{Handed, Input},
|
system::{Handed, Input},
|
||||||
util::Color128,
|
util::Color128,
|
||||||
};
|
};
|
||||||
|
use zbus::Connection;
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize)]
|
||||||
struct ControllerDatamap {
|
struct ControllerDatamap {
|
||||||
@@ -28,7 +30,7 @@ struct ControllerDatamap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct SkController {
|
pub struct SkController {
|
||||||
_node: OwnedNode,
|
object_handle: ObjectHandle<SpatialRef>,
|
||||||
input: Arc<InputMethod>,
|
input: Arc<InputMethod>,
|
||||||
handed: Handed,
|
handed: Handed,
|
||||||
model: Model,
|
model: Model,
|
||||||
@@ -37,9 +39,15 @@ pub struct SkController {
|
|||||||
datamap: ControllerDatamap,
|
datamap: ControllerDatamap,
|
||||||
}
|
}
|
||||||
impl SkController {
|
impl SkController {
|
||||||
pub fn new(handed: Handed) -> Result<Self> {
|
pub fn new(connection: &Connection, handed: Handed) -> Result<Self> {
|
||||||
let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
|
let (spatial, object_handle) = SpatialRef::create(
|
||||||
Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false);
|
connection,
|
||||||
|
&("/org/stardustxr/Controller/".to_string()
|
||||||
|
+ match handed {
|
||||||
|
Handed::Left => "left",
|
||||||
|
_ => "right",
|
||||||
|
}),
|
||||||
|
);
|
||||||
let model = Model::copy(Model::from_memory(
|
let model = Model::copy(Model::from_memory(
|
||||||
"cursor.glb",
|
"cursor.glb",
|
||||||
include_bytes!("cursor.glb"),
|
include_bytes!("cursor.glb"),
|
||||||
@@ -51,12 +59,12 @@ impl SkController {
|
|||||||
model_node.material(&material);
|
model_node.material(&material);
|
||||||
let tip = InputDataType::Tip(Tip::default());
|
let tip = InputDataType::Tip(Tip::default());
|
||||||
let input = InputMethod::add_to(
|
let input = InputMethod::add_to(
|
||||||
&_node.0,
|
&spatial.node().unwrap(),
|
||||||
tip,
|
tip,
|
||||||
Datamap::from_typed(ControllerDatamap::default())?,
|
Datamap::from_typed(ControllerDatamap::default())?,
|
||||||
)?;
|
)?;
|
||||||
Ok(SkController {
|
Ok(SkController {
|
||||||
_node,
|
object_handle,
|
||||||
input,
|
input,
|
||||||
handed,
|
handed,
|
||||||
model,
|
model,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
nodes::{
|
nodes::{
|
||||||
fields::{Field, Shape, EXPORTED_FIELDS},
|
fields::{Field, Shape, EXPORTED_FIELDS},
|
||||||
spatial::{Spatial, EXPORTED_SPATIALS},
|
spatial::{Spatial, EXPORTED_SPATIALS},
|
||||||
Node,
|
Node, OwnedNode,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use glam::{vec3, Mat4};
|
use glam::{vec3, Mat4};
|
||||||
@@ -14,14 +14,13 @@ use input::{
|
|||||||
sk_hand::SkHand,
|
sk_hand::SkHand,
|
||||||
};
|
};
|
||||||
use play_space::PlaySpaceBounds;
|
use play_space::PlaySpaceBounds;
|
||||||
use std::sync::Arc;
|
use std::{marker::PhantomData, 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, object_server::Interface, zvariant::OwnedObjectPath, Connection};
|
||||||
use zbus::{interface, Connection};
|
|
||||||
|
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod play_space;
|
pub mod play_space;
|
||||||
@@ -44,8 +43,8 @@ enum Inputs {
|
|||||||
|
|
||||||
pub struct ServerObjects {
|
pub struct ServerObjects {
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
hmd: (Arc<Spatial>, AbortHandle),
|
hmd: (Arc<Spatial>, ObjectHandle<SpatialRef>),
|
||||||
play_space: Option<(Arc<Spatial>, AbortHandle)>,
|
play_space: Option<(Arc<Spatial>, ObjectHandle<SpatialRef>)>,
|
||||||
inputs: Inputs,
|
inputs: Inputs,
|
||||||
}
|
}
|
||||||
impl ServerObjects {
|
impl ServerObjects {
|
||||||
@@ -68,9 +67,18 @@ impl ServerObjects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
|
let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
|
||||||
|
tokio::task::spawn({
|
||||||
|
let connection = connection.clone();
|
||||||
|
async move {
|
||||||
|
connection
|
||||||
|
.request_name("org.stardustxr.Controllers")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
Inputs::XR {
|
Inputs::XR {
|
||||||
controller_left: SkController::new(Handed::Left).unwrap(),
|
controller_left: SkController::new(&connection, Handed::Left).unwrap(),
|
||||||
controller_right: SkController::new(Handed::Right).unwrap(),
|
controller_right: SkController::new(&connection, Handed::Right).unwrap(),
|
||||||
hand_left: SkHand::new(Handed::Left).unwrap(),
|
hand_left: SkHand::new(Handed::Left).unwrap(),
|
||||||
hand_right: SkHand::new(Handed::Right).unwrap(),
|
hand_right: SkHand::new(Handed::Right).unwrap(),
|
||||||
eye_pointer: Device::has_eye_gaze()
|
eye_pointer: Device::has_eye_gaze()
|
||||||
@@ -156,35 +164,44 @@ impl ServerObjects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Drop for ServerObjects {
|
|
||||||
|
pub struct ObjectHandle<I: Interface>(Connection, OwnedObjectPath, PhantomData<I>);
|
||||||
|
impl<I: Interface> Drop for ObjectHandle<I> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.hmd.1.abort();
|
let connection = self.0.clone();
|
||||||
if let Some((_, play_space)) = self.play_space.take() {
|
let object_path = self.1.clone();
|
||||||
play_space.abort();
|
tokio::task::spawn(async move {
|
||||||
}
|
connection.object_server().remove::<I, _>(object_path);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SpatialRef(u64);
|
pub struct SpatialRef(u64, OwnedNode);
|
||||||
impl SpatialRef {
|
impl SpatialRef {
|
||||||
pub fn create(connection: &Connection, path: &str) -> (Arc<Spatial>, AbortHandle) {
|
pub fn create(connection: &Connection, path: &str) -> (Arc<Spatial>, ObjectHandle<SpatialRef>) {
|
||||||
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false)));
|
||||||
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
let spatial = Spatial::add_to(&node.0, 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.0.clone());
|
||||||
|
|
||||||
|
tokio::task::spawn({
|
||||||
let connection = connection.clone();
|
let connection = connection.clone();
|
||||||
let path = path.to_string();
|
let path = path.to_string();
|
||||||
(
|
async move {
|
||||||
spatial,
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
connection
|
connection
|
||||||
.object_server()
|
.object_server()
|
||||||
.at(path, Self(uid))
|
.at(path, Self(uid, node))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
})
|
}
|
||||||
.abort_handle(),
|
});
|
||||||
|
(
|
||||||
|
spatial,
|
||||||
|
ObjectHandle(
|
||||||
|
connection.clone(),
|
||||||
|
OwnedObjectPath::try_from(path.to_string()).unwrap(),
|
||||||
|
PhantomData,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,27 +213,37 @@ impl SpatialRef {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FieldRef(u64);
|
pub struct FieldRef(u64, OwnedNode);
|
||||||
impl FieldRef {
|
impl FieldRef {
|
||||||
pub fn create(connection: &Connection, path: &str, shape: Shape) -> (Arc<Field>, AbortHandle) {
|
pub fn create(
|
||||||
let node = Arc::new(Node::generate(&INTERNAL_CLIENT, false));
|
connection: &Connection,
|
||||||
Spatial::add_to(&node, None, Mat4::IDENTITY, false);
|
path: &str,
|
||||||
let field = Field::add_to(&node, shape).unwrap();
|
shape: Shape,
|
||||||
|
) -> (Arc<Field>, ObjectHandle<FieldRef>) {
|
||||||
|
let node = OwnedNode(Arc::new(Node::generate(&INTERNAL_CLIENT, false)));
|
||||||
|
Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
|
||||||
|
let field = Field::add_to(&node.0, 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.0.clone());
|
||||||
|
|
||||||
|
tokio::task::spawn({
|
||||||
let connection = connection.clone();
|
let connection = connection.clone();
|
||||||
let path = path.to_string();
|
let path = path.to_string();
|
||||||
(
|
async move {
|
||||||
field,
|
|
||||||
tokio::task::spawn(async move {
|
|
||||||
connection
|
connection
|
||||||
.object_server()
|
.object_server()
|
||||||
.at(path, Self(uid))
|
.at(path, Self(uid, node))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
})
|
}
|
||||||
.abort_handle(),
|
});
|
||||||
|
(
|
||||||
|
field,
|
||||||
|
ObjectHandle(
|
||||||
|
connection.clone(),
|
||||||
|
OwnedObjectPath::try_from(path.to_string()).unwrap(),
|
||||||
|
PhantomData,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user