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