| | 1 | | using DCL.Interface; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Contextual menu with different options about an user. |
| | 8 | | /// </summary> |
| | 9 | | [RequireComponent(typeof(RectTransform))] |
| | 10 | | public class UserContextMenu : MonoBehaviour |
| | 11 | | { |
| | 12 | | internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 13 | |
|
| | 14 | | const string BLOCK_BTN_BLOCK_TEXT = "Block"; |
| | 15 | | const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock"; |
| | 16 | | const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?"; |
| | 17 | |
|
| | 18 | | [System.Flags] |
| | 19 | | public enum MenuConfigFlags |
| | 20 | | { |
| | 21 | | Name = 1, |
| | 22 | | Friendship = 2, |
| | 23 | | Message = 4, |
| | 24 | | Passport = 8, |
| | 25 | | Block = 16, |
| | 26 | | Report = 32 |
| | 27 | | } |
| | 28 | |
|
| | 29 | | const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship; |
| | 30 | | const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message; |
| | 31 | |
|
| | 32 | | [Header("Optional: Set Confirmation Dialog")] |
| | 33 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 34 | |
|
| | 35 | | [Header("Enable Actions")] |
| 94 | 36 | | [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC |
| | 37 | |
|
| | 38 | | [Header("Containers")] |
| | 39 | | [SerializeField] internal GameObject headerContainer; |
| | 40 | | [SerializeField] internal GameObject bodyContainer; |
| | 41 | | [SerializeField] internal GameObject friendshipContainer; |
| | 42 | | [SerializeField] internal GameObject friendAddContainer; |
| | 43 | | [SerializeField] internal GameObject friendRemoveContainer; |
| | 44 | | [SerializeField] internal GameObject friendRequestedContainer; |
| | 45 | |
|
| | 46 | | [Header("Texts")] |
| | 47 | | [SerializeField] internal TextMeshProUGUI userName; |
| | 48 | | [SerializeField] internal TextMeshProUGUI blockText; |
| | 49 | |
|
| | 50 | | [Header("Buttons")] |
| | 51 | | [SerializeField] internal Button passportButton; |
| | 52 | | [SerializeField] internal Button blockButton; |
| | 53 | | [SerializeField] internal Button reportButton; |
| | 54 | | [SerializeField] internal Button addFriendButton; |
| | 55 | | [SerializeField] internal Button cancelFriendButton; |
| | 56 | | [SerializeField] internal Button deleteFriendButton; |
| | 57 | | [SerializeField] internal Button messageButton; |
| | 58 | |
|
| | 59 | | public static event System.Action<string> OnOpenPrivateChatRequest; |
| | 60 | |
|
| 0 | 61 | | public bool isVisible => gameObject.activeSelf; |
| | 62 | |
|
| | 63 | | public event System.Action OnShowMenu; |
| | 64 | | public event System.Action<string> OnPassport; |
| | 65 | | public event System.Action<string> OnReport; |
| | 66 | | public event System.Action<string, bool> OnBlock; |
| | 67 | | public event System.Action<string> OnUnfriend; |
| | 68 | | public event System.Action<string> OnAddFriend; |
| | 69 | | public event System.Action<string> OnCancelFriend; |
| | 70 | | public event System.Action<string> OnMessage; |
| | 71 | |
|
| | 72 | | private static StringVariable currentPlayerId = null; |
| | 73 | | private RectTransform rectTransform; |
| | 74 | | private string userId; |
| | 75 | | private bool isBlocked; |
| | 76 | | private MenuConfigFlags currentConfigFlags; |
| | 77 | | private IConfirmationDialog currentConfirmationDialog; |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Show context menu |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="userId"> user id</param> |
| 16 | 83 | | public void Show(string userId) { Show(userId, menuConfigFlags); } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Show context menu |
| | 87 | | /// </summary> |
| | 88 | | /// <param name="userId"> user id</param> |
| | 89 | | /// <param name="configFlags">set buttons to enable in menu</param> |
| | 90 | | public void Show(string userId, MenuConfigFlags configFlags) |
| | 91 | | { |
| 10 | 92 | | this.userId = userId; |
| 10 | 93 | | ProcessActiveElements(configFlags); |
| 10 | 94 | | Setup(userId, configFlags); |
| 10 | 95 | | if (currentConfirmationDialog == null && confirmationDialog != null) |
| | 96 | | { |
| 0 | 97 | | SetConfirmationDialog(confirmationDialog); |
| | 98 | | } |
| 10 | 99 | | gameObject.SetActive(true); |
| 10 | 100 | | OnShowMenu?.Invoke(); |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Set confirmation popup to reference use |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="confirmationPopup">confirmation popup reference</param> |
| 0 | 107 | | public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Hides the context menu. |
| | 111 | | /// </summary> |
| 64 | 112 | | public void Hide() { gameObject.SetActive(false); } |
| | 113 | |
|
| | 114 | | private void Awake() |
| | 115 | | { |
| 11 | 116 | | if (!currentPlayerId) |
| | 117 | | { |
| 1 | 118 | | currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| | 119 | | } |
| | 120 | |
|
| 11 | 121 | | rectTransform = GetComponent<RectTransform>(); |
| | 122 | |
|
| 11 | 123 | | passportButton.onClick.AddListener(OnPassportButtonPressed); |
| 11 | 124 | | blockButton.onClick.AddListener(OnBlockUserButtonPressed); |
| 11 | 125 | | reportButton.onClick.AddListener(OnReportUserButtonPressed); |
| 11 | 126 | | deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed); |
| 11 | 127 | | addFriendButton.onClick.AddListener(OnAddFriendButtonPressed); |
| 11 | 128 | | cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed); |
| 11 | 129 | | messageButton.onClick.AddListener(OnMessageButtonPressed); |
| 11 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | private void Update() { HideIfClickedOutside(); } |
| | 133 | |
|
| | 134 | | private void OnDisable() |
| | 135 | | { |
| 11 | 136 | | if (FriendsController.i) |
| | 137 | | { |
| 7 | 138 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| | 139 | | } |
| 11 | 140 | | } |
| | 141 | |
|
| | 142 | | private void OnPassportButtonPressed() |
| | 143 | | { |
| 2 | 144 | | OnPassport?.Invoke(userId); |
| 2 | 145 | | currentPlayerId.Set(userId); |
| 2 | 146 | | Hide(); |
| | 147 | |
|
| 2 | 148 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 2 | 149 | | } |
| | 150 | |
|
| | 151 | | private void OnReportUserButtonPressed() |
| | 152 | | { |
| 2 | 153 | | OnReport?.Invoke(userId); |
| 2 | 154 | | WebInterface.SendReportPlayer(userId); |
| 2 | 155 | | Hide(); |
| 2 | 156 | | } |
| | 157 | |
|
| | 158 | | private void OnDeleteUserButtonPressed() |
| | 159 | | { |
| 1 | 160 | | OnUnfriend?.Invoke(userId); |
| 1 | 161 | | if (currentConfirmationDialog != null) |
| | 162 | | { |
| 0 | 163 | | currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo |
| 0 | 164 | | currentConfirmationDialog.Show(() => |
| | 165 | | { |
| 0 | 166 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 167 | | { |
| | 168 | | userId = userId, |
| | 169 | | action = FriendshipAction.DELETED |
| | 170 | | }); |
| | 171 | |
|
| 0 | 172 | | WebInterface.UpdateFriendshipStatus( |
| | 173 | | new FriendsController.FriendshipUpdateStatusMessage() |
| | 174 | | { |
| | 175 | | action = FriendshipAction.DELETED, |
| | 176 | | userId = userId |
| | 177 | | }); |
| 0 | 178 | | }); |
| | 179 | | } |
| 1 | 180 | | Hide(); |
| 1 | 181 | | } |
| | 182 | |
|
| | 183 | | private void OnAddFriendButtonPressed() |
| | 184 | | { |
| 0 | 185 | | OnAddFriend?.Invoke(userId); |
| | 186 | |
|
| 0 | 187 | | if (!FriendsController.i) |
| | 188 | | { |
| 0 | 189 | | return; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | // NOTE: if we don't add this, the friend request has strange behaviors |
| 0 | 193 | | UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel() |
| | 194 | | { |
| | 195 | | userId = userId, |
| | 196 | | name = UserProfileController.userProfilesCatalog.Get(userId)?.userName |
| | 197 | | }); |
| | 198 | |
|
| 0 | 199 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 200 | | { |
| | 201 | | userId = userId, |
| | 202 | | action = FriendshipAction.REQUESTED_TO |
| | 203 | | }); |
| | 204 | |
|
| 0 | 205 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 206 | | { |
| | 207 | | userId = userId, action = FriendshipAction.REQUESTED_TO |
| | 208 | | }); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | private void OnCancelFriendRequestButtonPressed() |
| | 212 | | { |
| 0 | 213 | | OnCancelFriend?.Invoke(userId); |
| | 214 | |
|
| 0 | 215 | | if (!FriendsController.i) |
| | 216 | | { |
| 0 | 217 | | return; |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 221 | | { |
| | 222 | | userId = userId, |
| | 223 | | action = FriendshipAction.CANCELLED |
| | 224 | | }); |
| | 225 | |
|
| 0 | 226 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 227 | | { |
| | 228 | | userId = userId, action = FriendshipAction.CANCELLED |
| | 229 | | }); |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | private void OnMessageButtonPressed() |
| | 233 | | { |
| 0 | 234 | | OnMessage?.Invoke(userId); |
| 0 | 235 | | OnOpenPrivateChatRequest?.Invoke(userId); |
| 0 | 236 | | Hide(); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | private void OnBlockUserButtonPressed() |
| | 240 | | { |
| 1 | 241 | | bool blockUser = !isBlocked; |
| 1 | 242 | | OnBlock?.Invoke(userId, blockUser); |
| 1 | 243 | | if (blockUser) |
| | 244 | | { |
| 1 | 245 | | WebInterface.SendBlockPlayer(userId); |
| 1 | 246 | | } |
| | 247 | | else |
| | 248 | | { |
| 0 | 249 | | WebInterface.SendUnblockPlayer(userId); |
| | 250 | | } |
| 1 | 251 | | Hide(); |
| 1 | 252 | | } |
| | 253 | |
|
| 16 | 254 | | private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; } |
| | 255 | |
|
| | 256 | | private void HideIfClickedOutside() |
| | 257 | | { |
| 0 | 258 | | if (Input.GetMouseButtonDown(0) && |
| | 259 | | !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition)) |
| | 260 | | { |
| 0 | 261 | | Hide(); |
| | 262 | | } |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | private void ProcessActiveElements(MenuConfigFlags flags) |
| | 266 | | { |
| 27 | 267 | | headerContainer.SetActive((flags & headerFlags) != 0); |
| 27 | 268 | | userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0); |
| 27 | 269 | | friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 27 | 270 | | deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 27 | 271 | | passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0); |
| 27 | 272 | | blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0); |
| 27 | 273 | | reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0); |
| 27 | 274 | | messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0); |
| 27 | 275 | | } |
| | 276 | |
|
| | 277 | | private void Setup(string userId, MenuConfigFlags configFlags) |
| | 278 | | { |
| 10 | 279 | | this.userId = userId; |
| | 280 | |
|
| 10 | 281 | | UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 10 | 282 | | bool userHasWallet = profile?.hasConnectedWeb3 ?? false; |
| | 283 | |
|
| 10 | 284 | | if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3) |
| | 285 | | { |
| 2 | 286 | | configFlags &= ~usesFriendsApiFlags; |
| | 287 | | } |
| | 288 | |
|
| 10 | 289 | | this.currentConfigFlags = configFlags; |
| 10 | 290 | | ProcessActiveElements(configFlags); |
| | 291 | |
|
| 10 | 292 | | if ((configFlags & MenuConfigFlags.Block) != 0) |
| | 293 | | { |
| 8 | 294 | | isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 8 | 295 | | UpdateBlockButton(); |
| | 296 | | } |
| 10 | 297 | | if ((configFlags & MenuConfigFlags.Name) != 0) |
| | 298 | | { |
| 8 | 299 | | string name = profile?.userName; |
| 8 | 300 | | userName.text = name; |
| | 301 | | } |
| 10 | 302 | | if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i) |
| | 303 | | { |
| 6 | 304 | | if (FriendsController.i.friends.TryGetValue(userId, out FriendsController.UserStatus status)) |
| | 305 | | { |
| 0 | 306 | | SetupFriendship(status.friendshipStatus); |
| 0 | 307 | | } |
| | 308 | | else |
| | 309 | | { |
| 6 | 310 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 311 | | } |
| 6 | 312 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 6 | 313 | | FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate; |
| | 314 | | } |
| 10 | 315 | | } |
| | 316 | |
|
| | 317 | | private void SetupFriendship(FriendshipStatus friendshipStatus) |
| | 318 | | { |
| 12 | 319 | | bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0; |
| 12 | 320 | | bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0; |
| | 321 | |
|
| 12 | 322 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| | 323 | | { |
| 2 | 324 | | if (friendshipEnabled) |
| | 325 | | { |
| 1 | 326 | | friendAddContainer.SetActive(false); |
| 1 | 327 | | friendRemoveContainer.SetActive(true); |
| 1 | 328 | | friendRequestedContainer.SetActive(false); |
| 1 | 329 | | deleteFriendButton.gameObject.SetActive(true); |
| | 330 | | } |
| 2 | 331 | | if (messageEnabled) |
| | 332 | | { |
| 1 | 333 | | messageButton.gameObject.SetActive(true); |
| | 334 | | } |
| 1 | 335 | | } |
| 10 | 336 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_TO) |
| | 337 | | { |
| 2 | 338 | | if (friendshipEnabled) |
| | 339 | | { |
| 1 | 340 | | friendAddContainer.SetActive(false); |
| 1 | 341 | | friendRemoveContainer.SetActive(false); |
| 1 | 342 | | friendRequestedContainer.SetActive(true); |
| 1 | 343 | | deleteFriendButton.gameObject.SetActive(false); |
| | 344 | | } |
| 2 | 345 | | if (messageEnabled) |
| | 346 | | { |
| 1 | 347 | | messageButton.gameObject.SetActive(false); |
| | 348 | | } |
| 1 | 349 | | } |
| 8 | 350 | | else if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 351 | | { |
| 8 | 352 | | if (friendshipEnabled) |
| | 353 | | { |
| 6 | 354 | | friendAddContainer.SetActive(true); |
| 6 | 355 | | friendRemoveContainer.SetActive(false); |
| 6 | 356 | | friendRequestedContainer.SetActive(false); |
| 6 | 357 | | deleteFriendButton.gameObject.SetActive(false); |
| | 358 | | } |
| 8 | 359 | | if (messageEnabled) |
| | 360 | | { |
| 6 | 361 | | messageButton.gameObject.SetActive(false); |
| | 362 | | } |
| 6 | 363 | | } |
| 0 | 364 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| | 365 | | { |
| 0 | 366 | | if (friendshipEnabled) |
| | 367 | | { |
| 0 | 368 | | friendAddContainer.SetActive(true); |
| 0 | 369 | | friendRemoveContainer.SetActive(false); |
| 0 | 370 | | friendRequestedContainer.SetActive(false); |
| 0 | 371 | | deleteFriendButton.gameObject.SetActive(false); |
| | 372 | | } |
| 0 | 373 | | if (messageEnabled) |
| | 374 | | { |
| 0 | 375 | | messageButton.gameObject.SetActive(false); |
| | 376 | | } |
| | 377 | | } |
| 4 | 378 | | } |
| | 379 | |
|
| | 380 | | private void OnFriendActionUpdate(string userId, FriendshipAction action) |
| | 381 | | { |
| 6 | 382 | | if (this.userId != userId) |
| | 383 | | { |
| 0 | 384 | | return; |
| | 385 | | } |
| | 386 | |
|
| 6 | 387 | | if (action == FriendshipAction.APPROVED) |
| | 388 | | { |
| 2 | 389 | | SetupFriendship(FriendshipStatus.FRIEND); |
| 2 | 390 | | } |
| 4 | 391 | | else if (action == FriendshipAction.REQUESTED_TO) |
| | 392 | | { |
| 2 | 393 | | SetupFriendship(FriendshipStatus.REQUESTED_TO); |
| 2 | 394 | | } |
| 2 | 395 | | else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio |
| | 396 | | { |
| 2 | 397 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 398 | | } |
| 2 | 399 | | } |
| | 400 | |
|
| | 401 | | #if UNITY_EDITOR |
| | 402 | | //This is just to process buttons and container visibility on editor |
| | 403 | | private void OnValidate() |
| | 404 | | { |
| 7 | 405 | | if (headerContainer == null) |
| 0 | 406 | | return; |
| 7 | 407 | | ProcessActiveElements(menuConfigFlags); |
| 7 | 408 | | } |
| | 409 | | #endif |
| | 410 | | } |