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