< 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
 4423        public UIScreenSpace()
 24        {
 4425            DCLCharacterController.OnCharacterMoved += OnCharacterMoved;
 4426            model = new Model();
 4427        }
 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        {
 5243            if (!initialized)
 44            {
 4445                InitializeCanvas();
 4446                initialized = true;
 4447            }
 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
 5254            yield return null;
 5255        }
 56
 57        public override void Dispose()
 58        {
 6359            DCLCharacterController.OnCharacterMoved -= OnCharacterMoved;
 6360            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 61
 6362            if (childHookRectTransform != null)
 63            {
 4464                Utils.SafeDestroy(childHookRectTransform.gameObject);
 65            }
 6366        }
 67
 68        void OnCharacterMoved(DCLCharacterPosition newCharacterPosition)
 69        {
 13570            if (canvas == null)
 071                return;
 72
 13573            currentCharacterPosition = newCharacterPosition;
 74
 13575            UpdateCanvasVisibility();
 76
 13577            if (VERBOSE)
 78            {
 079                Debug.Log($"set screenspace = {currentCharacterPosition}");
 80            }
 13581        }
 82
 083        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 84
 85        private void UpdateCanvasVisibility()
 86        {
 13587            if (canvas == null || scene == null)
 088                return;
 89
 13590            var model = (Model) this.model;
 91
 13592            bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(currentCharacterPosition.
 13593            bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableObjec
 94
 13595            canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 13596            canvasGroup.blocksRaycasts = shouldBeVisible;
 13597        }
 98
 99        void InitializeCanvas()
 100        {
 44101            if (VERBOSE)
 102            {
 0103                Debug.Log("Started canvas initialization in " + id);
 104            }
 105
 44106            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 44107            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 44108            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 44109            canvasGameObject.transform.ResetLocalTRS();
 110
 111            // Canvas
 44112            canvas = canvasGameObject.AddComponent<Canvas>();
 44113            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
 114
 115            // Canvas Scaler (for maintaining ui aspect ratio)
 44116            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 44117            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 44118            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 44119            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 120
 121            // Graphics Raycaster (for allowing touch/click input on the ui components)
 44122            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 123
 44124            canvas.sortingOrder = -1;
 125
 126            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 44127            GameObject resizedPanel = new GameObject("ResizeUIArea");
 128
 44129            resizedPanel.AddComponent<CanvasRenderer>();
 44130            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 44131            childHookRectTransform.SetParent(canvas.transform);
 44132            childHookRectTransform.ResetLocalTRS();
 133
 44134            childHookRectTransform.anchorMin = Vector2.zero;
 44135            childHookRectTransform.anchorMax = new Vector2(1, 0);
 136
 137            // We scale the panel downwards to subtract the viewport's top 10%
 44138            float canvasHeight = canvasScaler.referenceResolution.y;
 44139            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 44140            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 44141            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
 44144            canvasHeight = childHookRectTransform.sizeDelta.y;
 44145            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 44146            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 44147            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 148
 149            // Canvas group
 44150            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 44151            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 44152            canvasGroup.blocksRaycasts = false;
 153
 44154            if (VERBOSE)
 155            {
 0156                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0157                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 158            }
 159
 44160            if (DCLCharacterController.i != null)
 161            {
 44162                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 163            }
 164
 44165            if (VERBOSE)
 166            {
 0167                Debug.Log("Finished canvas initialization in " + id);
 168            }
 169
 44170            if (!scene.isPersistent)
 171            {
 0172                UpdateCanvasVisibility();
 0173                CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange;
 174            }
 44175        }
 176    }
 177}