From 3df9a723aa77388acd3661b61d874717f5ae72eb Mon Sep 17 00:00:00 2001 From: LunaticWyrm467 Date: Sat, 2 Nov 2024 10:13:32 +0100 Subject: [PATCH] Added `this` and `this_dyn` functions. --- Cargo.lock | 2 +- node_tree_core/Cargo.toml | 4 ++-- node_tree_core/src/structs/node_base.rs | 31 +++++++++++++++++++++++++ node_tree_core/tests/signals.rs | 10 +++++++- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 684956e..d6f4d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,7 +107,7 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "node_tree" -version = "0.8.0" +version = "0.8.1" dependencies = [ "chrono", "node_tree_derive", diff --git a/node_tree_core/Cargo.toml b/node_tree_core/Cargo.toml index 719a129..aba7144 100644 --- a/node_tree_core/Cargo.toml +++ b/node_tree_core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "node_tree" -version = "0.8.0" +version = "0.8.1" edition = "2021" rust-version = "1.78" @@ -13,7 +13,7 @@ exclude = [ keywords = ["framework", "utility", "gamedev"] repository = "https://github.com/LunaticWyrm467/node_tree" description = """ -An extendable system made up of autonomous execution services known as nodes organized in a tree of processes. +An extendable scene graph made up of autonomous execution services known as nodes organized in a tree of processes. Inspired by Godot! """ license = "MIT OR Apache-2.0" diff --git a/node_tree_core/src/structs/node_base.rs b/node_tree_core/src/structs/node_base.rs index 151866b..b3a251a 100644 --- a/node_tree_core/src/structs/node_base.rs +++ b/node_tree_core/src/structs/node_base.rs @@ -79,6 +79,37 @@ impl NodeBase { depth: 0 } } + + /// Returns a `Tp` pointer for itself. + /// + /// # Panics + /// The correct type (type of `self`) will have to be provided, or this function will panic! + /// Panics also if this Node is not connected to a `NodeTree`. + pub fn this(&self) -> Tp { + if self.tree.is_none() { + panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!"); + } + + unsafe { + Tp::new(self.tree.unwrap_unchecked(), self.rid, self.rid).expect("Called `this` with the wrong type provided") + } + } + + /// Returns a `TpDyn` pointer for itself. + /// + /// # Panics + /// Panics if this Node is not connected to a `NodeTree`. + pub fn this_dyn(&self) -> TpDyn { + if self.tree.is_none() { + panic!("Cannot get a tree pointer to a node that is not in a `NodeTree`!"); + } + + // SAFETY: + // There are no invariants that could result in this being unwrapped on an `Err` value. + unsafe { + TpDyn::new(self.tree.unwrap_unchecked(), self.rid, self.rid).unwrap_unchecked() + } + } /// Gets the name of the node. /// Each name is unique within the context of the parent's children vector. diff --git a/node_tree_core/tests/signals.rs b/node_tree_core/tests/signals.rs index 130b2a7..2c5222c 100644 --- a/node_tree_core/tests/signals.rs +++ b/node_tree_core/tests/signals.rs @@ -11,13 +11,20 @@ class! { hk ready(&mut self) { let child: Tp = self.get_child(0).unwrap(); + let this: Tp = self.this(); + connect! { on_event -> child.listener }; + connect! { on_event -> this.listener }; } hk process(&mut self, _delta: f32) { self.on_event.emit(self.count); self.count += 1; } + + fn listener(&self, count: &u8) { + self.post(Log::Warn(&format!("Count is {count}"))); + } } @@ -26,7 +33,8 @@ class! { fn listener(&self, count: &u8) { if *count == 3 { - panic!("This was successful!"); + self.post(Log::Panic("This was successful!")); + panic!(); } } }