| | 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 | |
|
| | 145 | | private void Awake() |
| | 146 | | { |
| 17 | 147 | | closeActionDelegate = (x) => HideMenu(); |
| | 148 | |
|
| 17 | 149 | | buttonToggleMenu.onClick.AddListener(OpenStartMenu); |
| 17 | 150 | | buttonCopyAddress.onClick.AddListener(CopyAddress); |
| 17 | 151 | | buttonEditName.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 17 | 152 | | buttonEditNamePrefix.onPointerDown += () => ActivateProfileNameEditionMode(true); |
| 17 | 153 | | inputName.onValueChanged.AddListener(UpdateNameCharLimit); |
| 17 | 154 | | inputName.onDeselect.AddListener((x) => ActivateProfileNameEditionMode(false)); |
| 17 | 155 | | descriptionPreviewInput.onSelect.AddListener(x => |
| | 156 | | { |
| 0 | 157 | | ActivateDescriptionEditionMode(true); |
| 0 | 158 | | UpdateDescriptionCharLimit(descriptionPreviewInput.text); |
| 0 | 159 | | }); |
| 17 | 160 | | descriptionEditionInput.onValueChanged.AddListener(UpdateDescriptionCharLimit); |
| 17 | 161 | | descriptionEditionInput.onDeselect.AddListener(x => ActivateDescriptionEditionMode(false)); |
| 17 | 162 | | copyToast.gameObject.SetActive(false); |
| 17 | 163 | | } |
| | 164 | |
|
| | 165 | | internal void SetProfile(UserProfile userProfile) |
| | 166 | | { |
| 5 | 167 | | profile = userProfile; |
| 5 | 168 | | if (userProfile.hasClaimedName) |
| | 169 | | { |
| 1 | 170 | | HandleClaimedProfileName(userProfile); |
| 1 | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 4 | 174 | | HandleUnverifiedProfileName(userProfile); |
| | 175 | | } |
| | 176 | |
|
| 5 | 177 | | SetConnectedWalletSectionActive(userProfile.hasConnectedWeb3); |
| 5 | 178 | | HandleProfileAddress(userProfile); |
| 5 | 179 | | HandleProfileSnapshot(userProfile); |
| 5 | 180 | | SetDescription(userProfile.description); |
| 5 | 181 | | SetDescriptionEnabled(userProfile.hasConnectedWeb3); |
| 5 | 182 | | ForceLayoutToRefreshSize(); |
| 5 | 183 | | } |
| | 184 | |
|
| | 185 | | internal void OpenStartMenu() |
| | 186 | | { |
| 0 | 187 | | if (isStartMenuInitialized) |
| | 188 | | { |
| 0 | 189 | | if (!DataStore.i.exploreV2.isOpen.Get()) |
| | 190 | | { |
| 0 | 191 | | var exploreV2Analytics = new ExploreV2Analytics.ExploreV2Analytics(); |
| 0 | 192 | | exploreV2Analytics.SendStartMenuVisibility( |
| | 193 | | true, |
| | 194 | | ExploreUIVisibilityMethod.FromClick); |
| | 195 | | } |
| 0 | 196 | | DataStore.i.exploreV2.isOpen.Set(true); |
| 0 | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| 0 | 200 | | ToggleMenu(); |
| | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | public void SetStartMenuButtonActive(bool isActive) { isStartMenuInitialized = isActive; } |
| | 205 | |
|
| | 206 | | internal void ToggleMenu() |
| | 207 | | { |
| 2 | 208 | | if (menuShowHideAnimator.isVisible) |
| | 209 | | { |
| 1 | 210 | | HideMenu(); |
| 1 | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 1 | 214 | | menuShowHideAnimator.Show(); |
| 1 | 215 | | CommonScriptableObjects.isProfileHUDOpen.Set(true); |
| 1 | 216 | | OnOpen?.Invoke(); |
| | 217 | | } |
| 1 | 218 | | } |
| | 219 | |
|
| | 220 | | internal void HideMenu() |
| | 221 | | { |
| 1 | 222 | | if (menuShowHideAnimator.isVisible) |
| | 223 | | { |
| 1 | 224 | | menuShowHideAnimator.Hide(); |
| 1 | 225 | | CommonScriptableObjects.isProfileHUDOpen.Set(false); |
| 1 | 226 | | OnClose?.Invoke(); |
| | 227 | | } |
| 1 | 228 | | } |
| | 229 | |
|
| | 230 | | internal void SetVisibility(bool visible) |
| | 231 | | { |
| 3 | 232 | | if (visible && !mainShowHideAnimator.isVisible) |
| 0 | 233 | | mainShowHideAnimator.Show(); |
| 3 | 234 | | else if (!visible && mainShowHideAnimator.isVisible) |
| 1 | 235 | | mainShowHideAnimator.Hide(); |
| 3 | 236 | | } |
| | 237 | |
|
| | 238 | | private void HandleProfileSnapshot(UserProfile userProfile) |
| | 239 | | { |
| 5 | 240 | | loadingSpinner.SetActive(true); |
| 5 | 241 | | userProfile.snapshotObserver.AddListener(SetProfileImage); |
| 5 | 242 | | } |
| | 243 | |
|
| | 244 | | private void HandleClaimedProfileName(UserProfile userProfile) |
| | 245 | | { |
| 1 | 246 | | textName.text = userProfile.userName; |
| 1 | 247 | | SetActiveUnverifiedNameGOs(false); |
| 1 | 248 | | } |
| | 249 | |
|
| | 250 | | private void HandleUnverifiedProfileName(UserProfile userProfile) |
| | 251 | | { |
| 4 | 252 | | if (!String.IsNullOrEmpty(userProfile.userName) && |
| | 253 | | userProfile.userName.Length > NAME_POSTFIX_LENGTH) |
| | 254 | | { |
| 4 | 255 | | textName.text = userProfile.userName.Substring(0, userProfile.userName.Length - NAME_POSTFIX_LENGTH - 1); |
| 4 | 256 | | } |
| | 257 | | else |
| | 258 | | { |
| 0 | 259 | | textName.text = userProfile.userName; |
| | 260 | | } |
| | 261 | |
|
| 4 | 262 | | textPostfix.text = $"#{userProfile.userId.Substring(userProfile.userId.Length - NAME_POSTFIX_LENGTH)}"; |
| 4 | 263 | | SetActiveUnverifiedNameGOs(true); |
| 4 | 264 | | } |
| | 265 | |
|
| | 266 | | private void SetConnectedWalletSectionActive(bool active) |
| | 267 | | { |
| 5 | 268 | | connectedWalletSection.SetActive(active); |
| 5 | 269 | | nonConnectedWalletSection.SetActive(!active); |
| 5 | 270 | | buttonLogOut.gameObject.SetActive(active); |
| 5 | 271 | | } |
| | 272 | |
|
| 10 | 273 | | private void ForceLayoutToRefreshSize() { LayoutRebuilder.ForceRebuildLayoutImmediate(mainRootLayout); } |
| | 274 | |
|
| | 275 | | private void SetActiveUnverifiedNameGOs(bool active) |
| | 276 | | { |
| 40 | 277 | | for (int i = 0; i < hideOnNameClaimed.Length; i++) |
| | 278 | | { |
| 15 | 279 | | hideOnNameClaimed[i].SetActive(active); |
| | 280 | | } |
| 5 | 281 | | } |
| | 282 | |
|
| | 283 | | private void HandleProfileAddress(UserProfile userProfile) |
| | 284 | | { |
| 5 | 285 | | string address = userProfile.userId; |
| 5 | 286 | | string start = address.Substring(0, ADDRESS_CHUNK_LENGTH); |
| 5 | 287 | | string end = address.Substring(address.Length - ADDRESS_CHUNK_LENGTH); |
| 5 | 288 | | textAddress.text = $"{start}...{end}"; |
| 5 | 289 | | } |
| | 290 | |
|
| | 291 | | private void SetProfileImage(Texture2D texture) |
| | 292 | | { |
| 0 | 293 | | loadingSpinner.SetActive(false); |
| 0 | 294 | | imageAvatarThumbnail.texture = texture; |
| 0 | 295 | | } |
| | 296 | |
|
| | 297 | | private void OnDestroy() |
| | 298 | | { |
| 17 | 299 | | if (profile) |
| 4 | 300 | | profile.snapshotObserver.RemoveListener(SetProfileImage); |
| 17 | 301 | | } |
| | 302 | |
|
| | 303 | | private void CopyAddress() |
| | 304 | | { |
| 0 | 305 | | if (!profile) |
| | 306 | | { |
| 0 | 307 | | return; |
| | 308 | | } |
| | 309 | |
|
| 0 | 310 | | Environment.i.platform.clipboard.WriteText(profile.userId); |
| | 311 | |
|
| 0 | 312 | | copyTooltip.gameObject.SetActive(false); |
| 0 | 313 | | if (copyToastRoutine != null) |
| | 314 | | { |
| 0 | 315 | | StopCoroutine(copyToastRoutine); |
| | 316 | | } |
| | 317 | |
|
| 0 | 318 | | copyToastRoutine = StartCoroutine(ShowCopyToast()); |
| 0 | 319 | | } |
| | 320 | |
|
| | 321 | | private IEnumerator ShowCopyToast() |
| | 322 | | { |
| 0 | 323 | | if (!copyToast.gameObject.activeSelf) |
| | 324 | | { |
| 0 | 325 | | copyToast.gameObject.SetActive(true); |
| | 326 | | } |
| | 327 | |
|
| 0 | 328 | | copyToast.Show(); |
| 0 | 329 | | yield return new WaitForSeconds(COPY_TOAST_VISIBLE_TIME); |
| 0 | 330 | | copyToast.Hide(); |
| 0 | 331 | | } |
| | 332 | |
|
| 34 | 333 | | private void OnEnable() { closeAction.OnTriggered += closeActionDelegate; } |
| | 334 | |
|
| 34 | 335 | | private void OnDisable() { closeAction.OnTriggered -= closeActionDelegate; } |
| | 336 | |
|
| | 337 | | internal void ActivateProfileNameEditionMode(bool activate) |
| | 338 | | { |
| 2 | 339 | | if (profile != null && profile.hasClaimedName) |
| 0 | 340 | | return; |
| | 341 | |
|
| 2 | 342 | | textName.gameObject.SetActive(!activate); |
| 2 | 343 | | inputName.gameObject.SetActive(activate); |
| | 344 | |
|
| 2 | 345 | | if (activate) |
| | 346 | | { |
| 1 | 347 | | inputName.text = textName.text; |
| 1 | 348 | | inputName.Select(); |
| | 349 | | } |
| 2 | 350 | | } |
| | 351 | |
|
| 6 | 352 | | private void UpdateNameCharLimit(string newValue) { textCharLimit.text = $"{newValue.Length}/{inputName.characterLim |
| | 353 | |
|
| 2 | 354 | | internal void SetProfileName(string newName) { textName.text = newName; } |
| | 355 | |
|
| 0 | 356 | | internal void SetNameRegex(string namePattern) { nameRegex = new Regex(namePattern); } |
| | 357 | |
|
| | 358 | | internal bool IsValidAvatarName(string name) |
| | 359 | | { |
| 0 | 360 | | if (nameRegex == null) |
| 0 | 361 | | return true; |
| | 362 | |
|
| 0 | 363 | | return nameRegex.IsMatch(name); |
| | 364 | | } |
| | 365 | |
|
| | 366 | | internal void ActivateDescriptionEditionMode(bool active) |
| | 367 | | { |
| 22 | 368 | | charLimitDescriptionContainer.SetActive(active); |
| 22 | 369 | | descriptionEditionInput.gameObject.SetActive(active); |
| 22 | 370 | | descriptionPreviewInput.gameObject.SetActive(!active); |
| | 371 | |
|
| 22 | 372 | | if (active) |
| | 373 | | { |
| 3 | 374 | | descriptionEditionInput.text = descriptionPreviewInput.text; |
| 3 | 375 | | StartCoroutine(SelectComponentOnNextFrame(descriptionEditionInput)); |
| | 376 | | } |
| 22 | 377 | | } |
| | 378 | |
|
| | 379 | | private IEnumerator SelectComponentOnNextFrame(Selectable selectable) |
| | 380 | | { |
| 3 | 381 | | yield return null; |
| 0 | 382 | | selectable.Select(); |
| 0 | 383 | | } |
| | 384 | |
|
| | 385 | | internal void SetDescription(string description) |
| | 386 | | { |
| 7 | 387 | | descriptionPreviewInput.text = description; |
| 7 | 388 | | descriptionEditionInput.text = description; |
| 7 | 389 | | } |
| | 390 | |
|
| 18 | 391 | | private void UpdateDescriptionCharLimit(string newValue) { textCharLimitDescription.text = $"{newValue.Length}/{desc |
| | 392 | |
|
| 10 | 393 | | private void SetDescriptionEnabled(bool enabled) { descriptionContainer.SetActive(enabled); } |
| | 394 | |
|
| | 395 | | public void SetCardAsFullScreenMenuMode(bool isActive) |
| | 396 | | { |
| 0 | 397 | | buttonToggleMenu.gameObject.SetActive(!isActive); |
| 0 | 398 | | mainCanvas.sortingOrder = isActive ? 4 : 1; |
| 0 | 399 | | } |
| | 400 | | } |