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