| | 1 | | using DCL; |
| | 2 | | using ExploreV2Analytics; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Text.RegularExpressions; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | |
|
| | 11 | | public class ProfileHUDView : MonoBehaviour |
| | 12 | | { |
| | 13 | | private const int ADDRESS_CHUNK_LENGTH = 6; |
| | 14 | | private const int NAME_POSTFIX_LENGTH = 4; |
| | 15 | | private const float COPY_TOAST_VISIBLE_TIME = 3; |
| | 16 | |
|
| | 17 | | [SerializeField] |
| | 18 | | internal ShowHideAnimator mainShowHideAnimator; |
| | 19 | |
|
| | 20 | | [SerializeField] |
| | 21 | | internal ShowHideAnimator menuShowHideAnimator; |
| | 22 | |
|
| | 23 | | [SerializeField] |
| | 24 | | private RectTransform mainRootLayout; |
| | 25 | |
|
| | 26 | | [SerializeField] |
| | 27 | | internal GameObject loadingSpinner; |
| | 28 | |
|
| | 29 | | [SerializeField] |
| | 30 | | internal ShowHideAnimator copyToast; |
| | 31 | |
|
| | 32 | | [SerializeField] |
| | 33 | | internal GameObject copyTooltip; |
| | 34 | |
|
| | 35 | | [SerializeField] |
| | 36 | | internal InputAction_Trigger closeAction; |
| | 37 | |
|
| | 38 | | [SerializeField] |
| | 39 | | internal Canvas mainCanvas; |
| | 40 | |
|
| | 41 | | [Header("Hide GOs on claimed name")] |
| | 42 | | [SerializeField] |
| | 43 | | internal GameObject[] hideOnNameClaimed; |
| | 44 | |
|
| | 45 | | [Header("Connected wallet sections")] |
| | 46 | | [SerializeField] |
| | 47 | | internal GameObject connectedWalletSection; |
| | 48 | |
|
| | 49 | | [SerializeField] |
| | 50 | | internal GameObject nonConnectedWalletSection; |
| | 51 | |
|
| | 52 | | [Header("Thumbnail")] |
| | 53 | | [SerializeField] |
| | 54 | | internal RawImage imageAvatarThumbnail; |
| | 55 | |
|
| | 56 | | [SerializeField] |
| | 57 | | protected internal Button buttonToggleMenu; |
| | 58 | |
|
| | 59 | | [Header("Texts")] |
| | 60 | | [SerializeField] |
| | 61 | | internal TextMeshProUGUI textName; |
| | 62 | |
|
| | 63 | | [SerializeField] |
| | 64 | | internal TextMeshProUGUI textPostfix; |
| | 65 | |
|
| | 66 | | [SerializeField] |
| | 67 | | internal TextMeshProUGUI textAddress; |
| | 68 | |
|
| | 69 | | [Header("Buttons")] |
| | 70 | | [SerializeField] |
| | 71 | | protected internal Button buttonClaimName; |
| | 72 | |
|
| | 73 | | [SerializeField] |
| | 74 | | protected internal Button buttonCopyAddress; |
| | 75 | |
|
| | 76 | | [SerializeField] |
| | 77 | | protected internal Button buttonLogOut; |
| | 78 | |
|
| | 79 | | [SerializeField] |
| | 80 | | protected internal Button buttonSignUp; |
| | 81 | |
|
| | 82 | | [SerializeField] |
| | 83 | | protected internal Button_OnPointerDown buttonTermsOfServiceForConnectedWallets; |
| | 84 | |
|
| | 85 | | [SerializeField] |
| | 86 | | protected internal Button_OnPointerDown buttonPrivacyPolicyForConnectedWallets; |
| | 87 | |
|
| | 88 | | [SerializeField] |
| | 89 | | protected internal Button_OnPointerDown buttonTermsOfServiceForNonConnectedWallets; |
| | 90 | |
|
| | 91 | | [SerializeField] |
| | 92 | | protected internal Button_OnPointerDown buttonPrivacyPolicyForNonConnectedWallets; |
| | 93 | |
|
| | 94 | | [Header("Name Edition")] |
| | 95 | | [SerializeField] |
| | 96 | | protected internal Button_OnPointerDown buttonEditName; |
| | 97 | |
|
| | 98 | | [SerializeField] |
| | 99 | | protected internal Button_OnPointerDown buttonEditNamePrefix; |
| | 100 | |
|
| | 101 | | [SerializeField] |
| | 102 | | internal TMP_InputField inputName; |
| | 103 | |
|
| | 104 | | [SerializeField] |
| | 105 | | internal TextMeshProUGUI textCharLimit; |
| | 106 | |
|
| | 107 | | [SerializeField] |
| | 108 | | internal ManaCounterView manaCounterView; |
| | 109 | |
|
| | 110 | | [SerializeField] |
| | 111 | | internal ManaCounterView polygonManaCounterView; |
| | 112 | |
|
| | 113 | | [Header("Tutorial Config")] |
| | 114 | | [SerializeField] |
| | 115 | | internal RectTransform tutorialTooltipReference; |
| | 116 | |
|
| | 117 | | [Header("Description")] |
| | 118 | | [SerializeField] |
| | 119 | | internal TMP_InputField descriptionPreviewInput; |
| | 120 | |
|
| | 121 | | [SerializeField] |
| | 122 | | internal TMP_InputField descriptionEditionInput; |
| | 123 | |
|
| | 124 | | [SerializeField] |
| | 125 | | internal GameObject charLimitDescriptionContainer; |
| | 126 | |
|
| | 127 | | [SerializeField] |
| | 128 | | internal TextMeshProUGUI textCharLimitDescription; |
| | 129 | |
|
| | 130 | | [SerializeField] |
| | 131 | | internal GameObject descriptionContainer; |
| | 132 | |
|
| 0 | 133 | | public RectTransform expandedMenu => mainRootLayout; |
| | 134 | |
|
| | 135 | | private InputAction_Trigger.Triggered closeActionDelegate; |
| | 136 | |
|
| | 137 | | private Coroutine copyToastRoutine = null; |
| | 138 | | private UserProfile profile = null; |
| | 139 | | private Regex nameRegex = null; |
| | 140 | |
|
| | 141 | | internal event Action OnOpen; |
| | 142 | | internal event Action OnClose; |
| | 143 | | internal bool isStartMenuInitialized = false; |
| | 144 | | private HUDCanvasCameraModeController hudCanvasCameraModeController; |
| | 145 | |
|
| | 146 | | private void Awake() |
| | 147 | | { |
| 17 | 148 | | closeActionDelegate = (x) => HideMenu(); |
| | 149 | |
|
| 17 | 150 | | buttonToggleMenu.onClick.AddListener(OpenStartMenu); |
| 17 | 151 | | buttonCopyAddress.onClick.AddListener(CopyAddress); |
| 17 | 152 | | buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 17 | 153 | | buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 17 | 154 | | inputName.onValueChanged.AddListener(UpdateNameCharLimit); |
| 17 | 155 | | inputName.onDeselect.AddListener((x) => ActivateProfileNameEditionMode(false)); |
| 17 | 156 | | descriptionPreviewInput.onSelect.AddListener(x => |
| | 157 | | { |
| 0 | 158 | | ActivateDescriptionEditionMode(true); |
| 0 | 159 | | UpdateDescriptionCharLimit(descriptionPreviewInput.text); |
| 0 | 160 | | }); |
| 17 | 161 | | descriptionEditionInput.onValueChanged.AddListener(UpdateDescriptionCharLimit); |
| 17 | 162 | | descriptionEditionInput.onDeselect.AddListener(x => ActivateDescriptionEditionMode(false)); |
| 17 | 163 | | copyToast.gameObject.SetActive(false); |
| 17 | 164 | | hudCanvasCameraModeController = new HUDCanvasCameraModeController(GetComponent<Canvas>(), DataStore.i.camera.hud |
| 17 | 165 | | } |
| | 166 | |
|
| | 167 | | internal void SetProfile(UserProfile userProfile) |
| | 168 | | { |
| 5 | 169 | | profile = userProfile; |
| 5 | 170 | | if (userProfile.hasClaimedName) |
| | 171 | | { |
| 1 | 172 | | HandleClaimedProfileName(userProfile); |
| 1 | 173 | | } |
| | 174 | | else |
| | 175 | | { |
| 4 | 176 | | HandleUnverifiedProfileName(userProfile); |
| | 177 | | } |
| | 178 | |
|
| 5 | 179 | | SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3); |
| 5 | 180 | | HandleProfileAddress(userProfile); |
| 5 | 181 | | HandleProfileSnapshot(userProfile); |
| 5 | 182 | | SetDescription(userProfile.description); |
| 5 | 183 | | SetDescriptionEnabled(userProfile.hasConnectedWeb3); |
| 5 | 184 | | ForceLayoutToRefreshSize(); |
| 5 | 185 | | } |
| | 186 | |
|
| | 187 | | internal void OpenStartMenu() |
| | 188 | | { |
| 0 | 189 | | if (isStartMenuInitialized) |
| | 190 | | { |
| 0 | 191 | | if (!DataStore.i.exploreV2.isOpen.Get()) |
| | 192 | | { |
| 0 | 193 | | var exploreV2Analytics = new ExploreV2Analytics.ExploreV2Analytics(); |
| 0 | 194 | | exploreV2Analytics.SendStartMenuVisibility( |
| | 195 | | true, |
| | 196 | | ExploreUIVisibilityMethod.FromClick); |
| | 197 | | } |
| 0 | 198 | | DataStore.i.exploreV2.isOpen.Set(true); |
| 0 | 199 | | } |
| | 200 | | else |
| | 201 | | { |
| 0 | 202 | | ToggleMenu(); |
| | 203 | | } |
| 0 | 204 | | } |
| | 205 | |
|
| 0 | 206 | | public void SetStartMenuButtonActive(bool isActive) { isStartMenuInitialized = isActive; } |
| | 207 | |
|
| | 208 | | internal void ToggleMenu() |
| | 209 | | { |
| 2 | 210 | | if (menuShowHideAnimator.isVisible) |
| | 211 | | { |
| 1 | 212 | | HideMenu(); |
| 1 | 213 | | } |
| | 214 | | else |
| | 215 | | { |
| 1 | 216 | | menuShowHideAnimator.Show(); |
| 1 | 217 | | CommonScriptableObjects.isProfileHUDOpen.Set(true); |
| 1 | 218 | | OnOpen?.Invoke(); |
| | 219 | | } |
| 1 | 220 | | } |
| | 221 | |
|
| | 222 | | internal void HideMenu() |
| | 223 | | { |
| 1 | 224 | | if (menuShowHideAnimator.isVisible) |
| | 225 | | { |
| 1 | 226 | | menuShowHideAnimator.Hide(); |
| 1 | 227 | | CommonScriptableObjects.isProfileHUDOpen.Set(false); |
| 1 | 228 | | OnClose?.Invoke(); |
| | 229 | | } |
| 1 | 230 | | } |
| | 231 | |
|
| | 232 | | internal void SetVisibility(bool visible) |
| | 233 | | { |
| 3 | 234 | | if (visible && !mainShowHideAnimator.isVisible) |
| 0 | 235 | | mainShowHideAnimator.Show(); |
| 3 | 236 | | else if (!visible && mainShowHideAnimator.isVisible) |
| 1 | 237 | | mainShowHideAnimator.Hide(); |
| 3 | 238 | | } |
| | 239 | |
|
| | 240 | | private void HandleProfileSnapshot(UserProfile userProfile) |
| | 241 | | { |
| 5 | 242 | | loadingSpinner.SetActive(true); |
| 5 | 243 | | userProfile.snapshotObserver.AddListener(SetProfileImage); |
| 5 | 244 | | } |
| | 245 | |
|
| | 246 | | private void HandleClaimedProfileName(UserProfile userProfile) |
| | 247 | | { |
| 1 | 248 | | textName.text = userProfile.userName; |
| 1 | 249 | | SetActiveUnverifiedNameGOs(false); |
| 1 | 250 | | } |
| | 251 | |
|
| | 252 | | private void HandleUnverifiedProfileName(UserProfile userProfile) |
| | 253 | | { |
| 4 | 254 | | if (!String.IsNullOrEmpty(userProfile.userName) && |
| | 255 | | userProfile.userName.Length > NAME_POSTFIX_LENGTH) |
| | 256 | | { |
| 4 | 257 | | textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1); |
| 4 | 258 | | } |
| | 259 | | else |
| | 260 | | { |
| 0 | 261 | | textName.text = userProfile.userName; |
| | 262 | | } |
| | 263 | |
|
| 4 | 264 | | textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}"; |
| 4 | 265 | | SetActiveUnverifiedNameGOs(true); |
| 4 | 266 | | } |
| | 267 | |
|
| | 268 | | private void SetConnectedWalletSectionActive(bool active) |
| | 269 | | { |
| 5 | 270 | | connectedWalletSection.SetActive(active); |
| 5 | 271 | | nonConnectedWalletSection.SetActive(!active); |
| 5 | 272 | | buttonLogOut.gameObject.SetActive(active); |
| 5 | 273 | | } |
| | 274 | |
|
| 10 | 275 | | private void ForceLayoutToRefreshSize() { LayoutRebuilder.ForceRebuildLayoutImmediate(mainRootLayout); } |
| | 276 | |
|
| | 277 | | private void SetActiveUnverifiedNameGOs(bool active) |
| | 278 | | { |
| 40 | 279 | | for (int i = 0; i < hideOnNameClaimed.Length; i++) |
| | 280 | | { |
| 15 | 281 | | hideOnNameClaimed[i].SetActive(active); |
| | 282 | | } |
| 5 | 283 | | } |
| | 284 | |
|
| | 285 | | private void HandleProfileAddress(UserProfile userProfile) |
| | 286 | | { |
| 5 | 287 | | string address = userProfile.userId; |
| 5 | 288 | | string start = address.Substring(0, ADDRESS_CHUNK_LENGTH); |
| 5 | 289 | | string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH); |
| 5 | 290 | | textAddress.text = $"{start}...{end}"; |
| 5 | 291 | | } |
| | 292 | |
|
| | 293 | | private void SetProfileImage(Texture2D texture) |
| | 294 | | { |
| 0 | 295 | | loadingSpinner.SetActive(false); |
| 0 | 296 | | imageAvatarThumbnail.texture = texture; |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | private void OnDestroy() |
| | 300 | | { |
| 17 | 301 | | hudCanvasCameraModeController?.Dispose(); |
| 17 | 302 | | if (profile) |
| 4 | 303 | | profile.snapshotObserver.RemoveListener(SetProfileImage); |
| 17 | 304 | | } |
| | 305 | |
|
| | 306 | | private void CopyAddress() |
| | 307 | | { |
| 0 | 308 | | if (!profile) |
| | 309 | | { |
| 0 | 310 | | return; |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | Environment.i.platform.clipboard.WriteText(profile.userId); |
| | 314 | |
|
| 0 | 315 | | copyTooltip.gameObject.SetActive(false); |
| 0 | 316 | | if (copyToastRoutine != null) |
| | 317 | | { |
| 0 | 318 | | StopCoroutine(copyToastRoutine); |
| | 319 | | } |
| | 320 | |
|
| 0 | 321 | | copyToastRoutine = StartCoroutine(ShowCopyToast()); |
| 0 | 322 | | } |
| | 323 | |
|
| | 324 | | private IEnumerator ShowCopyToast() |
| | 325 | | { |
| 0 | 326 | | if (!copyToast.gameObject.activeSelf) |
| | 327 | | { |
| 0 | 328 | | copyToast.gameObject.SetActive(true); |
| | 329 | | } |
| | 330 | |
|
| 0 | 331 | | copyToast.Show(); |
| 0 | 332 | | yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME); |
| 0 | 333 | | copyToast.Hide(); |
| 0 | 334 | | } |
| | 335 | |
|
| 34 | 336 | | private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; } |
| | 337 | |
|
| 34 | 338 | | private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; } |
| | 339 | |
|
| | 340 | | internal void ActivateProfileNameEditionMode(bool activate) |
| | 341 | | { |
| 2 | 342 | | if (profile != null && profile.hasClaimedName) |
| 0 | 343 | | return; |
| | 344 | |
|
| 2 | 345 | | textName.gameObject.SetActive(!activate); |
| 2 | 346 | | inputName.gameObject.SetActive(activate); |
| | 347 | |
|
| 2 | 348 | | if (activate) |
| | 349 | | { |
| 1 | 350 | | inputName.text = textName.text; |
| 1 | 351 | | inputName.Select(); |
| | 352 | | } |
| 2 | 353 | | } |
| | 354 | |
|
| 6 | 355 | | private void UpdateNameCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLim |
| | 356 | |
|
| 2 | 357 | | internal void SetProfileName(string newName) { textName.text = newName; } |
| | 358 | |
|
| 0 | 359 | | internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); } |
| | 360 | |
|
| | 361 | | internal bool IsValidAvatarName(string name) |
| | 362 | | { |
| 0 | 363 | | if (nameRegex == null) |
| 0 | 364 | | return true; |
| | 365 | |
|
| 0 | 366 | | return nameRegex.IsMatch(name); |
| | 367 | | } |
| | 368 | |
|
| | 369 | | internal void ActivateDescriptionEditionMode(bool active) |
| | 370 | | { |
| 22 | 371 | | charLimitDescriptionContainer.SetActive(active); |
| 22 | 372 | | descriptionEditionInput.gameObject.SetActive(active); |
| 22 | 373 | | descriptionPreviewInput.gameObject.SetActive(!active); |
| | 374 | |
|
| 22 | 375 | | if (active) |
| | 376 | | { |
| 3 | 377 | | descriptionEditionInput.text = descriptionPreviewInput.text; |
| 3 | 378 | | StartCoroutine(SelectComponentOnNextFrame(descriptionEditionInput)); |
| | 379 | | } |
| 22 | 380 | | } |
| | 381 | |
|
| | 382 | | private IEnumerator SelectComponentOnNextFrame(Selectable selectable) |
| | 383 | | { |
| 3 | 384 | | yield return null; |
| 0 | 385 | | selectable.Select(); |
| 0 | 386 | | } |
| | 387 | |
|
| | 388 | | internal void SetDescription(string description) |
| | 389 | | { |
| 7 | 390 | | descriptionPreviewInput.text = description; |
| 7 | 391 | | descriptionEditionInput.text = description; |
| 7 | 392 | | } |
| | 393 | |
|
| 18 | 394 | | private void UpdateDescriptionCharLimit(string newValue) { textCharLimitDescription.text = $"{newValue.Length}/{desc |
| | 395 | |
|
| 10 | 396 | | private void SetDescriptionEnabled(bool enabled) { descriptionContainer.SetActive(enabled); } |
| | 397 | |
|
| | 398 | | public void SetCardAsFullScreenMenuMode(bool isActive) |
| | 399 | | { |
| 0 | 400 | | buttonToggleMenu.gameObject.SetActive(!isActive); |
| 0 | 401 | | mainCanvas.sortingOrder = isActive ? 4 : 1; |
| 0 | 402 | | } |
| | 403 | | } |