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