2024-05-04 08:26:16 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Rokojori
|
|
|
|
{
|
|
|
|
public class EventSlot<T>
|
|
|
|
{
|
|
|
|
protected List<Action<T>> _actions = new List<Action<T>>();
|
|
|
|
List<Action<T>> _once = new List<Action<T>>();
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
protected bool _canModify = true;
|
|
|
|
List<Action<T>> _additions = new List<Action<T>>();
|
|
|
|
List<Action<T>> _onceAdditions = new List<Action<T>>();
|
|
|
|
List<Action<T>> _removals = new List<Action<T>>();
|
|
|
|
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
public bool hasListeners => _once.Count > 0 || _actions.Count > 0;
|
|
|
|
|
|
|
|
public void AddAction( Action<T> action )
|
|
|
|
{
|
2025-01-08 18:46:17 +00:00
|
|
|
if ( _canModify )
|
|
|
|
{
|
|
|
|
_actions.Add( action );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_additions.Add( action );
|
|
|
|
}
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveAction( Action<T> action )
|
|
|
|
{
|
2025-01-08 18:46:17 +00:00
|
|
|
if ( _canModify )
|
|
|
|
{
|
|
|
|
_actions.Remove( action );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_removals.Add( action );
|
|
|
|
}
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Once( Action<T> action )
|
|
|
|
{
|
2025-01-08 18:46:17 +00:00
|
|
|
if ( _canModify )
|
|
|
|
{
|
|
|
|
_actions.Add( action );
|
|
|
|
_once.Add( action );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_onceAdditions.Add( action );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
protected void _UpdateLists()
|
2024-05-04 08:26:16 +00:00
|
|
|
{
|
|
|
|
_once.ForEach( a => { _actions.Remove( a ); } );
|
|
|
|
_once.Clear();
|
2025-01-08 18:46:17 +00:00
|
|
|
|
|
|
|
if ( _removals.Count > 0 )
|
|
|
|
{
|
|
|
|
Lists.RemoveRange( _actions, _removals );
|
|
|
|
_removals.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _additions.Count > 0 )
|
|
|
|
{
|
|
|
|
_actions.AddRange( _additions );
|
|
|
|
_additions.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _onceAdditions.Count > 0 )
|
|
|
|
{
|
|
|
|
_once.AddRange( _onceAdditions );
|
|
|
|
_actions.AddRange( _onceAdditions );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DispatchEvent( T t )
|
|
|
|
{
|
2025-01-08 18:46:17 +00:00
|
|
|
_canModify = false;
|
|
|
|
|
2024-05-04 08:26:16 +00:00
|
|
|
_actions.ForEach( a => { if ( a != null ){ a( t ); } } );
|
2025-01-08 18:46:17 +00:00
|
|
|
|
|
|
|
_UpdateLists();
|
2024-05-04 08:26:16 +00:00
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
_canModify = true;
|
2024-05-04 08:26:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|