using System; using UnityEngine; using UnityAtoms; namespace UnityAtoms.BaseAtoms { /// /// A List of Atom Variables (AtomBaseVariable). /// [CreateAssetMenu(menuName = "Unity Atoms/Collections/List", fileName = "List")] [EditorIcon("atom-icon-lime")] public class AtomList : AtomBaseVariable, IGetValue, IWithCollectionEvents { /// /// Get value as an `IAtomList`. Needed in order to inject Lists into the Variable Instancer class. /// /// The value as an `IAtomList`. public IAtomList GetValue() => this.Value; /// /// Event for when and item is added to the list. /// public AtomBaseVariableEvent Added { get => _added; set => _added = value; } /// /// Event for when and item is removed from the list. /// public AtomBaseVariableEvent Removed { get => _removed; set => _removed = value; } /// /// Event for when the list is cleared. /// public VoidEvent Cleared { get => _cleared; set => _cleared = value; } [SerializeField] private AtomBaseVariableEvent _added; [SerializeField] private AtomBaseVariableEvent _removed; [SerializeField] private VoidEvent _cleared; void OnEnable() { if (Value == null) return; Value.Added += PropogateAdded; Value.Removed += PropogateRemoved; Value.Cleared += PropogateCleared; } void OnDisable() { if (Value == null) return; Value.Added -= PropogateAdded; Value.Removed -= PropogateRemoved; Value.Cleared -= PropogateCleared; } #region Observable /// /// Make the add event into an `IObservable<T>`. Makes List's add Event compatible with for example UniRx. /// /// The add Event as an `IObservable<T>`. public IObservable ObserveAdd() { if (Added == null) { throw new Exception("You must assign an Added event in order to observe when adding to the list."); } return new ObservableEvent(Added.Register, Added.Unregister); } /// /// Make the remove event into an `IObservable<T>`. Makes List's remove Event compatible with for example UniRx. /// /// The remove Event as an `IObservable<T>`. public IObservable ObserveRemove() { if (Removed == null) { throw new Exception("You must assign a Removed event in order to observe when removing from the list."); } return new ObservableEvent(Removed.Register, Removed.Unregister); } /// /// Make the clear event into an `IObservable<Void>`. Makes List's clear Event compatible with for example UniRx. /// /// The clear Event as an `IObservable<Void>`. public IObservable ObserveClear() { if (Cleared == null) { throw new Exception("You must assign a Cleared event in order to observe when clearing the list."); } return new ObservableVoidEvent(Cleared.Register, Cleared.Unregister); } #endregion // Observable void PropogateAdded(AtomBaseVariable baseVariable) { if (_added == null) return; _added.Raise(baseVariable); } void PropogateRemoved(AtomBaseVariable baseVariable) { if (_removed == null) return; _removed.Raise(baseVariable); } void PropogateCleared() { if (_cleared == null) return; _cleared.Raise(); } } }