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