| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7; |
| | 3 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Models; |
| | 7 | | using Decentraland.Common; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using Screen = UnityEngine.Device.Screen; |
| | 11 | |
|
| | 12 | | namespace 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 | |
|
| 3 | 21 | | private float lastDevicePixelRatio = -1; |
| 3 | 22 | | private int lastViewportResolutionWidth = -1; |
| 3 | 23 | | private int lastScreenRealResolutionWidth = -1; |
| | 24 | |
|
| 3 | 25 | | public ECSUiCanvasInformationSystem( |
| | 26 | | IReadOnlyDictionary<int, ComponentWriter> componentsWriter, |
| | 27 | | WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>> componentPool, |
| | 28 | | BaseList<IParcelScene> loadedScenes) |
| | 29 | | { |
| 3 | 30 | | this.loadedScenes = loadedScenes; |
| 3 | 31 | | this.componentsWriter = componentsWriter; |
| 3 | 32 | | this.componentPool = componentPool; |
| | 33 | |
|
| 3 | 34 | | loadedScenes.OnAdded += OnSceneAdded; |
| | 35 | |
|
| 3 | 36 | | interactableArea = new BorderRect() |
| | 37 | | { |
| | 38 | | Bottom = 0, |
| | 39 | | Left = 0, |
| | 40 | | Right = 0, |
| | 41 | | Top = 0 |
| | 42 | | }; |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Dispose() |
| | 46 | | { |
| 1 | 47 | | loadedScenes.OnAdded -= OnSceneAdded; |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | public void Update() |
| | 51 | | { |
| 2 | 52 | | if (lastViewportResolutionWidth == Screen.width && lastScreenRealResolutionWidth == Screen.currentResolution |
| 0 | 53 | | return; |
| | 54 | |
|
| | 55 | | // 'Screen.mainWindowDisplayInfo' cannot be used in WebGL due to 'Screen.GetDisplayInfoForPoint' unsupported |
| 2 | 56 | | lastScreenRealResolutionWidth = Screen.currentResolution.width; |
| 2 | 57 | | lastViewportResolutionWidth = Screen.width; |
| 2 | 58 | | lastDevicePixelRatio = CalculateDevicePixelRatio(lastViewportResolutionWidth, lastScreenRealResolutionWidth) |
| | 59 | |
|
| 2 | 60 | | int scenesCount = loadedScenes.Count; |
| 8 | 61 | | for (int i = 0; i < scenesCount; i++) |
| | 62 | | { |
| 2 | 63 | | UpdateUiCanvasInformationComponent(loadedScenes[i].sceneData.sceneNumber); |
| | 64 | | } |
| 2 | 65 | | } |
| | 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 | | */ |
| 2 | 76 | | private static float CalculateDevicePixelRatio(int viewportPixelsWidth, int realScreenPixelsWidth) => realScreen |
| | 77 | |
|
| | 78 | | private void UpdateUiCanvasInformationComponent(int sceneNumber) |
| | 79 | | { |
| 3 | 80 | | if (!componentsWriter.TryGetValue(sceneNumber, out var writer)) |
| 0 | 81 | | return; |
| | 82 | |
|
| 3 | 83 | | var componentPooled = componentPool.Get(); |
| 3 | 84 | | var componentModel = componentPooled.WrappedComponent.Model; |
| 3 | 85 | | componentModel.InteractableArea = interactableArea; |
| 3 | 86 | | componentModel.Width = Screen.width; |
| 3 | 87 | | componentModel.Height = Screen.height; |
| 3 | 88 | | componentModel.DevicePixelRatio = lastDevicePixelRatio; |
| | 89 | |
|
| 3 | 90 | | writer.Put(SpecialEntityId.SCENE_ROOT_ENTITY, ComponentID.UI_CANVAS_INFORMATION, componentPooled); |
| 3 | 91 | | } |
| | 92 | |
|
| | 93 | | private void OnSceneAdded(IParcelScene scene) |
| | 94 | | { |
| 1 | 95 | | UpdateUiCanvasInformationComponent(scene.sceneData.sceneNumber); |
| 1 | 96 | | } |
| | 97 | | } |
| | 98 | | } |