2019-04-11 07:52:13 -04:00
using System ;
2018-10-30 15:05:06 -04:00
using System.Collections.Generic ;
using UnityEngine ;
namespace UnityAtoms
{
2019-10-15 14:44:25 -04:00
/// <summary>
/// None generic base class for Events. Inherits from `BaseAtom` and `ISerializationCallbackReceiver`.
/// </summary>
2019-10-14 10:51:54 -04:00
[EditorIcon("atom-icon-cherry")]
2019-10-14 20:16:11 -04:00
public abstract class AtomEvent : BaseAtom , ISerializationCallbackReceiver
2019-09-29 19:24:02 -04:00
{
2019-10-15 14:44:25 -04:00
/// <summary>
/// Event without value.
/// </summary>
2019-09-29 19:24:02 -04:00
public event Action OnEventNoValue ;
protected void RaiseNoValue ( )
{
OnEventNoValue ? . Invoke ( ) ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
2019-09-29 19:24:02 -04:00
public void Register ( Action del )
{
OnEventNoValue + = del ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
2019-09-29 19:24:02 -04:00
public void Unregister ( Action del )
{
OnEventNoValue - = del ;
}
public void OnBeforeSerialize ( ) { }
public virtual void OnAfterDeserialize ( )
{
// Clear all delegates when exiting play mode
if ( OnEventNoValue ! = null )
{
foreach ( var d in OnEventNoValue . GetInvocationList ( ) )
{
OnEventNoValue - = ( Action ) d ;
}
}
}
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Generic base class for Events. Inherits from `AtomEvent`.
/// </summary>
/// <typeparam name="T">The type for this Event.</typeparam>
2019-10-14 10:51:54 -04:00
[EditorIcon("atom-icon-cherry")]
2019-09-29 19:24:02 -04:00
public abstract class AtomEvent < T > : AtomEvent
2018-10-30 15:05:06 -04:00
{
2019-10-15 14:44:25 -04:00
/// <summary>
/// Actual event.
/// </summary>
2019-04-11 07:52:13 -04:00
public event Action < T > OnEvent ;
2018-10-30 15:05:06 -04:00
2019-10-15 14:44:25 -04:00
/// <summary>
/// Raise the Event.
/// </summary>
/// <param name="item">The value associated with the Event.</param>
2018-10-30 15:05:06 -04:00
public void Raise ( T item )
{
2019-09-29 19:24:02 -04:00
base . RaiseNoValue ( ) ;
2019-04-11 07:52:13 -04:00
OnEvent ? . Invoke ( item ) ;
2018-10-30 15:05:06 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
2019-04-16 16:32:17 -04:00
public void Register ( Action < T > del )
{
OnEvent + = del ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
2019-04-16 16:32:17 -04:00
public void Unregister ( Action < T > del )
{
OnEvent - = del ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
/// </summary>
/// <param name="listener">The Listenr to register.</param>
2019-09-25 15:05:06 -04:00
public void RegisterListener ( IAtomListener < T > listener )
2018-10-30 15:05:06 -04:00
{
2019-04-11 07:52:13 -04:00
OnEvent + = listener . OnEventRaised ;
2018-10-30 15:05:06 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister a listener that was registered using the `RegisterListener` method.
/// </summary>
/// <param name="listener">The Listenr to unregister.</param>
2019-09-25 15:05:06 -04:00
public void UnregisterListener ( IAtomListener < T > listener )
2018-10-30 15:05:06 -04:00
{
2019-04-11 07:52:13 -04:00
OnEvent - = listener . OnEventRaised ;
2018-10-30 15:05:06 -04:00
}
2019-04-16 16:32:17 -04:00
#region Observable
2019-10-15 14:44:25 -04:00
/// <summary>
/// Turn the Event into an `IObservable<T>`. Makes Events compatible with for example UniRx.
/// </summary>
/// <returns>The Event as an `IObservable<T>`.</returns>
2019-04-16 16:32:17 -04:00
public IObservable < T > Observe ( )
{
return new ObservableEvent < T > ( Register , Unregister ) ;
}
#endregion // Observable
2019-09-29 19:24:02 -04:00
public override void OnAfterDeserialize ( )
2019-04-16 16:32:17 -04:00
{
2019-09-29 19:24:02 -04:00
base . OnAfterDeserialize ( ) ;
2019-04-16 16:32:17 -04:00
// Clear all delegates when exiting play mode
if ( OnEvent ! = null )
{
foreach ( var d in OnEvent . GetInvocationList ( ) )
{
OnEvent - = ( Action < T > ) d ;
}
}
}
2018-10-30 15:05:06 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Generic base class for Events. Inherits from `AtomEvent`.
/// </summary>
/// <typeparam name="T1">The first type for this Event.</typeparam>
/// <typeparam name="T2">The second type for this Event.</typeparam>
2019-10-14 10:51:54 -04:00
[EditorIcon("atom-icon-cherry")]
2019-09-29 19:24:02 -04:00
public abstract class AtomEvent < T1 , T2 > : AtomEvent
2018-10-30 15:05:06 -04:00
{
2019-10-15 14:44:25 -04:00
/// <summary>
/// Actual event.
/// </summary>
2019-04-11 07:52:13 -04:00
public event Action < T1 , T2 > OnEvent ;
2018-10-30 15:05:06 -04:00
2019-10-15 14:44:25 -04:00
/// <summary>
/// Raise the Event.
/// </summary>
/// <param name="item1">The first value associated with the Event.</param>
/// <param name="item2">The second value associated with the Event.</param>
2018-10-30 15:05:06 -04:00
public void Raise ( T1 item1 , T2 item2 )
{
2019-09-29 19:24:02 -04:00
base . RaiseNoValue ( ) ;
2019-04-11 07:52:13 -04:00
OnEvent ? . Invoke ( item1 , item2 ) ;
2018-10-30 15:05:06 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
2019-04-16 16:32:17 -04:00
public void Register ( Action < T1 , T2 > del )
{
OnEvent + = del ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
2019-04-16 16:32:17 -04:00
public void Unregister ( Action < T1 , T2 > del )
{
OnEvent - = del ;
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
/// </summary>
/// <param name="listener">The Listenr to register.</param>
2019-09-25 15:05:06 -04:00
public void RegisterListener ( IAtomListener < T1 , T2 > listener )
2018-10-30 15:05:06 -04:00
{
2019-04-11 07:52:13 -04:00
OnEvent + = listener . OnEventRaised ;
2018-10-30 15:05:06 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister a listener that was registered using the `RegisterListener` method.
/// </summary>
/// <param name="listener">The Listenr to unregister.</param>
2019-09-25 15:05:06 -04:00
public void UnregisterListener ( IAtomListener < T1 , T2 > listener )
2018-10-30 15:05:06 -04:00
{
2019-04-11 07:52:13 -04:00
OnEvent - = listener . OnEventRaised ;
2018-10-30 15:05:06 -04:00
}
2019-04-16 16:32:17 -04:00
#region Observable
2019-10-15 14:44:25 -04:00
/// <summary>
/// Turn the Event into an `IObservable<M>`. Makes Events compatible with for example UniRx.
/// </summary>
/// <param name="resultSelector">Takes `T1` and `T2` and returns a new type of type `M`.abstract Most of the time this is going to be combination of T1 and T2, eg. `ValueTuple<T1, T2>`</param>
/// <typeparam name="M">The result selector type.</typeparam>
/// <returns>The Event as an `IObservable<M>`.</returns>
public IObservable < M > Observe < M > ( Func < T1 , T2 , M > resultSelector )
2019-04-16 16:32:17 -04:00
{
2019-10-15 14:44:25 -04:00
return new ObservableEvent < T1 , T2 , M > ( Register , Unregister , resultSelector ) ;
2019-04-16 16:32:17 -04:00
}
#endregion // Observable
2019-09-29 19:24:02 -04:00
public override void OnAfterDeserialize ( )
2019-04-16 16:32:17 -04:00
{
2019-09-29 19:24:02 -04:00
base . OnAfterDeserialize ( ) ;
2019-04-16 16:32:17 -04:00
// Clear all delegates when exiting play mode
if ( OnEvent ! = null )
foreach ( var d in OnEvent . GetInvocationList ( ) )
{
OnEvent - = ( Action < T1 , T2 > ) d ;
}
}
2018-10-30 15:05:06 -04:00
}
2019-03-17 18:43:20 -04:00
}