< 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:13
Coverable lines:79
Total lines:177
Line coverage:83.5% (66 of 79)
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%440100%
Dispose()0%220100%
OnPlayerWorldPositionChanged(...)0%3.043083.33%
AllUIHidden_OnChange(...)0%2100%
UpdateCanvasVisibility()0%8.128087.5%
InitializeCanvas()0%7.127086.36%

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 CanvasGroup canvasGroup;
 21
 4422        public UIScreenSpace()
 23        {
 4424            CommonScriptableObjects.playerWorldPosition.OnChange += OnPlayerWorldPositionChanged;
 4425            model = new Model();
 4426        }
 27
 028        public override int GetClassId() { return (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE; }
 29
 30        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 31        {
 032            Debug.LogError(
 33                "Aborted UIScreenShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 034        }
 35
 036        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 37
 38        private bool initialized = false;
 39
 40        public override IEnumerator ApplyChanges(BaseModel newModel)
 41        {
 5242            if (!initialized)
 43            {
 4444                InitializeCanvas();
 4445                initialized = true;
 4446            }
 47            else
 48            {
 849                OnPlayerWorldPositionChanged(CommonScriptableObjects.playerWorldPosition.Get(), CommonScriptableObjects.
 50            }
 51
 52            //We have to wait a frame for the Canvas Scaler to act
 5253            yield return null;
 5254        }
 55
 56        public override void Dispose()
 57        {
 1958            CommonScriptableObjects.playerWorldPosition.OnChange -= OnPlayerWorldPositionChanged;
 59            //DCLCharacterController.OnCharacterMoved -= OnCharacterMoved;
 1960            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 61
 1962            if (childHookRectTransform != null)
 63            {
 1964                Utils.SafeDestroy(childHookRectTransform.gameObject);
 65            }
 1966        }
 67
 68        void OnPlayerWorldPositionChanged(Vector3 current, Vector3 previous)
 69        {
 2091070            if (canvas == null)
 2085271                return;
 72
 5873            UpdateCanvasVisibility();
 74
 5875            if (VERBOSE)
 76            {
 077                Debug.Log($"set screenspace = {current}");
 78            }
 5879        }
 80
 081        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 82
 83        private void UpdateCanvasVisibility()
 84        {
 5885            if (canvas == null || scene == null)
 086                return;
 87
 5888            var model = (Model) this.model;
 89
 5890            bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(CommonScriptableObjects.p
 5891            bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableObjec
 92
 5893            canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 5894            canvasGroup.blocksRaycasts = shouldBeVisible;
 5895        }
 96
 97        void InitializeCanvas()
 98        {
 4499            if (VERBOSE)
 100            {
 0101                Debug.Log("Started canvas initialization in " + id);
 102            }
 103
 44104            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 44105            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 44106            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 44107            canvasGameObject.transform.ResetLocalTRS();
 108
 109            // Canvas
 44110            canvas = canvasGameObject.AddComponent<Canvas>();
 44111            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
 112
 113            // Canvas Scaler (for maintaining ui aspect ratio)
 44114            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 44115            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 44116            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 44117            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 118
 119            // Graphics Raycaster (for allowing touch/click input on the ui components)
 44120            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 121
 44122            canvas.sortingOrder = -1;
 123
 44124            if (scene.isPersistent && scene.sceneData.id != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID)
 125            {
 44126                canvas.sortingOrder -= 1;
 127            }
 128
 129            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 44130            GameObject resizedPanel = new GameObject("ResizeUIArea");
 131
 44132            resizedPanel.AddComponent<CanvasRenderer>();
 44133            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 44134            childHookRectTransform.SetParent(canvas.transform);
 44135            childHookRectTransform.ResetLocalTRS();
 136
 44137            childHookRectTransform.anchorMin = Vector2.zero;
 44138            childHookRectTransform.anchorMax = new Vector2(1, 0);
 139
 140            // We scale the panel downwards to subtract the viewport's top 10%
 44141            float canvasHeight = canvasScaler.referenceResolution.y;
 44142            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 44143            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 44144            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction);
 145
 146            // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar
 44147            canvasHeight = childHookRectTransform.sizeDelta.y;
 44148            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 44149            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 44150            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 151
 152            // Canvas group
 44153            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 44154            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 44155            canvasGroup.blocksRaycasts = false;
 156
 44157            if (VERBOSE)
 158            {
 0159                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0160                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 161            }
 162
 44163            OnPlayerWorldPositionChanged(CommonScriptableObjects.playerWorldPosition, CommonScriptableObjects.playerWorl
 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}