2018-10-30 20:05:06 +01:00
|
|
|
using UnityEngine;
|
2019-04-07 16:03:16 +02:00
|
|
|
using UnityEngine.Serialization;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
public abstract class ScriptableObjectVariable<T, E1, E2> : ScriptableObjectVariableBase<T>,
|
|
|
|
IWithOldValue<T>
|
|
|
|
where E1 : GameEvent<T>
|
|
|
|
where E2 : GameEvent<T, T>
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
public override T Value { get { return _value; } set { SetValue(value); } }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
public T OldValue { get { return _oldValue; } }
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("oldValue")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private T _oldValue;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
public E1 Changed;
|
|
|
|
|
|
|
|
public E2 ChangedWithHistory;
|
|
|
|
|
|
|
|
protected abstract bool AreEqual(T first, T second);
|
|
|
|
|
2019-03-17 23:43:20 +01:00
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
if (Changed == null) return;
|
|
|
|
Changed.Raise(Value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 11:52:07 +02:00
|
|
|
public bool SetValue(T newValue)
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (!AreEqual(_value, newValue))
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_oldValue = _value;
|
|
|
|
_value = newValue;
|
2019-04-07 11:52:07 +02:00
|
|
|
if (Changed != null) { Changed.Raise(newValue); }
|
2019-04-07 16:03:16 +02:00
|
|
|
if (ChangedWithHistory != null) { ChangedWithHistory.Raise(_value, _oldValue); }
|
2019-04-05 13:40:26 +02:00
|
|
|
return true;
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
|
2019-04-05 13:40:26 +02:00
|
|
|
return false;
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool SetValue(ScriptableObjectVariable<T, E1, E2> variable)
|
|
|
|
{
|
|
|
|
return SetValue(variable.Value);
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 13:40:26 +02:00
|
|
|
}
|