| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Components; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | |
|
| | 9 | | internal class UsersAroundListHUDListElementView : MonoBehaviour, IPoolLifecycleHandler, IPointerEnterHandler, IPointerE |
| | 10 | | { |
| 1 | 11 | | private static readonly int talkingAnimation = Animator.StringToHash("Talking"); |
| | 12 | |
|
| | 13 | | public event Action<string, bool> OnMuteUser; |
| | 14 | | public event Action<Vector3, string> OnShowUserContexMenu; |
| | 15 | |
|
| | 16 | | [SerializeField] internal TextMeshProUGUI userName; |
| | 17 | | [SerializeField] internal GameObject friendLabel; |
| | 18 | | [SerializeField] internal RawImage avatarPreview; |
| | 19 | | [SerializeField] internal GameObject blockedGO; |
| | 20 | | [SerializeField] internal Button soundButton; |
| | 21 | | [SerializeField] internal GameObject muteGO; |
| | 22 | | [SerializeField] internal GameObject backgroundHover; |
| | 23 | | [SerializeField] internal Button menuButton; |
| | 24 | | [SerializeField] internal Transform contexMenuRefPosition; |
| | 25 | | [SerializeField] internal Animator talkingAnimator; |
| | 26 | |
|
| | 27 | | private UserProfile profile; |
| | 28 | | private bool isMuted = false; |
| | 29 | | private bool isRecording = false; |
| | 30 | |
|
| | 31 | | private void Start() |
| | 32 | | { |
| 0 | 33 | | soundButton.onClick.AddListener(OnSoundButtonPressed); |
| 0 | 34 | | menuButton.onClick.AddListener(() => |
| | 35 | | { |
| 0 | 36 | | if (profile) |
| | 37 | | { |
| 0 | 38 | | OnShowUserContexMenu?.Invoke(contexMenuRefPosition.position, profile.userId); |
| | 39 | | } |
| 0 | 40 | | }); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | private void OnEnable() { talkingAnimator.SetBool(talkingAnimation, isRecording); } |
| | 44 | |
|
| | 45 | | public void SetUserProfile(UserProfile profile) |
| | 46 | | { |
| 3 | 47 | | this.profile = profile; |
| | 48 | |
|
| 3 | 49 | | userName.text = profile.userName; |
| | 50 | |
|
| 3 | 51 | | if (profile.faceSnapshot) |
| | 52 | | { |
| 0 | 53 | | SetAvatarPreviewImage(profile.faceSnapshot); |
| 0 | 54 | | } |
| | 55 | | else |
| | 56 | | { |
| 3 | 57 | | profile.OnFaceSnapshotReadyEvent += SetAvatarPreviewImage; |
| | 58 | | } |
| | 59 | |
|
| 3 | 60 | | SetupFriends(profile.userId); |
| 3 | 61 | | } |
| | 62 | |
|
| | 63 | | public void SetMuted(bool isMuted) |
| | 64 | | { |
| 3 | 65 | | this.isMuted = isMuted; |
| 3 | 66 | | muteGO.SetActive(isMuted); |
| 3 | 67 | | } |
| | 68 | |
|
| | 69 | | public void SetRecording(bool isRecording) |
| | 70 | | { |
| 0 | 71 | | this.isRecording = isRecording; |
| 0 | 72 | | talkingAnimator.SetBool(talkingAnimation, isRecording); |
| 0 | 73 | | } |
| | 74 | |
|
| 6 | 75 | | public void SetBlocked(bool blocked) { blockedGO.SetActive(blocked); } |
| | 76 | |
|
| | 77 | | public void OnPoolRelease() |
| | 78 | | { |
| 6 | 79 | | avatarPreview.texture = null; |
| 6 | 80 | | userName.text = string.Empty; |
| 6 | 81 | | isMuted = false; |
| 6 | 82 | | isRecording = false; |
| | 83 | |
|
| 6 | 84 | | if (profile) |
| | 85 | | { |
| 2 | 86 | | profile.OnFaceSnapshotReadyEvent -= SetAvatarPreviewImage; |
| 2 | 87 | | profile = null; |
| | 88 | | } |
| | 89 | |
|
| 6 | 90 | | if (FriendsController.i != null) |
| | 91 | | { |
| 6 | 92 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| | 93 | | } |
| | 94 | |
|
| 6 | 95 | | gameObject.SetActive(false); |
| 6 | 96 | | } |
| | 97 | |
|
| | 98 | | public void OnPoolGet() |
| | 99 | | { |
| 3 | 100 | | muteGO.SetActive(false); |
| | 101 | |
|
| 3 | 102 | | if (talkingAnimator.isActiveAndEnabled) |
| 0 | 103 | | talkingAnimator.SetBool(talkingAnimation, false); |
| | 104 | |
|
| 3 | 105 | | avatarPreview.texture = null; |
| 3 | 106 | | userName.text = string.Empty; |
| 3 | 107 | | backgroundHover.SetActive(false); |
| 3 | 108 | | menuButton.gameObject.SetActive(false); |
| 3 | 109 | | blockedGO.SetActive(false); |
| 3 | 110 | | gameObject.SetActive(true); |
| 3 | 111 | | } |
| | 112 | |
|
| | 113 | | void SetupFriends(string userId) |
| | 114 | | { |
| 3 | 115 | | if (FriendsController.i == null) |
| | 116 | | { |
| 0 | 117 | | return; |
| | 118 | | } |
| | 119 | |
|
| 3 | 120 | | if (FriendsController.i.friends.TryGetValue(userId, out FriendsController.UserStatus status)) |
| | 121 | | { |
| 0 | 122 | | SetupFriendship(status.friendshipStatus); |
| 0 | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 3 | 126 | | SetupFriendship(FriendshipStatus.NONE); |
| | 127 | | } |
| | 128 | |
|
| 3 | 129 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 3 | 130 | | FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate; |
| 3 | 131 | | } |
| | 132 | |
|
| 0 | 133 | | void SetAvatarPreviewImage(Texture texture) { avatarPreview.texture = texture; } |
| | 134 | |
|
| | 135 | | void OnSoundButtonPressed() |
| | 136 | | { |
| 0 | 137 | | if (profile == null) |
| | 138 | | { |
| 0 | 139 | | return; |
| | 140 | | } |
| | 141 | |
|
| 0 | 142 | | OnMuteUser?.Invoke(profile.userId, !isMuted); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) |
| | 146 | | { |
| 0 | 147 | | backgroundHover.SetActive(true); |
| 0 | 148 | | menuButton.gameObject.SetActive(true); |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) |
| | 152 | | { |
| 0 | 153 | | backgroundHover.SetActive(false); |
| 0 | 154 | | menuButton.gameObject.SetActive(false); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | void OnFriendActionUpdate(string userId, FriendshipAction action) |
| | 158 | | { |
| 0 | 159 | | if (profile.userId != userId) |
| | 160 | | { |
| 0 | 161 | | return; |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | friendLabel.SetActive(action == FriendshipAction.APPROVED); |
| 0 | 165 | | } |
| | 166 | |
|
| 6 | 167 | | void SetupFriendship(FriendshipStatus friendshipStatus) { friendLabel.SetActive(friendshipStatus == FriendshipStatus |
| | 168 | | } |