mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-22 08:08:51 -05:00
* added base class for AtomAction; modified VoidAction; added Register/UnregisterListener to non-generic AtomEvent; added parameterless AtomListener; small polishing * removed EditorIcon from AtomAction; fixed misspelled word in AtomEvent
This commit is contained in:
parent
e0fa81badd
commit
be6a479bc1
@ -4,11 +4,34 @@ using UnityEngine;
|
||||
namespace UnityAtoms
|
||||
{
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `BaseAtom`.
|
||||
/// Base abstract class for Actions. Inherits from `BaseAtom`.
|
||||
/// </summary>
|
||||
public abstract class AtomAction : BaseAtom
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public Action ActionNoValue;
|
||||
|
||||
/// <summary>
|
||||
/// Perform the Action.
|
||||
/// </summary>
|
||||
public virtual void Do()
|
||||
{
|
||||
if (ActionNoValue != null)
|
||||
{
|
||||
ActionNoValue();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `AtomAction`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type for this Action.</typeparam>
|
||||
[EditorIcon("atom-icon-purple")]
|
||||
public abstract class AtomAction<T1> : BaseAtom
|
||||
public abstract class AtomAction<T1> : AtomAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
@ -22,6 +45,8 @@ namespace UnityAtoms
|
||||
/// <param name="t1">The first parameter.</param>
|
||||
public virtual void Do(T1 t1)
|
||||
{
|
||||
base.Do();
|
||||
|
||||
if (Action != null)
|
||||
{
|
||||
Action(t1);
|
||||
@ -33,12 +58,11 @@ namespace UnityAtoms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `BaseAtom`.
|
||||
/// Generic abstract base class for Actions. Inherits from `AtomAction`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The first type for this Action.</typeparam>
|
||||
/// <typeparam name="T2">The second type for this Action.</typeparam>
|
||||
[EditorIcon("atom-icon-purple")]
|
||||
public abstract class AtomAction<T1, T2> : BaseAtom
|
||||
public abstract class AtomAction<T1, T2> : AtomAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
@ -53,6 +77,8 @@ namespace UnityAtoms
|
||||
/// <param name="t2">The second parameter.</param>
|
||||
public virtual void Do(T1 t1, T2 t2)
|
||||
{
|
||||
base.Do();
|
||||
|
||||
if (Action != null)
|
||||
{
|
||||
Action(t1, t2);
|
||||
@ -64,13 +90,12 @@ namespace UnityAtoms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `BaseAtom`.
|
||||
/// Generic abstract base class for Actions. Inherits from `AtomAction`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The first type for this Action.</typeparam>
|
||||
/// <typeparam name="T2">The second type for this Action.</typeparam>
|
||||
/// <typeparam name="T3">The third type for this Action.</typeparam>
|
||||
[EditorIcon("atom-icon-purple")]
|
||||
public abstract class AtomAction<T1, T2, T3> : BaseAtom
|
||||
public abstract class AtomAction<T1, T2, T3> : AtomAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
@ -86,6 +111,8 @@ namespace UnityAtoms
|
||||
/// <param name="t3">The third parameter.</param>
|
||||
public virtual void Do(T1 t1, T2 t2, T3 t3)
|
||||
{
|
||||
base.Do();
|
||||
|
||||
if (Action != null)
|
||||
{
|
||||
Action(t1, t2, t3);
|
||||
@ -97,14 +124,13 @@ namespace UnityAtoms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `BaseAtom`.
|
||||
/// Generic abstract base class for Actions. Inherits from `AtomAction`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The first type for this Action.</typeparam>
|
||||
/// <typeparam name="T2">The second type for this Action.</typeparam>
|
||||
/// <typeparam name="T3">The third type for this Action.</typeparam>
|
||||
/// <typeparam name="T4">The fourth type for this Action.</typeparam>
|
||||
[EditorIcon("atom-icon-purple")]
|
||||
public abstract class AtomAction<T1, T2, T3, T4> : BaseAtom
|
||||
public abstract class AtomAction<T1, T2, T3, T4> : AtomAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
@ -121,6 +147,8 @@ namespace UnityAtoms
|
||||
/// <param name="t4">The fourth parameter.</param>
|
||||
public virtual void Do(T1 t1, T2 t2, T3 t3, T4 t4)
|
||||
{
|
||||
base.Do();
|
||||
|
||||
if (Action != null)
|
||||
{
|
||||
Action(t1, t2, t3, t4);
|
||||
@ -132,15 +160,14 @@ namespace UnityAtoms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic abstract base class for Actions. Inherits from `BaseAtom`.
|
||||
/// Generic abstract base class for Actions. Inherits from `AtomAction`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The first type for this Action.</typeparam>
|
||||
/// <typeparam name="T2">The second type for this Action.</typeparam>
|
||||
/// <typeparam name="T3">The third type for this Action.</typeparam>
|
||||
/// <typeparam name="T4">The fourth type for this Action.</typeparam>
|
||||
/// <typeparam name="T5">The fifth type for this Action.</typeparam>
|
||||
[EditorIcon("atom-icon-purple")]
|
||||
public abstract class AtomAction<T1, T2, T3, T4, T5> : BaseAtom
|
||||
public abstract class AtomAction<T1, T2, T3, T4, T5> : AtomAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual Action.
|
||||
@ -158,6 +185,8 @@ namespace UnityAtoms
|
||||
/// <param name="t5">The fifth parameter.</param>
|
||||
public virtual void Do(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
|
||||
{
|
||||
base.Do();
|
||||
|
||||
if (Action != null)
|
||||
{
|
||||
Action(t1, t2, t3, t4, t5);
|
||||
|
@ -15,10 +15,5 @@ namespace UnityAtoms
|
||||
{
|
||||
Do();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Do the Action.
|
||||
/// </summary>
|
||||
public abstract void Do();
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,24 @@ namespace UnityAtoms
|
||||
OnEventNoValue -= del;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listener to register.</param>
|
||||
public void RegisterListener(IAtomListener listener)
|
||||
{
|
||||
OnEventNoValue += listener.OnEventRaised;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregister a listener that was registered using the `RegisterListener` method.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listener to unregister.</param>
|
||||
public void UnregisterListener(IAtomListener listener)
|
||||
{
|
||||
OnEventNoValue -= listener.OnEventRaised;
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize() { }
|
||||
|
||||
public virtual void OnAfterDeserialize()
|
||||
@ -96,7 +114,7 @@ namespace UnityAtoms
|
||||
/// <summary>
|
||||
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listenr to register.</param>
|
||||
/// <param name="listener">The Listener to register.</param>
|
||||
public void RegisterListener(IAtomListener<T> listener)
|
||||
{
|
||||
OnEvent += listener.OnEventRaised;
|
||||
@ -105,7 +123,7 @@ namespace UnityAtoms
|
||||
/// <summary>
|
||||
/// Unregister a listener that was registered using the `RegisterListener` method.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listenr to unregister.</param>
|
||||
/// <param name="listener">The Listener to unregister.</param>
|
||||
public void UnregisterListener(IAtomListener<T> listener)
|
||||
{
|
||||
OnEvent -= listener.OnEventRaised;
|
||||
@ -181,7 +199,7 @@ namespace UnityAtoms
|
||||
/// <summary>
|
||||
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listenr to register.</param>
|
||||
/// <param name="listener">The Listener to register.</param>
|
||||
public void RegisterListener(IAtomListener<T1, T2> listener)
|
||||
{
|
||||
OnEvent += listener.OnEventRaised;
|
||||
@ -190,7 +208,7 @@ namespace UnityAtoms
|
||||
/// <summary>
|
||||
/// Unregister a listener that was registered using the `RegisterListener` method.
|
||||
/// </summary>
|
||||
/// <param name="listener">The Listenr to unregister.</param>
|
||||
/// <param name="listener">The Listener to unregister.</param>
|
||||
public void UnregisterListener(IAtomListener<T1, T2> listener)
|
||||
{
|
||||
OnEvent -= listener.OnEventRaised;
|
||||
|
@ -6,7 +6,64 @@ using UnityEngine.Serialization;
|
||||
namespace UnityAtoms
|
||||
{
|
||||
/// <summary>
|
||||
/// Generic base class for Listeners. Inherits from `BaseAtomListener` and `IAtomListener<T>`.
|
||||
/// The most basic Listener. Can use every type of AtomEvent but doesn't support its value. Inherits from `BaseAtomListener` and implements `IAtomListener`.
|
||||
/// </summary>
|
||||
[EditorIcon("atom-icon-orange")]
|
||||
[AddComponentMenu("Unity Atoms/Listeners/Atom Listener")]
|
||||
public sealed class AtomListener : BaseAtomListener, IAtomListener
|
||||
{
|
||||
/// <summary>
|
||||
/// The Event that we are listening to.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private AtomEvent _event = null;
|
||||
|
||||
/// <summary>
|
||||
/// The Event we are listening for as a property.
|
||||
/// </summary>
|
||||
public AtomEvent Event { get { return _event; } set { _event = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// The Unity Event responses.
|
||||
/// NOTE: This variable is public due to this bug: https://issuetracker.unity3d.com/issues/events-generated-by-the-player-input-component-do-not-have-callbackcontext-set-as-their-parameter-type. Will be changed back to private when fixed (this could happen in a none major update).
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public UnityEvent _unityEventResponse = null;
|
||||
|
||||
/// <summary>
|
||||
/// The Action responses;
|
||||
/// </summary>
|
||||
/// <returns>A `List<AtomAction>` of Actions.</returns>
|
||||
[SerializeField]
|
||||
private List<AtomAction> _actionResponses = new List<AtomAction>();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (Event == null) return;
|
||||
Event.RegisterListener(this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (Event == null) return;
|
||||
Event.UnregisterListener(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handler for when the Event gets raised.
|
||||
/// </summary>
|
||||
public void OnEventRaised()
|
||||
{
|
||||
_unityEventResponse?.Invoke();
|
||||
for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i)
|
||||
{
|
||||
_actionResponses[i].Do();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic base class for Listeners. Inherits from `BaseAtomListener` and implements `IAtomListener<T>`.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type that we are listening for.</typeparam>
|
||||
/// <typeparam name="A">Acion of type `AtomAction<T>`.</typeparam>
|
||||
@ -24,7 +81,7 @@ namespace UnityAtoms
|
||||
private E _event = null;
|
||||
|
||||
/// <summary>
|
||||
/// The Event we are listening for as a proeprty.
|
||||
/// The Event we are listening for as a property.
|
||||
/// </summary>
|
||||
/// <value>The Event of type `E`.</value>
|
||||
public E Event { get { return _event; } set { _event = value; } }
|
||||
@ -61,7 +118,7 @@ namespace UnityAtoms
|
||||
/// <param name="item">The Event type.</param>
|
||||
public void OnEventRaised(T item)
|
||||
{
|
||||
if (_unityEventResponse != null) { _unityEventResponse.Invoke(item); }
|
||||
_unityEventResponse?.Invoke(item);
|
||||
for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i)
|
||||
{
|
||||
_actionResponses[i].Do(item);
|
||||
@ -78,7 +135,7 @@ namespace UnityAtoms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic base class for Listeners. Inherits from `BaseAtomListener` and `IAtomListener<T1, T2>`
|
||||
/// Generic base class for Listeners. Inherits from `BaseAtomListener` and implements `IAtomListener<T1, T2>`
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The first type that we are listening for.</typeparam>
|
||||
/// <typeparam name="T2">The second type that we are listening for.</typeparam>
|
||||
@ -98,7 +155,7 @@ namespace UnityAtoms
|
||||
private E _event;
|
||||
|
||||
/// <summary>
|
||||
/// The Event we are listening for as a proeprty.
|
||||
/// The Event we are listening for as a property.
|
||||
/// </summary>
|
||||
/// <value>The Event of type `E`.</value>
|
||||
public E Event { get { return _event; } set { _event = value; } }
|
||||
@ -136,7 +193,7 @@ namespace UnityAtoms
|
||||
/// <param name="second">The second Event type.</param>
|
||||
public void OnEventRaised(T1 first, T2 second)
|
||||
{
|
||||
if (_unityEventResponse != null) { _unityEventResponse.Invoke(first, second); }
|
||||
_unityEventResponse?.Invoke(first, second);
|
||||
for (int i = 0; _actionResponses != null && i < _actionResponses.Count; ++i)
|
||||
{
|
||||
_actionResponses[i].Do(first, second);
|
||||
|
@ -1,5 +1,10 @@
|
||||
namespace UnityAtoms
|
||||
{
|
||||
public interface IAtomListener
|
||||
{
|
||||
void OnEventRaised();
|
||||
}
|
||||
|
||||
public interface IAtomListener<T>
|
||||
{
|
||||
void OnEventRaised(T item);
|
||||
|
Loading…
Reference in New Issue
Block a user