< 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:24
Uncovered lines:0
Coverable lines:24
Total lines:68
Line coverage:100% (24 of 24)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using DG.Tweening;
 2using UnityEngine;
 3
 4[RequireComponent(typeof(CanvasGroup)), DisallowMultipleComponent]
 5public class ShowHideAnimator : MonoBehaviour
 6{
 7    private const float BASE_DURATION = 0.2f;
 8
 9    public event System.Action<ShowHideAnimator> OnWillFinishHide;
 10    public event System.Action<ShowHideAnimator> OnWillFinishStart;
 11
 1709212    public bool hideOnEnable = true;
 1709213    public float animSpeedFactor = 1.0f;
 14    public bool disableAfterFadeOut;
 15
 16    [SerializeField] private CanvasGroup canvasGroup;
 17
 4918    public bool isVisible => canvasGroup == null || canvasGroup.blocksRaycasts;
 19
 20    private void Awake()
 21    {
 1133922        if (TryGetComponent(out Animator animator)) //Remove old behaviour
 1109523            Destroy(animator);
 24
 1133925        if (canvasGroup == null)
 604426            canvasGroup = GetComponent<CanvasGroup>();
 1133927    }
 28
 29    private void OnEnable()
 30    {
 1762531        if (hideOnEnable) { Hide(true); }
 1713132    }
 33
 34    public void Show(bool instant = false)
 35    {
 15936        canvasGroup.blocksRaycasts = true;
 37
 38        //When instant, we use duration 0 instead of just modifying the canvas group to mock the old animator behaviour 
 15939        var duration = instant ? 0 : BASE_DURATION * animSpeedFactor;
 15940        canvasGroup.DOKill();
 41
 15942        canvasGroup.DOFade(1, duration)
 43                   .SetEase(Ease.InOutQuad)
 844                   .OnComplete(() => OnWillFinishStart?.Invoke(this))
 45                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDestroy)
 46                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDisable);
 15947    }
 48
 49    public void Hide(bool instant = false)
 50    {
 85851        canvasGroup.blocksRaycasts = false;
 52
 53        //When instant, we use duration 0 instead of just modifying the canvas group to mock the old animator behaviour 
 85854        var duration = instant ? 0 : BASE_DURATION * animSpeedFactor;
 85855        canvasGroup.DOKill();
 56
 85857        canvasGroup.DOFade(0, duration)
 58                   .SetEase(Ease.InOutQuad)
 59                   .OnComplete(() =>
 60                    {
 3461                        OnWillFinishHide?.Invoke(this);
 62
 3863                        if (disableAfterFadeOut && gameObject != null) { gameObject.SetActive(false); }
 3464                    })
 65                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDestroy)
 66                   .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDisable);
 85867    }
 68}