< Summary

Class:DCL.Components.UIScreenSpace
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScreenSpace/UIScreenSpace.cs
Covered lines:66
Uncovered lines:14
Coverable lines:80
Total lines:177
Line coverage:82.5% (66 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIScreenSpace()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges()0%550100%
Dispose()0%220100%
OnCharacterMoved(...)0%3.213071.43%
AllUIHidden_OnChange(...)0%2100%
UpdateCanvasVisibility()0%8.128087.5%
InitializeCanvas()0%6.16086.05%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScreenSpace/UIScreenSpace.cs

#LineLine coverage
 1using DCL.Configuration;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using System;
 6using System.Collections;
 7using System.Collections.Generic;
 8using UnityEngine;
 9using UnityEngine.UI;
 10
 11namespace DCL.Components
 12{
 13    public class UIScreenSpace : UIShape
 14    {
 15        static bool VERBOSE = false;
 16
 17        public Canvas canvas;
 18        public GraphicRaycaster graphicRaycaster;
 19
 20        private DCLCharacterPosition currentCharacterPosition;
 21        private CanvasGroup canvasGroup;
 22
 4323        public UIScreenSpace()
 24        {
 4325            DCLCharacterController.OnCharacterMoved += OnCharacterMoved;
 4326            model = new Model();
 4327        }
 28
 029        public override int GetClassId() { return (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE; }
 30
 31        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 32        {
 033            Debug.LogError(
 34                "Aborted UIScreenShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 035        }
 36
 037        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 38
 39        private bool initialized = false;
 40
 41        public override IEnumerator ApplyChanges(BaseModel newModel)
 42        {
 5143            if (!initialized)
 44            {
 4345                InitializeCanvas();
 4346                initialized = true;
 4347            }
 848            else if (DCLCharacterController.i != null)
 49            {
 850                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 51            }
 52
 53            //We have to wait a frame for the Canvas Scaler to act
 5154            yield return null;
 5155        }
 56
 57        public override void Dispose()
 58        {
 6259            DCLCharacterController.OnCharacterMoved -= OnCharacterMoved;
 6260            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 61
 6262            if (childHookRectTransform != null)
 63            {
 4364                Utils.SafeDestroy(childHookRectTransform.gameObject);
 65            }
 6266        }
 67
 68        void OnCharacterMoved(DCLCharacterPosition newCharacterPosition)
 69        {
 12770            if (canvas == null)
 071                return;
 72
 12773            currentCharacterPosition = newCharacterPosition;
 74
 12775            UpdateCanvasVisibility();
 76
 12777            if (VERBOSE)
 78            {
 079                Debug.Log($"set screenspace = {currentCharacterPosition}");
 80            }
 12781        }
 82
 083        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 84
 85        private void UpdateCanvasVisibility()
 86        {
 12787            if (canvas == null || scene == null)
 088                return;
 89
 12790            var model = (Model) this.model;
 91
 12792            bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(currentCharacterPosition.
 12793            bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableObjec
 94
 12795            canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 12796            canvasGroup.blocksRaycasts = shouldBeVisible;
 12797        }
 98
 99        void InitializeCanvas()
 100        {
 43101            if (VERBOSE)
 102            {
 0103                Debug.Log("Started canvas initialization in " + id);
 104            }
 105
 43106            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 43107            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 43108            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 43109            canvasGameObject.transform.ResetLocalTRS();
 110
 111            // Canvas
 43112            canvas = canvasGameObject.AddComponent<Canvas>();
 43113            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
 114
 115            // Canvas Scaler (for maintaining ui aspect ratio)
 43116            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 43117            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 43118            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 43119            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 120
 121            // Graphics Raycaster (for allowing touch/click input on the ui components)
 43122            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 123
 43124            canvas.sortingOrder = -1;
 125
 126            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 43127            GameObject resizedPanel = new GameObject("ResizeUIArea");
 128
 43129            resizedPanel.AddComponent<CanvasRenderer>();
 43130            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 43131            childHookRectTransform.SetParent(canvas.transform);
 43132            childHookRectTransform.ResetLocalTRS();
 133
 43134            childHookRectTransform.anchorMin = Vector2.zero;
 43135            childHookRectTransform.anchorMax = new Vector2(1, 0);
 136
 137            // We scale the panel downwards to subtract the viewport's top 10%
 43138            float canvasHeight = canvasScaler.referenceResolution.y;
 43139            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 43140            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 43141            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction);
 142
 143            // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar
 43144            canvasHeight = childHookRectTransform.sizeDelta.y;
 43145            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 43146            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 43147            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 148
 149            // Canvas group
 43150            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 43151            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 43152            canvasGroup.blocksRaycasts = false;
 153
 43154            if (VERBOSE)
 155            {
 0156                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0157                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 158            }
 159
 43160            if (DCLCharacterController.i != null)
 161            {
 43162                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 163            }
 164
 43165            if (VERBOSE)
 166            {
 0167                Debug.Log("Finished canvas initialization in " + id);
 168            }
 169
 43170            if (!scene.isPersistent)
 171            {
 0172                UpdateCanvasVisibility();
 0173                CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange;
 174            }
 43175        }
 176    }
 177}