2019-10-15 19:43:51 -04:00
using UnityEngine ;
using UnityEngine.Serialization ;
namespace UnityAtoms.MonoHooks
{
/// <summary>
/// Generic base class for all Mono Hooks.
/// </summary>
2020-03-01 20:26:06 -05:00
/// <typeparam name="E">Event of type `AtomEvent<EV>`</typeparam>
2019-10-15 19:43:51 -04:00
/// <typeparam name="EV">Event value type</typeparam>
/// <typeparam name="F">Function type `AtomFunction<GameObject, GameObject>`</typeparam>
[EditorIcon("atom-icon-delicate")]
2020-03-02 13:52:08 -05:00
public abstract class MonoHook < E , EV , ER , F > : MonoBehaviour
2020-03-01 20:26:06 -05:00
where E : AtomEvent < EV >
2020-03-02 13:52:08 -05:00
where ER : IGetEvent , ISetEvent
2019-10-15 19:43:51 -04:00
where F : AtomFunction < GameObject , GameObject >
{
/// <summary>
/// The Event
/// </summary>
2020-03-02 13:52:08 -05:00
public E Event { get = > _eventReference . GetEvent < E > ( ) ; set = > _eventReference . SetEvent < E > ( value ) ; }
2020-03-01 15:32:52 -05:00
[SerializeField]
2020-03-02 13:52:08 -05:00
private ER _eventReference ;
2019-10-15 19:43:51 -04:00
/// <summary>
/// Selector function for the Event `EventWithGameObjectReference`. Makes it possible to for example select the parent GameObject and pass that a long to the `EventWithGameObjectReference`.
/// </summary>
[SerializeField]
protected F _selectGameObjectReference ;
protected void OnHook ( EV value )
{
if ( Event ! = null )
{
Event . Raise ( value ) ;
}
2020-03-01 20:26:06 -05:00
RaiseWithGameObject ( value , _selectGameObjectReference ! = null ? _selectGameObjectReference . Call ( gameObject ) : gameObject ) ;
2019-10-15 19:43:51 -04:00
}
2020-03-01 15:32:52 -05:00
protected abstract void RaiseWithGameObject ( EV value , GameObject gameObject ) ;
2019-10-15 19:43:51 -04:00
}
}