rokojori_action_library/Runtime/Godot/NodeState.cs

261 lines
5.1 KiB
C#
Raw Normal View History

2024-08-04 09:08:12 +00:00
using Godot;
using System.Collections.Generic;
using System;
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
namespace Rokojori;
[RokojoriActionCoreExport]
public static class NodeState
{
public static bool IsVisible( this Node n )
2025-07-20 11:22:12 +00:00
{
2026-05-22 12:25:02 +00:00
if ( n is Node3D )
{
return ( n as Node3D ).Visible;
}
if ( n is Node2D )
{
return ( n as Node2D ).Visible;
}
if ( n is CanvasItem )
{
return ( n as CanvasItem ).Visible;
}
return false;
2025-07-20 11:22:12 +00:00
}
2026-05-22 12:25:02 +00:00
public static bool IsProcessingInHierarchy( this Node n )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
if ( n.ProcessMode == Node.ProcessModeEnum.Disabled )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
return false;
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( n.ProcessMode == Node.ProcessModeEnum.Always )
{
return true;
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
var paused = n.GetTree().Paused;
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( n.ProcessMode == Node.ProcessModeEnum.Pausable )
{
return ! paused;
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
if ( n.ProcessMode == Node.ProcessModeEnum.WhenPaused )
2025-12-10 14:17:07 +00:00
{
2026-05-22 12:25:02 +00:00
return paused;
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
var p = n.GetParent();
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
if ( p == null )
{
return ! paused;
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
return p.IsProcessingInHierarchy();
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
public static void ConfigureWith( Node target, NodeStateConfiguration configuration )
{
PartialConfigure( target,
configuration.processEnabled, configuration.inputEnabled, configuration.physicsEnabled,
configuration.signalsEnabled, configuration.visible, configuration.setProcessMode, configuration.processMode
);
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
public static void SetNodeState( this Node target, NodeStateConfiguration configuration )
{
ConfigureWith( target, configuration );
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
public static void SetVisibility( this Node target, bool visible )
{
PartialConfigure( target, Trillean.Any, Trillean.Any, Trillean.Any, Trillean.Any, TrilleanLogic.FromBool( visible ), false, Node.ProcessModeEnum.Inherit );
}
2025-12-10 14:17:07 +00:00
2026-05-22 12:25:02 +00:00
public static void PartialConfigure(
Node target,
Trillean processEnabled, Trillean inputEnabled,Trillean physicsEnabled,
Trillean signalsEnabled,Trillean visible, bool setProcessMode, Node.ProcessModeEnum processMode
)
{
if ( ! Node.IsInstanceValid( target ) )
{
return;
2025-12-10 14:17:07 +00:00
}
2026-05-22 12:25:02 +00:00
if ( Trillean.Any != processEnabled )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
target.SetProcess( TrilleanLogic.ToBool( processEnabled ) );
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
if ( Trillean.Any != inputEnabled )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
target.SetProcessInput( TrilleanLogic.ToBool( inputEnabled ) );
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
if ( Trillean.Any != physicsEnabled )
2025-11-26 14:23:59 +00:00
{
2026-05-22 12:25:02 +00:00
target.SetPhysicsProcess( TrilleanLogic.ToBool( physicsEnabled ) );
2025-11-26 14:23:59 +00:00
}
2026-05-22 12:25:02 +00:00
if ( Trillean.Any != signalsEnabled )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
target.SetBlockSignals( ! TrilleanLogic.ToBool( signalsEnabled ) );
}
2025-07-25 15:35:19 +00:00
2026-05-22 12:25:02 +00:00
if ( setProcessMode )
{
target.ProcessMode = processMode;
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( Trillean.Any != visible )
{
var n = target;
var visibleFlag = TrilleanLogic.ToBool( visible );
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( n is Node3D )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
( n as Node3D ).Visible = visibleFlag;
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
if ( n is Node2D )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
( n as Node2D ).Visible = visibleFlag;
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
if ( n is CanvasItem )
2025-07-22 14:08:22 +00:00
{
2026-05-22 12:25:02 +00:00
( n as CanvasItem ).Visible = visibleFlag;
}
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
DispatchNodeStateChanged( target );
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
static void DispatchNodeStateChanged( Node target)
{
#if !ROKOJORI_ACTION_CORE_GD
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( target is iNodeState ins )
{
ins.OnNodeStateChanged();
}
#else
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
/*
if target.has_method( "on_node_state_changed" ):
var ins:Variant = target
ins.on_node_state_changed()
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
*/
GDGlue.CommentToGD();
#endif
}
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
public static void Configure( Node n, bool processEnabled, bool inputEnabled, bool physicsEnabled, bool signalsEnabled,
Node.ProcessModeEnum processMode, bool visible )
{
if ( n == null || ! Node.IsInstanceValid( n ) )
{
return;
2025-07-22 14:08:22 +00:00
}
2026-05-22 12:25:02 +00:00
n.SetProcess( processEnabled );
n.SetProcessInput( inputEnabled );
n.SetPhysicsProcess( physicsEnabled );
n.SetBlockSignals( ! signalsEnabled );
n.ProcessMode = processMode;
2025-07-22 14:08:22 +00:00
2026-05-22 12:25:02 +00:00
if ( n is Node3D n3 )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
n3.Visible = visible;
2024-08-04 09:08:12 +00:00
}
2026-05-22 12:25:02 +00:00
if ( n is Node2D n2 )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
n2.Visible = visible;
2024-08-04 09:08:12 +00:00
}
2026-05-22 12:25:02 +00:00
if ( n is CanvasItem ci )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
ci.Visible = visible;
}
2024-08-04 09:08:12 +00:00
2026-05-22 12:25:02 +00:00
DispatchNodeStateChanged( n );
2024-08-04 09:08:12 +00:00
2026-05-22 12:25:02 +00:00
if ( n is CollisionShape3D cs )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
cs.Disabled = ! physicsEnabled;
2024-08-04 09:08:12 +00:00
}
2026-05-22 12:25:02 +00:00
}
2024-08-04 09:08:12 +00:00
2026-05-22 12:25:02 +00:00
public static void SetNode( Node n, bool enabled )
{
Configure( n, enabled, enabled, enabled, enabled, enabled ? Node.ProcessModeEnum.Inherit : Node.ProcessModeEnum.Disabled, enabled );
}
public static void SetAll( Node[] nodes, bool enabled )
{
foreach ( var n in nodes )
2024-08-04 09:08:12 +00:00
{
2026-05-22 12:25:02 +00:00
SetNode( n, enabled );
2024-08-04 16:57:47 +00:00
}
2026-05-22 12:25:02 +00:00
}
public static void Enable( this Node n )
{
SetNode( n, true );
}
public static void EnableAll( params Node[] n )
{
SetAll( n, true );
}
public static void Disable( this Node n )
{
SetNode( n, false );
}
public static void DisableAll( params Node[] n )
{
SetAll( n, false );
}
public static void SetEnabledState( this Node n, bool isEnabled )
{
if ( isEnabled )
2024-08-04 16:57:47 +00:00
{
2026-05-22 12:25:02 +00:00
n.Enable();
2024-08-04 09:08:12 +00:00
}
2026-05-22 12:25:02 +00:00
else
2025-07-19 06:22:56 +00:00
{
2026-05-22 12:25:02 +00:00
n.Disable();
2025-07-19 06:22:56 +00:00
}
2024-08-04 09:08:12 +00:00
}
2026-05-22 12:25:02 +00:00
}
public interface iNodeState
{
public void OnNodeStateChanged();
}