< Summary

Class:ShowHideAnimator
Assembly:HUDCommon
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Common/ShowHideAnimator/ShowHideAnimator.cs
Covered lines:28
Uncovered lines:3
Coverable lines:31
Total lines:95
Line coverage:90.3% (28 of 31)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:8
Method coverage:100% (8 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ShowHideAnimator()0%110100%
Awake()0%220100%
OnEnable()0%220100%
Show(...)0%110100%
Hide(...)0%110100%
ShowDelayHide(...)0%110100%
SetVisibility(...)0%6.396077.78%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Common/ShowHideAnimator/ShowHideAnimator.cs

#LineLine coverage
 1using DG.Tweening;
 2using DG.Tweening.Core;
 3using DG.Tweening.Plugins.Options;
 4using System;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8[RequireComponent(typeof(CanvasGroup))] [DisallowMultipleComponent]
 9public class ShowHideAnimator : MonoBehaviour
 10{
 11    private const float BASE_DURATION = 0.2f;
 12
 3117213    public bool hideOnEnable = true;
 3117214    public float animSpeedFactor = 1.0f;
 15    public bool disableAfterFadeOut;
 16
 17    [SerializeField] private CanvasGroup canvasGroup;
 18
 19    private GraphicRaycaster raycaster;
 20
 3221    public bool isVisible => canvasGroup == null || canvasGroup.blocksRaycasts;
 22
 23    public event Action<ShowHideAnimator> OnWillFinishHide;
 24    public event Action<ShowHideAnimator> OnWillFinishStart;
 25
 26    private void Awake()
 27    {
 1742128        if (canvasGroup == null)
 927029            canvasGroup = GetComponent<CanvasGroup>();
 30
 1742131        raycaster = GetComponent<GraphicRaycaster>();
 1742132    }
 33
 34    private void OnEnable()
 35    {
 1857736        if (hideOnEnable)
 89737            Hide(instant: true);
 1857738    }
 39
 40    public void Show(bool instant = false)
 41    {
 11742        SetVisibility(visible: true, OnShowCompleted, instant);
 43
 44        void OnShowCompleted() =>
 945            OnWillFinishStart?.Invoke(this);
 11746    }
 47
 48    public void Hide(bool instant = false)
 49    {
 151950        SetVisibility(visible: false, OnHideCompleted, instant);
 51
 52        void OnHideCompleted()
 53        {
 37354            OnWillFinishHide?.Invoke(this);
 55
 37356            if (disableAfterFadeOut && gameObject != null)
 1457                gameObject.SetActive(false);
 37358        }
 151959    }
 60
 61    /// <summary>
 62    /// Show and then hide after delay (Show->Delay->Hide)
 63    /// </summary>
 64    public void ShowDelayHide(float delay)
 65    {
 166        SetVisibility(visible: true, onComplete: HideAfterDelay);
 67
 68        void HideAfterDelay() =>
 069            SetVisibility(visible: false, null).SetDelay(delay);
 170    }
 71
 72    private TweenerCore<float, float, FloatOptions> SetVisibility(bool visible, TweenCallback onComplete, bool instant =
 73    {
 163774        if (canvasGroup == null)
 75        {
 076            Debug.LogError($"Show Hide Animator in GameObject: {gameObject.name} has no canvasGroup assigned", gameObjec
 077            return null;
 78        }
 79
 163780        if (raycaster != null)
 59381            raycaster.enabled = visible;
 82
 83        // When instant, we use duration 0 instead of just modifying the canvas group to mock the old animator behaviour
 163784        float duration = instant ? 0 : BASE_DURATION * animSpeedFactor;
 85
 163786        canvasGroup.blocksRaycasts = visible;
 163787        canvasGroup.DOKill();
 88
 163789        return canvasGroup.DOFade(visible ? 1 : 0, duration)
 90                   .SetEase(Ease.InOutQuad)
 91                   .OnComplete(onComplete)
 92                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDestroy)
 93                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDisable);
 94    }
 95}