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