< Summary

Class:DCL.Components.UIScreenSpace
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScreenSpace/UIScreenSpace.cs
Covered lines:78
Uncovered lines:9
Coverable lines:87
Total lines:191
Line coverage:89.6% (78 of 87)
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%330100%
OnChangeSceneUI(...)0%110100%
OnPlayerWorldPositionChanged(...)0%3.043083.33%
AllUIHidden_OnChange(...)0%110100%
UpdateCanvasVisibility()0%990100%
InitializeCanvas()0%6.036090.7%

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        private bool isInsideSceneBounds;
 13922        private BaseVariable<bool> isUIEnabled => DataStore.i.HUDs.isSceneUIEnabled;
 23        private HUDCanvasCameraModeController hudCanvasCameraModeController;
 24
 4425        public UIScreenSpace()
 26        {
 4427            CommonScriptableObjects.playerWorldPosition.OnChange += OnPlayerWorldPositionChanged;
 4428            DataStore.i.HUDs.isSceneUIEnabled.OnChange += OnChangeSceneUI;
 4429            OnChangeSceneUI(isUIEnabled.Get(), true);
 4430            model = new Model();
 4431        }
 32
 033        public override int GetClassId() { return (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE; }
 34
 35        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 36        {
 037            Debug.LogError(
 38                "Aborted UIScreenShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 039        }
 40
 041        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 42
 43        private bool initialized = false;
 44
 45        public override IEnumerator ApplyChanges(BaseModel newModel)
 46        {
 5247            if (!initialized)
 48            {
 4449                InitializeCanvas();
 4450                initialized = true;
 4451            }
 52            else
 53            {
 854                OnPlayerWorldPositionChanged(CommonScriptableObjects.playerWorldPosition.Get(), CommonScriptableObjects.
 55            }
 56
 57            //We have to wait a frame for the Canvas Scaler to act
 5258            yield return null;
 5259        }
 60
 61        public override void Dispose()
 62        {
 1963            hudCanvasCameraModeController?.Dispose();
 1964            CommonScriptableObjects.playerWorldPosition.OnChange -= OnPlayerWorldPositionChanged;
 1965            DataStore.i.HUDs.isSceneUIEnabled.OnChange -= OnChangeSceneUI;
 1966            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 67
 1968            if (childHookRectTransform != null)
 69            {
 1970                Utils.SafeDestroy(childHookRectTransform.gameObject);
 71            }
 1972        }
 73
 74        void OnChangeSceneUI(bool current, bool previous)
 75        {
 4476            UpdateCanvasVisibility();
 4477        }
 78
 79        void OnPlayerWorldPositionChanged(Vector3 current, Vector3 previous)
 80        {
 679381            if (canvas == null)
 673582                return;
 83
 5884            UpdateCanvasVisibility();
 85
 5886            if (VERBOSE)
 87            {
 088                Debug.Log($"set screenspace = {current}");
 89            }
 5890        }
 91
 10892        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 93
 94        private void UpdateCanvasVisibility()
 95        {
 20096            if (canvas == null || scene == null)
 9897                return;
 98
 10299            var model = (Model) this.model;
 100
 102101            isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(CommonScriptableObjects.player
 102
 102103            if (isInsideSceneBounds)
 104            {
 102105                DataStore.i.Get<DataStore_World>().currentRaycaster.Set(graphicRaycaster);
 106            }
 107
 102108            bool shouldBeVisible = model.visible && isInsideSceneBounds && !CommonScriptableObjects.allUIHidden.Get() &&
 109
 102110            canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 102111            canvasGroup.blocksRaycasts = shouldBeVisible;
 102112        }
 113
 114        void InitializeCanvas()
 115        {
 44116            if (VERBOSE)
 117            {
 0118                Debug.Log("Started canvas initialization in " + id);
 119            }
 120
 44121            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 44122            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 44123            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 44124            canvasGameObject.transform.ResetLocalTRS();
 125
 126            // Canvas
 44127            canvas = canvasGameObject.AddComponent<Canvas>();
 44128            hudCanvasCameraModeController = new HUDCanvasCameraModeController(canvas, DataStore.i.camera.hudsCamera);
 129
 130            // Canvas Scaler (for maintaining ui aspect ratio)
 44131            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 44132            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 44133            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 44134            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 135
 136            // Graphics Raycaster (for allowing touch/click input on the ui components)
 44137            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 138
 44139            canvas.sortingOrder = -1;
 140
 44141            if (scene.isPersistent && scene.sceneData.id != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID)
 142            {
 44143                canvas.sortingOrder -= 1;
 144            }
 145
 146            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 44147            GameObject resizedPanel = new GameObject("ResizeUIArea");
 148
 44149            resizedPanel.AddComponent<CanvasRenderer>();
 44150            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 44151            childHookRectTransform.SetParent(canvas.transform);
 44152            childHookRectTransform.ResetLocalTRS();
 153
 44154            childHookRectTransform.anchorMin = Vector2.zero;
 44155            childHookRectTransform.anchorMax = new Vector2(1, 0);
 156
 157            // We scale the panel downwards to subtract the viewport's top 10%
 44158            float canvasHeight = canvasScaler.referenceResolution.y;
 44159            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 44160            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 44161            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction);
 162
 163            // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar
 44164            canvasHeight = childHookRectTransform.sizeDelta.y;
 44165            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 44166            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 44167            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 168
 169            // Canvas group
 44170            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 44171            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 44172            canvasGroup.blocksRaycasts = false;
 173
 44174            if (VERBOSE)
 175            {
 0176                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0177                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 178            }
 179
 44180            OnPlayerWorldPositionChanged(CommonScriptableObjects.playerWorldPosition, CommonScriptableObjects.playerWorl
 181
 44182            if (VERBOSE)
 183            {
 0184                Debug.Log("Finished canvas initialization in " + id);
 185            }
 186
 44187            UpdateCanvasVisibility();
 44188            CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange;
 44189        }
 190    }
 191}