2024-05-04 08:26:16 +00:00
|
|
|
using Godot;
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
using System.Collections.Generic;
|
2024-07-26 09:26:24 +00:00
|
|
|
using System;
|
2024-05-04 08:26:16 +00:00
|
|
|
|
|
|
|
namespace Rokojori
|
|
|
|
{
|
|
|
|
public class Nodes
|
|
|
|
{
|
2024-08-04 09:08:12 +00:00
|
|
|
public static T Find<T>( Node root, NodePathLocatorType type = NodePathLocatorType.DirectChildren, int parentOffset = 0 ) where T:Node
|
|
|
|
{
|
|
|
|
var it = root;
|
|
|
|
|
|
|
|
while ( parentOffset > 0 && it != null )
|
|
|
|
{
|
|
|
|
it = it.GetParent();
|
|
|
|
parentOffset --;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( it == null )
|
|
|
|
{
|
|
|
|
return default(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( type )
|
|
|
|
{
|
|
|
|
case NodePathLocatorType.DirectChildren:
|
|
|
|
{
|
|
|
|
return GetDirectChild<T>( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
case NodePathLocatorType.Siblings:
|
|
|
|
{
|
|
|
|
return GetSibling<T>( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
case NodePathLocatorType.DirectChildrenAndSiblings:
|
|
|
|
{
|
|
|
|
var child = GetDirectChild<T>( it );
|
|
|
|
|
|
|
|
if ( child != null )
|
|
|
|
{
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetSibling<T>( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
case NodePathLocatorType.AnyChildren:
|
|
|
|
{
|
|
|
|
return GetAnyChild<T>( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
case NodePathLocatorType.SiblingsAndAnyChildren:
|
|
|
|
{
|
|
|
|
var sibling = GetSibling<T>( it );
|
|
|
|
|
|
|
|
if ( sibling != null )
|
|
|
|
{
|
|
|
|
return sibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetAnyChild<T>( it );;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return default(T);
|
|
|
|
}
|
|
|
|
|
2024-07-26 09:26:24 +00:00
|
|
|
public static void ForEach<T>( Node root, Action<T> callback ) where T:class
|
|
|
|
{
|
|
|
|
var walker = nodesWalker;
|
|
|
|
|
|
|
|
walker.Iterate( root,
|
|
|
|
( n )=>
|
|
|
|
{
|
|
|
|
var t = n as T;
|
|
|
|
|
|
|
|
if ( t == null )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback( t );
|
|
|
|
|
|
|
|
}
|
|
|
|
,false );
|
|
|
|
}
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
public static T GetSibling<T>( Node node ) where T:Node
|
|
|
|
{
|
|
|
|
if ( node == null )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent = node.GetParent();
|
|
|
|
|
|
|
|
if ( parent == null )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetDirectChild<T>( parent );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static T GetDirectChild<T>( Node parent ) where T:Node
|
|
|
|
{
|
|
|
|
if ( parent == null )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var numChildren = parent.GetChildCount();
|
|
|
|
|
|
|
|
for ( int i = 0; i < numChildren; i++ )
|
|
|
|
{
|
|
|
|
var node = parent.GetChild( i );
|
|
|
|
|
|
|
|
if ( node is T )
|
|
|
|
{
|
2024-05-12 17:03:20 +00:00
|
|
|
return (T) node;
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
public static List<T> GetDirectChildren<T>( Node parent ) where T:Node
|
|
|
|
{
|
|
|
|
if ( parent == null )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var list = new List<T>();
|
|
|
|
|
|
|
|
var numChildren = parent.GetChildCount();
|
|
|
|
|
|
|
|
for ( int i = 0; i < numChildren; i++ )
|
|
|
|
{
|
|
|
|
var node = parent.GetChild( i );
|
|
|
|
var script = node.GetScript();
|
|
|
|
|
2024-08-04 09:08:12 +00:00
|
|
|
// RJLog.Log( "Node is", typeof(T), node.Name, node.GetType(), script.GetType(), ">>", ( node is T ) );
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if ( ! ( node is T ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add( node as T );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2024-05-12 17:03:20 +00:00
|
|
|
public static void ForEachDirectChild<T>( Node parent, System.Action<T> action ) where T:Node
|
|
|
|
{
|
|
|
|
if ( parent == null || action == null )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var numChildren = parent.GetChildCount();
|
|
|
|
|
|
|
|
for ( int i = 0; i < numChildren; i++ )
|
|
|
|
{
|
|
|
|
var node = parent.GetChild( i );
|
|
|
|
|
|
|
|
if ( ! ( node is T ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
action( node as T );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
static NodesWalker nodesWalker = new NodesWalker();
|
|
|
|
|
|
|
|
public static T GetAnyChild<T>( Node parent ) where T:Node
|
|
|
|
{
|
2024-07-26 09:26:24 +00:00
|
|
|
var result = nodesWalker.Find( parent,
|
|
|
|
( n )=>
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var castedNode = n as T;
|
|
|
|
|
2024-08-04 09:08:12 +00:00
|
|
|
// RJLog.Log( "Testing", n.UniqueNameInOwner, castedNode != null, n.GetType() );
|
2024-07-26 09:26:24 +00:00
|
|
|
|
|
|
|
return castedNode != null;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
true );
|
|
|
|
|
|
|
|
return (T) result;
|
2024-05-05 07:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-05-04 08:26:16 +00:00
|
|
|
|
|
|
|
public static void Enable( Node n, bool affectProcess = true, bool affectPhysicsProcess = true, bool affectInput = true )
|
|
|
|
{
|
|
|
|
SetState.SetStateOfNode( NodeStateType.Enabled, n, affectProcess, affectPhysicsProcess, affectInput );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Disable( Node n, bool affectProcess = true, bool affectPhysicsProcess = true, bool affectInput = true )
|
|
|
|
{
|
|
|
|
SetState.SetStateOfNode( NodeStateType.Disabled, n, affectProcess, affectPhysicsProcess, affectInput );
|
|
|
|
}
|
|
|
|
|
2024-05-12 17:03:20 +00:00
|
|
|
*/
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
public static void Iterate( Node[] nodes, System.Action<Node> callback )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < nodes.Length; i++ )
|
|
|
|
{
|
|
|
|
nodesWalker.Iterate( nodes[ i ], callback );
|
|
|
|
}
|
|
|
|
}
|
2024-05-05 07:52:06 +00:00
|
|
|
|
2024-05-12 17:03:20 +00:00
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|