-
Notifications
You must be signed in to change notification settings - Fork 1
/
NX.cs
92 lines (89 loc) · 2.57 KB
/
NX.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Godot;
using Godot.Collections;
public class NX : Node
{
//<summary>
//Find the first child node of type T
//</summary>
public static T Find<T>(Node root, bool recursive = false) where T : Node
{
for(int i = 0;i < root.GetChildCount(); ++i)
{
Node child = root.GetChild(i);
if (child is T) return (T)child;
if (recursive && child.GetChildCount() > 0)
{
T grandChild = Find<T>(child, true);
if (grandChild != null) return grandChild;
}
}
return null;
}
//<summary>
//Find all nodes of type T in children
//</summary>
public static Array<T> FindAll<T>(Node root, bool recursive = false) where T : Node
{
Array<T> nodes = new Array<T>();
for (int i = 0; i < root.GetChildCount(); ++i)
{
Node child = root.GetChild(i);
if (child is T) nodes.Add((T)child);
if (recursive && child.GetChildCount() > 0)
{
Array<T> grandChildren = FindAll<T>(child, true);
if (grandChildren.Count != 0)
{
foreach(T grandChild in grandChildren)
{
nodes.Add(grandChild);
}
}
}
}
return nodes;
}
//<summary>
//Find first parent node of type T
//</summary>
public static T FindParent<T>(Node root) where T : Node
{
root = root.GetParentOrNull<Node>();
while (root != null)
{
if (root is T) break;
root = root.GetParentOrNull<Node>();
}
return (T)root;
}
//<summary>
//Find all parent nodes of type T
//</summary>
public static Array<T> FindAllParent<T>(Node root) where T : Node
{
Array<T> nodes = new Array<T>();
root = root.GetParentOrNull<Node>();
while (root != null)
{
if (root is T) nodes.Add((T)root);
root = root.GetParentOrNull<Node>();
}
return nodes;
}
//<summary>
//Find node whose name matches its type
//</summary>
public static T GetNodeWithNameType<T>(Node root) where T : Node
{
string name = typeof(T).ToString();
int pos = name.FindLast(".");
return root.GetNode<T>(name.Right(pos + 1));
}
//<summary>
//Check if node is a child of root
//</summary>
public static bool IsUnderRoot(Node node)
{
return node.GetTree().Root == node.GetParent();
}
}