using System; using System.Collections.Generic; using UnityEngine; namespace UnityAtoms { [UseIcon("atom-icon-cherry")] public abstract class AtomEvent : BaseAtom, ISerializationCallbackReceiver, IAtomEventIcon { public event Action OnEventNoValue; protected void RaiseNoValue() { OnEventNoValue?.Invoke(); } public void Register(Action del) { OnEventNoValue += del; } 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; } } } } [UseIcon("atom-icon-cherry")] public abstract class AtomEvent : AtomEvent { public event Action OnEvent; public void Raise(T item) { base.RaiseNoValue(); OnEvent?.Invoke(item); } public void Register(Action del) { OnEvent += del; } public void Unregister(Action del) { OnEvent -= del; } public void RegisterListener(IAtomListener listener) { OnEvent += listener.OnEventRaised; } public void UnregisterListener(IAtomListener listener) { OnEvent -= listener.OnEventRaised; } #region Observable 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; } } } } [UseIcon("atom-icon-cherry")] public abstract class AtomEvent : AtomEvent { public event Action OnEvent; public void Raise(T1 item1, T2 item2) { base.RaiseNoValue(); OnEvent?.Invoke(item1, item2); } public void Register(Action del) { OnEvent += del; } public void Unregister(Action del) { OnEvent -= del; } public void RegisterListener(IAtomListener listener) { OnEvent += listener.OnEventRaised; } public void UnregisterListener(IAtomListener listener) { OnEvent -= listener.OnEventRaised; } #region Observable public IObservable Observe(Func createCombinedModel) { return new ObservableEvent(Register, Unregister, createCombinedModel); } #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; } } } }