< 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
 4223        public UIScreenSpace()
 24        {
 4225            DCLCharacterController.OnCharacterMoved += OnCharacterMoved;
 4226            model = new Model();
 4227        }
 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        {
 5043            var model = (Model) newModel;
 44
 5045            if (!initialized)
 46            {
 4247                InitializeCanvas();
 4248                initialized = true;
 4249            }
 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
 5056            yield return null;
 5057        }
 58
 59        public override void Dispose()
 60        {
 6061            DCLCharacterController.OnCharacterMoved -= OnCharacterMoved;
 6062            CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange;
 63
 6064            if (childHookRectTransform != null)
 65            {
 4266                Utils.SafeDestroy(childHookRectTransform.gameObject);
 67            }
 6068        }
 69
 70        void OnCharacterMoved(DCLCharacterPosition newCharacterPosition)
 71        {
 24772            if (canvas != null)
 73            {
 24774                currentCharacterPosition = newCharacterPosition;
 75
 24776                UpdateCanvasVisibility();
 77
 24778                if (VERBOSE)
 79                {
 080                    Debug.Log($"set screenspace = {currentCharacterPosition}");
 81                }
 82            }
 24783        }
 84
 085        private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); }
 86
 87        private void UpdateCanvasVisibility()
 88        {
 24789            if (canvas != null && scene != null)
 90            {
 24791                var model = (Model) this.model;
 92
 24793                bool isInsideSceneBounds = scene.IsInsideSceneBoundaries(Utils.WorldToGridPosition(currentCharacterPosit
 24794                bool shouldBeVisible = scene.isPersistent || (model.visible && isInsideSceneBounds && !CommonScriptableO
 24795                canvasGroup.alpha = shouldBeVisible ? 1f : 0f;
 24796                canvasGroup.blocksRaycasts = shouldBeVisible;
 97            }
 24798        }
 99
 100        void InitializeCanvas()
 101        {
 42102            if (VERBOSE)
 103            {
 0104                Debug.Log("Started canvas initialization in " + id);
 105            }
 106
 42107            GameObject canvasGameObject = new GameObject("UIScreenSpace");
 42108            canvasGameObject.layer = LayerMask.NameToLayer("UI");
 42109            canvasGameObject.transform.SetParent(scene.GetSceneTransform());
 42110            canvasGameObject.transform.ResetLocalTRS();
 111
 112            // Canvas
 42113            canvas = canvasGameObject.AddComponent<Canvas>();
 42114            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
 115
 116            // Canvas Scaler (for maintaining ui aspect ratio)
 42117            CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>();
 42118            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
 42119            canvasScaler.referenceResolution = new Vector2(1280f, 720f);
 42120            canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects
 121
 122            // Graphics Raycaster (for allowing touch/click input on the ui components)
 42123            graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>();
 124
 42125            canvas.sortingOrder = -1;
 126
 127            // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot
 42128            GameObject resizedPanel = new GameObject("ResizeUIArea");
 129
 42130            resizedPanel.AddComponent<CanvasRenderer>();
 42131            childHookRectTransform = resizedPanel.AddComponent<RectTransform>();
 42132            childHookRectTransform.SetParent(canvas.transform);
 42133            childHookRectTransform.ResetLocalTRS();
 134
 42135            childHookRectTransform.anchorMin = Vector2.zero;
 42136            childHookRectTransform.anchorMax = new Vector2(1, 0);
 137
 138            // We scale the panel downwards to subtract the viewport's top 10%
 42139            float canvasHeight = canvasScaler.referenceResolution.y;
 42140            childHookRectTransform.pivot = new Vector2(0.5f, 0f);
 42141            float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100;
 42142            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
 42145            canvasHeight = childHookRectTransform.sizeDelta.y;
 42146            childHookRectTransform.pivot = new Vector2(0.5f, 1f);
 42147            childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f);
 42148            childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2);
 149
 150            // Canvas group
 42151            canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>();
 42152            canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene
 42153            canvasGroup.blocksRaycasts = false;
 154
 42155            if (VERBOSE)
 156            {
 0157                Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width);
 0158                Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height);
 159            }
 160
 42161            if (DCLCharacterController.i != null)
 162            {
 42163                OnCharacterMoved(DCLCharacterController.i.characterPosition);
 164            }
 165
 42166            if (VERBOSE)
 167            {
 0168                Debug.Log("Finished canvas initialization in " + id);
 169            }
 170
 42171            if (!scene.isPersistent)
 172            {
 0173                UpdateCanvasVisibility();
 0174                CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange;
 175            }
 42176        }
 177    }
 178}