| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces; |
| | 5 | | using NFTShape_Internal; |
| | 6 | | using System; |
| | 7 | | using System.Collections; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.UI; |
| | 11 | |
|
| | 12 | | internal interface INFTPromptHUDView : IDisposable |
| | 13 | | { |
| | 14 | | event Action OnOwnerLabelPointerEnter; |
| | 15 | | event Action OnOwnerLabelPointerExit; |
| | 16 | | event Action OnOwnersTooltipFocusLost; |
| | 17 | | event Action OnOwnersTooltipFocus; |
| | 18 | | event Action OnViewAllPressed; |
| | 19 | | event Action OnOwnersPopupClosed; |
| | 20 | | void SetActive(bool active); |
| | 21 | | bool IsActive(); |
| | 22 | | IOwnersTooltipView GetOwnersTooltip(); |
| | 23 | | IOwnersPopupView GetOwnersPopup(); |
| | 24 | | OwnerInfoElement GetOwnerElementPrefab(); |
| | 25 | | void SetLoading(); |
| | 26 | |
|
| | 27 | | void SetNFTInfo(NFTInfo info, string comment); |
| | 28 | | void OnError(string error); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | internal class NFTPromptHUDView : MonoBehaviour, INFTPromptHUDView |
| | 32 | | { |
| | 33 | | private const string MULTIPLE_OWNERS_FORMAT = "{0} owners"; |
| | 34 | | private const int ADDRESS_MAX_CHARS = 11; |
| | 35 | |
|
| | 36 | | public event Action OnOwnerLabelPointerEnter; |
| | 37 | | public event Action OnOwnerLabelPointerExit; |
| | 38 | | public event Action OnOwnersTooltipFocusLost; |
| | 39 | | public event Action OnOwnersTooltipFocus; |
| | 40 | | public event Action OnViewAllPressed; |
| | 41 | | public event Action OnOwnersPopupClosed; |
| | 42 | |
|
| | 43 | | [SerializeField] internal GameObject content; |
| | 44 | | [SerializeField] internal GameObject nftContent; |
| | 45 | | [SerializeField] internal GameObject mainErrorFeedbackContent; |
| | 46 | | [SerializeField] internal GameObject imageErrorFeedbackContent; |
| | 47 | |
|
| | 48 | | [SerializeField] RawImage imageNft; |
| | 49 | | [SerializeField] Image imageNftBackground; |
| | 50 | | [SerializeField] TextMeshProUGUI textNftName; |
| | 51 | | [SerializeField] TextMeshProUGUI textOwner; |
| | 52 | | [SerializeField] TextMeshProUGUI textMultipleOwner; |
| | 53 | | [SerializeField] UIHoverCallback multipleOwnersContainer; |
| | 54 | |
|
| | 55 | | [Header("Last Sale")] [SerializeField] TextMeshProUGUI textLastSaleSymbol; |
| | 56 | | [SerializeField] TextMeshProUGUI textLastSalePrice; |
| | 57 | | [SerializeField] TextMeshProUGUI textLastSaleNeverSold; |
| | 58 | |
|
| | 59 | | [Header("Price")] [SerializeField] TextMeshProUGUI textPriceSymbol; |
| | 60 | | [SerializeField] TextMeshProUGUI textPrice; |
| | 61 | | [SerializeField] TextMeshProUGUI textPriceNotForSale; |
| | 62 | |
|
| | 63 | | [Header("Description & Comment")] |
| | 64 | | [SerializeField] |
| | 65 | | TextMeshProUGUI textDescription; |
| | 66 | |
|
| | 67 | | [SerializeField] TextMeshProUGUI textComment; |
| | 68 | | [SerializeField] GameObject containerDescription; |
| | 69 | | [SerializeField] GameObject containerComment; |
| | 70 | |
|
| | 71 | | [Header("Spinners")] [SerializeField] GameObject spinnerGeneral; |
| | 72 | | [SerializeField] GameObject spinnerNftImage; |
| | 73 | |
|
| | 74 | | [Header("Buttons")] [SerializeField] internal Button buttonClose; |
| | 75 | | [SerializeField] internal Button buttonCancel; |
| | 76 | | [SerializeField] internal Button buttonOpenMarket; |
| | 77 | | [SerializeField] TextMeshProUGUI textOpenMarketButton; |
| | 78 | |
|
| | 79 | | [Header("Owners")] |
| | 80 | | [SerializeField] internal OwnerInfoElement ownerElementPrefab; |
| | 81 | | [SerializeField] internal OwnersTooltipView ownersTooltip; |
| | 82 | | [SerializeField] internal OwnersPopupView ownersPopup; |
| | 83 | |
|
| | 84 | | Coroutine fetchNFTImageRoutine = null; |
| | 85 | |
|
| | 86 | | private string nftTokenId; |
| | 87 | | bool backgroundColorSet = false; |
| | 88 | | string marketUrl = null; |
| | 89 | |
|
| | 90 | | private bool isDestroyed = false; |
| | 91 | | internal INFTAssetRetriever nftAssetRetriever; |
| | 92 | | private INFTAsset nftAsset; |
| | 93 | |
|
| | 94 | | private void Awake() |
| | 95 | | { |
| 8 | 96 | | name = "_NFTPromptHUD"; |
| | 97 | |
|
| 8 | 98 | | buttonClose.onClick.AddListener(Hide); |
| 8 | 99 | | buttonCancel.onClick.AddListener(Hide); |
| 8 | 100 | | buttonOpenMarket.onClick.AddListener(OpenMarketUrl); |
| | 101 | |
|
| 8 | 102 | | multipleOwnersContainer.OnPointerEnter += OwnerLabelPointerEnter; |
| 8 | 103 | | multipleOwnersContainer.OnPointerExit += OwnerLabelPointerExit; |
| 8 | 104 | | ownersTooltip.OnViewAllPressed += OnViewAllOwnersPressed; |
| 8 | 105 | | ownersTooltip.OnFocusLost += OnOwnersTooltipLostFocus; |
| 8 | 106 | | ownersTooltip.OnFocus += OnOwnersTooltipGainFocus; |
| 8 | 107 | | ownersPopup.OnClosePopup += OnOwnersPopupClose; |
| 8 | 108 | | } |
| | 109 | |
|
| | 110 | | public void Dispose() |
| | 111 | | { |
| 8 | 112 | | if (!isDestroyed) |
| | 113 | | { |
| 8 | 114 | | Destroy(gameObject); |
| | 115 | | } |
| | 116 | |
|
| 8 | 117 | | nftAssetRetriever?.Dispose(); |
| 8 | 118 | | nftAsset?.Dispose(); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | internal void Hide() |
| | 122 | | { |
| 1 | 123 | | content.SetActive(false); |
| | 124 | |
|
| 1 | 125 | | nftAssetRetriever?.Dispose(); |
| 1 | 126 | | nftAsset?.Dispose(); |
| | 127 | |
|
| 1 | 128 | | if (fetchNFTImageRoutine != null) |
| 0 | 129 | | StopCoroutine(fetchNFTImageRoutine); |
| | 130 | |
|
| 1 | 131 | | fetchNFTImageRoutine = null; |
| | 132 | |
|
| 1 | 133 | | AudioScriptableObjects.dialogClose.Play(true); |
| 1 | 134 | | } |
| | 135 | |
|
| 2 | 136 | | IOwnersPopupView INFTPromptHUDView.GetOwnersPopup() { return ownersPopup; } |
| | 137 | |
|
| 2 | 138 | | IOwnersTooltipView INFTPromptHUDView.GetOwnersTooltip() { return ownersTooltip; } |
| | 139 | |
|
| 18 | 140 | | void INFTPromptHUDView.SetActive(bool active) { content.SetActive(active); } |
| | 141 | |
|
| 2 | 142 | | bool INFTPromptHUDView.IsActive() { return content.activeSelf; } |
| | 143 | |
|
| 8 | 144 | | OwnerInfoElement INFTPromptHUDView.GetOwnerElementPrefab() { return ownerElementPrefab; } |
| | 145 | |
|
| | 146 | | void INFTPromptHUDView.SetLoading() |
| | 147 | | { |
| 2 | 148 | | Show(); |
| | 149 | |
|
| 2 | 150 | | if (fetchNFTImageRoutine != null) |
| 0 | 151 | | StopCoroutine(fetchNFTImageRoutine); |
| | 152 | |
|
| 2 | 153 | | SetTransparentBackground(); |
| | 154 | |
|
| 2 | 155 | | imageNft.gameObject.SetActive(false); |
| 2 | 156 | | textNftName.gameObject.SetActive(false); |
| 2 | 157 | | textOwner.gameObject.SetActive(false); |
| 2 | 158 | | multipleOwnersContainer.gameObject.SetActive(false); |
| 2 | 159 | | textLastSaleSymbol.gameObject.SetActive(false); |
| 2 | 160 | | textLastSalePrice.gameObject.SetActive(false); |
| 2 | 161 | | textLastSaleNeverSold.gameObject.SetActive(false); |
| 2 | 162 | | textPriceSymbol.gameObject.SetActive(false); |
| 2 | 163 | | textPrice.gameObject.SetActive(false); |
| 2 | 164 | | textPriceNotForSale.gameObject.SetActive(false); |
| 2 | 165 | | containerDescription.SetActive(false); |
| 2 | 166 | | containerComment.SetActive(false); |
| 2 | 167 | | buttonCancel.gameObject.SetActive(false); |
| 2 | 168 | | buttonOpenMarket.gameObject.SetActive(false); |
| | 169 | |
|
| 2 | 170 | | nftContent.SetActive(false); |
| 2 | 171 | | ShowImageLoading(false); |
| 2 | 172 | | ShowMainLoading(true); |
| 2 | 173 | | ShowMainErrorFeedback(false); |
| 2 | 174 | | } |
| | 175 | |
|
| | 176 | | void INFTPromptHUDView.SetNFTInfo(NFTInfo info, string comment) |
| | 177 | | { |
| 0 | 178 | | Show(); |
| | 179 | |
|
| 0 | 180 | | ShowMainLoading(false); |
| 0 | 181 | | nftContent.SetActive(true); |
| | 182 | |
|
| 0 | 183 | | nftTokenId = info.tokenId; |
| 0 | 184 | | SetTransparentBackground(); |
| 0 | 185 | | backgroundColorSet = info.backgroundColor != null; |
| 0 | 186 | | if (backgroundColorSet) |
| | 187 | | { |
| 0 | 188 | | imageNftBackground.color = info.backgroundColor.Value; |
| | 189 | | } |
| | 190 | |
|
| 0 | 191 | | textNftName.text = info.name; |
| 0 | 192 | | textNftName.gameObject.SetActive(true); |
| | 193 | |
|
| 0 | 194 | | bool hasMultipleOwners = info.owners is { Length: > 1 }; |
| 0 | 195 | | if (hasMultipleOwners) |
| | 196 | | { |
| 0 | 197 | | textMultipleOwner.text = string.Format(MULTIPLE_OWNERS_FORMAT, info.owners.Length); |
| | 198 | | } |
| | 199 | | else |
| | 200 | | { |
| 0 | 201 | | textOwner.text = info.owners is { Length: 1 } |
| | 202 | | ? NFTPromptHUDController.FormatOwnerAddress(info.owners[0].address, ADDRESS_MAX_CHARS) |
| | 203 | | : NFTPromptHUDController.FormatOwnerAddress("0x0000000000000000000000000000000000000000", ADDRESS_MAX_CH |
| | 204 | | } |
| 0 | 205 | | textOwner.gameObject.SetActive(!hasMultipleOwners); |
| 0 | 206 | | multipleOwnersContainer.gameObject.SetActive(hasMultipleOwners); |
| | 207 | |
|
| 0 | 208 | | if (!string.IsNullOrEmpty(info.lastSaleAmount)) |
| | 209 | | { |
| 0 | 210 | | textLastSalePrice.text = ShortDecimals(info.lastSaleAmount, 4); |
| 0 | 211 | | textLastSalePrice.gameObject.SetActive(true); |
| | 212 | | } |
| | 213 | | else |
| | 214 | | { |
| 0 | 215 | | textLastSaleNeverSold.gameObject.SetActive(true); |
| | 216 | | } |
| | 217 | |
|
| 0 | 218 | | if (!string.IsNullOrEmpty(info.currentPrice)) |
| | 219 | | { |
| 0 | 220 | | textPrice.text = ShortDecimals(info.currentPrice, 4); |
| 0 | 221 | | textPrice.gameObject.SetActive(true); |
| | 222 | |
|
| 0 | 223 | | if (info.currentPriceToken != null) |
| | 224 | | { |
| 0 | 225 | | SetTokenSymbol(textPriceSymbol, info.currentPriceToken.Value.symbol); |
| | 226 | | } |
| | 227 | | } |
| | 228 | | else |
| | 229 | | { |
| 0 | 230 | | textPriceNotForSale.gameObject.SetActive(true); |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | if (info.lastSaleToken != null) |
| | 234 | | { |
| 0 | 235 | | SetTokenSymbol(textLastSaleSymbol, info.lastSaleToken.Value.symbol); |
| | 236 | | } |
| | 237 | |
|
| 0 | 238 | | if (!string.IsNullOrEmpty(info.description)) |
| | 239 | | { |
| 0 | 240 | | textDescription.text = info.description; |
| 0 | 241 | | containerDescription.SetActive(true); |
| | 242 | | } |
| | 243 | |
|
| 0 | 244 | | if (!string.IsNullOrEmpty(comment)) |
| | 245 | | { |
| 0 | 246 | | textComment.text = comment; |
| 0 | 247 | | containerComment.SetActive(true); |
| | 248 | | } |
| | 249 | |
|
| 0 | 250 | | textOpenMarketButton.text = "VIEW"; |
| 0 | 251 | | if (info.marketInfo != null) |
| | 252 | | { |
| 0 | 253 | | textOpenMarketButton.text = $"{textOpenMarketButton.text} ON {info.marketInfo.Value.name.ToUpper()}"; |
| | 254 | | } |
| | 255 | |
|
| 0 | 256 | | marketUrl = null; |
| 0 | 257 | | if (!string.IsNullOrEmpty(info.marketLink)) |
| | 258 | | { |
| 0 | 259 | | marketUrl = info.marketLink; |
| | 260 | | } |
| 0 | 261 | | else if (!string.IsNullOrEmpty(info.assetLink)) |
| | 262 | | { |
| 0 | 263 | | marketUrl = info.assetLink; |
| | 264 | | } |
| | 265 | |
|
| 0 | 266 | | buttonCancel.gameObject.SetActive(true); |
| 0 | 267 | | buttonOpenMarket.gameObject.SetActive(true); |
| | 268 | |
|
| 0 | 269 | | fetchNFTImageRoutine = StartCoroutine(FetchNFTImage(info)); |
| 0 | 270 | | } |
| | 271 | |
|
| | 272 | | private void Show() |
| | 273 | | { |
| 2 | 274 | | content.SetActive(true); |
| 2 | 275 | | Utils.UnlockCursor(); |
| 2 | 276 | | } |
| | 277 | |
|
| | 278 | | private IEnumerator FetchNFTImage(NFTInfo nftInfo) |
| | 279 | | { |
| 0 | 280 | | ShowImageErrorFeedback(false); |
| 0 | 281 | | ShowImageLoading(true); |
| | 282 | |
|
| 0 | 283 | | nftAssetRetriever?.Dispose(); |
| 0 | 284 | | nftAsset?.Dispose(); |
| | 285 | |
|
| 0 | 286 | | nftAssetRetriever = new NFTAssetRetriever(); |
| 0 | 287 | | yield return nftAssetRetriever.LoadNFTAsset( |
| | 288 | | nftInfo.previewImageUrl, |
| | 289 | | OnSuccess: nftAsset => |
| | 290 | | { |
| 0 | 291 | | this.nftAsset = nftAsset; |
| 0 | 292 | | nftAsset.OnTextureUpdate += UpdateTexture; |
| | 293 | |
|
| 0 | 294 | | if (!(nftAsset is Asset_Gif)) |
| | 295 | | { |
| 0 | 296 | | if (!backgroundColorSet) |
| | 297 | | { |
| 0 | 298 | | SetTransparentBackground(); |
| | 299 | | } |
| | 300 | | } |
| | 301 | |
|
| 0 | 302 | | UpdateTexture(nftAsset.previewAsset.texture); |
| 0 | 303 | | SetNFTImageSize(nftAsset.previewAsset.texture); |
| 0 | 304 | | imageNft.gameObject.SetActive(true); |
| 0 | 305 | | ShowImageLoading(false); |
| 0 | 306 | | }, |
| | 307 | | OnFail: |
| 0 | 308 | | (exc) => { ShowImageErrorFeedback(true); }); |
| 0 | 309 | | } |
| | 310 | |
|
| | 311 | | private void UpdateTexture(Texture2D texture) |
| | 312 | | { |
| 0 | 313 | | imageNft.texture = texture; |
| 0 | 314 | | } |
| | 315 | |
|
| | 316 | | private void SetNFTImageSize(Texture2D texture) |
| | 317 | | { |
| 0 | 318 | | RectTransform rt = (RectTransform)imageNft.transform.parent; |
| | 319 | |
|
| | 320 | | float h, w; |
| | 321 | |
|
| 0 | 322 | | if (texture.height > texture.width) |
| | 323 | | { |
| 0 | 324 | | h = rt.rect.height; |
| 0 | 325 | | w = h * (texture.width / (float)texture.height); |
| | 326 | | } |
| | 327 | | else |
| | 328 | | { |
| 0 | 329 | | w = rt.rect.width; |
| 0 | 330 | | h = w * (texture.height / (float)texture.width); |
| | 331 | | } |
| | 332 | |
|
| 0 | 333 | | imageNft.rectTransform.sizeDelta = new Vector2(w, h); |
| 0 | 334 | | } |
| | 335 | |
|
| | 336 | | private string ShortDecimals(string value, int decimalCount) |
| | 337 | | { |
| 0 | 338 | | int pointPosition = value.IndexOf('.'); |
| | 339 | |
|
| 0 | 340 | | if (pointPosition <= 0) |
| 0 | 341 | | return value; |
| | 342 | |
|
| 0 | 343 | | string ret = value.Substring(0, pointPosition + Mathf.Min(value.Length - pointPosition, decimalCount + 1)); |
| | 344 | |
|
| 0 | 345 | | for (int i = ret.Length - 1; i >= 0; i--) |
| | 346 | | { |
| 0 | 347 | | if (ret[i] == '.') |
| | 348 | | { |
| 0 | 349 | | return ret.Substring(0, i); |
| | 350 | | } |
| | 351 | |
|
| 0 | 352 | | if (ret[i] != '0') |
| | 353 | | { |
| 0 | 354 | | return ret.Substring(0, i + 1); |
| | 355 | | } |
| | 356 | | } |
| | 357 | |
|
| 0 | 358 | | return ret; |
| | 359 | | } |
| | 360 | |
|
| | 361 | | private void SetTransparentBackground() |
| | 362 | | { |
| 2 | 363 | | imageNftBackground.color = new Color( |
| | 364 | | imageNftBackground.color.r, |
| | 365 | | imageNftBackground.color.g, |
| | 366 | | imageNftBackground.color.b, |
| | 367 | | 0f); |
| 2 | 368 | | } |
| | 369 | |
|
| | 370 | | private void SetTokenSymbol(TextMeshProUGUI textToken, string symbol) |
| | 371 | | { |
| 0 | 372 | | textToken.text = symbol; |
| 0 | 373 | | textToken.gameObject.SetActive(true); |
| 0 | 374 | | } |
| | 375 | |
|
| | 376 | | private void OpenMarketUrl() |
| | 377 | | { |
| 0 | 378 | | if (!string.IsNullOrEmpty(marketUrl)) |
| | 379 | | { |
| 0 | 380 | | WebInterface.OpenURL(marketUrl); |
| 0 | 381 | | AnalyticsHelper.SendExternalLinkAnalytic(marketUrl, nftTokenId); |
| | 382 | | } |
| | 383 | | else |
| | 384 | | { |
| 0 | 385 | | Hide(); |
| | 386 | | } |
| 0 | 387 | | } |
| | 388 | |
|
| | 389 | | void INFTPromptHUDView.OnError(string error) |
| | 390 | | { |
| 0 | 391 | | Debug.LogError(error); |
| 0 | 392 | | ShowMainErrorFeedback(true); |
| 0 | 393 | | } |
| | 394 | |
|
| | 395 | | private void OnDestroy() |
| | 396 | | { |
| 8 | 397 | | isDestroyed = true; |
| | 398 | |
|
| 8 | 399 | | multipleOwnersContainer.OnPointerEnter -= OwnerLabelPointerEnter; |
| 8 | 400 | | multipleOwnersContainer.OnPointerExit -= OwnerLabelPointerExit; |
| 8 | 401 | | ownersTooltip.OnViewAllPressed -= OnViewAllOwnersPressed; |
| 8 | 402 | | ownersTooltip.OnFocusLost -= OnOwnersTooltipLostFocus; |
| 8 | 403 | | ownersTooltip.OnFocus -= OnOwnersTooltipGainFocus; |
| 8 | 404 | | ownersPopup.OnClosePopup -= OnOwnersPopupClose; |
| | 405 | |
|
| 8 | 406 | | nftAssetRetriever?.Dispose(); |
| 8 | 407 | | nftAsset?.Dispose(); |
| 0 | 408 | | } |
| | 409 | |
|
| 0 | 410 | | private void OnViewAllOwnersPressed() { OnViewAllPressed?.Invoke(); } |
| | 411 | |
|
| 0 | 412 | | private void OnOwnersTooltipGainFocus() { OnOwnersTooltipFocus?.Invoke(); } |
| | 413 | |
|
| 0 | 414 | | private void OnOwnersTooltipLostFocus() { OnOwnersTooltipFocusLost?.Invoke(); } |
| | 415 | |
|
| 0 | 416 | | private void OnOwnersPopupClose() { OnOwnersPopupClosed?.Invoke(); } |
| | 417 | |
|
| 0 | 418 | | private void OwnerLabelPointerEnter() { OnOwnerLabelPointerEnter?.Invoke(); } |
| | 419 | |
|
| 0 | 420 | | private void OwnerLabelPointerExit() { OnOwnerLabelPointerExit?.Invoke(); } |
| | 421 | |
|
| | 422 | | private void ShowMainLoading(bool isVisible) |
| | 423 | | { |
| 2 | 424 | | if (spinnerGeneral == null) |
| 0 | 425 | | return; |
| | 426 | |
|
| 2 | 427 | | spinnerGeneral.SetActive(isVisible); |
| 2 | 428 | | } |
| | 429 | |
|
| | 430 | | private void ShowMainErrorFeedback(bool isVisible) |
| | 431 | | { |
| 2 | 432 | | if (mainErrorFeedbackContent == null) |
| 0 | 433 | | return; |
| | 434 | |
|
| 2 | 435 | | if (isVisible) |
| 0 | 436 | | ShowMainLoading(false); |
| | 437 | |
|
| 2 | 438 | | mainErrorFeedbackContent.SetActive(isVisible); |
| 2 | 439 | | } |
| | 440 | |
|
| | 441 | | private void ShowImageLoading(bool isVisible) |
| | 442 | | { |
| 2 | 443 | | if (spinnerNftImage == null) |
| 0 | 444 | | return; |
| | 445 | |
|
| 2 | 446 | | spinnerNftImage.SetActive(isVisible); |
| 2 | 447 | | } |
| | 448 | |
|
| | 449 | | private void ShowImageErrorFeedback(bool isVisible) |
| | 450 | | { |
| 0 | 451 | | if (imageErrorFeedbackContent == null) |
| 0 | 452 | | return; |
| | 453 | |
|
| 0 | 454 | | if (isVisible) |
| 0 | 455 | | ShowImageLoading(false); |
| | 456 | |
|
| 0 | 457 | | imageErrorFeedbackContent.SetActive(isVisible); |
| 0 | 458 | | } |
| | 459 | | } |