using System; using UnityEngine; namespace UnityAtoms { /// /// An Event Reference lets you define an event in your script where you then from the inspector can choose if it's going to use the Event from an Event, Variable or a Variable Instancer. /// /// The type of the event. /// Variable of type T. /// Event of type T. /// Event x 2 of type T. /// Function of type T => T. /// Variable Instancer of type T. public abstract class AtomEventReference : AtomEventReferenceBase where V : AtomVariable where E1 : AtomEvent where E2 : AtomEvent where F : AtomFunction where VI : AtomVariableInstancer { /// /// Get or set the event for the Event Reference. /// /// The event of type `E1`. public E1 Event { get { switch (_usage) { case (AtomEventReferenceBase.Usage.Variable): return _variable.Changed; case (AtomEventReferenceBase.Usage.VariableInstancer): return _variableInstancer.Variable.Changed; case (AtomEventReferenceBase.Usage.Event): default: return _event; } } set { switch (_usage) { case (AtomEventReferenceBase.Usage.Variable): { _variable.Changed = value; break; } case (AtomEventReferenceBase.Usage.VariableInstancer): { _variableInstancer.Variable.Changed = value; break; } case (AtomEventReferenceBase.Usage.Event): { _event = value; break; } default: throw new NotSupportedException($"Event not reassignable for usage {_usage}."); } } } /// /// Event used if `Usage` is set to `Event`. /// [SerializeField] private E1 _event; /// /// Variable used if `Usage` is set to `Variable`. /// [SerializeField] private V _variable; /// /// Variable Instancer used if `Usage` is set to `VariableInstancer`. /// [SerializeField] private VI _variableInstancer; protected AtomEventReference() { _usage = AtomEventReferenceBase.Usage.Event; } public static implicit operator E1(AtomEventReference reference) { return reference.Event; } } }