< 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:67
Uncovered lines:12
Coverable lines:79
Total lines:178
Line coverage:84.8% (67 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%550100%
Dispose()0%220100%
OnCharacterMoved(...)0%3.043083.33%
AllUIHidden_OnChange(...)0%2100%
UpdateCanvasVisibility()0%880100%
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            var model = (Model) newModel;
 44
 5145            if (!initialized)
 46            {
 4347                InitializeCanvas();
 4348                initialized = true;
 4349            }
 850            else if (DCLCharacterController.i != null)
 51            {
 852                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 53            }
 54
 55            //We have to wait a frame for the Canvas Scaler to act
 5156            yield return null;
 5157        }
 58
 59        public override void Dispose()
 60        {
 6261            DCLCharacterController.OnCharacterMoved -= OnCharacterMoved;
 6262            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 63
 6264            if (childHookRectTransform != null)
 65            {
 4366                Utils.SafeDestroy(childHookRectTransform.gameObject);
 67            }
 6268        }
 69
 70        void OnCharacterMoved(DCLCharacterPosition newCharacterPosition)
 71        {
 12972            if (canvas != null)
 73            {
 12974                currentCharacterPosition = newCharacterPosition;
 75
 12976                UpdateCanvasVisibility();
 77
 12978                if (VERBOSE)
 79                {
 080                    Debug.Log($"set screenspace = {currentCharacterPosition}");
 81                }
 82            }
 12983        }
 84
 085        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 86
 87        private void UpdateCanvasVisibility()
 88        {
 12989            if (canvas != null && scene != null)
 90            {
 12991                var model = (Model) this.model;
 92
 12993                bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(currentCharacterPosit
 12994                bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableO
 12995                canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 12996                canvasGroup.blocksRaycasts = shouldBeVisible;
 97            }
 12998        }
 99
 100        void InitializeCanvas()
 101        {
 43102            if (VERBOSE)
 103            {
 0104                Debug.Log("Started canvas initialization in " + id);
 105            }
 106
 43107            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 43108            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 43109            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 43110            canvasGameObject.transform.ResetLocalTRS();
 111
 112            // Canvas
 43113            canvas = canvasGameObject.AddComponent<Canvas>();
 43114            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
 115
 116            // Canvas Scaler (for maintaining ui aspect ratio)
 43117            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 43118            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 43119            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 43120            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 121
 122            // Graphics Raycaster (for allowing touch/click input on the ui components)
 43123            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 124
 43125            canvas.sortingOrder = -1;
 126
 127            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 43128            GameObject resizedPanel = new GameObject("ResizeUIArea");
 129
 43130            resizedPanel.AddComponent<CanvasRenderer>();
 43131            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 43132            childHookRectTransform.SetParent(canvas.transform);
 43133            childHookRectTransform.ResetLocalTRS();
 134
 43135            childHookRectTransform.anchorMin = Vector2.zero;
 43136            childHookRectTransform.anchorMax = new Vector2(1, 0);
 137
 138            // We scale the panel downwards to subtract the viewport's top 10%
 43139            float canvasHeight = canvasScaler.referenceResolution.y;
 43140            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 43141            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 43142            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction);
 143
 144            // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar
 43145            canvasHeight = childHookRectTransform.sizeDelta.y;
 43146            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 43147            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 43148            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 149
 150            // Canvas group
 43151            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 43152            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 43153            canvasGroup.blocksRaycasts = false;
 154
 43155            if (VERBOSE)
 156            {
 0157                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0158                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 159            }
 160
 43161            if (DCLCharacterController.i != null)
 162            {
 43163                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 164            }
 165
 43166            if (VERBOSE)
 167            {
 0168                Debug.Log("Finished canvas initialization in " + id);
 169            }
 170
 43171            if (!scene.isPersistent)
 172            {
 0173                UpdateCanvasVisibility();
 0174                CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange;
 175            }
 43176        }
 177    }
 178}