rokojori_action_library/Runtime/Godot/Unique.cs

108 lines
2.1 KiB
C#
Raw Normal View History

2024-05-04 08:26:16 +00:00
using System.Collections;
using System.Collections.Generic;
using Godot;
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
namespace Rokojori;
[RokojoriActionCoreExport]
[RokojoriActionCoreGenericAsString( "Prepend" )]
public class Unique<N> where N:Node
2024-05-04 08:26:16 +00:00
{
2026-05-22 12:25:02 +00:00
#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;
2024-05-04 08:26:16 +00:00
2024-07-26 09:26:24 +00:00
public static N Get( Node n = null )
2024-05-04 08:26:16 +00:00
{
2025-03-13 16:19:28 +00:00
// RJLog.Log( typeof( N ).Name );
2024-05-04 08:26:16 +00:00
if ( _singleton != null )
{
return _singleton;
}
2024-07-26 09:26:24 +00:00
var rootWindow = n == null ? Root.Window() : n.Owner;
2024-07-26 09:26:24 +00:00
if ( rootWindow == null )
{
if ( Engine.IsEditorHint() )
{
2025-12-18 10:29:54 +00:00
#if TOOLS
rootWindow = EditorInterface.Singleton.GetEditedSceneRoot();
2025-12-18 10:29:54 +00:00
#endif
2026-05-22 12:25:02 +00:00
}
else
{
return null;
}
2024-07-26 09:26:24 +00:00
}
_singleton = Nodes.GetAnyChild<N>( rootWindow );
if ( _singleton == null )
{
if ( Engine.IsEditorHint() )
{
2025-12-18 10:29:54 +00:00
#if TOOLS
rootWindow = EditorInterface.Singleton.GetEditedSceneRoot();
2025-12-18 10:29:54 +00:00
#endif
_singleton = Nodes.GetAnyChild<N>( rootWindow );
}
}
2024-05-04 08:26:16 +00:00
return _singleton;
}
2024-07-26 09:26:24 +00:00
2026-05-22 12:25:02 +00:00
#endif
public static void test()
{
var t = Unique<Node>.Get();
TypeTools.IsScriptOfType<Node>( null );
2024-05-04 08:26:16 +00:00
}
2026-05-22 12:25:02 +00:00
}