Skip to content

Commit

Permalink
fix(objects): properly destroy nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Jul 1, 2024
1 parent 1e8d3a3 commit b3fa529
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/nodes/input/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub struct InputMethod {
pub data: Mutex<InputDataType>,
pub datamap: Mutex<Datamap>,

pub capture_requests: Registry<InputHandler>,
pub captures: Registry<InputHandler>,
handler_aliases: AliasList,
handler_field_aliases: AliasList,
pub(super) handler_order: Mutex<Vec<Weak<InputHandler>>>,
pub capture_requests: Registry<InputHandler>,
pub captures: Registry<InputHandler>,
}
impl InputMethod {
pub fn add_to(
Expand All @@ -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(),
};
<InputMethod as InputMethodRefAspect>::add_node_members(node);
<InputMethod as InputMethodAspect>::add_node_members(node);
Expand Down
15 changes: 15 additions & 0 deletions src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ pub type Method = fn(Arc<Node>, Arc<Client>, Message, MethodResponseSender);

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 {
enabled: AtomicBool,
id: u64,
Expand Down Expand Up @@ -95,6 +102,14 @@ impl Node {
.scenegraph
.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 {
self.enabled.load(Ordering::Relaxed)
}
Expand Down
15 changes: 10 additions & 5 deletions src/objects/input/eye_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,21 +28,26 @@ pub struct KeyboardEvent {
}

pub struct EyePointer {
node: OwnedNode,
spatial: Arc<Spatial>,
pointer: Arc<InputMethod>,
}
impl EyePointer {
pub fn new() -> Result<Self> {
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();
Expand Down
14 changes: 7 additions & 7 deletions src/objects/input/mouse_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct KeyboardEvent {

#[allow(unused)]
pub struct MousePointer {
node: Arc<Node>,
node: OwnedNode,
keymap: DefaultKey,
spatial: Arc<Spatial>,
pointer: Arc<InputMethod>,
Expand All @@ -65,10 +65,10 @@ pub struct MousePointer {
}
impl MousePointer {
pub fn new() -> Result<Self> {
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())?,
)?;
Expand All @@ -80,7 +80,7 @@ impl MousePointer {
);

let keyboard_sender = PulseSender::add_to(
&node,
&node.0,
Datamap::from_typed(KeyboardEvent::default()).unwrap(),
)
.unwrap();
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/objects/input/sk_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +28,7 @@ struct ControllerDatamap {
}

pub struct SkController {
_node: Arc<Node>,
_node: OwnedNode,
input: Arc<InputMethod>,
handed: Handed,
model: Model,
Expand All @@ -38,8 +38,8 @@ pub struct SkController {
}
impl SkController {
pub fn new(handed: Handed) -> Result<Self> {
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"),
Expand All @@ -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())?,
)?;
Expand Down
9 changes: 5 additions & 4 deletions src/objects/input/sk_hand.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -32,22 +33,22 @@ struct HandDatamap {
}

pub struct SkHand {
_node: Arc<Node>,
_node: OwnedNode,
handed: Handed,
input: Arc<InputMethod>,
capture: Option<Arc<InputHandler>>,
datamap: HandDatamap,
}
impl SkHand {
pub fn new(handed: Handed) -> Result<Self> {
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 {
Expand Down

0 comments on commit b3fa529

Please sign in to comment.