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