using System; using System.Collections.Generic; using UnityEngine; namespace UnityAtoms { /// /// Generic base class for Variables. Inherits from `AtomBaseVariable<T>`. /// /// The Variable value type. /// IPair of type `T`. /// Event of type `AtomEvent<T>`. /// Event of type `AtomEvent<T, T>`. /// Function of type `FunctionEvent<T, T>`. [EditorIcon("atom-icon-lush")] public abstract class AtomVariable : AtomBaseVariable, IGetEvent, ISetEvent where P : struct, IPair where E1 : AtomEvent where E2 : AtomEvent

where F : AtomFunction { ///

/// The Variable value as a property. /// /// Get or set the Variable's value. public override T Value { get { return _value; } set { SetValue(value); } } /// /// The inital value of the Variable. /// [SerializeField] private T _initialValue = default(T); /// /// The inital Variable value as a property. /// /// Get the Variable's initial value. public T InitialValue { get { return _initialValue; } } /// /// The value the Variable had before its value got changed last time. /// /// Get the Variable's old value. public T OldValue { get { return _oldValue; } } [SerializeField] private T _oldValue; /// /// Changed Event triggered when the Variable value gets changed. /// public E1 Changed; /// /// Changed with history Event triggered when the Variable value gets changed. /// public E2 ChangedWithHistory; /// /// When setting the value of a Variable the new value will be piped through all the pre change transformers, which allows you to create custom logic and restriction on for example what values can be set for this Variable. /// /// Get the list of pre change transformers. public List PreChangeTransformers { get => _preChangeTransformers; set { if (value == null) { _preChangeTransformers.Clear(); } else { _preChangeTransformers = value; } } } [SerializeField] private List _preChangeTransformers = new List(); protected abstract bool ValueEquals(T other); private void OnValidate() { _initialValue = RunPreChangeTransformers(_initialValue); _value = RunPreChangeTransformers(_value); } private void OnEnable() { _oldValue = _initialValue; _value = _initialValue; if (Changed == null) return; Changed.Raise(Value); } /// /// Reset the Variable to its `_initalValue`. /// /// Set to `true` if Events should be triggered on reset, otherwise `false`. public override sealed void Reset(bool shouldTriggerEvents = false) { if (!shouldTriggerEvents) { _oldValue = _value; _value = _initialValue; } else { SetValue(_initialValue); } } /// /// Set the Variable value. /// /// The new value to set. /// `true` if the value got changed, otherwise `false`. public bool SetValue(T newValue) { var preProcessedNewValue = RunPreChangeTransformers(newValue); if (!ValueEquals(preProcessedNewValue)) { _oldValue = _value; _value = preProcessedNewValue; if (Changed != null) { Changed.Raise(_value); } if (ChangedWithHistory != null) { // NOTE: Doing new P() here, even though it is cleaner, generates garbage. var pair = default(P); pair.Item1 = _value; pair.Item2 = _oldValue; ChangedWithHistory.Raise(pair); } return true; } return false; } /// /// Set the Variable value. /// /// The value to set provided from another Variable. /// `true` if the value got changed, otherwise `false`. public bool SetValue(AtomVariable variable) { return SetValue(variable.Value); } #region Observable /// /// Turn the Variable's change Event into an `IObservable<T>`. Makes the Variable's change Event compatible with for example UniRx. /// /// The Variable's change Event as an `IObservable<T>`. public IObservable ObserveChange() { if (Changed == null) { throw new Exception("You must assign a Changed event in order to observe variable changes."); } return new ObservableEvent(Changed.Register, Changed.Unregister); } /// /// Turn the Variable's change with history Event into an `IObservable<T, T>`. Makes the Variable's change with history Event compatible with for example UniRx. /// /// The Variable's change Event as an `IObservable<T, T>`. public IObservable

ObserveChangeWithHistory() { if (ChangedWithHistory == null) { throw new Exception("You must assign a ChangedWithHistory event in order to observe variable changes."); } return new ObservableEvent

(ChangedWithHistory.Register, ChangedWithHistory.Unregister); } #endregion // Observable private T RunPreChangeTransformers(T value) { if (_preChangeTransformers.Count <= 0) { return value; } var preProcessedValue = value; for (var i = 0; i < _preChangeTransformers.Count; ++i) { var Transformer = _preChangeTransformers[i]; if (Transformer != null) { preProcessedValue = Transformer.Call(preProcessedValue); } } return preProcessedValue; } ///

/// Get event by type. Don't use directly! Used only so that we don't need two implementations of Event Instancer and Listeners (one for `T` and one for `IPair<T>`) /// /// /// The event. public E GetEvent() where E : AtomEventBase { if (typeof(E) == typeof(E1)) return (Changed as E); if (typeof(E) == typeof(E2)) return (ChangedWithHistory as E); throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}."); } /// /// Set event by type. Don't use directly! Used only so that we don't need two implementations of Event Instancer and Listeners (one for `T` and one for `IPair<T>`) /// /// The new event value. /// public void SetEvent(E e) where E : AtomEventBase { if (typeof(E) == typeof(E1)) { Changed = (e as E1); return; } if (typeof(E) == typeof(E2)) { ChangedWithHistory = (e as E2); return; } throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}."); } } }