< Summary

Class:ECSSystems.UiCanvasInformationSystem.ECSUiCanvasInformationSystem
Assembly:ECS7Plugin.Systems.UiCanvasInformation
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/Systems/UiCanvasInformationSystem/ECSUiCanvasInformationSystem.cs
Covered lines:32
Uncovered lines:2
Coverable lines:34
Total lines:98
Line coverage:94.1% (32 of 34)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:6
Method coverage:100% (6 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSUiCanvasInformationSystem(...)0%110100%
Dispose()0%110100%
Update()0%4.014090.91%
CalculateDevicePixelRatio(...)0%110100%
UpdateUiCanvasInformationComponent(...)0%22090%
OnSceneAdded(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/Systems/UiCanvasInformationSystem/ECSUiCanvasInformationSystem.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7;
 3using DCL.ECS7.ComponentWrapper.Generic;
 4using DCL.ECSComponents;
 5using DCL.ECSRuntime;
 6using DCL.Models;
 7using Decentraland.Common;
 8using System;
 9using System.Collections.Generic;
 10using Screen = UnityEngine.Device.Screen;
 11
 12namespace ECSSystems.UiCanvasInformationSystem
 13{
 14    public class ECSUiCanvasInformationSystem : IDisposable
 15    {
 16        private readonly IReadOnlyDictionary<int, ComponentWriter> componentsWriter;
 17        private readonly WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>> componentPool;
 18        private readonly BaseList<IParcelScene> loadedScenes;
 19        private readonly BorderRect interactableArea;
 20
 321        private float lastDevicePixelRatio = -1;
 322        private int lastViewportResolutionWidth = -1;
 323        private int lastScreenRealResolutionWidth = -1;
 24
 325        public ECSUiCanvasInformationSystem(
 26            IReadOnlyDictionary<int, ComponentWriter> componentsWriter,
 27            WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>> componentPool,
 28            BaseList<IParcelScene> loadedScenes)
 29        {
 330            this.loadedScenes = loadedScenes;
 331            this.componentsWriter = componentsWriter;
 332            this.componentPool = componentPool;
 33
 334            loadedScenes.OnAdded += OnSceneAdded;
 35
 336            interactableArea = new BorderRect()
 37            {
 38                Bottom = 0,
 39                Left = 0,
 40                Right = 0,
 41                Top = 0
 42            };
 343        }
 44
 45        public void Dispose()
 46        {
 147            loadedScenes.OnAdded -= OnSceneAdded;
 148        }
 49
 50        public void Update()
 51        {
 252            if (lastViewportResolutionWidth == Screen.width && lastScreenRealResolutionWidth == Screen.currentResolution
 053                return;
 54
 55            // 'Screen.mainWindowDisplayInfo' cannot be used in WebGL due to 'Screen.GetDisplayInfoForPoint' unsupported
 256            lastScreenRealResolutionWidth = Screen.currentResolution.width;
 257            lastViewportResolutionWidth = Screen.width;
 258            lastDevicePixelRatio = CalculateDevicePixelRatio(lastViewportResolutionWidth, lastScreenRealResolutionWidth)
 59
 260            int scenesCount = loadedScenes.Count;
 861            for (int i = 0; i < scenesCount; i++)
 62            {
 263                UpdateUiCanvasInformationComponent(loadedScenes[i].sceneData.sceneNumber);
 64            }
 265        }
 66
 67        /*
 68         * Calculates what web browsers call "device pixel ratio": a ratio between the application virtual resolution
 69         * and the screen resolution. It represents how many physical/real pixels are needed to draw the virtual pixels
 70         * of the application. The smaller the application resolution is, compared to the real screen resolution, the
 71         * higher the device pixel ratio is. Normally the result can be between 1~3.
 72         *
 73         * Note: Explorer's 'rendering scale' doesn't affect this, as that is only for 3D rendering, so it doesn't affec
 74         * the whole application resolution (e.g. UIs are unaffected).
 75        */
 276        private static float CalculateDevicePixelRatio(int viewportPixelsWidth, int realScreenPixelsWidth) => realScreen
 77
 78        private void UpdateUiCanvasInformationComponent(int sceneNumber)
 79        {
 380            if (!componentsWriter.TryGetValue(sceneNumber, out var writer))
 081                return;
 82
 383            var componentPooled = componentPool.Get();
 384            var componentModel = componentPooled.WrappedComponent.Model;
 385            componentModel.InteractableArea = interactableArea;
 386            componentModel.Width = Screen.width;
 387            componentModel.Height = Screen.height;
 388            componentModel.DevicePixelRatio = lastDevicePixelRatio;
 89
 390            writer.Put(SpecialEntityId.SCENE_ROOT_ENTITY, ComponentID.UI_CANVAS_INFORMATION, componentPooled);
 391        }
 92
 93        private void OnSceneAdded(IParcelScene scene)
 94        {
 195            UpdateUiCanvasInformationComponent(scene.sceneData.sceneNumber);
 196        }
 97    }
 98}