| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using Hertzole.ScriptableValues.Helpers; |
| | | 5 | | using UnityEngine; |
| | | 6 | | using UnityEngine.Events; |
| | | 7 | | |
| | | 8 | | namespace Hertzole.ScriptableValues |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Listens to a <see cref="ScriptableEvent" /> without arguments. |
| | | 12 | | /// </summary> |
| | | 13 | | #if UNITY_EDITOR |
| | | 14 | | [AddComponentMenu("Scriptable Values/Listeners/Events/Scriptable Event Listener", 1100)] |
| | | 15 | | #endif |
| | | 16 | | public class ScriptableEventListener : ScriptableListenerBase |
| | | 17 | | { |
| | | 18 | | [SerializeField] |
| | | 19 | | [EditorTooltip("The event to listen to.")] |
| | 78 | 20 | | private ScriptableEvent? targetEvent = null; |
| | | 21 | | [SerializeField] |
| | | 22 | | [EditorTooltip("The event to invoke when the target event is raised.")] |
| | 78 | 23 | | private UnityEvent onInvoked = new UnityEvent(); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The event to listen to. |
| | | 27 | | /// </summary> |
| | | 28 | | public ScriptableEvent? TargetEvent |
| | | 29 | | { |
| | 54 | 30 | | get { return targetEvent; } |
| | 252 | 31 | | set { SetTargetEvent(value); } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The event to invoke when the target event is raised. |
| | | 36 | | /// </summary> |
| | | 37 | | public UnityEvent OnInvoked |
| | | 38 | | { |
| | 36 | 39 | | get { return onInvoked; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | protected sealed override void SetListening(bool listen) |
| | 168 | 44 | | { |
| | 168 | 45 | | base.SetListening(listen); |
| | | 46 | | |
| | 168 | 47 | | if (targetEvent == null) |
| | 66 | 48 | | { |
| | 66 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | 102 | 52 | | SetListeningToObject(targetEvent, listen); |
| | 168 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Sets the target event. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="newEvent">The new event.</param> |
| | | 59 | | private void SetTargetEvent(ScriptableEvent? newEvent) |
| | 84 | 60 | | { |
| | | 61 | | // If the target event is the same, do nothing. |
| | 84 | 62 | | if (newEvent == targetEvent) |
| | 6 | 63 | | { |
| | 6 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // If we are currently listening to the old event, stop listening. |
| | 78 | 68 | | if (targetEvent != null && IsListening) |
| | 18 | 69 | | { |
| | 18 | 70 | | SetListeningToObject(targetEvent, false); |
| | 18 | 71 | | } |
| | | 72 | | |
| | 78 | 73 | | targetEvent = newEvent; |
| | | 74 | | |
| | | 75 | | // If we should start listening to the new event, start listening. |
| | 78 | 76 | | if (targetEvent != null && IsListening) |
| | 72 | 77 | | { |
| | 72 | 78 | | SetListeningToObject(targetEvent, true); |
| | 72 | 79 | | } |
| | 84 | 80 | | } |
| | | 81 | | |
| | | 82 | | private void SetListeningToObject(ScriptableEvent target, bool listen) |
| | 192 | 83 | | { |
| | 192 | 84 | | Guard.IsNotNull(target, nameof(target)); |
| | | 85 | | |
| | 192 | 86 | | if (listen) |
| | 96 | 87 | | { |
| | 96 | 88 | | target.OnInvoked += OnEventInvoked; |
| | 96 | 89 | | } |
| | | 90 | | else |
| | 96 | 91 | | { |
| | 96 | 92 | | target.OnInvoked -= OnEventInvoked; |
| | 96 | 93 | | } |
| | 192 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Called when the target event is invoked. |
| | | 98 | | /// </summary> |
| | | 99 | | private void OnEventInvoked(object sender, EventArgs eventArgs) |
| | 18 | 100 | | { |
| | 18 | 101 | | if (!OnBeforeEventInvoked(sender)) |
| | 0 | 102 | | { |
| | 0 | 103 | | return; |
| | | 104 | | } |
| | | 105 | | |
| | 18 | 106 | | onInvoked.Invoke(); |
| | | 107 | | |
| | 18 | 108 | | OnAfterEventInvoked(sender); |
| | 18 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Called before the event is invoked. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="sender">The object that sent the event.</param> |
| | | 115 | | /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns> |
| | | 116 | | protected virtual bool OnBeforeEventInvoked(object sender) |
| | 18 | 117 | | { |
| | 18 | 118 | | return true; |
| | 18 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Called after the event is invoked. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="sender">The object that sent the event.</param> |
| | 36 | 125 | | protected virtual void OnAfterEventInvoked(object sender) { } |
| | | 126 | | } |
| | | 127 | | } |