using UnityEngine;
using UnityEngine.Serialization;
namespace UnityAtoms.MonoHooks
{
///
/// Generic base class for all Mono Hooks.
///
/// Event of type `AtomEvent<EV>`
/// Event value type
/// Function type `AtomFunction<GameObject, GameObject>`
[EditorIcon("atom-icon-delicate")]
public abstract class MonoHook : MonoBehaviour
where E : AtomEvent
where ER : IGetEvent, ISetEvent
where F : AtomFunction
{
///
/// The Event
///
public E Event { get => _eventReference.GetEvent(); set => _eventReference.SetEvent(value); }
[SerializeField]
private ER _eventReference = default(ER);
///
/// Selector function for the Event `EventWithGameObjectReference`. Makes it possible to for example select the parent GameObject and pass that a long to the `EventWithGameObjectReference`.
///
[SerializeField]
protected F _selectGameObjectReference = default(F);
protected void OnHook(EV value)
{
if (Event != null)
{
Event.Raise(value);
}
RaiseWithGameObject(value, _selectGameObjectReference != null ? _selectGameObjectReference.Call(gameObject) : gameObject);
}
protected abstract void RaiseWithGameObject(EV value, GameObject gameObject);
}
}