using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
///
/// None generic base class for Events. Inherits from `BaseAtom` and `ISerializationCallbackReceiver`.
///
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEvent : BaseAtom, ISerializationCallbackReceiver
{
///
/// Event without value.
///
public event Action OnEventNoValue;
protected void RaiseNoValue()
{
OnEventNoValue?.Invoke();
}
///
/// Register handler to be called when the Event triggers.
///
/// The handler.
public void Register(Action del)
{
OnEventNoValue += del;
}
///
/// Unregister handler that was registered using the `Register` method.
///
/// The handler.
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;
}
}
}
}
///
/// Generic base class for Events. Inherits from `AtomEvent`.
///
/// The type for this Event.
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEvent : AtomEvent
{
///
/// Actual event.
///
public event Action OnEvent;
///
/// Raise the Event.
///
/// The value associated with the Event.
public void Raise(T item)
{
base.RaiseNoValue();
OnEvent?.Invoke(item);
}
///
/// Register handler to be called when the Event triggers.
///
/// The handler.
public void Register(Action del)
{
OnEvent += del;
}
///
/// Unregister handler that was registered using the `Register` method.
///
/// The handler.
public void Unregister(Action del)
{
OnEvent -= del;
}
///
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
///
/// The Listenr to register.
public void RegisterListener(IAtomListener listener)
{
OnEvent += listener.OnEventRaised;
}
///
/// Unregister a listener that was registered using the `RegisterListener` method.
///
/// The Listenr to unregister.
public void UnregisterListener(IAtomListener listener)
{
OnEvent -= listener.OnEventRaised;
}
#region Observable
///
/// Turn the Event into an `IObservable<T>`. Makes Events compatible with for example UniRx.
///
/// The Event as an `IObservable<T>`.
public IObservable Observe()
{
return new ObservableEvent(Register, Unregister);
}
#endregion // Observable
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
{
foreach (var d in OnEvent.GetInvocationList())
{
OnEvent -= (Action)d;
}
}
}
}
///
/// Generic base class for Events. Inherits from `AtomEvent`.
///
/// The first type for this Event.
/// The second type for this Event.
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEvent : AtomEvent
{
///
/// Actual event.
///
public event Action OnEvent;
///
/// Raise the Event.
///
/// The first value associated with the Event.
/// The second value associated with the Event.
public void Raise(T1 item1, T2 item2)
{
base.RaiseNoValue();
OnEvent?.Invoke(item1, item2);
}
///
/// Register handler to be called when the Event triggers.
///
/// The handler.
public void Register(Action del)
{
OnEvent += del;
}
///
/// Unregister handler that was registered using the `Register` method.
///
/// The handler.
public void Unregister(Action del)
{
OnEvent -= del;
}
///
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
///
/// The Listenr to register.
public void RegisterListener(IAtomListener listener)
{
OnEvent += listener.OnEventRaised;
}
///
/// Unregister a listener that was registered using the `RegisterListener` method.
///
/// The Listenr to unregister.
public void UnregisterListener(IAtomListener listener)
{
OnEvent -= listener.OnEventRaised;
}
#region Observable
///
/// Turn the Event into an `IObservable<M>`. Makes Events compatible with for example UniRx.
///
/// 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>`
/// The result selector type.
/// The Event as an `IObservable<M>`.
public IObservable Observe(Func resultSelector)
{
return new ObservableEvent(Register, Unregister, resultSelector);
}
#endregion // Observable
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
foreach (var d in OnEvent.GetInvocationList())
{
OnEvent -= (Action)d;
}
}
}
}