rj-action-library/Runtime/Interactions/Pointable.cs

73 lines
1.2 KiB
C#
Raw Normal View History

2024-08-04 09:08:12 +00:00
using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;
namespace Rokojori
{
[GlobalClass]
2025-01-08 18:46:17 +00:00
public partial class Pointable:Node3D
2024-08-04 09:08:12 +00:00
{
2025-01-08 18:46:17 +00:00
[Export]
public int pointingPriority;
2024-08-04 09:08:12 +00:00
2025-01-08 18:46:17 +00:00
[Export]
public Action onPointed;
[Export]
public Action onUnpointed;
[Export]
public Action onPointerAdded;
[Export]
public Action onPointerRemoved;
List<Pointer> _pointers = new List<Pointer>();
public int numPointing => _pointers.Count;
2024-08-04 09:08:12 +00:00
2025-01-08 18:46:17 +00:00
public bool IsPointedBy( Pointer pointer )
2024-08-04 09:08:12 +00:00
{
return _pointers.Contains( pointer );
}
2025-01-08 18:46:17 +00:00
public void UpdatePointerState( Pointer pointer, bool pointed )
2024-08-04 09:08:12 +00:00
{
var isCurrentlyPointed = IsPointedBy( pointer );
if ( isCurrentlyPointed == pointed )
{
return;
}
if ( pointed )
{
_pointers.Add( pointer );
2025-01-08 18:46:17 +00:00
Action.Trigger( onPointerAdded );
2024-08-04 09:08:12 +00:00
if ( _pointers.Count == 1 )
{
2025-01-08 18:46:17 +00:00
Action.Trigger( onPointed );
2024-08-04 09:08:12 +00:00
}
}
else
{
_pointers.Remove( pointer );
2025-01-08 18:46:17 +00:00
Action.Trigger( onPointerRemoved );
2024-08-04 09:08:12 +00:00
if ( _pointers.Count == 0 )
{
2025-01-08 18:46:17 +00:00
Action.Trigger( onUnpointed );
2024-08-04 09:08:12 +00:00
}
}
}
}
}