using System;
using System.Linq;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace UnityAtoms
{
///
/// Generic base class for Events. Inherits from `AtomEventBase`.
///
/// The type for this Event.
[EditorIcon("atom-icon-cherry")]
public class AtomEvent : AtomEventBase
{
public T InspectorRaiseValue { get => _inspectorRaiseValue; }
///
/// Retrieve Replay Buffer as a List. This call will allocate memory so use sparsely.
///
///
public List ReplayBuffer { get => _replayBuffer.ToList(); }
public int ReplayBufferSize { get => _replayBufferSize; set => _replayBufferSize = value; }
protected event Action _onEvent;
///
/// The event replays the specified number of old values to new subscribers. Works like a ReplaySubject in Rx.
///
[SerializeField]
[Range(0, 10)]
[Tooltip("The number of old values (between 0-10) being replayed when someone subscribes to this Event.")]
private int _replayBufferSize = 1;
private Queue _replayBuffer = new Queue();
#if UNITY_EDITOR
///
/// Set of all AtomVariable instances in editor.
///
private static HashSet> _instances = new HashSet>();
#endif
private void OnEnable()
{
#if UNITY_EDITOR
if (EditorSettings.enterPlayModeOptionsEnabled)
{
_instances.Add(this);
EditorApplication.playModeStateChanged -= HandlePlayModeStateChange;
EditorApplication.playModeStateChanged += HandlePlayModeStateChange;
}
#endif
}
#if UNITY_EDITOR
private static void HandlePlayModeStateChange(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingEditMode // BEFORE any GO is initialized:
|| state == PlayModeStateChange.EnteredEditMode) // AFTER Playmode stopped
{
foreach (var instance in _instances)
{
instance._replayBuffer.Clear();
instance.UnregisterAll();
}
}
}
#endif
private void OnDisable()
{
// NOTE: This will not be called when deleting the Atom from the editor.
// Therefore, there might still be null instances, but even though not ideal,
// it should not cause any problems.
// More info: https://issuetracker.unity3d.com/issues/ondisable-and-ondestroy-methods-are-not-called-when-a-scriptableobject-is-deleted-manually-in-project-window
#if UNITY_EDITOR
_instances.Remove(this);
#endif
// Clear all delegates when exiting play mode
UnregisterAll();
}
///
/// Used when raising values from the inspector for debugging purposes.
///
[SerializeField]
[Tooltip("Value that will be used when using the Raise button in the editor inspector.")]
private T _inspectorRaiseValue = default(T);
///
/// Raise the Event.
///
/// The value associated with the Event.
public void Raise(T item)
{
#if !UNITY_ATOMS_GENERATE_DOCS && UNITY_EDITOR
StackTraces.AddStackTrace(GetInstanceID(), StackTraceEntry.Create(item));
#endif
base.Raise();
_onEvent?.Invoke(item);
AddToReplayBuffer(item);
}
///
/// Used in editor scipts since Raise is ambigious when using reflection to get method.
///
///
public void RaiseEditor(T item) => Raise(item);
///
/// Register handler to be called when the Event triggers.
///
///
/// Replays the event buffer to the handler.
///
/// The handler.
public void Register(Action action)
{
Register(action, replayEventsBuffer: true);
}
///
/// Register handler to be called when the Event triggers.
///
/// The handler.
/// If this replays the events buffer to the handler.
public void Register(Action action, bool replayEventsBuffer)
{
_onEvent += action;
if (replayEventsBuffer)
{
ReplayBufferToSubscriber(action);
}
}
///
/// Unregister handler that was registered using the `Register` method.
///
/// The handler.
public void Unregister(Action action)
{
_onEvent -= action;
}
///
/// Unregister all handlers that were registered using the `Register` method.
///
public override void UnregisterAll()
{
base.UnregisterAll();
_onEvent = null;
}
///
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
///
/// The Listener to register.
/// If this replays the events buffer to the new listener. Defaults to true.
public void RegisterListener(IAtomListener listener, bool replayEventsBuffer = true)
{
_onEvent += listener.OnEventRaised;
if (replayEventsBuffer)
{
ReplayBufferToSubscriber(listener.OnEventRaised);
}
}
///
/// Unregister a listener that was registered using the `RegisterListener` method.
///
/// The Listener 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
protected void AddToReplayBuffer(T item)
{
if (_replayBufferSize > 0)
{
while (_replayBuffer.Count >= _replayBufferSize) { _replayBuffer.Dequeue(); }
_replayBuffer.Enqueue(item);
}
}
private void ReplayBufferToSubscriber(Action action)
{
if (_replayBufferSize > 0 && _replayBuffer.Count > 0)
{
var enumerator = _replayBuffer.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
action(enumerator.Current);
}
}
finally
{
enumerator.Dispose();
}
}
}
}
}