rokojori_action_library/Runtime/Godot/Unique.cs

108 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Godot;
using Rokojori.Extensions;
namespace Rokojori;
[RokojoriActionCoreExport]
[RokojoriActionCoreGenericAsString( "Prepend" )]
public class Unique<N> where N:Node
{
#if ROKOJORI_ACTION_CORE_GD
static Dictionary<string,Node> _singletons;
public static Node Get( Node n = null )
{
/*
if _singletons.has( type ):
return _singletons[ type ]
var window = RJ_Root.window() if n == null else n
if ( window == null ):
if ( Engine.is_editor_hint() ):
window = EditorInterface.get_edited_scene_root()
else:
return null
var walker = RJ_NodesWalker._get_()
var uniqueNode = walker.find( window, func (n): return RJ_TypeTools.is_of_type( type, n ), true )
if ( uniqueNode == null ):
return null
_singletons[ type ] = uniqueNode
return _singletons[ type ]
*/
GDGlue.CommentToGD();
}
#else
static N _singleton;
public static N Get( Node n = null )
{
// RJLog.Log( typeof( N ).Name );
if ( _singleton != null )
{
return _singleton;
}
var rootWindow = n == null ? Root.Window() : n.Owner;
if ( rootWindow == null )
{
if ( Engine.IsEditorHint() )
{
#if TOOLS
rootWindow = EditorInterface.Singleton.GetEditedSceneRoot();
#endif
}
else
{
return null;
}
}
_singleton = Nodes.GetAnyChild<N>( rootWindow );
if ( _singleton == null )
{
if ( Engine.IsEditorHint() )
{
#if TOOLS
rootWindow = EditorInterface.Singleton.GetEditedSceneRoot();
#endif
_singleton = Nodes.GetAnyChild<N>( rootWindow );
}
}
return _singleton;
}
#endif
public static void test()
{
var t = Unique<Node>.Get();
TypeTools.IsScriptOfType<Node>( null );
}
}