2018-10-30 15:05:06 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
|
|
|
public abstract class ScriptableObjectVariable<T, E1, E2> : ScriptableObjectVariableBase<T>, IWithOldValue<T> where E1 : GameEvent<T> where E2 : GameEvent<T, T>
|
|
|
|
{
|
|
|
|
public override T Value { get { return value; } set { SetValue(value); } }
|
|
|
|
|
|
|
|
public T OldValue { get { return oldValue; } }
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private T oldValue;
|
|
|
|
|
|
|
|
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-04-07 05:52:07 -04:00
|
|
|
public bool SetValue(T newValue)
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-07 05:52:07 -04:00
|
|
|
if (!AreEqual(value, newValue))
|
2018-10-30 15:05:06 -04:00
|
|
|
{
|
2019-04-07 05:52:07 -04:00
|
|
|
oldValue = value;
|
|
|
|
value = newValue;
|
|
|
|
if (Changed != null) { Changed.Raise(newValue); }
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public bool SetValue(ScriptableObjectVariable<T, E1, E2> variable)
|
|
|
|
{
|
|
|
|
return SetValue(variable.Value);
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 07:40:26 -04:00
|
|
|
}
|