| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using Hertzole.ScriptableValues.Helpers; |
| | | 4 | | using UnityEngine; |
| | | 5 | | using UnityEngine.Assertions; |
| | | 6 | | using UnityEngine.Events; |
| | | 7 | | |
| | | 8 | | namespace Hertzole.ScriptableValues |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// When listeners should invoke their events. |
| | | 12 | | /// </summary> |
| | | 13 | | public enum EventInvokeEvents |
| | | 14 | | { |
| | | 15 | | /// <summary>When any argument is specified.</summary> |
| | | 16 | | Any = 0, |
| | | 17 | | /// <summary>When the argument matches a specific value.</summary> |
| | | 18 | | FromValue = 1, |
| | | 19 | | /// <summary>When the argument matches a specific value.</summary> |
| | | 20 | | ToValue = 2 |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Base class for a component that listens to <see cref="ScriptableEvent{T}" />. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <typeparam name="TValue">The type to listen for.</typeparam> |
| | | 27 | | public abstract class ScriptableEventListener<TValue> : ScriptableListenerBase |
| | | 28 | | { |
| | | 29 | | [SerializeField] |
| | | 30 | | [EditorTooltip("The event to listen to.")] |
| | 4092 | 31 | | private ScriptableEvent<TValue>? targetEvent = null; |
| | | 32 | | [SerializeField] |
| | | 33 | | [EditorTooltip("When the listener should invoke its events.")] |
| | 4092 | 34 | | private EventInvokeEvents invokeOn = EventInvokeEvents.Any; |
| | | 35 | | [SerializeField] |
| | | 36 | | [EditorTooltip("What the argument needs to have been for the event to be invoked.")] |
| | 4092 | 37 | | private TValue? fromValue = default; |
| | | 38 | | [SerializeField] |
| | | 39 | | [EditorTooltip("What the argument needs to be for the event to be invoked.")] |
| | 4092 | 40 | | private TValue? toValue = default; |
| | | 41 | | [SerializeField] |
| | | 42 | | [EditorTooltip("The event to invoke when the target event is raised.")] |
| | 4092 | 43 | | private UnityEvent<TValue> onInvoked = new UnityEvent<TValue>(); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The event to listen to. |
| | | 47 | | /// </summary> |
| | | 48 | | public ScriptableEvent<TValue>? TargetEvent |
| | | 49 | | { |
| | 1404 | 50 | | get { return targetEvent; } |
| | 14904 | 51 | | set { SetTargetEvent(value); } |
| | | 52 | | } |
| | | 53 | | /// <summary> |
| | | 54 | | /// When the listener should invoke its events. |
| | | 55 | | /// </summary> |
| | | 56 | | public EventInvokeEvents InvokeOn |
| | | 57 | | { |
| | 7128 | 58 | | get { return invokeOn; } |
| | 7128 | 59 | | set { invokeOn = value; } |
| | | 60 | | } |
| | | 61 | | /// <summary> |
| | | 62 | | /// What the argument needs to have been for the event to be invoked. |
| | | 63 | | /// </summary> |
| | | 64 | | public TValue? FromValue |
| | | 65 | | { |
| | 2376 | 66 | | get { return fromValue; } |
| | 2376 | 67 | | set { fromValue = value; } |
| | | 68 | | } |
| | | 69 | | /// <summary> |
| | | 70 | | /// What the argument needs to be for the event to be invoked. |
| | | 71 | | /// </summary> |
| | | 72 | | public TValue? ToValue |
| | | 73 | | { |
| | 2376 | 74 | | get { return toValue; } |
| | 2376 | 75 | | set { toValue = value; } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// The event to invoke when the target event is raised. |
| | | 80 | | /// </summary> |
| | | 81 | | public UnityEvent<TValue> OnInvoked |
| | | 82 | | { |
| | 7128 | 83 | | get { return onInvoked; } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | protected sealed override void SetListening(bool listen) |
| | 8496 | 88 | | { |
| | 8496 | 89 | | base.SetListening(listen); |
| | | 90 | | |
| | 8496 | 91 | | if (targetEvent == null) |
| | 3780 | 92 | | { |
| | 3780 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | 4716 | 96 | | SetListeningToObject(targetEvent, listen); |
| | 8496 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Sets the target event. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="newEvent">The new event.</param> |
| | | 103 | | private void SetTargetEvent(ScriptableEvent<TValue>? newEvent) |
| | 4968 | 104 | | { |
| | | 105 | | // If the event is the same, do nothing. |
| | 4968 | 106 | | if (newEvent == targetEvent) |
| | 156 | 107 | | { |
| | 156 | 108 | | return; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | // If we're already listening to an event, stop listening to it. |
| | 4812 | 112 | | if (targetEvent != null && IsListening) |
| | 1344 | 113 | | { |
| | 1344 | 114 | | SetListeningToObject(targetEvent, false); |
| | 1344 | 115 | | } |
| | | 116 | | |
| | 4812 | 117 | | targetEvent = newEvent; |
| | | 118 | | |
| | | 119 | | // If we're supposed to be listening to the new event, start listening to it. |
| | 4812 | 120 | | if (targetEvent != null && IsListening) |
| | 4812 | 121 | | { |
| | 4812 | 122 | | SetListeningToObject(targetEvent, true); |
| | 4812 | 123 | | } |
| | 4968 | 124 | | } |
| | | 125 | | |
| | | 126 | | private void SetListeningToObject(ScriptableEvent<TValue> target, bool listen) |
| | 10872 | 127 | | { |
| | 10872 | 128 | | Guard.IsNotNull(target, nameof(target)); |
| | | 129 | | |
| | 10872 | 130 | | if (listen) |
| | 5436 | 131 | | { |
| | 5436 | 132 | | target.OnInvoked += OnEventInvoked; |
| | 5436 | 133 | | } |
| | | 134 | | else |
| | 5436 | 135 | | { |
| | 5436 | 136 | | target.OnInvoked -= OnEventInvoked; |
| | 5436 | 137 | | } |
| | 10872 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Called when the target event is invoked. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="sender">The object that sent the event.</param> |
| | | 144 | | /// <param name="args">The arguments.</param> |
| | | 145 | | private void OnEventInvoked(object sender, TValue args) |
| | 4128 | 146 | | { |
| | 4128 | 147 | | Assert.IsNotNull(targetEvent); |
| | | 148 | | |
| | 4128 | 149 | | if (ShouldInvoke(invokeOn, targetEvent!.PreviousArgs, args, fromValue, toValue) && OnBeforeEventInvoked(send |
| | 3564 | 150 | | { |
| | 3564 | 151 | | onInvoked.Invoke(args); |
| | | 152 | | |
| | 3564 | 153 | | OnAfterEventInvoked(sender, args); |
| | 3564 | 154 | | } |
| | 4128 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Called before the event is invoked. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="sender">The object that sent the event.</param> |
| | | 161 | | /// <param name="args">The event arguments.</param> |
| | | 162 | | /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns> |
| | | 163 | | protected virtual bool OnBeforeEventInvoked(object sender, TValue args) |
| | 3564 | 164 | | { |
| | 3564 | 165 | | return true; |
| | 3564 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Called after the event is invoked. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <param name="sender">The object that sent the event.</param> |
| | | 172 | | /// <param name="args">The event arguments.</param> |
| | 7128 | 173 | | protected virtual void OnAfterEventInvoked(object sender, TValue args) { } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Determines if the event should be invoked. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="invokeOn"></param> |
| | | 179 | | /// <param name="previousValue"></param> |
| | | 180 | | /// <param name="newValue"></param> |
| | | 181 | | /// <param name="fromValue"></param> |
| | | 182 | | /// <param name="toValue"></param> |
| | | 183 | | /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns> |
| | | 184 | | private static bool ShouldInvoke(EventInvokeEvents invokeOn, TValue? previousValue, TValue? newValue, TValue? fr |
| | 4128 | 185 | | { |
| | 4128 | 186 | | switch (invokeOn) |
| | | 187 | | { |
| | | 188 | | case EventInvokeEvents.FromValue: // If the old value is the from value. |
| | 1470 | 189 | | return EqualityHelper.Equals(previousValue, fromValue); |
| | | 190 | | case EventInvokeEvents.ToValue: |
| | 1470 | 191 | | return EqualityHelper.Equals(newValue, toValue); // If the new value is the to value. |
| | | 192 | | case EventInvokeEvents.Any: |
| | | 193 | | default: // If anything happened (includes any) |
| | 1188 | 194 | | return true; |
| | | 195 | | } |
| | 4128 | 196 | | } |
| | | 197 | | } |
| | | 198 | | } |