rj-action-library/Runtime/Godot/NodeState.cs

190 lines
4.1 KiB
C#
Raw Normal View History

2024-08-04 09:08:12 +00:00
using Godot;
using System.Collections.Generic;
using System;
namespace Rokojori
{
2025-07-20 11:22:12 +00:00
public interface iNodeState
{
2025-07-22 14:08:22 +00:00
public void OnNodeStateChanged();
2025-07-20 11:22:12 +00:00
}
2025-06-27 05:12:53 +00:00
public static class NodeState
2024-08-04 09:08:12 +00:00
{
2025-07-22 14:08:22 +00:00
public static bool IsVisible( this Node n )
{
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;
}
public static void Configure( Node target, NodeStateConfiguration configuration )
{
Configure( target,
configuration.processEnabled, configuration.inputEnabled, configuration.physicsEnabled,
configuration.signalsEnabled, configuration.visible, configuration.setProcessMode, configuration.processMode
);
}
public static void SetNodeState( this Node target, NodeStateConfiguration configuration )
{
Configure( target, configuration );
}
public static void Configure(
Node target,
Trillean processEnabled, Trillean inputEnabled,Trillean physicsEnabled,
Trillean signalsEnabled,Trillean visible, bool setProcessMode, Node.ProcessModeEnum processMode
)
{
if ( Trillean.Any != processEnabled )
{
target.SetProcess( TrilleanLogic.ToBool( processEnabled ) );
}
if ( Trillean.Any != inputEnabled )
{
target.SetProcessInput( TrilleanLogic.ToBool( inputEnabled ) );
}
if ( Trillean.Any != physicsEnabled )
{
target.SetPhysicsProcess( TrilleanLogic.ToBool( physicsEnabled ) );
}
if ( Trillean.Any != signalsEnabled )
{
target.SetBlockSignals( ! TrilleanLogic.ToBool( signalsEnabled ) );
}
if ( setProcessMode )
{
target.ProcessMode = processMode;
}
if ( Trillean.Any != visible )
{
var n = target;
var visibleFlag = TrilleanLogic.ToBool( visible );
if ( n is Node3D )
{
( n as Node3D ).Visible = visibleFlag;
}
if ( n is Node2D )
{
( n as Node2D ).Visible = visibleFlag;
}
if ( n is CanvasItem )
{
( n as CanvasItem ).Visible = visibleFlag;
}
}
if ( target is iNodeState ins )
{
ins.OnNodeStateChanged();
}
}
2024-08-04 09:08:12 +00:00
public static void Configure( Node n, bool processEnabled, bool inputEnabled, bool physicsEnabled, bool signalsEnabled,
2024-08-04 16:57:47 +00:00
Node.ProcessModeEnum processMode, bool visible )
2024-08-04 09:08:12 +00:00
{
2024-08-04 16:57:47 +00:00
if ( n == null )
{
return;
}
2024-08-04 09:08:12 +00:00
n.SetProcess( processEnabled );
n.SetProcessInput( inputEnabled );
n.SetPhysicsProcess( physicsEnabled );
n.SetBlockSignals( ! signalsEnabled );
n.ProcessMode = processMode;
2024-08-04 16:57:47 +00:00
if ( n is Node3D )
{
( n as Node3D ).Visible = visible;
}
if ( n is Node2D )
{
( n as Node2D ).Visible = visible;
}
if ( n is CanvasItem )
{
( n as CanvasItem ).Visible = visible;
2025-07-22 14:08:22 +00:00
}
if ( n is iNodeState ins )
{
ins.OnNodeStateChanged();
}
2024-08-04 09:08:12 +00:00
}
public static void Set( Node n, bool enabled )
{
2024-08-04 16:57:47 +00:00
Configure( n, enabled, enabled, enabled, enabled, enabled ? Node.ProcessModeEnum.Inherit : Node.ProcessModeEnum.Disabled, enabled );
2024-08-04 09:08:12 +00:00
}
public static void Set( Node[] nodes, bool enabled )
{
Arrays.ForEach( nodes, n => Set( n, enabled ) );
}
2025-06-27 05:12:53 +00:00
public static void Enable( this Node n )
2024-08-04 09:08:12 +00:00
{
Set( n, true );
}
2024-08-04 16:57:47 +00:00
public static void Enable( params Node[] n )
2024-08-04 09:08:12 +00:00
{
Set( n, true );
}
2025-06-27 05:12:53 +00:00
public static void Disable( this Node n )
2024-08-04 09:08:12 +00:00
{
2024-08-04 16:57:47 +00:00
Set( n, false );
}
public static void Disable( params Node[] n )
{
Set( n, false );
2024-08-04 09:08:12 +00:00
}
2025-07-19 06:22:56 +00:00
public static void SetEnabledState( this Node n, bool isEnabled )
{
if ( isEnabled )
{
n.Enable();
}
else
{
n.Disable();
}
}
2024-08-04 09:08:12 +00:00
}
}