< Summary

Class:CursorController
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/CursorController.cs
Covered lines:46
Uncovered lines:8
Coverable lines:54
Total lines:121
Line coverage:85.1% (46 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnDestroy()0%110100%
OnChangeType(...)0%330100%
AllUIVisible_OnChange(...)0%110100%
ChangeCursorVisible(...)0%330100%
Show()0%13.585030%
Hide()0%7.355054.55%
SetNormalCursor()0%110100%
SetHoverCursor()0%110100%
SetAlpha()0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/CursorController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7public class CursorController : MonoBehaviour
 8{
 9    private const float ALPHA_INTERPOLATION_DURATION = 0.1f;
 10
 011    public static CursorController i { get; private set; }
 12    public Image cursorImage;
 13    public Sprite normalCursor;
 14    public Sprite hoverCursor;
 15    public CanvasGroup canvasGroup;
 16
 17    private Coroutine alphaRoutine;
 18    private bool isAllUIHidden;
 19
 20    void Awake()
 21    {
 60122        i = this;
 60123        DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>();
 60124        data.cursorVisible.OnChange += ChangeCursorVisible;
 60125        data.cursorType.OnChange += OnChangeType;
 60126        CommonScriptableObjects.allUIHidden.OnChange += AllUIVisible_OnChange;
 60127    }
 28
 29    private void OnDestroy()
 30    {
 60131        DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>();
 60132        data.cursorVisible.OnChange -= ChangeCursorVisible;
 60133        data.cursorType.OnChange -= OnChangeType;
 60134        CommonScriptableObjects.allUIHidden.OnChange -= AllUIVisible_OnChange;
 60135    }
 36
 37    private void OnChangeType(DataStore_Cursor.CursorType current, DataStore_Cursor.CursorType previous)
 38    {
 39        switch (current)
 40        {
 41            case DataStore_Cursor.CursorType.NORMAL:
 342                SetNormalCursor();
 343                break;
 44            case DataStore_Cursor.CursorType.HOVER:
 545                SetHoverCursor();
 46                break;
 47        }
 548    }
 49
 50    private void AllUIVisible_OnChange(bool current, bool previous)
 51    {
 1452        isAllUIHidden = current;
 53
 1454        DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>();
 1455        ChangeCursorVisible(data.cursorVisible.Get(), false);
 1456    }
 57
 58    private void ChangeCursorVisible(bool current, bool previous)
 59    {
 5160        if (current && !isAllUIHidden)
 161            Show();
 62        else
 5063            Hide();
 5064    }
 65
 66    public void Show()
 67    {
 168        if (cursorImage == null) return;
 269        if (cursorImage.gameObject.activeSelf) return;
 70
 071        cursorImage.gameObject.SetActive(true);
 72
 073        if (gameObject.activeSelf)
 74        {
 075            if (alphaRoutine != null) StopCoroutine(alphaRoutine);
 076            alphaRoutine = StartCoroutine(SetAlpha(0f, 1f, ALPHA_INTERPOLATION_DURATION));
 77        }
 078    }
 79
 80    public void Hide()
 81    {
 5082        if (cursorImage == null) return;
 5083        if (!cursorImage.gameObject.activeSelf) return;
 84
 5085        if (gameObject.activeSelf)
 86        {
 5087            if (alphaRoutine != null) StopCoroutine(alphaRoutine);
 5088            alphaRoutine = StartCoroutine(SetAlpha(1f, 0f, ALPHA_INTERPOLATION_DURATION,
 489                () => cursorImage.gameObject.SetActive(false)));
 5090        }
 91        else
 092            cursorImage.gameObject.SetActive(false);
 093    }
 94
 95    public void SetNormalCursor()
 96    {
 897        cursorImage.sprite = normalCursor;
 898        cursorImage.SetNativeSize();
 899    }
 100
 101    public void SetHoverCursor()
 102    {
 5103        cursorImage.sprite = hoverCursor;
 5104        cursorImage.SetNativeSize();
 5105    }
 106
 107    private IEnumerator SetAlpha(float from, float to, float duration, Action callback = null)
 108    {
 50109        var time = 0f;
 110
 345111        while (time < duration)
 112        {
 341113            time += Time.deltaTime;
 341114            canvasGroup.alpha = Mathf.Lerp(from, to, time / duration);
 341115            yield return null;
 116        }
 117
 4118        canvasGroup.alpha = to;
 4119        callback?.Invoke();
 4120    }
 121}