unity-atoms/Packages/Core/Runtime/References/AtomReference.cs
Adam Ramberg 8a6b8a97a6
Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110)
AtomVariableInstancer
- Added AtomVariableInstancer as an option to AtomReference.
- Added AtomVariableInstancer to generator.
- Added editor icon for AtomVariableInstancer.

AtomEventReference
- Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above).
- Added AtomEventReference and AtomEventX2Reference to generator.
- Added a drawer for AtomEventReference.
- Listeners are now using AtomEventReference instead of AtomEvent.
- Refactoring of VoidHooks since Listeners are now using AtomEventReference.

AtomCollection
- Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences).
- Added new editor icon for collections.
- Created a SerializableDictionary class, which AtomCollection is using.
- Custom property drawer for SerializableDictionary.
- SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection.
- AtomCollections have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed.

AtomList
- Renamed old AtomList to AtomValueList
- Added AtomList, like Collection, but a list
- Added new icon for AtomList
- Created a AtomBaseVariableList class, which AtomList is using.
- Custom property drawer for AtomBaseVariableList.
- AtomLists have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-23 02:39:43 +01:00

124 lines
4.2 KiB
C#

using System;
using UnityEngine;
namespace UnityAtoms
{
/// <summary>
/// A Reference lets you define a variable in your script where you then from the inspector can choose if it's going to be taking the value from a Constant, Variable, Value or a Variable Instancer.
/// </summary>
/// <typeparam name="T">The type of the variable.</typeparam>
/// <typeparam name="C">Constant of type T.</typeparam>
/// <typeparam name="V">Variable of type T.</typeparam>
/// <typeparam name="E1">Event of type T.</typeparam>
/// <typeparam name="E2">Event x 2 of type T.</typeparam>
/// <typeparam name="F">Function of type T => T.</typeparam>
/// <typeparam name="VI">Variable Instancer of type T.</typeparam>
public abstract class AtomReference<T, C, V, E1, E2, F, VI> : AtomReferenceBase, IEquatable<AtomReference<T, C, V, E1, E2, F, VI>>
where C : AtomBaseVariable<T>
where V : AtomVariable<T, E1, E2, F>
where E1 : AtomEvent<T>
where E2 : AtomEvent<T, T>
where F : AtomFunction<T, T>
where VI : AtomVariableInstancer<V, T, E1, E2, F>
{
/// <summary>
/// Get or set the value for the Reference.
/// </summary>
/// <value>The value of type `T`.</value>
public T Value
{
get
{
switch (_usage)
{
case (AtomReferenceBase.Usage.Constant): return _constant == null ? default(T) : _constant.Value;
case (AtomReferenceBase.Usage.Variable): return _variable == null ? default(T) : _variable.Value;
case (AtomReferenceBase.Usage.VariableInstancer): return _variableInstancer == null ? default(T) : _variableInstancer.Value;
case (AtomReferenceBase.Usage.Value):
default:
return _value;
}
}
set
{
switch (_usage)
{
case (AtomReferenceBase.Usage.Variable):
{
_variable.Value = value;
break;
}
case (AtomReferenceBase.Usage.Value):
{
_value = value;
break;
}
case (AtomReferenceBase.Usage.VariableInstancer):
{
_variableInstancer.Value = value;
break;
}
case (AtomReferenceBase.Usage.Constant):
default:
throw new NotSupportedException("Can't reassign constant value");
}
}
}
/// <summary>
/// Value used if `Usage` is set to `Value`.
/// </summary>
[SerializeField]
private T _value;
/// <summary>
/// Constant used if `Usage` is set to `Constant`.
/// </summary>
[SerializeField]
private C _constant;
/// <summary>
/// Variable used if `Usage` is set to `Variable`.
/// </summary>
[SerializeField]
private V _variable;
/// <summary>
/// Variable Instancer used if `Usage` is set to `VariableInstancer`.
/// </summary>
[SerializeField]
private VI _variableInstancer;
protected AtomReference()
{
_usage = AtomReferenceBase.Usage.Value;
}
protected AtomReference(T value) : this()
{
_usage = AtomReferenceBase.Usage.Value;
_value = value;
}
public static implicit operator T(AtomReference<T, C, V, E1, E2, F, VI> reference)
{
return reference.Value;
}
protected abstract bool ValueEquals(T other);
public bool Equals(AtomReference<T, C, V, E1, E2, F, VI> other)
{
if (other == null)
return false;
return ValueEquals(other.Value);
}
public override int GetHashCode()
{
return Value == null ? 0 : Value.GetHashCode();
}
}
}