< 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:26
Uncovered lines:5
Coverable lines:31
Total lines:95
Line coverage:83.8% (26 of 31)
Covered branches:0
Total branches:0

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%2100%
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
 1802813    public bool hideOnEnable = true;
 1802814    public float animSpeedFactor = 1.0f;
 15    public bool disableAfterFadeOut;
 16
 17    [SerializeField] private CanvasGroup canvasGroup;
 18
 19    private GraphicRaycaster raycaster;
 20
 5821    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    {
 1221728        if (canvasGroup == null)
 631829            canvasGroup = GetComponent<CanvasGroup>();
 30
 1221731        raycaster = GetComponent<GraphicRaycaster>();
 1221732    }
 33
 34    private void OnEnable()
 35    {
 1799636        if (hideOnEnable)
 88537            Hide(instant: true);
 1799638    }
 39
 40    public void Show(bool instant = false)
 41    {
 16942        SetVisibility(visible: true, OnShowCompleted, instant);
 43
 44        void OnShowCompleted() =>
 845            OnWillFinishStart?.Invoke(this);
 16946    }
 47
 48    public void Hide(bool instant = false)
 49    {
 133650        SetVisibility(visible: false, OnHideCompleted, instant);
 51
 52        void OnHideCompleted()
 53        {
 41654            OnWillFinishHide?.Invoke(this);
 55
 41656            if (disableAfterFadeOut && gameObject != null)
 157                gameObject.SetActive(false);
 41658        }
 133659    }
 60
 61    /// <summary>
 62    /// Show and then hide after delay (Show->Delay->Hide)
 63    /// </summary>
 64    public void ShowDelayHide(float delay)
 65    {
 066        SetVisibility(visible: true, onComplete: HideAfterDelay);
 67
 68        void HideAfterDelay() =>
 069            SetVisibility(visible: false, null).SetDelay(delay);
 070    }
 71
 72    private TweenerCore<float, float, FloatOptions> SetVisibility(bool visible, TweenCallback onComplete, bool instant =
 73    {
 150574        if (canvasGroup == null)
 75        {
 076            Debug.LogError($"Show Hide Animator in GameObject: {gameObject.name} has no canvasGroup assigned", gameObjec
 077            return null;
 78        }
 79
 150580        if (raycaster != null)
 59681            raycaster.enabled = visible;
 82
 83        // When instant, we use duration 0 instead of just modifying the canvas group to mock the old animator behaviour
 150584        float duration = instant ? 0 : BASE_DURATION * animSpeedFactor;
 85
 150586        canvasGroup.blocksRaycasts = visible;
 150587        canvasGroup.DOKill();
 88
 150589        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}