fix(objects): properly destroy nodes

This commit is contained in:
Nova
2024-06-30 22:45:20 -04:00
parent 1e8d3a3d4c
commit b3fa529f77
6 changed files with 46 additions and 25 deletions

View File

@@ -22,11 +22,11 @@ pub struct InputMethod {
pub data: Mutex<InputDataType>, pub data: Mutex<InputDataType>,
pub datamap: Mutex<Datamap>, pub datamap: Mutex<Datamap>,
pub capture_requests: Registry<InputHandler>,
pub captures: Registry<InputHandler>,
handler_aliases: AliasList, handler_aliases: AliasList,
handler_field_aliases: AliasList, handler_field_aliases: AliasList,
pub(super) handler_order: Mutex<Vec<Weak<InputHandler>>>, pub(super) handler_order: Mutex<Vec<Weak<InputHandler>>>,
pub capture_requests: Registry<InputHandler>,
pub captures: Registry<InputHandler>,
} }
impl InputMethod { impl InputMethod {
pub fn add_to( pub fn add_to(
@@ -39,11 +39,11 @@ impl InputMethod {
data: Mutex::new(data), data: Mutex::new(data),
datamap: Mutex::new(datamap), datamap: Mutex::new(datamap),
capture_requests: Registry::new(),
captures: Registry::new(),
handler_aliases: AliasList::default(), handler_aliases: AliasList::default(),
handler_field_aliases: AliasList::default(), handler_field_aliases: AliasList::default(),
handler_order: Mutex::new(Vec::new()), handler_order: Mutex::new(Vec::new()),
capture_requests: Registry::new(),
captures: Registry::new(),
}; };
<InputMethod as InputMethodRefAspect>::add_node_members(node); <InputMethod as InputMethodRefAspect>::add_node_members(node);
<InputMethod as InputMethodAspect>::add_node_members(node); <InputMethod as InputMethodAspect>::add_node_members(node);

View File

@@ -50,6 +50,13 @@ pub type Method = fn(Arc<Node>, Arc<Client>, Message, MethodResponseSender);
stardust_xr_server_codegen::codegen_node_protocol!(); stardust_xr_server_codegen::codegen_node_protocol!();
pub struct OwnedNode(pub Arc<Node>);
impl Drop for OwnedNode {
fn drop(&mut self) {
self.0.destroy();
}
}
pub struct Node { pub struct Node {
enabled: AtomicBool, enabled: AtomicBool,
id: u64, id: u64,
@@ -95,6 +102,14 @@ impl Node {
.scenegraph .scenegraph
.add_node(self)) .add_node(self))
} }
pub fn add_to_scenegraph_owned(self) -> Result<OwnedNode> {
Ok(OwnedNode(
self.get_client()
.ok_or_else(|| eyre!("Internal: Unable to get client"))?
.scenegraph
.add_node(self),
))
}
pub fn enabled(&self) -> bool { pub fn enabled(&self) -> bool {
self.enabled.load(Ordering::Relaxed) self.enabled.load(Ordering::Relaxed)
} }

View File

@@ -4,7 +4,7 @@ use crate::{
fields::{FieldTrait, Ray}, fields::{FieldTrait, Ray},
input::{InputDataType, InputMethod, Pointer, INPUT_HANDLER_REGISTRY}, input::{InputDataType, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
spatial::Spatial, spatial::Spatial,
Node, Node, OwnedNode,
}, },
}; };
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@@ -28,21 +28,26 @@ pub struct KeyboardEvent {
} }
pub struct EyePointer { pub struct EyePointer {
node: OwnedNode,
spatial: Arc<Spatial>, spatial: Arc<Spatial>,
pointer: Arc<InputMethod>, pointer: Arc<InputMethod>,
} }
impl EyePointer { impl EyePointer {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false); let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
let pointer = InputMethod::add_to( let pointer = InputMethod::add_to(
&node, &node.0,
InputDataType::Pointer(Pointer::default()), InputDataType::Pointer(Pointer::default()),
Datamap::from_typed(EyeDatamap::default())?, Datamap::from_typed(EyeDatamap::default())?,
) )
.unwrap(); .unwrap();
Ok(EyePointer { spatial, pointer }) Ok(EyePointer {
node,
spatial,
pointer,
})
} }
pub fn update(&self) { pub fn update(&self) {
let ray = Input::get_eyes(); let ray = Input::get_eyes();

View File

@@ -7,7 +7,7 @@ use crate::{
fields::{FieldTrait, Ray}, fields::{FieldTrait, Ray},
input::{InputDataType, InputHandler, InputMethod, Pointer, INPUT_HANDLER_REGISTRY}, input::{InputDataType, InputHandler, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
spatial::Spatial, spatial::Spatial,
Node, Node, OwnedNode,
}, },
}; };
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@@ -54,7 +54,7 @@ pub struct KeyboardEvent {
#[allow(unused)] #[allow(unused)]
pub struct MousePointer { pub struct MousePointer {
node: Arc<Node>, node: OwnedNode,
keymap: DefaultKey, keymap: DefaultKey,
spatial: Arc<Spatial>, spatial: Arc<Spatial>,
pointer: Arc<InputMethod>, pointer: Arc<InputMethod>,
@@ -65,10 +65,10 @@ pub struct MousePointer {
} }
impl MousePointer { impl MousePointer {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; let node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
let spatial = Spatial::add_to(&node, None, Mat4::IDENTITY, false); let spatial = Spatial::add_to(&node.0, None, Mat4::IDENTITY, false);
let pointer = InputMethod::add_to( let pointer = InputMethod::add_to(
&node, &node.0,
InputDataType::Pointer(Pointer::default()), InputDataType::Pointer(Pointer::default()),
Datamap::from_typed(MouseEvent::default())?, Datamap::from_typed(MouseEvent::default())?,
)?; )?;
@@ -80,7 +80,7 @@ impl MousePointer {
); );
let keyboard_sender = PulseSender::add_to( let keyboard_sender = PulseSender::add_to(
&node, &node.0,
Datamap::from_typed(KeyboardEvent::default()).unwrap(), Datamap::from_typed(KeyboardEvent::default()).unwrap(),
) )
.unwrap(); .unwrap();
@@ -287,7 +287,7 @@ impl MousePointer {
if !self.keyboard_datamap.keys.is_empty() { if !self.keyboard_datamap.keys.is_empty() {
pulse_receiver_client::data( pulse_receiver_client::data(
&rx.node.upgrade().unwrap(), &rx.node.upgrade().unwrap(),
&self.node, &self.node.0,
&Datamap::from_typed(&self.keyboard_datamap).unwrap(), &Datamap::from_typed(&self.keyboard_datamap).unwrap(),
) )
.unwrap(); .unwrap();

View File

@@ -4,7 +4,7 @@ use crate::{
fields::FieldTrait, fields::FieldTrait,
input::{InputDataType, InputHandler, InputMethod, Tip, INPUT_HANDLER_REGISTRY}, input::{InputDataType, InputHandler, InputMethod, Tip, INPUT_HANDLER_REGISTRY},
spatial::Spatial, spatial::Spatial,
Node, Node, OwnedNode,
}, },
}; };
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@@ -28,7 +28,7 @@ struct ControllerDatamap {
} }
pub struct SkController { pub struct SkController {
_node: Arc<Node>, _node: OwnedNode,
input: Arc<InputMethod>, input: Arc<InputMethod>,
handed: Handed, handed: Handed,
model: Model, model: Model,
@@ -38,8 +38,8 @@ pub struct SkController {
} }
impl SkController { impl SkController {
pub fn new(handed: Handed) -> Result<Self> { pub fn new(handed: Handed) -> Result<Self> {
let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
Spatial::add_to(&_node, None, Mat4::IDENTITY, false); Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false);
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,7 +51,7 @@ 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, &_node.0,
tip, tip,
Datamap::from_typed(ControllerDatamap::default())?, Datamap::from_typed(ControllerDatamap::default())?,
)?; )?;

View File

@@ -1,6 +1,7 @@
use crate::core::client::INTERNAL_CLIENT; use crate::core::client::INTERNAL_CLIENT;
use crate::nodes::fields::{Field, FieldTrait}; use crate::nodes::fields::{Field, FieldTrait};
use crate::nodes::input::{InputDataType, InputHandler, INPUT_HANDLER_REGISTRY}; use crate::nodes::input::{InputDataType, InputHandler, INPUT_HANDLER_REGISTRY};
use crate::nodes::OwnedNode;
use crate::nodes::{ use crate::nodes::{
input::{Hand, InputMethod, Joint}, input::{Hand, InputMethod, Joint},
spatial::Spatial, spatial::Spatial,
@@ -32,7 +33,7 @@ struct HandDatamap {
} }
pub struct SkHand { pub struct SkHand {
_node: Arc<Node>, _node: OwnedNode,
handed: Handed, handed: Handed,
input: Arc<InputMethod>, input: Arc<InputMethod>,
capture: Option<Arc<InputHandler>>, capture: Option<Arc<InputHandler>>,
@@ -40,14 +41,14 @@ pub struct SkHand {
} }
impl SkHand { impl SkHand {
pub fn new(handed: Handed) -> Result<Self> { pub fn new(handed: Handed) -> Result<Self> {
let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph()?; let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
Spatial::add_to(&_node, None, Mat4::IDENTITY, false); Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false);
let hand = InputDataType::Hand(Hand { let hand = InputDataType::Hand(Hand {
right: handed == Handed::Right, right: handed == Handed::Right,
..Default::default() ..Default::default()
}); });
let datamap = Datamap::from_typed(HandDatamap::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); Input::hand_visible(handed, false);
Ok(SkHand { Ok(SkHand {