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, Event Instancer, Variable or a Variable Instancer.
///
/// The type of the event.
/// Variable of type `T`.
/// Event of type `T`.
/// Variable Instancer of type `T`.
/// Event Instancer of type `T`.
public abstract class AtomEventReference : AtomBaseEventReference, IGetEvent, ISetEvent
where V : IGetEvent, ISetEvent
where E : AtomEvent
where VI : IGetEvent, ISetEvent
where EI : AtomEventInstancer
{
///
/// Get or set the Event used by the Event Reference.
///
/// The event of type `E`.
public override E Event
{
get
{
switch (_usage)
{
case (AtomEventReferenceUsage.VARIABLE): return _variable.GetEvent();
case (AtomEventReferenceUsage.VARIABLE_INSTANCER): return _variableInstancer.GetEvent();
case (AtomEventReferenceUsage.EVENT_INSTANCER): return _eventInstancer.Event;
case (AtomEventReferenceUsage.EVENT):
default:
return _event;
}
}
set
{
switch (_usage)
{
case (AtomEventReferenceUsage.VARIABLE):
{
_variable.SetEvent(value);
break;
}
case (AtomEventReferenceUsage.VARIABLE_INSTANCER):
{
_variableInstancer.SetEvent(value);
break;
}
case (AtomEventReferenceUsage.EVENT):
{
_event = value;
break;
}
default:
throw new NotSupportedException($"Event not reassignable for usage {_usage}.");
}
}
}
///
/// Variable used if `Usage` is set to `Variable`.
///
[SerializeField]
private V _variable = default(V);
///
/// Variable Instancer used if `Usage` is set to `VariableInstancer`.
///
[SerializeField]
private VI _variableInstancer = default(VI);
protected AtomEventReference()
{
_usage = AtomEventReferenceUsage.EVENT;
}
public static implicit operator E(AtomEventReference reference)
{
return reference.Event;
}
}
}