namespace Zinnia.Event.Proxy
{
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Extension;
///
/// Emits a UnityEvent with a single payload whenever the Receive method is called.
///
/// The value for Receive,
/// The event type to emit.
public abstract class SingleEventProxyEmitter : EventProxyEmitter where TEvent : UnityEvent, new()
{
[Tooltip("The payload data to emit.")]
[SerializeField]
private TValue payload;
///
/// The payload data to emit.
///
public TValue Payload
{
get
{
return payload;
}
set
{
payload = value;
}
}
///
/// Is emitted when Receive is called.
///
public TEvent Emitted = new TEvent();
///
/// Attempts to emit the Emitted event with the given payload.
///
///
public virtual void Receive(TValue payload)
{
if (!this.IsValidState())
{
return;
}
TValue previousPayloadValue = Payload;
Payload = payload;
if (!IsValid())
{
Payload = previousPayloadValue;
return;
}
EmitPayload();
}
///
/// Emits the last received payload.
///
public virtual void EmitPayload()
{
if (!this.IsValidState() || !IsValid())
{
return;
}
Emitted?.Invoke(Payload);
}
///
/// Clears the to the default value.
///
public virtual void ClearPayload()
{
Payload = default;
}
}
}