unity-atoms/Packages/Core/Runtime/Events/AtomEvent.cs

164 lines
5.3 KiB
C#
Raw Normal View History

2019-04-11 07:52:13 -04:00
using System;
2020-03-08 08:24:46 -04:00
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
2018-10-30 15:05:06 -04:00
namespace UnityAtoms
{
2019-10-15 14:44:25 -04:00
/// <summary>
Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110) AtomVariableInstancer - Added AtomVariableInstancer as an option to AtomReference. - Added AtomVariableInstancer to generator. - Added editor icon for AtomVariableInstancer. AtomEventReference - Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above). - Added AtomEventReference and AtomEventX2Reference to generator. - Added a drawer for AtomEventReference. - Listeners are now using AtomEventReference instead of AtomEvent. - Refactoring of VoidHooks since Listeners are now using AtomEventReference. AtomCollection - Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences). - Added new editor icon for collections. - Created a SerializableDictionary class, which AtomCollection is using. - Custom property drawer for SerializableDictionary. - SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection. - AtomCollections have 3 events: Added, Removed, Cleared. - Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed. AtomList - Renamed old AtomList to AtomValueList - Added AtomList, like Collection, but a list - Added new icon for AtomList - Created a AtomBaseVariableList class, which AtomList is using. - Custom property drawer for AtomBaseVariableList. - AtomLists have 3 events: Added, Removed, Cleared. - Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-22 20:39:43 -05:00
/// Generic base class for Events. Inherits from `AtomEventBase`.
2019-10-15 14:44:25 -04:00
/// </summary>
/// <typeparam name="T">The type for this Event.</typeparam>
2019-10-14 10:51:54 -04:00
[EditorIcon("atom-icon-cherry")]
2020-03-04 18:48:39 -05:00
public class AtomEvent<T> : AtomEventBase
2018-10-30 15:05:06 -04:00
{
2020-03-01 15:32:52 -05:00
public T InspectorRaiseValue { get => _inspectorRaiseValue; }
2020-03-08 08:24:46 -04:00
/// <summary>
/// Retrieve Replay Buffer as a List. This call will allocate memory so use sparsely.
/// </summary>
/// <returns></returns>
public List<T> ReplayBuffer { get => _replayBuffer.ToList(); }
2020-03-11 16:42:59 -04:00
public int ReplayBufferSize { get => _replayBufferSize; set => _replayBufferSize = value; }
[SerializeField]
2020-03-08 19:37:52 -04:00
protected event Action<T> _onEvent;
/// <summary>
/// The event replays the specified number of old values to new subscribers. Works like a ReplaySubject in Rx.
/// </summary>
[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<T> _replayBuffer = new Queue<T>();
private void OnDisable()
{
// Clear all delegates when exiting play mode
if (_onEvent != null)
{
var invocationList = _onEvent.GetInvocationList();
foreach (var d in invocationList)
{
_onEvent -= (Action<T>)d;
}
}
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
2019-10-15 14:44:25 -04:00
/// </summary>
[SerializeField]
[Tooltip("Value that will be used when using the Raise button in the editor inspector.")]
2020-03-02 14:27:38 -05:00
private T _inspectorRaiseValue = default(T);
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)
{
2020-03-04 18:48:39 -05:00
base.Raise();
_onEvent?.Invoke(item);
AddToReplayBuffer(item);
2018-10-30 15:05:06 -04:00
}
2020-03-11 20:30:25 -04:00
/// <summary>
/// Used in editor scipts since Raise is ambigious when using reflection to get method.
/// </summary>
/// <param name="item"></param>
public void RaiseEditor(T item) => Raise(item);
2019-10-15 14:44:25 -04:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="action">The handler.</param>
public void Register(Action<T> action)
{
_onEvent += action;
2020-03-08 08:24:46 -04:00
ReplayBufferToSubscriber(action);
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="action">The handler.</param>
public void Unregister(Action<T> action)
{
_onEvent -= action;
}
2020-03-09 18:51:14 -04:00
/// <summary>
/// Unregister all handlers that were registered using the `Register` method.
/// </summary>
public void UnregisterAll()
{
_onEvent = null;
}
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 Listener to register.</param>
2020-03-15 19:11:18 -04:00
public void RegisterListener(IAtomListener<T> listener, bool replayEventsBuffer = true)
2018-10-30 15:05:06 -04:00
{
_onEvent += listener.OnEventRaised;
2020-03-15 19:11:18 -04:00
if (replayEventsBuffer)
{
ReplayBufferToSubscriber(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 Listener to unregister.</param>
public void UnregisterListener(IAtomListener<T> listener)
2018-10-30 15:05:06 -04:00
{
_onEvent -= listener.OnEventRaised;
2018-10-30 15:05:06 -04:00
}
#region Observable
2019-10-15 14:44:25 -04:00
/// <summary>
/// Turn the Event into an `IObservable&lt;T&gt;`. Makes Events compatible with for example UniRx.
/// </summary>
/// <returns>The Event as an `IObservable&lt;T&gt;`.</returns>
public IObservable<T> Observe()
{
return new ObservableEvent<T>(Register, Unregister);
}
#endregion // Observable
2020-03-08 19:37:52 -04:00
protected void AddToReplayBuffer(T item)
{
if (_replayBufferSize > 0)
{
while (_replayBuffer.Count >= _replayBufferSize) { _replayBuffer.Dequeue(); }
_replayBuffer.Enqueue(item);
}
}
2020-03-08 08:24:46 -04:00
private void ReplayBufferToSubscriber(Action<T> action)
{
if (_replayBufferSize > 0 && _replayBuffer.Count > 0)
{
var enumerator = _replayBuffer.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
action(enumerator.Current);
}
}
finally
{
enumerator.Dispose();
}
}
}
2018-10-30 15:05:06 -04:00
}
Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 18:43:20 -04:00
}