2019-04-11 07:52:13 -04:00
using System ;
2020-02-23 15:44:04 -05:00
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")]
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
public abstract class AtomEvent < T > : AtomEventBase
2018-10-30 15:05:06 -04:00
{
2020-02-23 15:44:04 -05:00
[SerializeField]
private 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>
2020-02-23 15:44:04 -05:00
/// Used when raising values from the inspector for debugging purposes.
2019-10-15 14:44:25 -04:00
/// </summary>
2020-02-23 15:44:04 -05:00
[SerializeField]
[Tooltip("Value that will be used when using the Raise button in the editor inspector.")]
private T _raiseValue ;
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 ( ) ;
2020-02-23 15:44:04 -05:00
_onEvent ? . Invoke ( item ) ;
AddToReplayBuffer ( 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>
2020-02-23 15:44:04 -05:00
/// <param name="action">The handler.</param>
public void Register ( Action < T > action )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
_onEvent + = action ;
ReplayBuffer ( action ) ;
2019-04-16 16:32:17 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
2020-02-23 15:44:04 -05:00
/// <param name="action">The handler.</param>
public void Unregister ( Action < T > action )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
_onEvent - = action ;
2019-04-16 16:32:17 -04:00
}
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>
2019-11-26 14:12:54 -05:00
/// <param name="listener">The Listener 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
{
2020-02-23 15:44:04 -05:00
_onEvent + = listener . OnEventRaised ;
ReplayBuffer ( 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>
2019-11-26 14:12:54 -05:00
/// <param name="listener">The Listener 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
{
2020-02-23 15:44:04 -05: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
2020-02-23 15:44:04 -05:00
private void AddToReplayBuffer ( T item )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
if ( _replayBufferSize > 0 )
{
while ( _replayBuffer . Count > = _replayBufferSize ) { _replayBuffer . Dequeue ( ) ; }
_replayBuffer . Enqueue ( item ) ;
}
}
private void ReplayBuffer ( Action < T > action )
{
if ( _replayBufferSize > 0 & & _replayBuffer . Count > 0 )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
var enumerator = _replayBuffer . GetEnumerator ( ) ;
try
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
while ( enumerator . MoveNext ( ) )
{
action ( enumerator . Current ) ;
}
}
finally
{
enumerator . Dispose ( ) ;
2019-04-16 16:32:17 -04:00
}
}
}
2018-10-30 15:05:06 -04:00
}
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="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")]
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
public abstract class AtomEvent < T1 , T2 > : AtomEventBase
2018-10-30 15:05:06 -04:00
{
2020-02-23 15:44:04 -05:00
[SerializeField]
private event Action < T1 , T2 > _onEvent ;
2019-10-15 14:44:25 -04:00
/// <summary>
2020-02-23 15:44:04 -05:00
/// The event replays the specified number of old values to new subscribers. Works like a ReplaySubject in Rx.
2019-10-15 14:44:25 -04:00
/// </summary>
2020-02-23 15:44:04 -05:00
[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 < ( T1 , T2 ) > _replayBuffer = new Queue < ( T1 , T2 ) > ( ) ;
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
/// </summary>
[SerializeField]
[Tooltip("First value that will be used when using the Raise button in the editor inspector.")]
private T1 _raiseValue1 ;
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
/// </summary>
[SerializeField]
[Tooltip("Second value that will be used when using the Raise button in the editor inspector.")]
private T2 _raiseValue2 ;
private void OnDisable ( )
{
// Clear all delegates when exiting play mode
if ( _onEvent ! = null )
{
var invocationList = _onEvent . GetInvocationList ( ) ;
foreach ( var d in invocationList )
{
_onEvent - = ( Action < T1 , T2 > ) d ;
}
}
}
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 ( ) ;
2020-02-23 15:44:04 -05:00
_onEvent ? . Invoke ( item1 , item2 ) ;
AddToReplayBuffer ( 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>
2020-02-23 15:44:04 -05:00
/// <param name="action">The handler.</param>
public void Register ( Action < T1 , T2 > action )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
_onEvent + = action ;
ReplayBuffer ( action ) ;
2019-04-16 16:32:17 -04:00
}
2019-10-15 14:44:25 -04:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
2020-02-23 15:44:04 -05:00
/// <param name="action">The handler.</param>
public void Unregister ( Action < T1 , T2 > action )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
_onEvent - = action ;
2019-04-16 16:32:17 -04:00
}
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>
2019-11-26 14:12:54 -05:00
/// <param name="listener">The Listener 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
{
2020-02-23 15:44:04 -05:00
_onEvent + = listener . OnEventRaised ;
ReplayBuffer ( 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>
2019-11-26 14:12:54 -05:00
/// <param name="listener">The Listener 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
{
2020-02-23 15:44:04 -05: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
2020-02-23 15:44:04 -05:00
private void AddToReplayBuffer ( T1 item1 , T2 item2 )
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
if ( _replayBufferSize > 0 )
{
while ( _replayBuffer . Count > = _replayBufferSize ) { _replayBuffer . Dequeue ( ) ; }
_replayBuffer . Enqueue ( ( item1 , item2 ) ) ;
}
}
private void ReplayBuffer ( Action < T1 , T2 > action )
{
if ( _replayBufferSize > 0 & & _replayBuffer . Count > 0 )
{
var enumerator = _replayBuffer . GetEnumerator ( ) ;
try
2019-04-16 16:32:17 -04:00
{
2020-02-23 15:44:04 -05:00
while ( enumerator . MoveNext ( ) )
{
action ( enumerator . Current . Item1 , enumerator . Current . Item2 ) ;
}
2019-04-16 16:32:17 -04:00
}
2020-02-23 15:44:04 -05:00
finally
{
enumerator . Dispose ( ) ;
}
}
2019-04-16 16:32:17 -04:00
}
2018-10-30 15:05:06 -04:00
}
2019-03-17 18:43:20 -04:00
}