| | 1 | | using DCL.Interface; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class NavmapToastView : MonoBehaviour |
| | 9 | | { |
| 1 | 10 | | private static readonly int triggerLoadingComplete = Animator.StringToHash("LoadingComplete"); |
| | 11 | |
|
| | 12 | | [SerializeField] internal TextMeshProUGUI sceneTitleText; |
| | 13 | | [SerializeField] internal TextMeshProUGUI sceneOwnerText; |
| | 14 | | [SerializeField] internal TextMeshProUGUI sceneLocationText; |
| | 15 | | [SerializeField] internal RectTransform toastContainer; |
| | 16 | | [SerializeField] internal GameObject scenePreviewContainer; |
| | 17 | | [SerializeField] internal RawImageFillParent scenePreviewImage; |
| | 18 | | [SerializeField] internal Sprite scenePreviewFailImage; |
| | 19 | | [SerializeField] internal Animator toastAnimator; |
| | 20 | |
|
| | 21 | | [SerializeField] internal Button goToButton; |
| | 22 | |
|
| | 23 | | Vector2Int location; |
| | 24 | | RectTransform rectTransform; |
| | 25 | | MinimapMetadata minimapMetadata; |
| | 26 | |
|
| | 27 | | AssetPromise_Texture texturePromise; |
| | 28 | | string currentImageUrl; |
| | 29 | |
|
| | 30 | | private void Open(int cursorTileX, int cursorTileY) |
| | 31 | | { |
| 0 | 32 | | if (gameObject.activeInHierarchy) |
| 0 | 33 | | Close(); |
| | 34 | |
|
| 0 | 35 | | var sceneInfo = minimapMetadata.GetSceneInfo(cursorTileX, cursorTileY); |
| 0 | 36 | | if (sceneInfo == null) |
| 0 | 37 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15); |
| | 38 | |
|
| 0 | 39 | | Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private void Awake() |
| | 43 | | { |
| 54 | 44 | | minimapMetadata = MinimapMetadata.GetMetadata(); |
| 54 | 45 | | rectTransform = transform as RectTransform; |
| 54 | 46 | | } |
| | 47 | |
|
| | 48 | | private void Start() |
| | 49 | | { |
| 0 | 50 | | gameObject.SetActive(false); |
| | 51 | |
|
| 0 | 52 | | goToButton.onClick.AddListener(OnGotoClick); |
| | 53 | |
|
| 0 | 54 | | minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated; |
| 0 | 55 | | MapRenderer.OnParcelClicked += Open; |
| 0 | 56 | | MapRenderer.OnCursorFarFromParcel += Close; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | private void OnDestroy() |
| | 60 | | { |
| 54 | 61 | | minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated; |
| 54 | 62 | | MapRenderer.OnParcelClicked -= Open; |
| 54 | 63 | | MapRenderer.OnCursorFarFromParcel -= Close; |
| | 64 | |
|
| 54 | 65 | | if (texturePromise != null) |
| | 66 | | { |
| 0 | 67 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 0 | 68 | | texturePromise = null; |
| | 69 | | } |
| 54 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Populate(Vector2Int coordinates, MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 73 | | { |
| 1 | 74 | | if (!gameObject.activeSelf) |
| 0 | 75 | | AudioScriptableObjects.dialogOpen.Play(true); |
| | 76 | |
|
| 1 | 77 | | bool sceneInfoExists = sceneInfo != null; |
| | 78 | |
|
| 1 | 79 | | gameObject.SetActive(true); |
| 1 | 80 | | scenePreviewImage.gameObject.SetActive(false); |
| 1 | 81 | | location = coordinates; |
| | 82 | |
|
| 1 | 83 | | PositionToast(coordinates); |
| | 84 | |
|
| 1 | 85 | | sceneLocationText.text = $"{coordinates.x}, {coordinates.y}"; |
| | 86 | |
|
| 1 | 87 | | sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne |
| 1 | 88 | | sceneTitleText.text = "Untitled Scene"; |
| | 89 | |
|
| 1 | 90 | | bool useDefaultThumbnail = |
| | 91 | | !sceneInfoExists || (sceneInfoExists && string.IsNullOrEmpty(sceneInfo.previewImageUrl)); |
| | 92 | |
|
| 1 | 93 | | if (useDefaultThumbnail) |
| | 94 | | { |
| 1 | 95 | | DisplayThumbnail(scenePreviewFailImage.texture); |
| 1 | 96 | | currentImageUrl = ""; |
| | 97 | | } |
| | 98 | |
|
| 1 | 99 | | if (sceneInfoExists) |
| | 100 | | { |
| 1 | 101 | | sceneTitleText.text = sceneInfo.name; |
| 1 | 102 | | sceneOwnerText.text = $"Created by: {sceneInfo.owner}"; |
| | 103 | |
|
| 1 | 104 | | if (currentImageUrl == sceneInfo.previewImageUrl) |
| | 105 | | { |
| 0 | 106 | | DisplayThumbnail(texturePromise?.asset?.texture); |
| 0 | 107 | | return; |
| | 108 | | } |
| | 109 | |
|
| 1 | 110 | | if (texturePromise != null) |
| | 111 | | { |
| 0 | 112 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 0 | 113 | | texturePromise = null; |
| | 114 | | } |
| | 115 | |
|
| | 116 | |
|
| 1 | 117 | | if (!string.IsNullOrEmpty(sceneInfo.previewImageUrl)) |
| | 118 | | { |
| 0 | 119 | | texturePromise = new AssetPromise_Texture(sceneInfo.previewImageUrl, storeTexAsNonReadable: false); |
| 0 | 120 | | texturePromise.OnSuccessEvent += (textureAsset) => { DisplayThumbnail(textureAsset.texture); }; |
| 0 | 121 | | texturePromise.OnFailEvent += (textureAsset, error) => { DisplayThumbnail(scenePreviewFailImage.text |
| 0 | 122 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : ""; |
| | 126 | | } |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 130 | | { |
| 0 | 131 | | if (!gameObject.activeInHierarchy) |
| 0 | 132 | | return; |
| | 133 | |
|
| 0 | 134 | | bool updatedCurrentLocationInfo = false; |
| 0 | 135 | | foreach (Vector2Int parcel in sceneInfo.parcels) |
| | 136 | | { |
| 0 | 137 | | if (parcel == location) |
| | 138 | | { |
| 0 | 139 | | updatedCurrentLocationInfo = true; |
| 0 | 140 | | break; |
| | 141 | | } |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | if (updatedCurrentLocationInfo) |
| 0 | 145 | | Populate(location, sceneInfo); |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | void PositionToast(Vector2Int coordinates) |
| | 149 | | { |
| 1 | 150 | | if (toastContainer == null || rectTransform == null) |
| 0 | 151 | | return; |
| | 152 | |
|
| | 153 | | // position the toast over the parcel parcelHighlightImage so that we can easily check with LOCAL pos info w |
| 1 | 154 | | toastContainer.position = MapRenderer.i.GetParcelHighlightTransform(); |
| | 155 | |
|
| 1 | 156 | | bool useBottom = toastContainer.localPosition.y > 0; |
| | 157 | |
|
| 1 | 158 | | bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4; |
| 1 | 159 | | bool useLeft = false; |
| | 160 | |
|
| 1 | 161 | | if (shouldOffsetHorizontally) |
| 1 | 162 | | useLeft = toastContainer.localPosition.x > 0; |
| | 163 | |
|
| | 164 | | // By setting the pivot accordingly BEFORE we position the toast, we can have it always visible in an easier |
| 1 | 165 | | toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0); |
| 1 | 166 | | toastContainer.position = MapRenderer.i.GetParcelHighlightTransform(); |
| | 167 | |
|
| 1 | 168 | | } |
| | 169 | |
|
| | 170 | | public void Close() |
| | 171 | | { |
| 1 | 172 | | if (gameObject.activeSelf) |
| 1 | 173 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 174 | |
|
| 1 | 175 | | gameObject.SetActive(false); |
| 1 | 176 | | } |
| | 177 | |
|
| | 178 | | private void OnGotoClick() |
| | 179 | | { |
| 0 | 180 | | DataStore.i.HUDs.navmapVisible.Set(false); |
| 0 | 181 | | Environment.i.world.teleportController.Teleport(location.x, location.y); |
| | 182 | |
|
| 0 | 183 | | Close(); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void DisplayThumbnail(Texture2D texture) |
| | 187 | | { |
| 1 | 188 | | scenePreviewImage.texture = texture; |
| 1 | 189 | | toastAnimator.SetTrigger(triggerLoadingComplete); |
| 1 | 190 | | } |
| | 191 | | } |
| | 192 | | } |