| | 1 | | using UnityEngine; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine.UI; |
| | 4 | | using DCL.Interface; |
| | 5 | | using DCL; |
| | 6 | | using System; |
| | 7 | |
|
| | 8 | | namespace GotoPanel |
| | 9 | | { |
| | 10 | | public class GotoPanelHUDView : MonoBehaviour, IGotoPanelHUDView |
| | 11 | | { |
| | 12 | | [SerializeField] private Button teleportButton; |
| | 13 | | [SerializeField] private TextMeshProUGUI panelText; |
| | 14 | | [SerializeField] public GameObject container; |
| | 15 | | [SerializeField] internal TextMeshProUGUI sceneTitleText; |
| | 16 | | [SerializeField] internal TextMeshProUGUI sceneOwnerText; |
| | 17 | | [SerializeField] internal Sprite scenePreviewFailImage; |
| | 18 | | [SerializeField] internal RawImageFillParent scenePreviewImage; |
| | 19 | | [SerializeField] internal GameObject loadingSpinner; |
| | 20 | | [SerializeField] internal Button closeButton; |
| | 21 | | [SerializeField] internal Button cancelButton; |
| | 22 | | [SerializeField] internal ShowHideAnimator contentAnimator; |
| | 23 | |
|
| | 24 | | private bool isDestroyed = false; |
| | 25 | |
|
| | 26 | | internal ParcelCoordinates targetCoordinates; |
| | 27 | |
|
| | 28 | | AssetPromise_Texture texturePromise = null; |
| | 29 | |
|
| | 30 | | public event Action<ParcelCoordinates> OnTeleportPressed; |
| | 31 | | public event Action OnClosePressed; |
| | 32 | |
|
| | 33 | | private void Start() |
| | 34 | | { |
| 3 | 35 | | teleportButton.onClick.RemoveAllListeners(); |
| 3 | 36 | | teleportButton.onClick.AddListener(TeleportTo); |
| 3 | 37 | | closeButton.onClick.AddListener(ClosePanel); |
| 3 | 38 | | cancelButton.onClick.AddListener(ClosePanel); |
| 3 | 39 | | container.SetActive(false); |
| 3 | 40 | | contentAnimator.OnWillFinishHide += (animator) => Hide(); |
| 3 | 41 | | } |
| | 42 | |
|
| | 43 | | public static IGotoPanelHUDView CreateView() |
| | 44 | | { |
| 0 | 45 | | GotoPanelHUDView view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("GotoPanelHUD")).GetCompon |
| 0 | 46 | | view.name = "_GotoPanelHUD"; |
| 0 | 47 | | return view; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | private void TeleportTo() |
| | 51 | | { |
| 0 | 52 | | OnTeleportPressed?.Invoke(targetCoordinates); |
| 0 | 53 | | ClosePanel(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public void SetVisible(bool isVisible) |
| | 57 | | { |
| 2 | 58 | | container.SetActive(isVisible); |
| 2 | 59 | | contentAnimator.Show(!isVisible); |
| 2 | 60 | | loadingSpinner.SetActive(isVisible); |
| 2 | 61 | | scenePreviewImage.texture = null; |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetPanelInfo(ParcelCoordinates parcelCoordinates) |
| | 65 | | { |
| 1 | 66 | | MinimapMetadata.MinimapSceneInfo sceneInfo = MinimapMetadata.GetMetadata().GetSceneInfo(parcelCoordinates.x, |
| 1 | 67 | | if (sceneInfo != null) |
| | 68 | | { |
| 0 | 69 | | sceneTitleText.text = sceneInfo.name; |
| 0 | 70 | | sceneOwnerText.text = sceneInfo.owner; |
| 0 | 71 | | SetParcelImage(sceneInfo); |
| 0 | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 1 | 75 | | sceneTitleText.text = "Untitled Scene"; |
| 1 | 76 | | sceneOwnerText.text = "Unknown"; |
| 1 | 77 | | DisplayThumbnail(scenePreviewFailImage.texture); |
| | 78 | | } |
| 1 | 79 | | targetCoordinates = parcelCoordinates; |
| 1 | 80 | | panelText.text = parcelCoordinates.ToString(); |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | private void SetParcelImage(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 84 | | { |
| 0 | 85 | | DisplayThumbnail(scenePreviewFailImage.texture); |
| 0 | 86 | | if (!string.IsNullOrEmpty(sceneInfo.previewImageUrl)) |
| | 87 | | { |
| 0 | 88 | | if (texturePromise != null) |
| 0 | 89 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| | 90 | |
|
| 0 | 91 | | texturePromise = new AssetPromise_Texture(sceneInfo.previewImageUrl, storeTexAsNonReadable: false); |
| 0 | 92 | | texturePromise.OnSuccessEvent += (textureAsset) => { DisplayThumbnail(textureAsset.texture); }; |
| 0 | 93 | | texturePromise.OnFailEvent += (textureAsset, error) => { DisplayThumbnail(scenePreviewFailImage.texture) |
| 0 | 94 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void DisplayThumbnail(Texture2D texture) |
| | 99 | | { |
| 1 | 100 | | loadingSpinner.SetActive(false); |
| 1 | 101 | | scenePreviewImage.texture = texture; |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | private void ClosePanel() |
| | 105 | | { |
| 0 | 106 | | OnClosePressed?.Invoke(); |
| 0 | 107 | | contentAnimator.Hide(true); |
| 0 | 108 | | } |
| | 109 | |
|
| 0 | 110 | | private void Hide() => container.SetActive(false); |
| | 111 | |
|
| 6 | 112 | | private void OnDestroy() { isDestroyed = true; } |
| | 113 | |
|
| | 114 | | public void Dispose() |
| | 115 | | { |
| 0 | 116 | | if (isDestroyed) |
| 0 | 117 | | return; |
| 0 | 118 | | isDestroyed = true; |
| 0 | 119 | | ClearPromise(); |
| 0 | 120 | | Destroy(gameObject); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void ClearPromise() |
| | 124 | | { |
| 0 | 125 | | if (texturePromise != null) |
| | 126 | | { |
| 0 | 127 | | texturePromise.ClearEvents(); |
| 0 | 128 | | AssetPromiseKeeper_Texture.i.Forget(texturePromise); |
| 0 | 129 | | texturePromise = null; |
| | 130 | | } |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | } |
| | 134 | | } |