| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Configuration; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | | using UnityEngine.Events; |
| | 10 | | using UnityEngine.EventSystems; |
| | 11 | | using UnityEngine.UI; |
| | 12 | | using System.Linq; |
| | 13 | |
|
| | 14 | | public class PlayerInfoCardHUDView : MonoBehaviour |
| | 15 | | { |
| | 16 | | private const string PREFAB_PATH = "PlayerInfoCardHUD"; |
| | 17 | |
|
| | 18 | | public enum Tabs |
| | 19 | | { |
| | 20 | | Passport, |
| | 21 | | Trade, |
| | 22 | | Block |
| | 23 | | } |
| | 24 | |
|
| | 25 | | [System.Serializable] |
| | 26 | | internal class TabsMapping |
| | 27 | | { |
| | 28 | | public GameObject container; |
| | 29 | | public Toggle toggle; |
| | 30 | | public Tabs tab; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | [SerializeField] internal GenericFactory collectiblesFactory; |
| | 34 | | [SerializeField] internal Canvas cardCanvas; |
| | 35 | | [SerializeField] internal TabsMapping[] tabsMapping; |
| | 36 | | [SerializeField] internal Button hideCardButton; |
| | 37 | |
|
| | 38 | | [Space] [SerializeField] internal RawImage avatarPicture; |
| | 39 | | [SerializeField] internal Image blockedAvatarOverlay; |
| | 40 | | [SerializeField] internal TextMeshProUGUI name; |
| | 41 | |
|
| | 42 | | [Header("Friends")] [SerializeField] internal GameObject friendStatusContainer; |
| | 43 | | [SerializeField] internal Button requestSentButton; |
| | 44 | | [SerializeField] internal Button addFriendButton; |
| | 45 | | [SerializeField] internal GameObject alreadyFriendsContainer; |
| | 46 | | [SerializeField] internal GameObject requestReceivedContainer; |
| | 47 | | [SerializeField] internal Button acceptRequestButton; |
| | 48 | | [SerializeField] internal Button rejectRequestButton; |
| | 49 | |
|
| | 50 | | [Header("Passport")] [SerializeField] internal TextMeshProUGUI description; |
| | 51 | |
|
| | 52 | | [Header("Trade")] [SerializeField] private RectTransform wearablesContainer; |
| | 53 | | [SerializeField] private GameObject emptyCollectiblesImage; |
| | 54 | |
|
| | 55 | | [Header("Block")] [SerializeField] internal Button reportPlayerButton; |
| | 56 | | [SerializeField] internal Button blockPlayerButton; |
| | 57 | | [SerializeField] internal Button unblockPlayerButton; |
| | 58 | |
|
| 26 | 59 | | internal readonly List<PlayerInfoCollectibleItem> playerInfoCollectibles = new List<PlayerInfoCollectibleItem>(10); |
| | 60 | | internal UserProfile currentUserProfile; |
| 4 | 61 | | private UnityAction<bool> toggleChangedDelegate => (x) => UpdateTabs(); |
| 12 | 62 | | private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 63 | |
|
| | 64 | | private MouseCatcher mouseCatcher; |
| 26 | 65 | | private List<string> loadedWearables = new List<string>(); |
| | 66 | |
|
| 13 | 67 | | public static PlayerInfoCardHUDView CreateView() { return Instantiate(Resources.Load<GameObject>(PREFAB_PATH)).GetCo |
| | 68 | |
|
| | 69 | | public void Initialize(UnityAction cardClosedCallback, |
| | 70 | | UnityAction reportPlayerCallback, |
| | 71 | | UnityAction blockPlayerCallback, |
| | 72 | | UnityAction unblockPlayerCallback, |
| | 73 | | UnityAction addFriendCallback, |
| | 74 | | UnityAction cancelInvitation, |
| | 75 | | UnityAction acceptFriendRequest, |
| | 76 | | UnityAction rejectFriendRequest) |
| | 77 | | { |
| 14 | 78 | | hideCardButton.onClick.RemoveAllListeners(); |
| 14 | 79 | | hideCardButton.onClick.AddListener(cardClosedCallback); |
| | 80 | |
|
| 14 | 81 | | reportPlayerButton.onClick.RemoveAllListeners(); |
| 14 | 82 | | reportPlayerButton.onClick.AddListener(reportPlayerCallback); |
| | 83 | |
|
| 14 | 84 | | blockPlayerButton.onClick.RemoveAllListeners(); |
| 14 | 85 | | blockPlayerButton.onClick.AddListener(blockPlayerCallback); |
| | 86 | |
|
| 14 | 87 | | unblockPlayerButton.onClick.RemoveAllListeners(); |
| 14 | 88 | | unblockPlayerButton.onClick.AddListener(unblockPlayerCallback); |
| | 89 | |
|
| 14 | 90 | | addFriendButton.onClick.RemoveAllListeners(); |
| 14 | 91 | | addFriendButton.onClick.AddListener(addFriendCallback); |
| | 92 | |
|
| 14 | 93 | | requestSentButton.onClick.RemoveAllListeners(); |
| 14 | 94 | | requestSentButton.onClick.AddListener(cancelInvitation); |
| | 95 | |
|
| 14 | 96 | | acceptRequestButton.onClick.RemoveAllListeners(); |
| 14 | 97 | | acceptRequestButton.onClick.AddListener(acceptFriendRequest); |
| | 98 | |
|
| 14 | 99 | | rejectRequestButton.onClick.RemoveAllListeners(); |
| 14 | 100 | | rejectRequestButton.onClick.AddListener(rejectFriendRequest); |
| | 101 | |
|
| 112 | 102 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 103 | | { |
| 42 | 104 | | var tab = tabsMapping[index]; |
| 42 | 105 | | tab.toggle.onValueChanged.RemoveListener(toggleChangedDelegate); |
| 42 | 106 | | tab.toggle.onValueChanged.AddListener(toggleChangedDelegate); |
| | 107 | | } |
| | 108 | |
|
| 28 | 109 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 110 | | { |
| 14 | 111 | | if (tabsMapping[index].tab == Tabs.Passport) |
| | 112 | | { |
| 14 | 113 | | tabsMapping[index].toggle.isOn = true; |
| 14 | 114 | | break; |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | |
|
| 14 | 119 | | FriendsController.i.OnUpdateFriendship -= OnFriendStatusUpdated; |
| 14 | 120 | | FriendsController.i.OnUpdateFriendship += OnFriendStatusUpdated; |
| | 121 | |
|
| 14 | 122 | | if (InitialSceneReferences.i != null) |
| | 123 | | { |
| 14 | 124 | | var mouseCatcher = DCL.InitialSceneReferences.i.mouseCatcher; |
| | 125 | |
|
| 14 | 126 | | if (mouseCatcher != null) |
| | 127 | | { |
| 14 | 128 | | this.mouseCatcher = mouseCatcher; |
| 14 | 129 | | mouseCatcher.OnMouseDown += OnPointerDown; |
| | 130 | | } |
| | 131 | | } |
| 14 | 132 | | } |
| | 133 | |
|
| | 134 | | private void OnFriendStatusUpdated(string userId, FriendshipAction action) |
| | 135 | | { |
| 0 | 136 | | if (currentUserProfile == null) |
| 0 | 137 | | return; |
| | 138 | |
|
| 0 | 139 | | UpdateFriendButton(); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | public void SetCardActive(bool active) |
| | 143 | | { |
| 8 | 144 | | if (active && mouseCatcher != null) |
| | 145 | | { |
| 3 | 146 | | mouseCatcher.UnlockCursor(); |
| 3 | 147 | | } |
| 5 | 148 | | else if (!active) |
| | 149 | | { |
| 5 | 150 | | CatalogController.RemoveWearablesInUse(loadedWearables); |
| 5 | 151 | | loadedWearables.Clear(); |
| | 152 | | } |
| | 153 | |
|
| 8 | 154 | | cardCanvas.enabled = active; |
| 8 | 155 | | CommonScriptableObjects.playerInfoCardVisibleState.Set(active); |
| 8 | 156 | | } |
| | 157 | |
|
| | 158 | | private void UpdateTabs() |
| | 159 | | { |
| 32 | 160 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 161 | | { |
| 12 | 162 | | tabsMapping[index].container.SetActive(tabsMapping[index].toggle.isOn); |
| | 163 | | } |
| 4 | 164 | | } |
| | 165 | |
|
| 0 | 166 | | public void SetFaceSnapshot(Texture2D texture) { avatarPicture.texture = texture; } |
| | 167 | |
|
| | 168 | | public void SetUserProfile(UserProfile userProfile) |
| | 169 | | { |
| 4 | 170 | | Assert.IsTrue(userProfile != null, "userProfile can't be null"); |
| | 171 | |
|
| 4 | 172 | | name.text = userProfile.userName; |
| 4 | 173 | | description.text = userProfile.description; |
| | 174 | |
|
| 4 | 175 | | ClearCollectibles(); |
| | 176 | |
|
| 4 | 177 | | CatalogController.RequestOwnedWearables(userProfile.userId) |
| | 178 | | .Then((ownedWearables) => |
| | 179 | | { |
| 0 | 180 | | userProfile.SetInventory(ownedWearables.Select(x => x.id).ToArray()); |
| 0 | 181 | | loadedWearables.AddRange(ownedWearables.Select(x => x.id)); |
| | 182 | |
|
| 0 | 183 | | var collectiblesIds = currentUserProfile.GetInventoryItemsIds(); |
| 0 | 184 | | for (int index = 0; index < collectiblesIds.Length; index++) |
| | 185 | | { |
| 0 | 186 | | string collectibleId = collectiblesIds[index]; |
| 0 | 187 | | CatalogController.wearableCatalog.TryGetValue(collectibleId, out WearableItem collectib |
| 0 | 188 | | if (collectible == null) |
| | 189 | | continue; |
| | 190 | |
|
| 0 | 191 | | var playerInfoCollectible = |
| | 192 | | collectiblesFactory.Instantiate<PlayerInfoCollectibleItem>(collectible.rarity, |
| | 193 | | wearablesContainer.transform); |
| 0 | 194 | | if (playerInfoCollectible == null) |
| | 195 | | continue; |
| 0 | 196 | | playerInfoCollectibles.Add(playerInfoCollectible); |
| 0 | 197 | | playerInfoCollectible.Initialize(collectible); |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | emptyCollectiblesImage.SetActive(collectiblesIds.Length == 0); |
| 0 | 201 | | }) |
| 2 | 202 | | .Catch((error) => Debug.Log(error)); |
| | 203 | |
|
| 4 | 204 | | SetIsBlocked(IsBlocked(userProfile.userId)); |
| | 205 | |
|
| | 206 | | // Remove old profile listener and set the new one |
| 4 | 207 | | if ( currentUserProfile != null ) |
| 0 | 208 | | currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot); |
| | 209 | |
|
| 4 | 210 | | userProfile.snapshotObserver.AddListener(SetFaceSnapshot); |
| | 211 | |
|
| 4 | 212 | | currentUserProfile = userProfile; |
| | 213 | |
|
| 4 | 214 | | UpdateFriendButton(); |
| 4 | 215 | | } |
| | 216 | |
|
| | 217 | | private void UpdateFriendButton() |
| | 218 | | { |
| 4 | 219 | | if (currentUserProfile == null) |
| | 220 | | { |
| 0 | 221 | | requestReceivedContainer.SetActive(false); |
| 0 | 222 | | addFriendButton.gameObject.SetActive(false); |
| 0 | 223 | | alreadyFriendsContainer.SetActive(false); |
| 0 | 224 | | requestSentButton.gameObject.SetActive(false); |
| 0 | 225 | | return; |
| | 226 | | } |
| | 227 | |
|
| 4 | 228 | | bool canUseFriendButton = FriendsController.i != null && FriendsController.i.isInitialized && currentUserProfile |
| | 229 | |
|
| 4 | 230 | | friendStatusContainer.SetActive(canUseFriendButton); |
| | 231 | |
|
| 4 | 232 | | if (!canUseFriendButton) |
| | 233 | | { |
| 4 | 234 | | addFriendButton.gameObject.SetActive(false); |
| 4 | 235 | | alreadyFriendsContainer.SetActive(false); |
| 4 | 236 | | requestSentButton.gameObject.SetActive(false); |
| 4 | 237 | | return; |
| | 238 | | } |
| | 239 | |
|
| 0 | 240 | | var status = FriendsController.i.GetUserStatus(currentUserProfile.userId); |
| | 241 | |
|
| 0 | 242 | | addFriendButton.gameObject.SetActive(false); |
| 0 | 243 | | alreadyFriendsContainer.SetActive(false); |
| 0 | 244 | | requestReceivedContainer.SetActive(false); |
| 0 | 245 | | requestSentButton.gameObject.SetActive(false); |
| | 246 | |
|
| 0 | 247 | | switch (status.friendshipStatus) |
| | 248 | | { |
| | 249 | | case FriendshipStatus.NONE: |
| 0 | 250 | | addFriendButton.gameObject.SetActive(true); |
| 0 | 251 | | break; |
| | 252 | | case FriendshipStatus.FRIEND: |
| 0 | 253 | | alreadyFriendsContainer.SetActive(true); |
| 0 | 254 | | break; |
| | 255 | | case FriendshipStatus.REQUESTED_FROM: |
| 0 | 256 | | requestReceivedContainer.SetActive(true); |
| 0 | 257 | | break; |
| | 258 | | case FriendshipStatus.REQUESTED_TO: |
| 0 | 259 | | requestSentButton.gameObject.SetActive(true); |
| | 260 | | break; |
| | 261 | | } |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | public void SetIsBlocked(bool isBlocked) |
| | 265 | | { |
| 4 | 266 | | unblockPlayerButton.gameObject.SetActive(isBlocked); |
| 4 | 267 | | blockedAvatarOverlay.gameObject.SetActive(isBlocked); |
| 4 | 268 | | } |
| | 269 | |
|
| | 270 | | public void SetVisibility(bool visible) |
| | 271 | | { |
| 1 | 272 | | if (gameObject.activeSelf != visible) |
| | 273 | | { |
| 0 | 274 | | if ( visible ) |
| | 275 | | { |
| 0 | 276 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 0 | 277 | | currentUserProfile.snapshotObserver.AddListener(SetFaceSnapshot); |
| 0 | 278 | | } |
| | 279 | | else |
| | 280 | | { |
| 0 | 281 | | AudioScriptableObjects.dialogClose.Play(true); |
| 0 | 282 | | currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot); |
| | 283 | | } |
| | 284 | | } |
| | 285 | |
|
| 1 | 286 | | gameObject.SetActive(visible); |
| 1 | 287 | | } |
| | 288 | |
|
| | 289 | | private void ClearCollectibles() |
| | 290 | | { |
| 8 | 291 | | for (var i = playerInfoCollectibles.Count - 1; i >= 0; i--) |
| | 292 | | { |
| 0 | 293 | | var playerInfoCollectible = playerInfoCollectibles[i]; |
| 0 | 294 | | playerInfoCollectibles.RemoveAt(i); |
| 0 | 295 | | Destroy(playerInfoCollectible.gameObject); |
| | 296 | | } |
| 4 | 297 | | } |
| | 298 | |
|
| | 299 | | internal bool IsBlocked(string userId) |
| | 300 | | { |
| 4 | 301 | | if (ownUserProfile == null || ownUserProfile.blocked == null) |
| 0 | 302 | | return false; |
| | 303 | |
|
| 8 | 304 | | for (int i = 0; i < ownUserProfile.blocked.Count; i++) |
| | 305 | | { |
| 0 | 306 | | if (ownUserProfile.blocked[i] == userId) |
| 0 | 307 | | return true; |
| | 308 | | } |
| | 309 | |
|
| 4 | 310 | | return false; |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | private void OnPointerDown() { hideCardButton.onClick.Invoke(); } |
| | 314 | |
|
| | 315 | | private void OnDestroy() |
| | 316 | | { |
| 13 | 317 | | if ( currentUserProfile != null ) |
| 4 | 318 | | currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot); |
| | 319 | |
|
| 13 | 320 | | if (mouseCatcher != null) |
| 13 | 321 | | mouseCatcher.OnMouseDown -= OnPointerDown; |
| 13 | 322 | | } |
| | 323 | | } |