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