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