diff --git a/src/nodes/input/method.rs b/src/nodes/input/method.rs index 3ac536d..d5a738f 100644 --- a/src/nodes/input/method.rs +++ b/src/nodes/input/method.rs @@ -22,11 +22,11 @@ pub struct InputMethod { pub data: Mutex, pub datamap: Mutex, - pub capture_requests: Registry, - pub captures: Registry, handler_aliases: AliasList, handler_field_aliases: AliasList, pub(super) handler_order: Mutex>>, + pub capture_requests: Registry, + pub captures: Registry, } impl InputMethod { pub fn add_to( @@ -39,11 +39,11 @@ impl InputMethod { data: Mutex::new(data), datamap: Mutex::new(datamap), - capture_requests: Registry::new(), - captures: Registry::new(), handler_aliases: AliasList::default(), handler_field_aliases: AliasList::default(), handler_order: Mutex::new(Vec::new()), + capture_requests: Registry::new(), + captures: Registry::new(), }; ::add_node_members(node); ::add_node_members(node); diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 01f16d7..caeeb22 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -50,6 +50,13 @@ pub type Method = fn(Arc, Arc, Message, MethodResponseSender); stardust_xr_server_codegen::codegen_node_protocol!(); +pub struct OwnedNode(pub Arc); +impl Drop for OwnedNode { + fn drop(&mut self) { + self.0.destroy(); + } +} + pub struct Node { enabled: AtomicBool, id: u64, @@ -95,6 +102,14 @@ impl Node { .scenegraph .add_node(self)) } + pub fn add_to_scenegraph_owned(self) -> Result { + Ok(OwnedNode( + self.get_client() + .ok_or_else(|| eyre!("Internal: Unable to get client"))? + .scenegraph + .add_node(self), + )) + } pub fn enabled(&self) -> bool { self.enabled.load(Ordering::Relaxed) } diff --git a/src/objects/input/eye_pointer.rs b/src/objects/input/eye_pointer.rs index ce49151..bae4316 100644 --- a/src/objects/input/eye_pointer.rs +++ b/src/objects/input/eye_pointer.rs @@ -4,7 +4,7 @@ use crate::{ fields::{FieldTrait, Ray}, input::{InputDataType, InputMethod, Pointer, INPUT_HANDLER_REGISTRY}, spatial::Spatial, - Node, + Node, OwnedNode, }, }; use color_eyre::eyre::Result; @@ -28,21 +28,26 @@ pub struct KeyboardEvent { } pub struct EyePointer { + node: OwnedNode, spatial: Arc, pointer: Arc, } impl EyePointer { pub fn new() -> Result { - let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; - let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false); + let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?; + let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); let pointer = InputMethod::add_to( - &node, + &node.0, InputDataType::Pointer(Pointer::default()), Datamap::from_typed(EyeDatamap::default())?, ) .unwrap(); - Ok(EyePointer { spatial, pointer }) + Ok(EyePointer { + node, + spatial, + pointer, + }) } pub fn update(&self) { let ray = Input::get_eyes(); diff --git a/src/objects/input/mouse_pointer.rs b/src/objects/input/mouse_pointer.rs index 82c627a..b9f470f 100644 --- a/src/objects/input/mouse_pointer.rs +++ b/src/objects/input/mouse_pointer.rs @@ -7,7 +7,7 @@ use crate::{ fields::{FieldTrait, Ray}, input::{InputDataType, InputHandler, InputMethod, Pointer, INPUT_HANDLER_REGISTRY}, spatial::Spatial, - Node, + Node, OwnedNode, }, }; use color_eyre::eyre::Result; @@ -54,7 +54,7 @@ pub struct KeyboardEvent { #[allow(unused)] pub struct MousePointer { - node: Arc, + node: OwnedNode, keymap: DefaultKey, spatial: Arc, pointer: Arc, @@ -65,10 +65,10 @@ pub struct MousePointer { } impl MousePointer { pub fn new() -> Result { - let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; - let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false); + let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?; + let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false); let pointer = InputMethod::add_to( - &node, + &node.0, InputDataType::Pointer(Pointer::default()), Datamap::from_typed(MouseEvent::default())?, )?; @@ -80,7 +80,7 @@ impl MousePointer { ); let keyboard_sender = PulseSender::add_to( - &node, + &node.0, Datamap::from_typed(KeyboardEvent::default()).unwrap(), ) .unwrap(); @@ -287,7 +287,7 @@ impl MousePointer { if !self.keyboard_datamap.keys.is_empty() { pulse_receiver_client::data( &rx.node.upgrade().unwrap(), - &self.node, + &self.node.0, &Datamap::from_typed(&self.keyboard_datamap).unwrap(), ) .unwrap(); diff --git a/src/objects/input/sk_controller.rs b/src/objects/input/sk_controller.rs index c5d1d9d..8912d48 100644 --- a/src/objects/input/sk_controller.rs +++ b/src/objects/input/sk_controller.rs @@ -4,7 +4,7 @@ use crate::{ fields::FieldTrait, input::{InputDataType, InputHandler, InputMethod, Tip, INPUT_HANDLER_REGISTRY}, spatial::Spatial, - Node, + Node, OwnedNode, }, }; use color_eyre::eyre::Result; @@ -28,7 +28,7 @@ struct ControllerDatamap { } pub struct SkController { - _node: Arc, + _node: OwnedNode, input: Arc, handed: Handed, model: Model, @@ -38,8 +38,8 @@ pub struct SkController { } impl SkController { pub fn new(handed: Handed) -> Result { - let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; - Spatial::add_to(&_node, None, Mat4::IDENTITY, false); + let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?; + Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false); let model = Model::copy(Model::from_memory( "cursor.glb", include_bytes!("cursor.glb"), @@ -51,7 +51,7 @@ impl SkController { model_node.material(&material); let tip = InputDataType::Tip(Tip::default()); let input = InputMethod::add_to( - &_node, + &_node.0, tip, Datamap::from_typed(ControllerDatamap::default())?, )?; diff --git a/src/objects/input/sk_hand.rs b/src/objects/input/sk_hand.rs index 0ddb06c..efebb5a 100644 --- a/src/objects/input/sk_hand.rs +++ b/src/objects/input/sk_hand.rs @@ -1,6 +1,7 @@ use crate::core::client::INTERNAL_CLIENT; use crate::nodes::fields::{Field, FieldTrait}; use crate::nodes::input::{InputDataType, InputHandler, INPUT_HANDLER_REGISTRY}; +use crate::nodes::OwnedNode; use crate::nodes::{ input::{Hand, InputMethod, Joint}, spatial::Spatial, @@ -32,7 +33,7 @@ struct HandDatamap { } pub struct SkHand { - _node: Arc, + _node: OwnedNode, handed: Handed, input: Arc, capture: Option>, @@ -40,14 +41,14 @@ pub struct SkHand { } impl SkHand { pub fn new(handed: Handed) -> Result { - let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; - Spatial::add_to(&_node, None, Mat4::IDENTITY, false); + let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?; + Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false); let hand = InputDataType::Hand(Hand { right: handed == Handed::Right, ..Default::default() }); let datamap = Datamap::from_typed(HandDatamap::default())?; - let input = InputMethod::add_to(&_node, hand, datamap)?; + let input = InputMethod::add_to(&_node.0, hand, datamap)?; Input::hand_visible(handed, false); Ok(SkHand {