| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using UnityEngine; |
| | 7 | | using Environment = DCL.Environment; |
| | 8 | | using WaitUntil = UnityEngine.WaitUntil; |
| | 9 | |
|
| | 10 | | public class ProfileHUDController : IHUD |
| | 11 | | { |
| | 12 | | private readonly IUserProfileBridge userProfileBridge; |
| | 13 | |
|
| | 14 | | [Serializable] |
| | 15 | | public struct Configuration |
| | 16 | | { |
| | 17 | | public bool connectedWallet; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | private const string URL_CLAIM_NAME = "https://builder.decentraland.org/claim-name"; |
| | 21 | | private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet"; |
| | 22 | | private const string URL_MANA_PURCHASE = "https://account.decentraland.org"; |
| | 23 | | private const string URL_TERMS_OF_USE = "https://decentraland.org/terms"; |
| | 24 | | private const string URL_PRIVACY_POLICY = "https://decentraland.org/privacy"; |
| | 25 | | private const float FETCH_MANA_INTERVAL = 60; |
| | 26 | |
|
| | 27 | | public readonly ProfileHUDView view; |
| | 28 | | internal AvatarEditorHUDController avatarEditorHud; |
| | 29 | |
|
| 42 | 30 | | private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 31 | | private IMouseCatcher mouseCatcher; |
| | 32 | | private Coroutine fetchManaIntervalRoutine = null; |
| | 33 | | private Coroutine fetchPolygonManaIntervalRoutine = null; |
| | 34 | |
|
| 0 | 35 | | public RectTransform tutorialTooltipReference { get => view.tutorialTooltipReference; } |
| | 36 | |
|
| | 37 | | public event Action OnOpen; |
| | 38 | | public event Action OnClose; |
| | 39 | |
|
| 17 | 40 | | public ProfileHUDController(IUserProfileBridge userProfileBridge) |
| | 41 | | { |
| 17 | 42 | | this.userProfileBridge = userProfileBridge; |
| 17 | 43 | | mouseCatcher = SceneReferences.i?.mouseCatcher; |
| | 44 | |
|
| | 45 | |
|
| 17 | 46 | | view = UnityEngine.Object.Instantiate(GetViewPrefab()).GetComponent<ProfileHUDView>(); |
| 17 | 47 | | view.name = "_ProfileHUD"; |
| | 48 | |
|
| 17 | 49 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange += ChangeVisibilityForBuilderInWor |
| 17 | 50 | | DataStore.i.exploreV2.profileCardIsOpen.OnChange += SetAsFullScreenMenuMode; |
| | 51 | |
|
| 17 | 52 | | view.connectedWalletSection.SetActive(false); |
| 17 | 53 | | view.nonConnectedWalletSection.SetActive(false); |
| 17 | 54 | | view.ActivateDescriptionEditionMode(false); |
| | 55 | |
|
| 17 | 56 | | view.buttonLogOut.onClick.AddListener(WebInterface.LogOut); |
| 17 | 57 | | view.buttonSignUp.onClick.AddListener(WebInterface.RedirectToSignUp); |
| 17 | 58 | | view.buttonClaimName.onClick.AddListener(() => WebInterface.OpenURL(URL_CLAIM_NAME)); |
| 17 | 59 | | view.buttonTermsOfServiceForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE); |
| 17 | 60 | | view.buttonPrivacyPolicyForConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY); |
| 17 | 61 | | view.buttonTermsOfServiceForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_TERMS_OF_USE); |
| 17 | 62 | | view.buttonPrivacyPolicyForNonConnectedWallets.onPointerDown += () => WebInterface.OpenURL(URL_PRIVACY_POLICY); |
| 17 | 63 | | view.inputName.onSubmit.AddListener(UpdateProfileName); |
| 17 | 64 | | view.descriptionEditionInput.onSubmit.AddListener(UpdateProfileDescription); |
| 17 | 65 | | view.OnOpen += () => |
| | 66 | | { |
| 1 | 67 | | WebInterface.RequestOwnProfileUpdate(); |
| 1 | 68 | | OnOpen?.Invoke(); |
| 0 | 69 | | }; |
| 18 | 70 | | view.OnClose += () => OnClose?.Invoke(); |
| | 71 | |
|
| 17 | 72 | | if (view.manaCounterView) |
| | 73 | | { |
| 17 | 74 | | view.manaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO); |
| 17 | 75 | | view.manaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCHASE)); |
| | 76 | | } |
| | 77 | |
|
| 17 | 78 | | if (view.polygonManaCounterView) |
| | 79 | | { |
| 17 | 80 | | view.polygonManaCounterView.buttonManaInfo.onPointerDown += () => WebInterface.OpenURL(URL_MANA_INFO); |
| 17 | 81 | | view.polygonManaCounterView.buttonManaPurchase.onClick.AddListener(() => WebInterface.OpenURL(URL_MANA_PURCH |
| | 82 | | } |
| | 83 | |
|
| 17 | 84 | | ownUserProfile.OnUpdate += OnProfileUpdated; |
| 17 | 85 | | if (mouseCatcher != null) |
| 17 | 86 | | mouseCatcher.OnMouseLock += OnMouseLocked; |
| | 87 | |
|
| 17 | 88 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 89 | | { |
| 0 | 90 | | KernelConfig.i.EnsureConfigInitialized().Then(config => OnKernelConfigChanged(config, null)); |
| 0 | 91 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| | 92 | | } |
| | 93 | |
|
| 17 | 94 | | DataStore.i.exploreV2.isInitialized.OnChange += ExploreV2Changed; |
| 17 | 95 | | ExploreV2Changed(DataStore.i.exploreV2.isInitialized.Get(), false); |
| 17 | 96 | | } |
| | 97 | |
|
| | 98 | | protected virtual GameObject GetViewPrefab() |
| | 99 | | { |
| 17 | 100 | | return Resources.Load<GameObject>("ProfileHUD"); |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); } |
| | 104 | |
|
| | 105 | | public void SetVisibility(bool visible) |
| | 106 | | { |
| 3 | 107 | | view?.SetVisibility(visible); |
| | 108 | |
|
| 3 | 109 | | if (visible && fetchManaIntervalRoutine == null) |
| | 110 | | { |
| 2 | 111 | | fetchManaIntervalRoutine = CoroutineStarter.Start(ManaIntervalRoutine()); |
| 2 | 112 | | } |
| 1 | 113 | | else if (!visible && fetchManaIntervalRoutine != null) |
| | 114 | | { |
| 0 | 115 | | CoroutineStarter.Stop(fetchManaIntervalRoutine); |
| 0 | 116 | | fetchManaIntervalRoutine = null; |
| | 117 | | } |
| | 118 | |
|
| 3 | 119 | | if (visible && fetchPolygonManaIntervalRoutine == null) |
| | 120 | | { |
| 2 | 121 | | fetchPolygonManaIntervalRoutine = CoroutineStarter.Start(PolygonManaIntervalRoutine()); |
| 2 | 122 | | } |
| 1 | 123 | | else if (!visible && fetchPolygonManaIntervalRoutine != null) |
| | 124 | | { |
| 0 | 125 | | CoroutineStarter.Stop(fetchPolygonManaIntervalRoutine); |
| 0 | 126 | | fetchPolygonManaIntervalRoutine = null; |
| | 127 | | } |
| 1 | 128 | | } |
| | 129 | |
|
| | 130 | | public void Dispose() |
| | 131 | | { |
| 17 | 132 | | if (fetchManaIntervalRoutine != null) |
| | 133 | | { |
| 2 | 134 | | CoroutineStarter.Stop(fetchManaIntervalRoutine); |
| 2 | 135 | | fetchManaIntervalRoutine = null; |
| | 136 | | } |
| | 137 | |
|
| 17 | 138 | | if (view) |
| | 139 | | { |
| 17 | 140 | | GameObject.Destroy(view.gameObject); |
| | 141 | | } |
| | 142 | |
|
| 17 | 143 | | ownUserProfile.OnUpdate -= OnProfileUpdated; |
| 17 | 144 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor |
| 17 | 145 | | if (mouseCatcher != null) |
| 17 | 146 | | mouseCatcher.OnMouseLock -= OnMouseLocked; |
| | 147 | |
|
| 17 | 148 | | if (!DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| | 149 | | { |
| 0 | 150 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 151 | | } |
| | 152 | |
|
| 17 | 153 | | view.descriptionPreviewInput.onSubmit.RemoveListener(UpdateProfileDescription); |
| 17 | 154 | | DataStore.i.exploreV2.profileCardIsOpen.OnChange -= SetAsFullScreenMenuMode; |
| | 155 | |
|
| 17 | 156 | | DataStore.i.exploreV2.isInitialized.OnChange -= ExploreV2Changed; |
| 17 | 157 | | } |
| | 158 | |
|
| 10 | 159 | | void OnProfileUpdated(UserProfile profile) { view?.SetProfile(profile); } |
| | 160 | |
|
| 0 | 161 | | void OnMouseLocked() { HideProfileMenu(); } |
| | 162 | |
|
| | 163 | | IEnumerator ManaIntervalRoutine() |
| | 164 | | { |
| 0 | 165 | | while (true) |
| | 166 | | { |
| 2 | 167 | | WebInterface.FetchBalanceOfMANA(); |
| 2 | 168 | | yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL); |
| | 169 | | } |
| | 170 | | } |
| | 171 | |
|
| | 172 | | IEnumerator PolygonManaIntervalRoutine() |
| | 173 | | { |
| 0 | 174 | | while (true) |
| | 175 | | { |
| 4 | 176 | | yield return new WaitUntil(() => ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId)); |
| | 177 | |
|
| 2 | 178 | | Promise<double> promise = Environment.i.platform.serviceProviders.theGraph.QueryPolygonMana(ownUserProfile.u |
| | 179 | |
|
| | 180 | | // This can be null if theGraph is mocked |
| 2 | 181 | | if ( promise != null ) |
| | 182 | | { |
| 0 | 183 | | yield return promise; |
| 0 | 184 | | SetPolygonManaBalance(promise.value); |
| | 185 | | } |
| | 186 | |
|
| 2 | 187 | | yield return WaitForSecondsCache.Get(FETCH_MANA_INTERVAL); |
| 0 | 188 | | } |
| | 189 | | } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Set an amount of MANA on the HUD. |
| | 193 | | /// </summary> |
| | 194 | | /// <param name="balance">Amount of MANA.</param> |
| 2 | 195 | | public void SetManaBalance(string balance) { view.manaCounterView?.SetBalance(balance); } |
| | 196 | |
|
| 0 | 197 | | public void SetPolygonManaBalance(double balance) { view.polygonManaCounterView.SetBalance(balance); } |
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Close the Profile menu. |
| | 201 | | /// </summary> |
| 0 | 202 | | public void HideProfileMenu() { view?.HideMenu(); } |
| | 203 | |
|
| | 204 | | private void UpdateProfileName(string newName) |
| | 205 | | { |
| 0 | 206 | | if (view.inputName.wasCanceled) |
| 0 | 207 | | return; |
| | 208 | |
|
| 0 | 209 | | if (!view.IsValidAvatarName(newName)) |
| | 210 | | { |
| 0 | 211 | | view.inputName.ActivateInputField(); |
| 0 | 212 | | return; |
| | 213 | | } |
| | 214 | |
|
| 0 | 215 | | if (view != null) |
| | 216 | | { |
| 0 | 217 | | view.SetProfileName(newName); |
| 0 | 218 | | view.ActivateProfileNameEditionMode(false); |
| | 219 | | } |
| | 220 | |
|
| 0 | 221 | | userProfileBridge.SaveUnverifiedName(newName); |
| 0 | 222 | | } |
| | 223 | |
|
| 0 | 224 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { view?.SetNameRegex(curre |
| | 225 | |
|
| | 226 | | private void UpdateProfileDescription(string description) |
| | 227 | | { |
| 2 | 228 | | if (view.descriptionEditionInput.wasCanceled |
| | 229 | | || !ownUserProfile.hasConnectedWeb3 |
| | 230 | | || description.Length > view.descriptionEditionInput.characterLimit) |
| | 231 | | { |
| 1 | 232 | | view.ActivateDescriptionEditionMode(false); |
| 1 | 233 | | return; |
| | 234 | | } |
| | 235 | |
|
| 1 | 236 | | view.SetDescription(description); |
| 1 | 237 | | view.ActivateDescriptionEditionMode(false); |
| 1 | 238 | | userProfileBridge.SaveDescription(description); |
| 1 | 239 | | } |
| | 240 | |
|
| | 241 | | private void SetAsFullScreenMenuMode(bool currentIsFullScreenMenuMode, bool previousIsFullScreenMenuMode) |
| | 242 | | { |
| 0 | 243 | | view.SetCardAsFullScreenMenuMode(currentIsFullScreenMenuMode); |
| | 244 | |
|
| 0 | 245 | | if (currentIsFullScreenMenuMode != CommonScriptableObjects.isProfileHUDOpen.Get()) |
| 0 | 246 | | view.ToggleMenu(); |
| 0 | 247 | | } |
| | 248 | |
|
| 0 | 249 | | private void ExploreV2Changed(bool current, bool previous) { view.SetStartMenuButtonActive(current); } |
| | 250 | | } |