| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | | using UnityEngine.Events; |
| | 10 | | using UnityEngine.UI; |
| | 11 | |
|
| | 12 | | public class PlayerInfoCardHUDView : MonoBehaviour |
| | 13 | | { |
| | 14 | | private const string PREFAB_PATH = "PlayerInfoCardHUD"; |
| | 15 | |
|
| | 16 | | public enum Tabs |
| | 17 | | { |
| | 18 | | Passport, |
| | 19 | | Trade, |
| | 20 | | Block |
| | 21 | | } |
| | 22 | |
|
| | 23 | | [Serializable] |
| | 24 | | internal class TabsMapping |
| | 25 | | { |
| | 26 | | public GameObject container; |
| | 27 | | public Toggle toggle; |
| | 28 | | public Tabs tab; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | [SerializeField] internal GenericFactory collectiblesFactory; |
| | 32 | | [SerializeField] internal Canvas cardCanvas; |
| | 33 | | [SerializeField] internal TabsMapping[] tabsMapping; |
| | 34 | | [SerializeField] internal Button hideCardButton; |
| | 35 | |
|
| | 36 | | [Space] [SerializeField] internal RawImage avatarPicture; |
| | 37 | | [SerializeField] internal Image blockedAvatarOverlay; |
| | 38 | | [SerializeField] internal TextMeshProUGUI name; |
| | 39 | |
|
| | 40 | | [Header("Friends")] [SerializeField] internal GameObject friendStatusContainer; |
| | 41 | | [SerializeField] internal Button requestSentButton; |
| | 42 | | [SerializeField] internal Button addFriendButton; |
| | 43 | | [SerializeField] internal GameObject alreadyFriendsContainer; |
| | 44 | | [SerializeField] internal GameObject requestReceivedContainer; |
| | 45 | | [SerializeField] internal Button acceptRequestButton; |
| | 46 | | [SerializeField] internal Button rejectRequestButton; |
| | 47 | |
|
| | 48 | | [Header("Passport")] [SerializeField] internal TextMeshProUGUI description; |
| | 49 | |
|
| | 50 | | [Header("Trade")] [SerializeField] private RectTransform wearablesContainer; |
| | 51 | | [SerializeField] private GameObject emptyCollectiblesImage; |
| | 52 | |
|
| | 53 | | [Header("Block")] [SerializeField] internal Button reportPlayerButton; |
| | 54 | | [SerializeField] internal Button blockPlayerButton; |
| | 55 | | [SerializeField] internal Button unblockPlayerButton; |
| | 56 | |
|
| 27 | 57 | | internal readonly List<PlayerInfoCollectibleItem> playerInfoCollectibles = new List<PlayerInfoCollectibleItem>(10); |
| 4 | 58 | | private UnityAction<bool> toggleChangedDelegate => (x) => UpdateTabs(); |
| | 59 | |
|
| | 60 | | private MouseCatcher mouseCatcher; |
| | 61 | |
|
| | 62 | | public static PlayerInfoCardHUDView CreateView() |
| | 63 | | { |
| 26 | 64 | | return Instantiate(Resources.Load<GameObject>(PREFAB_PATH)).GetComponent<PlayerInfoCardHUDView>(); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public void Initialize(UnityAction cardClosedCallback, |
| | 68 | | UnityAction reportPlayerCallback, |
| | 69 | | UnityAction blockPlayerCallback, |
| | 70 | | UnityAction unblockPlayerCallback, |
| | 71 | | UnityAction addFriendCallback, |
| | 72 | | UnityAction cancelInvitation, |
| | 73 | | UnityAction acceptFriendRequest, |
| | 74 | | UnityAction rejectFriendRequest) |
| | 75 | | { |
| 27 | 76 | | hideCardButton.onClick.RemoveAllListeners(); |
| 27 | 77 | | hideCardButton.onClick.AddListener(cardClosedCallback); |
| | 78 | |
|
| 27 | 79 | | reportPlayerButton.onClick.RemoveAllListeners(); |
| 27 | 80 | | reportPlayerButton.onClick.AddListener(reportPlayerCallback); |
| | 81 | |
|
| 27 | 82 | | blockPlayerButton.onClick.RemoveAllListeners(); |
| 27 | 83 | | blockPlayerButton.onClick.AddListener(blockPlayerCallback); |
| | 84 | |
|
| 27 | 85 | | unblockPlayerButton.onClick.RemoveAllListeners(); |
| 27 | 86 | | unblockPlayerButton.onClick.AddListener(unblockPlayerCallback); |
| | 87 | |
|
| 27 | 88 | | addFriendButton.onClick.RemoveAllListeners(); |
| 27 | 89 | | addFriendButton.onClick.AddListener(addFriendCallback); |
| | 90 | |
|
| 27 | 91 | | requestSentButton.onClick.RemoveAllListeners(); |
| 27 | 92 | | requestSentButton.onClick.AddListener(cancelInvitation); |
| | 93 | |
|
| 27 | 94 | | acceptRequestButton.onClick.RemoveAllListeners(); |
| 27 | 95 | | acceptRequestButton.onClick.AddListener(acceptFriendRequest); |
| | 96 | |
|
| 27 | 97 | | rejectRequestButton.onClick.RemoveAllListeners(); |
| 27 | 98 | | rejectRequestButton.onClick.AddListener(rejectFriendRequest); |
| | 99 | |
|
| 216 | 100 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 101 | | { |
| 81 | 102 | | var tab = tabsMapping[index]; |
| 81 | 103 | | tab.toggle.onValueChanged.RemoveListener(toggleChangedDelegate); |
| 81 | 104 | | tab.toggle.onValueChanged.AddListener(toggleChangedDelegate); |
| | 105 | | } |
| | 106 | |
|
| 54 | 107 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 108 | | { |
| 27 | 109 | | if (tabsMapping[index].tab == Tabs.Passport) |
| | 110 | | { |
| 27 | 111 | | tabsMapping[index].toggle.isOn = true; |
| 27 | 112 | | break; |
| | 113 | | } |
| | 114 | | } |
| | 115 | |
|
| 27 | 116 | | if (SceneReferences.i != null) |
| | 117 | | { |
| 27 | 118 | | var mouseCatcher = DCL.SceneReferences.i.mouseCatcher; |
| | 119 | |
|
| 27 | 120 | | if (mouseCatcher != null) |
| | 121 | | { |
| 0 | 122 | | this.mouseCatcher = mouseCatcher; |
| 0 | 123 | | mouseCatcher.OnMouseDown += OnPointerDown; |
| | 124 | | } |
| | 125 | | } |
| 27 | 126 | | } |
| | 127 | |
|
| | 128 | | public void SetCardActive(bool active) |
| | 129 | | { |
| 21 | 130 | | if (active && mouseCatcher != null) |
| | 131 | | { |
| 0 | 132 | | mouseCatcher.UnlockCursor(); |
| | 133 | | } |
| | 134 | |
|
| 21 | 135 | | cardCanvas.enabled = active; |
| 21 | 136 | | CommonScriptableObjects.playerInfoCardVisibleState.Set(active); |
| 21 | 137 | | } |
| | 138 | |
|
| | 139 | | private void UpdateTabs() |
| | 140 | | { |
| 32 | 141 | | for (int index = 0; index < tabsMapping.Length; index++) |
| | 142 | | { |
| 12 | 143 | | tabsMapping[index].container.SetActive(tabsMapping[index].toggle.isOn); |
| | 144 | | } |
| 4 | 145 | | } |
| | 146 | |
|
| | 147 | | public void SetFaceSnapshot(Texture2D texture) |
| | 148 | | { |
| 0 | 149 | | avatarPicture.texture = texture; |
| 0 | 150 | | } |
| | 151 | |
|
| 32 | 152 | | public void SetName(string name) => this.name.text = name; |
| | 153 | |
|
| 32 | 154 | | public void SetDescription(string description) => this.description.text = description; |
| | 155 | |
|
| | 156 | | public void HideFriendshipInteraction() |
| | 157 | | { |
| 0 | 158 | | requestReceivedContainer.SetActive(false); |
| 0 | 159 | | addFriendButton.gameObject.SetActive(false); |
| 0 | 160 | | alreadyFriendsContainer.SetActive(false); |
| 0 | 161 | | requestSentButton.gameObject.SetActive(false); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | public void UpdateFriendshipInteraction(bool canUseFriendButton, |
| | 165 | | FriendsController.UserStatus status) |
| | 166 | | { |
| 32 | 167 | | if (status == null) |
| | 168 | | { |
| 27 | 169 | | requestReceivedContainer.SetActive(false); |
| 27 | 170 | | addFriendButton.gameObject.SetActive(false); |
| 27 | 171 | | alreadyFriendsContainer.SetActive(false); |
| 27 | 172 | | requestSentButton.gameObject.SetActive(false); |
| 27 | 173 | | return; |
| | 174 | | } |
| | 175 | |
|
| 5 | 176 | | friendStatusContainer.SetActive(canUseFriendButton); |
| | 177 | |
|
| 5 | 178 | | if (!canUseFriendButton) |
| | 179 | | { |
| 1 | 180 | | addFriendButton.gameObject.SetActive(false); |
| 1 | 181 | | alreadyFriendsContainer.SetActive(false); |
| 1 | 182 | | requestSentButton.gameObject.SetActive(false); |
| 1 | 183 | | return; |
| | 184 | | } |
| | 185 | |
|
| 4 | 186 | | addFriendButton.gameObject.SetActive(false); |
| 4 | 187 | | alreadyFriendsContainer.SetActive(false); |
| 4 | 188 | | requestReceivedContainer.SetActive(false); |
| 4 | 189 | | requestSentButton.gameObject.SetActive(false); |
| | 190 | |
|
| 4 | 191 | | switch (status.friendshipStatus) |
| | 192 | | { |
| | 193 | | case FriendshipStatus.NOT_FRIEND: |
| 1 | 194 | | addFriendButton.gameObject.SetActive(true); |
| 1 | 195 | | break; |
| | 196 | | case FriendshipStatus.FRIEND: |
| 1 | 197 | | alreadyFriendsContainer.SetActive(true); |
| 1 | 198 | | break; |
| | 199 | | case FriendshipStatus.REQUESTED_FROM: |
| 1 | 200 | | requestReceivedContainer.SetActive(true); |
| 1 | 201 | | break; |
| | 202 | | case FriendshipStatus.REQUESTED_TO: |
| 1 | 203 | | requestSentButton.gameObject.SetActive(true); |
| | 204 | | break; |
| | 205 | | } |
| 1 | 206 | | } |
| | 207 | |
|
| | 208 | | public void SetIsBlocked(bool isBlocked) |
| | 209 | | { |
| 32 | 210 | | unblockPlayerButton.gameObject.SetActive(isBlocked); |
| 32 | 211 | | blockedAvatarOverlay.gameObject.SetActive(isBlocked); |
| 32 | 212 | | } |
| | 213 | |
|
| | 214 | | public void SetVisibility(bool visible) |
| | 215 | | { |
| 1 | 216 | | if (gameObject.activeSelf != visible) |
| | 217 | | { |
| 0 | 218 | | if (visible) |
| | 219 | | { |
| 0 | 220 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 0 | 221 | | } |
| | 222 | | else |
| | 223 | | { |
| 0 | 224 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| 1 | 228 | | gameObject.SetActive(visible); |
| 1 | 229 | | } |
| | 230 | |
|
| | 231 | | public void ClearCollectibles() |
| | 232 | | { |
| 194 | 233 | | for (var i = playerInfoCollectibles.Count - 1; i >= 0; i--) |
| | 234 | | { |
| 65 | 235 | | var playerInfoCollectible = playerInfoCollectibles[i]; |
| 65 | 236 | | playerInfoCollectibles.RemoveAt(i); |
| 65 | 237 | | Destroy(playerInfoCollectible.gameObject); |
| | 238 | | } |
| 32 | 239 | | } |
| | 240 | |
|
| | 241 | | public void SetWearables(IEnumerable<WearableItem> wearables) |
| | 242 | | { |
| 31 | 243 | | var emptyWearables = true; |
| | 244 | |
|
| 362 | 245 | | foreach (var wearable in wearables) |
| | 246 | | { |
| 150 | 247 | | emptyWearables = false; |
| 150 | 248 | | var playerInfoCollectible = |
| | 249 | | collectiblesFactory.Instantiate<PlayerInfoCollectibleItem>(wearable.rarity, |
| | 250 | | wearablesContainer.transform); |
| 150 | 251 | | if (playerInfoCollectible == null) continue; |
| 150 | 252 | | playerInfoCollectibles.Add(playerInfoCollectible); |
| 150 | 253 | | playerInfoCollectible.Initialize(wearable); |
| | 254 | | } |
| | 255 | |
|
| 31 | 256 | | emptyCollectiblesImage.SetActive(emptyWearables); |
| 31 | 257 | | } |
| | 258 | |
|
| | 259 | | private void OnPointerDown() |
| | 260 | | { |
| 0 | 261 | | hideCardButton.onClick.Invoke(); |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | private void OnDestroy() |
| | 265 | | { |
| 26 | 266 | | if (mouseCatcher != null) |
| 0 | 267 | | mouseCatcher.OnMouseDown -= OnPointerDown; |
| 26 | 268 | | } |
| | 269 | | } |