< Summary

Information
Class: Hertzole.ScriptableValues.ScriptableEventListener[TValue]
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Listeners/Events/GenericScriptableEventListener.cs
Tag: 52_28197466029
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 198
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ScriptableEventListener()0%110100%
SetListening(...)0%220100%
SetTargetEvent(...)0%660100%
SetListeningToObject(...)0%220100%
OnEventInvoked(...)0%330100%
OnBeforeEventInvoked(...)0%110100%
OnAfterEventInvoked(...)0%110100%
ShouldInvoke(...)0%440100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Listeners/Events/GenericScriptableEventListener.cs

#LineLine coverage
 1#nullable enable
 2
 3using Hertzole.ScriptableValues.Helpers;
 4using UnityEngine;
 5using UnityEngine.Assertions;
 6using UnityEngine.Events;
 7
 8namespace 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.")]
 409231        private ScriptableEvent<TValue>? targetEvent = null;
 32        [SerializeField]
 33        [EditorTooltip("When the listener should invoke its events.")]
 409234        private EventInvokeEvents invokeOn = EventInvokeEvents.Any;
 35        [SerializeField]
 36        [EditorTooltip("What the argument needs to have been for the event to be invoked.")]
 409237        private TValue? fromValue = default;
 38        [SerializeField]
 39        [EditorTooltip("What the argument needs to be for the event to be invoked.")]
 409240        private TValue? toValue = default;
 41        [SerializeField]
 42        [EditorTooltip("The event to invoke when the target event is raised.")]
 409243        private UnityEvent<TValue> onInvoked = new UnityEvent<TValue>();
 44
 45        /// <summary>
 46        ///     The event to listen to.
 47        /// </summary>
 48        public ScriptableEvent<TValue>? TargetEvent
 49        {
 140450            get { return targetEvent; }
 1490451            set { SetTargetEvent(value); }
 52        }
 53        /// <summary>
 54        ///     When the listener should invoke its events.
 55        /// </summary>
 56        public EventInvokeEvents InvokeOn
 57        {
 712858            get { return invokeOn; }
 712859            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        {
 237666            get { return fromValue; }
 237667            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        {
 237674            get { return toValue; }
 237675            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        {
 712883            get { return onInvoked; }
 84        }
 85
 86        /// <inheritdoc />
 87        protected sealed override void SetListening(bool listen)
 849688        {
 849689            base.SetListening(listen);
 90
 849691            if (targetEvent == null)
 378092            {
 378093                return;
 94            }
 95
 471696            SetListeningToObject(targetEvent, listen);
 849697        }
 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)
 4968104        {
 105            // If the event is the same, do nothing.
 4968106            if (newEvent == targetEvent)
 156107            {
 156108                return;
 109            }
 110
 111            // If we're already listening to an event, stop listening to it.
 4812112            if (targetEvent != null && IsListening)
 1344113            {
 1344114                SetListeningToObject(targetEvent, false);
 1344115            }
 116
 4812117            targetEvent = newEvent;
 118
 119            // If we're supposed to be listening to the new event, start listening to it.
 4812120            if (targetEvent != null && IsListening)
 4812121            {
 4812122                SetListeningToObject(targetEvent, true);
 4812123            }
 4968124        }
 125
 126        private void SetListeningToObject(ScriptableEvent<TValue> target, bool listen)
 10872127        {
 10872128            Guard.IsNotNull(target, nameof(target));
 129
 10872130            if (listen)
 5436131            {
 5436132                target.OnInvoked += OnEventInvoked;
 5436133            }
 134            else
 5436135            {
 5436136                target.OnInvoked -= OnEventInvoked;
 5436137            }
 10872138        }
 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)
 4128146        {
 4128147            Assert.IsNotNull(targetEvent);
 148
 4128149            if (ShouldInvoke(invokeOn, targetEvent!.PreviousArgs, args, fromValue, toValue) && OnBeforeEventInvoked(send
 3564150            {
 3564151                onInvoked.Invoke(args);
 152
 3564153                OnAfterEventInvoked(sender, args);
 3564154            }
 4128155        }
 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)
 3564164        {
 3564165            return true;
 3564166        }
 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>
 7128173        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
 4128185        {
 4128186            switch (invokeOn)
 187            {
 188                case EventInvokeEvents.FromValue: // If the old value is the from value.
 1470189                    return EqualityHelper.Equals(previousValue, fromValue);
 190                case EventInvokeEvents.ToValue:
 1470191                    return EqualityHelper.Equals(newValue, toValue); // If the new value is the to value.
 192                case EventInvokeEvents.Any:
 193                default: // If anything happened (includes any)
 1188194                    return true;
 195            }
 4128196        }
 197    }
 198}