2019-04-16 16:32:17 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-10-30 15:05:06 -04:00
|
|
|
using UnityEngine;
|
2019-04-07 10:03:16 -04:00
|
|
|
using UnityEngine.Serialization;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-10-14 10:51:54 -04:00
|
|
|
[EditorIcon("atom-icon-lush")]
|
2019-09-30 16:55:33 -04:00
|
|
|
public abstract class AtomVariable<T, E1, E2> : AtomBaseVariable<T>,
|
2019-10-14 20:16:11 -04:00
|
|
|
ISerializationCallbackReceiver
|
2019-09-25 15:05:06 -04:00
|
|
|
where E1 : AtomEvent<T>
|
|
|
|
where E2 : AtomEvent<T, T>
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-08 13:46:02 -04:00
|
|
|
public override T Value { get { return _value; } set { SetValue(value); } }
|
|
|
|
|
|
|
|
[SerializeField]
|
2019-10-03 15:34:57 -04:00
|
|
|
private T _initialValue = default(T);
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
public T OldValue { get { return _oldValue; } }
|
2018-10-30 15:05:06 -04:00
|
|
|
|
2019-04-07 10:03:16 -04:00
|
|
|
[FormerlySerializedAs("oldValue")]
|
2018-10-30 15:05:06 -04:00
|
|
|
[SerializeField]
|
2019-04-07 10:03:16 -04:00
|
|
|
private T _oldValue;
|
2018-10-30 15:05:06 -04:00
|
|
|
|
|
|
|
public E1 Changed;
|
|
|
|
|
|
|
|
public E2 ChangedWithHistory;
|
|
|
|
|
|
|
|
protected abstract bool AreEqual(T first, T second);
|
|
|
|
|
2019-03-17 18:43:20 -04:00
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
if (Changed == null) return;
|
|
|
|
Changed.Raise(Value);
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:39:22 -04:00
|
|
|
public override sealed void Reset(bool shouldTriggerEvents = false)
|
2019-09-29 18:26:53 -04:00
|
|
|
{
|
2019-09-30 16:55:33 -04:00
|
|
|
if (!shouldTriggerEvents)
|
|
|
|
{
|
|
|
|
_oldValue = _value;
|
|
|
|
_value = _initialValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetValue(_initialValue);
|
|
|
|
}
|
2019-09-29 18:26:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-07 05:52:07 -04:00
|
|
|
public bool SetValue(T newValue)
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-08 13:46:02 -04:00
|
|
|
if (!AreEqual(_value, newValue))
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-08 13:46:02 -04:00
|
|
|
_oldValue = _value;
|
|
|
|
_value = newValue;
|
|
|
|
if (Changed != null) { Changed.Raise(_value); }
|
|
|
|
if (ChangedWithHistory != null) { ChangedWithHistory.Raise(_value, _oldValue); }
|
2019-04-05 07:40:26 -04:00
|
|
|
return true;
|
2018-10-30 15:05:06 -04:00
|
|
|
}
|
|
|
|
|
2019-04-05 07:40:26 -04:00
|
|
|
return false;
|
2018-10-30 15:05:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-25 15:05:06 -04:00
|
|
|
public bool SetValue(AtomVariable<T, E1, E2> variable)
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
|
|
|
return SetValue(variable.Value);
|
|
|
|
}
|
2019-04-08 13:46:02 -04:00
|
|
|
|
|
|
|
public void OnBeforeSerialize() { }
|
|
|
|
public void OnAfterDeserialize() { _value = _initialValue; }
|
2019-04-16 16:32:17 -04:00
|
|
|
|
|
|
|
#region Observable
|
|
|
|
public IObservable<T> ObserveChange()
|
|
|
|
{
|
|
|
|
if (Changed == null)
|
|
|
|
{
|
|
|
|
throw new Exception("You must assign a Changed event in order to observe variable changes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ObservableEvent<T>(Changed.Register, Changed.Unregister);
|
|
|
|
}
|
2019-04-21 07:51:20 -04:00
|
|
|
|
|
|
|
public IObservable<ValueTuple<T, T>> ObserveChangeWithHistory()
|
|
|
|
{
|
|
|
|
if (ChangedWithHistory == null)
|
|
|
|
{
|
|
|
|
throw new Exception("You must assign a ChangedWithHistory event in order to observe variable changes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ObservableEvent<T, T, ValueTuple<T, T>>(
|
|
|
|
register: ChangedWithHistory.Register,
|
|
|
|
unregister: ChangedWithHistory.Unregister,
|
|
|
|
createCombinedModel: (n, o) => new ValueTuple<T, T>(n, o)
|
|
|
|
);
|
|
|
|
}
|
2019-04-16 16:32:17 -04:00
|
|
|
#endregion // Observable
|
2018-10-30 15:05:06 -04:00
|
|
|
}
|
2019-04-05 07:40:26 -04:00
|
|
|
}
|