2019-11-09 23:08:40 +01:00
|
|
|
using System;
|
2019-10-16 18:02:08 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
2018-10-30 20:05:06 +01:00
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-10-15 22:02:44 +02:00
|
|
|
/// <summary>
|
|
|
|
/// None generic base class for `AtomReference<T, V>`.
|
|
|
|
/// </summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
public abstract class AtomReference
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Enum for how to use the Reference.
|
|
|
|
/// </summary>
|
|
|
|
public enum Usage
|
|
|
|
{
|
|
|
|
Value = 0,
|
|
|
|
Constant = 1,
|
|
|
|
Variable = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Should we use the provided value (via inspector), the Constant value or the Variable value?
|
|
|
|
/// </summary>
|
|
|
|
[SerializeField]
|
|
|
|
protected Usage _usage;
|
|
|
|
}
|
2019-04-17 13:05:23 +02:00
|
|
|
|
2019-10-16 18:02:08 +02:00
|
|
|
public abstract class AtomReference<T, V, C> : AtomReference
|
2019-09-30 22:55:33 +02:00
|
|
|
where V : AtomBaseVariable<T>
|
2019-10-16 18:02:08 +02:00
|
|
|
where C : AtomBaseVariable<T>
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-10-15 22:02:44 +02:00
|
|
|
/// <summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
/// Value used if `Usage` is set to `Value`.
|
2019-10-15 22:02:44 +02:00
|
|
|
/// </summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
[SerializeField]
|
|
|
|
private T _value;
|
2019-04-07 16:03:16 +02:00
|
|
|
|
2019-10-15 22:02:44 +02:00
|
|
|
/// <summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
/// Constant used if `Usage` is set to `Constant`.
|
2019-10-15 22:02:44 +02:00
|
|
|
/// </summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
public C _constant;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-10-15 22:02:44 +02:00
|
|
|
/// <summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
/// Variable used if `Usage` is set to `Variable`.
|
2019-10-15 22:02:44 +02:00
|
|
|
/// </summary>
|
2019-10-16 18:02:08 +02:00
|
|
|
public V _variable;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-09-25 21:05:06 +02:00
|
|
|
protected AtomReference()
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-10-16 18:02:08 +02:00
|
|
|
_usage = AtomReference.Usage.Value;
|
2019-04-07 16:03:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:05:06 +02:00
|
|
|
protected AtomReference(T value) : this()
|
2019-04-07 16:03:16 +02:00
|
|
|
{
|
2019-10-16 18:02:08 +02:00
|
|
|
_usage = AtomReference.Usage.Value;
|
|
|
|
_value = value;
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-15 22:02:44 +02:00
|
|
|
/// <summary>
|
2019-11-09 23:08:40 +01:00
|
|
|
/// Get or set the value for the Reference.
|
2019-10-15 22:02:44 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The value of type `T`.</value>
|
2018-10-30 20:05:06 +01:00
|
|
|
public T Value
|
|
|
|
{
|
2019-10-16 18:02:08 +02:00
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (_usage)
|
|
|
|
{
|
|
|
|
case (AtomReference.Usage.Constant): return _constant.Value;
|
|
|
|
case (AtomReference.Usage.Variable): return _variable.Value;
|
|
|
|
case (AtomReference.Usage.Value):
|
|
|
|
default:
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 23:08:40 +01:00
|
|
|
set
|
|
|
|
{
|
|
|
|
switch (_usage)
|
|
|
|
{
|
|
|
|
case (AtomReference.Usage.Variable):
|
|
|
|
{
|
|
|
|
_variable.Value = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (AtomReference.Usage.Value):
|
|
|
|
{
|
|
|
|
_value = value;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-11 21:01:10 +01:00
|
|
|
case (AtomReference.Usage.Constant):
|
|
|
|
default:
|
|
|
|
throw new NotSupportedException("Can't reassign constant value");
|
2019-11-09 23:08:40 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:02:08 +02:00
|
|
|
public static implicit operator T(AtomReference<T, V, C> reference)
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
|
|
|
return reference.Value;
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 11:10:09 +02:00
|
|
|
}
|