| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers.NFT; |
| | 3 | | using UnityEngine; |
| | 4 | | using Object = UnityEngine.Object; |
| | 5 | |
|
| | 6 | | public class NFTPromptHUDController : IHUD |
| | 7 | | { |
| | 8 | | internal const string VIEW_PREFAB_PATH = "NFTPromptHUD"; |
| | 9 | | internal const string COULD_NOT_FETCH_NFT_FROM_API = "Couldn't fetch NFT: '{0}/{1}'."; |
| | 10 | | internal const string DOES_NOT_SUPPORT_POLYGON = "Warning: OpenSea API does not support fetching Polygon assets."; |
| | 11 | |
|
| 0 | 12 | | internal INFTPromptHUDView view { get; private set; } |
| | 13 | |
|
| | 14 | | private readonly OwnersInfoController ownersInfoController; |
| | 15 | |
|
| | 16 | | private Coroutine fetchNFTRoutine = null; |
| | 17 | | private NFTInfoSingleAsset? lastNFTInfo = null; |
| | 18 | |
|
| | 19 | | private bool isPointerInTooltipArea = false; |
| | 20 | | private bool isPointerInOwnerArea = false; |
| | 21 | |
|
| 7 | 22 | | public NFTPromptHUDController() |
| | 23 | | { |
| 7 | 24 | | view = Object.Instantiate(Resources.Load<GameObject>(VIEW_PREFAB_PATH)) |
| | 25 | | .GetComponent<NFTPromptHUDView>(); |
| 7 | 26 | | view.SetActive(false); |
| | 27 | |
|
| 7 | 28 | | view.OnOwnerLabelPointerEnter += ShowOwnersTooltip; |
| 7 | 29 | | view.OnOwnerLabelPointerExit += TryHideOwnersTooltip; |
| 7 | 30 | | view.OnOwnersTooltipFocusLost += OnOwnersTooltipFocusLost; |
| 7 | 31 | | view.OnOwnersTooltipFocus += OnOwnersTooltipFocus; |
| 7 | 32 | | view.OnViewAllPressed += ShowOwnersPopup; |
| 7 | 33 | | view.OnOwnersPopupClosed += HideOwnersPopup; |
| | 34 | |
|
| 7 | 35 | | ownersInfoController = new OwnersInfoController(view.GetOwnerElementPrefab()); |
| 7 | 36 | | DataStore.i.common.onOpenNFTPrompt.OnChange += OpenNftInfoDialog; |
| 7 | 37 | | } |
| | 38 | |
|
| | 39 | | public void OpenNftInfoDialog(NFTPromptModel model, NFTPromptModel prevModel) |
| | 40 | | { |
| 1 | 41 | | if (!view.IsActive()) |
| | 42 | | { |
| 1 | 43 | | HideOwnersTooltip(true); |
| 1 | 44 | | HideOwnersPopup(true); |
| | 45 | |
|
| 1 | 46 | | if (fetchNFTRoutine != null) |
| 0 | 47 | | CoroutineStarter.Stop(fetchNFTRoutine); |
| | 48 | |
|
| 1 | 49 | | if (lastNFTInfo != null && lastNFTInfo.Value.Equals(model.contactAddress, model.tokenId)) |
| | 50 | | { |
| 0 | 51 | | SetNFT(lastNFTInfo.Value, model.comment, false); |
| 0 | 52 | | return; |
| | 53 | | } |
| | 54 | |
|
| 1 | 55 | | view.SetLoading(); |
| | 56 | |
|
| 1 | 57 | | fetchNFTRoutine = CoroutineStarter.Start(NFTUtils.FetchNFTInfoSingleAsset(model.contactAddress, model.tokenI |
| 0 | 58 | | (nftInfo) => SetNFT(nftInfo, model.comment, true), |
| 0 | 59 | | (error) => view.OnError(string.Format(COULD_NOT_FETCH_NFT_FROM_API + " " + error + ". " + DOES_NOT_SUPPO |
| | 60 | | )); |
| | 61 | | } |
| 1 | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetVisibility(bool visible) |
| | 65 | | { |
| 1 | 66 | | view.SetActive(visible); |
| | 67 | |
|
| 1 | 68 | | if (!visible) |
| | 69 | | { |
| 0 | 70 | | HideOwnersTooltip(true); |
| 0 | 71 | | HideOwnersPopup(true); |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Dispose() |
| | 78 | | { |
| 7 | 79 | | view.OnOwnerLabelPointerEnter -= ShowOwnersTooltip; |
| 7 | 80 | | view.OnOwnerLabelPointerExit -= TryHideOwnersTooltip; |
| 7 | 81 | | view.OnOwnersTooltipFocusLost -= OnOwnersTooltipFocusLost; |
| 7 | 82 | | view.OnOwnersTooltipFocus -= OnOwnersTooltipFocus; |
| 7 | 83 | | view.OnViewAllPressed -= ShowOwnersPopup; |
| 7 | 84 | | view.OnOwnersPopupClosed -= HideOwnersPopup; |
| | 85 | |
|
| 7 | 86 | | CoroutineStarter.Stop(fetchNFTRoutine); |
| | 87 | |
|
| 7 | 88 | | view?.Dispose(); |
| | 89 | |
|
| 7 | 90 | | DataStore.i.common.onOpenNFTPrompt.OnChange -= OpenNftInfoDialog; |
| 7 | 91 | | } |
| | 92 | |
|
| | 93 | | private void SetNFT(NFTInfoSingleAsset info, string comment, bool shouldRefreshOwners) |
| | 94 | | { |
| 0 | 95 | | lastNFTInfo = info; |
| 0 | 96 | | view.SetNFTInfo(info, comment); |
| 0 | 97 | | if (shouldRefreshOwners) |
| | 98 | | { |
| 0 | 99 | | ownersInfoController.SetOwners(info.owners); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void ShowOwnersTooltip() |
| | 104 | | { |
| 0 | 105 | | isPointerInOwnerArea = true; |
| | 106 | |
|
| 0 | 107 | | if (lastNFTInfo == null || lastNFTInfo.Value.owners.Length <= 1) |
| 0 | 108 | | return; |
| | 109 | |
|
| 0 | 110 | | var tooltip = view.GetOwnersTooltip(); |
| | 111 | |
|
| 0 | 112 | | if (tooltip.IsActive()) |
| | 113 | | { |
| 0 | 114 | | tooltip.KeepOnScreen(); |
| 0 | 115 | | return; |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | tooltip.SetElements(ownersInfoController.GetElements()); |
| 0 | 119 | | tooltip.Show(); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | void TryHideOwnersTooltip() |
| | 123 | | { |
| 0 | 124 | | isPointerInOwnerArea = false; |
| | 125 | |
|
| 0 | 126 | | if (!isPointerInTooltipArea) |
| | 127 | | { |
| 0 | 128 | | HideOwnersTooltip(); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | void OnOwnersTooltipFocusLost() |
| | 133 | | { |
| 0 | 134 | | isPointerInTooltipArea = false; |
| 0 | 135 | | if (!isPointerInOwnerArea) |
| | 136 | | { |
| 0 | 137 | | HideOwnersTooltip(); |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | void OnOwnersTooltipFocus() |
| | 142 | | { |
| 0 | 143 | | isPointerInTooltipArea = true; |
| 0 | 144 | | var tooltip = view.GetOwnersTooltip(); |
| 0 | 145 | | if (tooltip.IsActive()) |
| | 146 | | { |
| 0 | 147 | | tooltip.KeepOnScreen(); |
| | 148 | | } |
| 0 | 149 | | } |
| | 150 | |
|
| 0 | 151 | | private void HideOwnersTooltip() { HideOwnersTooltip(false); } |
| | 152 | |
|
| | 153 | | private void HideOwnersTooltip(bool instant) |
| | 154 | | { |
| 1 | 155 | | var tooltip = view.GetOwnersTooltip(); |
| 1 | 156 | | if (!tooltip.IsActive()) |
| 0 | 157 | | return; |
| | 158 | |
|
| 1 | 159 | | tooltip.Hide(instant); |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | private void ShowOwnersPopup() |
| | 163 | | { |
| 0 | 164 | | HideOwnersTooltip(true); |
| 0 | 165 | | isPointerInTooltipArea = false; |
| 0 | 166 | | isPointerInOwnerArea = false; |
| | 167 | |
|
| 0 | 168 | | var popup = view.GetOwnersPopup(); |
| | 169 | |
|
| 0 | 170 | | if (popup.IsActive()) |
| 0 | 171 | | return; |
| | 172 | |
|
| 0 | 173 | | popup.SetElements(ownersInfoController.GetElements()); |
| 0 | 174 | | popup.Show(); |
| 0 | 175 | | } |
| | 176 | |
|
| 0 | 177 | | private void HideOwnersPopup() { HideOwnersPopup(false); } |
| | 178 | |
|
| 2 | 179 | | private void HideOwnersPopup(bool instant) { view.GetOwnersPopup().Hide(instant); } |
| | 180 | |
|
| | 181 | | internal static string FormatOwnerAddress(string address, int maxCharacters) |
| | 182 | | { |
| | 183 | | const string ellipsis = "..."; |
| | 184 | |
|
| 0 | 185 | | if (address.Length <= maxCharacters) |
| | 186 | | { |
| 0 | 187 | | return address; |
| | 188 | | } |
| | 189 | | else |
| | 190 | | { |
| 0 | 191 | | int segmentLength = Mathf.FloorToInt((maxCharacters - ellipsis.Length) * 0.5f); |
| 0 | 192 | | return $"{address.Substring(0, segmentLength)}{ellipsis}{address.Substring(address.Length - segmentLength, s |
| | 193 | | } |
| | 194 | | } |
| | 195 | | } |