| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.UI; |
| | 10 | |
|
| | 11 | | namespace 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; |
| 133 | 22 | | private BaseVariable<bool> isUIEnabled => DataStore.i.HUDs.isSceneUIEnabled; |
| | 23 | | private HUDCanvasCameraModeController hudCanvasCameraModeController; |
| 44 | 24 | | private readonly DataStore_Player dataStorePlayer = DataStore.i.player; |
| | 25 | |
|
| 44 | 26 | | public UIScreenSpace() |
| | 27 | | { |
| 44 | 28 | | dataStorePlayer.playerGridPosition.OnChange += OnPlayerCoordinatesChanged; |
| 44 | 29 | | DataStore.i.HUDs.isSceneUIEnabled.OnChange += OnChangeSceneUI; |
| 44 | 30 | | OnChangeSceneUI(isUIEnabled.Get(), true); |
| 44 | 31 | | model = new Model(); |
| 44 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public override int GetClassId() { return (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE; } |
| | 35 | |
|
| | 36 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 37 | | { |
| 0 | 38 | | Debug.LogError( |
| | 39 | | "Aborted UIScreenShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 43 | |
|
| | 44 | | private bool initialized = false; |
| | 45 | |
|
| | 46 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 47 | | { |
| 52 | 48 | | if (!initialized) |
| | 49 | | { |
| 44 | 50 | | InitializeCanvas(); |
| 44 | 51 | | initialized = true; |
| 44 | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 8 | 55 | | OnPlayerCoordinatesChanged(dataStorePlayer.playerGridPosition.Get(), dataStorePlayer.playerGridPosition. |
| | 56 | | } |
| | 57 | |
|
| | 58 | | //We have to wait a frame for the Canvas Scaler to act |
| 52 | 59 | | yield return null; |
| 52 | 60 | | } |
| | 61 | |
|
| | 62 | | public override void Dispose() |
| | 63 | | { |
| 19 | 64 | | hudCanvasCameraModeController?.Dispose(); |
| 19 | 65 | | dataStorePlayer.playerGridPosition.OnChange -= OnPlayerCoordinatesChanged; |
| 19 | 66 | | DataStore.i.HUDs.isSceneUIEnabled.OnChange -= OnChangeSceneUI; |
| 19 | 67 | | CommonScriptableObjects.allUIHidden.OnChange -= AllUIHidden_OnChange; |
| | 68 | |
|
| 19 | 69 | | if (childHookRectTransform != null) |
| | 70 | | { |
| 19 | 71 | | Utils.SafeDestroy(childHookRectTransform.gameObject); |
| | 72 | | } |
| 19 | 73 | | } |
| | 74 | |
|
| | 75 | | void OnChangeSceneUI(bool current, bool previous) |
| | 76 | | { |
| 44 | 77 | | UpdateCanvasVisibility(); |
| 44 | 78 | | } |
| | 79 | |
|
| | 80 | | void OnPlayerCoordinatesChanged(Vector2Int current, Vector2Int previous) |
| | 81 | | { |
| 52 | 82 | | if (canvas == null) |
| 0 | 83 | | return; |
| | 84 | |
|
| 52 | 85 | | UpdateCanvasVisibility(); |
| | 86 | |
|
| 52 | 87 | | if (VERBOSE) |
| | 88 | | { |
| 0 | 89 | | Debug.Log($"set screenspace = {current}"); |
| | 90 | | } |
| 52 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | private void AllUIHidden_OnChange(bool current, bool previous) { UpdateCanvasVisibility(); } |
| | 94 | |
|
| | 95 | | private void UpdateCanvasVisibility() |
| | 96 | | { |
| 140 | 97 | | if (canvas == null || scene == null) |
| 44 | 98 | | return; |
| | 99 | |
|
| 96 | 100 | | var model = (Model) this.model; |
| | 101 | |
|
| 96 | 102 | | isInsideSceneBounds = scene.IsInsideSceneBoundaries(dataStorePlayer.playerGridPosition.Get()); |
| | 103 | |
|
| 96 | 104 | | if (isInsideSceneBounds) |
| | 105 | | { |
| 96 | 106 | | DataStore.i.Get<DataStore_World>().currentRaycaster.Set(graphicRaycaster); |
| | 107 | | } |
| | 108 | |
|
| 96 | 109 | | bool shouldBeVisible = model.visible && isInsideSceneBounds && !CommonScriptableObjects.allUIHidden.Get() && |
| | 110 | |
|
| 96 | 111 | | canvasGroup.alpha = shouldBeVisible ? 1f : 0f; |
| 96 | 112 | | canvasGroup.blocksRaycasts = shouldBeVisible; |
| 96 | 113 | | } |
| | 114 | |
|
| | 115 | | void InitializeCanvas() |
| | 116 | | { |
| 44 | 117 | | if (VERBOSE) |
| | 118 | | { |
| 0 | 119 | | Debug.Log("Started canvas initialization in " + id); |
| | 120 | | } |
| | 121 | |
|
| 44 | 122 | | GameObject canvasGameObject = new GameObject("UIScreenSpace"); |
| 44 | 123 | | canvasGameObject.layer = LayerMask.NameToLayer("UI"); |
| 44 | 124 | | canvasGameObject.transform.SetParent(scene.GetSceneTransform()); |
| 44 | 125 | | canvasGameObject.transform.ResetLocalTRS(); |
| | 126 | |
|
| | 127 | | // Canvas |
| 44 | 128 | | canvas = canvasGameObject.AddComponent<Canvas>(); |
| 44 | 129 | | hudCanvasCameraModeController = new HUDCanvasCameraModeController(canvas, DataStore.i.camera.hudsCamera); |
| | 130 | |
|
| | 131 | | // Canvas Scaler (for maintaining ui aspect ratio) |
| 44 | 132 | | CanvasScaler canvasScaler = canvasGameObject.AddComponent<CanvasScaler>(); |
| 44 | 133 | | canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; |
| 44 | 134 | | canvasScaler.referenceResolution = new Vector2(1280f, 720f); |
| 44 | 135 | | canvasScaler.matchWidthOrHeight = 1f; // Match height, recommended for landscape projects |
| | 136 | |
|
| | 137 | | // Graphics Raycaster (for allowing touch/click input on the ui components) |
| 44 | 138 | | graphicRaycaster = canvasGameObject.AddComponent<GraphicRaycaster>(); |
| | 139 | |
|
| 44 | 140 | | canvas.sortingOrder = -1; |
| | 141 | |
|
| 44 | 142 | | if (scene.isPersistent && scene.sceneData.id != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID) |
| | 143 | | { |
| 44 | 144 | | canvas.sortingOrder -= 1; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | // We create a middleman-gameobject to change the size of the parcel-devs accessible canvas, to have its bot |
| 44 | 148 | | GameObject resizedPanel = new GameObject("ResizeUIArea"); |
| | 149 | |
|
| 44 | 150 | | resizedPanel.AddComponent<CanvasRenderer>(); |
| 44 | 151 | | childHookRectTransform = resizedPanel.AddComponent<RectTransform>(); |
| 44 | 152 | | childHookRectTransform.SetParent(canvas.transform); |
| 44 | 153 | | childHookRectTransform.ResetLocalTRS(); |
| | 154 | |
|
| 44 | 155 | | childHookRectTransform.anchorMin = Vector2.zero; |
| 44 | 156 | | childHookRectTransform.anchorMax = new Vector2(1, 0); |
| | 157 | |
|
| | 158 | | // We scale the panel downwards to subtract the viewport's top 10% |
| 44 | 159 | | float canvasHeight = canvasScaler.referenceResolution.y; |
| 44 | 160 | | childHookRectTransform.pivot = new Vector2(0.5f, 0f); |
| 44 | 161 | | float canvasSubtraction = canvasHeight * UISettings.RESERVED_CANVAS_TOP_PERCENTAGE / 100; |
| 44 | 162 | | childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction); |
| | 163 | |
|
| | 164 | | // We scale the panel upwards to subtract the viewport's bottom 5% for Decentraland's taskbar |
| 44 | 165 | | canvasHeight = childHookRectTransform.sizeDelta.y; |
| 44 | 166 | | childHookRectTransform.pivot = new Vector2(0.5f, 1f); |
| 44 | 167 | | childHookRectTransform.anchoredPosition = new Vector3(0, canvasHeight, 0f); |
| 44 | 168 | | childHookRectTransform.sizeDelta = new Vector2(0, canvasHeight - canvasSubtraction / 2); |
| | 169 | |
|
| | 170 | | // Canvas group |
| 44 | 171 | | canvasGroup = canvas.gameObject.AddComponent<CanvasGroup>(); |
| 44 | 172 | | canvasGroup.alpha = 0f; // Alpha will be updated later when the player enters this scene |
| 44 | 173 | | canvasGroup.blocksRaycasts = false; |
| | 174 | |
|
| 44 | 175 | | if (VERBOSE) |
| | 176 | | { |
| 0 | 177 | | Debug.Log("canvas initialized, width: " + childHookRectTransform.rect.width); |
| 0 | 178 | | Debug.Log("canvas initialized, height: " + childHookRectTransform.rect.height); |
| | 179 | | } |
| | 180 | |
|
| 44 | 181 | | OnPlayerCoordinatesChanged(dataStorePlayer.playerGridPosition.Get(), dataStorePlayer.playerGridPosition.Get( |
| | 182 | |
|
| 44 | 183 | | if (VERBOSE) |
| | 184 | | { |
| 0 | 185 | | Debug.Log("Finished canvas initialization in " + id); |
| | 186 | | } |
| | 187 | |
|
| 44 | 188 | | UpdateCanvasVisibility(); |
| 44 | 189 | | CommonScriptableObjects.allUIHidden.OnChange += AllUIHidden_OnChange; |
| 44 | 190 | | } |
| | 191 | | } |
| | 192 | | } |