-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the
scene!
macro and NodeScene
!
- Loading branch information
1 parent
aa6a640
commit 25474fc
Showing
18 changed files
with
487 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ pub mod node_path; | |
pub mod node_tree_base; | ||
pub mod tree_pointer; | ||
pub mod rid; | ||
|
||
#[macro_use] | ||
pub mod node_scene; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::traits::{ node::Node, instanceable::Instanceable }; | ||
|
||
|
||
/* | ||
* Node Scene | ||
* Struct | ||
*/ | ||
|
||
|
||
/// A recursive structure that allows for the storage of a dormant scene of nodes. | ||
/// The root node is what every node in the scene will have its owner set to. | ||
#[derive(Debug)] | ||
pub struct NodeScene { | ||
this: *mut dyn Node, | ||
children: Vec<NodeScene> | ||
} | ||
|
||
impl NodeScene { | ||
|
||
/// Creates a new `NodeScene` with a root node. | ||
pub fn new<N: Node>(root: N) -> Self { | ||
NodeScene { | ||
this: Box::into_raw(root.to_dyn_box()), | ||
children: Vec::new() | ||
} | ||
} | ||
|
||
/// Appends a `NodeScene` as a child. | ||
pub fn append(&mut self, child: NodeScene) { | ||
self.children.push(child); | ||
} | ||
|
||
/// Returns this `NodeScene` instance's associated node. | ||
/// | ||
/// # Safety | ||
/// This is marked unsafe as if the resulting `Box<T>` is dropped, the internal pointer could | ||
/// be invalidated. | ||
pub unsafe fn get_node(&self) -> Box<dyn Node> { | ||
Box::from_raw(self.this) | ||
} | ||
|
||
/// Gets the children. | ||
pub fn children(&self) -> &[NodeScene] { | ||
&self.children | ||
} | ||
} | ||
|
||
impl Instanceable for NodeScene { | ||
fn iterate<F: FnMut(Option<*mut dyn Node>, *mut dyn Node)>(self, mut iterator: F) { | ||
iterator(None, self.this); | ||
|
||
// Recursive function to traverse the tree | ||
fn traverse<F: FnMut(Option<*mut dyn Node>, *mut dyn Node)>( | ||
node: NodeScene, | ||
parent: *mut dyn Node, | ||
iterator: &mut F | ||
) { | ||
for child in node.children { | ||
|
||
// Call the iterator for the child node | ||
iterator(Some(parent), child.this); | ||
|
||
// Recursively traverse the child's children | ||
let child_this: *mut dyn Node = child.this; | ||
traverse(child, child_this, iterator); | ||
} | ||
} | ||
|
||
// Start the traversal from the root. | ||
let self_this: *mut dyn Node = self.this; | ||
traverse(self, self_this, &mut iterator); | ||
} | ||
} |
Oops, something went wrong.