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