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