< Summary

Class:BuilderInWorldLoadingView
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/BuilderInWorldLoadingView.cs
Covered lines:57
Uncovered lines:17
Coverable lines:74
Total lines:194
Line coverage:77% (57 of 74)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldLoadingView()0%110100%
Create()0%110100%
Awake()0%110100%
CreateLoadingTips()0%330100%
Show()0%330100%
Hide(...)0%2.062075%
Dispose()0%3.333066.67%
StartTipsCarousel()0%110100%
StopTipsCarousel()0%220100%
TryToHideCoroutine()0%7.17087.5%
RunTipsCarouselCoroutine()0%7.464040%
IncrementTipIndex()0%220100%
RunTipsAnimationCoroutine()0%20400%
ForceToEndCurrentTipsAnimation()0%110100%
SetPercentage(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/BuilderInWorldLoadingView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7public interface IBuilderInWorldLoadingView
 8{
 9    bool isActive { get; }
 10    GameObject gameObject { get; }
 11    void Show();
 12    void Hide(bool forceHide = false, Action onHideAction = null);
 13    void StartTipsCarousel();
 14    void StopTipsCarousel();
 15    void SetPercentage(float newValue);
 16    void Dispose();
 17}
 18
 19public class BuilderInWorldLoadingView : MonoBehaviour, IBuilderInWorldLoadingView
 20{
 21    private const string VIEW_PATH = "BuilderInWorldLoadingView";
 22
 23    [Header("Loading Tips Config")]
 24    [SerializeField] internal BuilderInWorldLoadingTip loadingTipPrefab;
 25
 26    [SerializeField] internal RectTransform loadingTipsContainer;
 27    [SerializeField] internal ScrollRect loadingTipsScroll;
 28    [SerializeField] internal List<BuilderInWorldLoadingTipModel> loadingTips;
 1729    [SerializeField] internal float timeBetweenTips = 3f;
 30    [SerializeField] internal AnimationCurve animationTipsCurve;
 1731    [SerializeField] internal float animationTipsTransitionTime = 1f;
 32
 33    [Header("Other Config")]
 1734    [SerializeField] internal float minVisibilityTime = 1.5f;
 35
 36    [SerializeField] internal LoadingBar loadingBar;
 37
 038    public bool isActive => gameObject.activeSelf;
 39
 40    internal Coroutine tipsCoroutine;
 41    internal Coroutine hideCoroutine;
 42    internal float showTime = 0f;
 43    internal int currentTipIndex = 0;
 44    internal float currentFinalNormalizedPos;
 45
 46    internal static BuilderInWorldLoadingView Create()
 47    {
 248        var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<BuilderInWorldLoadingView>();
 249        view.gameObject.name = "_BuilderInWorldLoadingView";
 50
 251        return view;
 52    }
 53
 3254    private void Awake() { CreateLoadingTips(); }
 55
 56    private void CreateLoadingTips()
 57    {
 1658        RectTransform tipsContainerRectTransform = loadingTipsContainer;
 1659        float tipsContainerHorizontalOffset = 0;
 60
 9661        for (int i = 0; i < loadingTips.Count; i++)
 62        {
 3263            BuilderInWorldLoadingTip newTip = Instantiate(loadingTipPrefab, loadingTipsContainer);
 3264            newTip.Configure(loadingTips[i]);
 65
 3266            if (i > 0)
 67            {
 1668                tipsContainerHorizontalOffset += ((RectTransform)newTip.transform).rect.size.x;
 69            }
 70        }
 71
 1672        tipsContainerRectTransform.offsetMax = new Vector2(tipsContainerHorizontalOffset, tipsContainerRectTransform.off
 1673    }
 74
 75    public void Show()
 76    {
 177        if (gameObject != null)
 178            gameObject.SetActive(true);
 79
 180        showTime = Time.realtimeSinceStartup;
 81
 182        if (loadingTips.Count > 0)
 83        {
 184            StartTipsCarousel();
 85        }
 86
 187        AudioScriptableObjects.builderEnter.Play();
 188    }
 89
 90    public void Hide(bool forceHide = false, Action onHideAction = null)
 91    {
 192        if (hideCoroutine != null)
 093            CoroutineStarter.Stop(hideCoroutine);
 94
 195        hideCoroutine = CoroutineStarter.Start(TryToHideCoroutine(forceHide, onHideAction));
 196    }
 97
 98    public void Dispose()
 99    {
 16100        StopTipsCarousel();
 101
 16102        if (hideCoroutine != null)
 0103            CoroutineStarter.Stop(hideCoroutine);
 104
 16105        if (tipsCoroutine != null)
 0106            CoroutineStarter.Stop(tipsCoroutine);
 16107    }
 108
 109    public void StartTipsCarousel()
 110    {
 1111        StopTipsCarousel();
 1112        tipsCoroutine = CoroutineStarter.Start(RunTipsCarouselCoroutine());
 1113    }
 114
 115    public void StopTipsCarousel()
 116    {
 18117        if (tipsCoroutine == null)
 17118            return;
 119
 1120        CoroutineStarter.Stop(tipsCoroutine);
 1121        tipsCoroutine = null;
 122
 1123        ForceToEndCurrentTipsAnimation();
 1124    }
 125
 126    internal IEnumerator TryToHideCoroutine(bool forceHide, Action onHideAction)
 127    {
 1128        while (!forceHide && (Time.realtimeSinceStartup - showTime) < minVisibilityTime)
 129        {
 0130            yield return null;
 131        }
 132
 1133        StopTipsCarousel();
 134
 1135        if (gameObject != null)
 1136            gameObject.SetActive(false);
 137
 1138        onHideAction?.Invoke();
 139
 1140        AudioScriptableObjects.builderReady.Play();
 1141    }
 142
 143    internal IEnumerator RunTipsCarouselCoroutine()
 144    {
 1145        currentTipIndex = 0;
 146
 0147        while (true)
 148        {
 1149            yield return new WaitForSeconds(timeBetweenTips);
 0150            yield return RunTipsAnimationCoroutine();
 0151            IncrementTipIndex();
 152        }
 153    }
 154
 155    private void IncrementTipIndex()
 156    {
 1157        currentTipIndex++;
 1158        if (currentTipIndex >= loadingTips.Count - 1)
 159        {
 1160            currentTipIndex = 0;
 1161            loadingTipsScroll.horizontalNormalizedPosition = 0f;
 162
 163            // Moving the last tip game object to the first position in the hierarchy, we make the carousel cyclical.
 1164            loadingTipsContainer.GetChild(loadingTipsContainer.childCount - 1).SetAsFirstSibling();
 165        }
 1166    }
 167
 168    internal IEnumerator RunTipsAnimationCoroutine()
 169    {
 0170        float currentAnimationTime = 0f;
 0171        float initialNormalizedPos = loadingTipsScroll.horizontalNormalizedPosition;
 0172        currentFinalNormalizedPos = initialNormalizedPos + (1f / (loadingTips.Count - 1));
 173
 0174        while (currentAnimationTime <= animationTipsTransitionTime)
 175        {
 0176            loadingTipsScroll.horizontalNormalizedPosition = Mathf.Lerp(
 177                initialNormalizedPos,
 178                currentFinalNormalizedPos,
 179                animationTipsCurve.Evaluate(currentAnimationTime / animationTipsTransitionTime));
 180
 0181            currentAnimationTime += Time.deltaTime;
 182
 0183            yield return null;
 184        }
 0185    }
 186
 187    internal void ForceToEndCurrentTipsAnimation()
 188    {
 1189        loadingTipsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos;
 1190        IncrementTipIndex();
 1191    }
 192
 0193    public void SetPercentage(float newValue) { loadingBar.SetPercentage(newValue); }
 194}