< Summary

Information
Class: Hertzole.ScriptableValues.ScriptableEventListener
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Listeners/Events/ScriptableEventListener.cs
Tag: 52_28197466029
Line coverage
95%
Covered lines: 45
Uncovered lines: 2
Coverable lines: 47
Total lines: 127
Line coverage: 95.7%
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%2.092071.43%
OnBeforeEventInvoked(...)0%110100%
OnAfterEventInvoked(...)0%110100%

File(s)

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

#LineLine coverage
 1#nullable enable
 2
 3using System;
 4using Hertzole.ScriptableValues.Helpers;
 5using UnityEngine;
 6using UnityEngine.Events;
 7
 8namespace 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.")]
 7820        private ScriptableEvent? targetEvent = null;
 21        [SerializeField]
 22        [EditorTooltip("The event to invoke when the target event is raised.")]
 7823        private UnityEvent onInvoked = new UnityEvent();
 24
 25        /// <summary>
 26        ///     The event to listen to.
 27        /// </summary>
 28        public ScriptableEvent? TargetEvent
 29        {
 5430            get { return targetEvent; }
 25231            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        {
 3639            get { return onInvoked; }
 40        }
 41
 42        /// <inheritdoc />
 43        protected sealed override void SetListening(bool listen)
 16844        {
 16845            base.SetListening(listen);
 46
 16847            if (targetEvent == null)
 6648            {
 6649                return;
 50            }
 51
 10252            SetListeningToObject(targetEvent, listen);
 16853        }
 54
 55        /// <summary>
 56        ///     Sets the target event.
 57        /// </summary>
 58        /// <param name="newEvent">The new event.</param>
 59        private void SetTargetEvent(ScriptableEvent? newEvent)
 8460        {
 61            // If the target event is the same, do nothing.
 8462            if (newEvent == targetEvent)
 663            {
 664                return;
 65            }
 66
 67            // If we are currently listening to the old event, stop listening.
 7868            if (targetEvent != null && IsListening)
 1869            {
 1870                SetListeningToObject(targetEvent, false);
 1871            }
 72
 7873            targetEvent = newEvent;
 74
 75            // If we should start listening to the new event, start listening.
 7876            if (targetEvent != null && IsListening)
 7277            {
 7278                SetListeningToObject(targetEvent, true);
 7279            }
 8480        }
 81
 82        private void SetListeningToObject(ScriptableEvent target, bool listen)
 19283        {
 19284            Guard.IsNotNull(target, nameof(target));
 85
 19286            if (listen)
 9687            {
 9688                target.OnInvoked += OnEventInvoked;
 9689            }
 90            else
 9691            {
 9692                target.OnInvoked -= OnEventInvoked;
 9693            }
 19294        }
 95
 96        /// <summary>
 97        ///     Called when the target event is invoked.
 98        /// </summary>
 99        private void OnEventInvoked(object sender, EventArgs eventArgs)
 18100        {
 18101            if (!OnBeforeEventInvoked(sender))
 0102            {
 0103                return;
 104            }
 105
 18106            onInvoked.Invoke();
 107
 18108            OnAfterEventInvoked(sender);
 18109        }
 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)
 18117        {
 18118            return true;
 18119        }
 120
 121        /// <summary>
 122        ///     Called after the event is invoked.
 123        /// </summary>
 124        /// <param name="sender">The object that sent the event.</param>
 36125        protected virtual void OnAfterEventInvoked(object sender) { }
 126    }
 127}