| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using static DCL.SettingsCommon.GeneralSettings; |
| | 6 | |
|
| | 7 | | public class VoiceChatWindowComponentView : BaseComponentView, IVoiceChatWindowComponentView, IComponentModelConfig<Voic |
| | 8 | | { |
| | 9 | | private const string ALLOW_USERS_TITLE_ALL = "All"; |
| | 10 | | private const string ALLOW_USERS_TITLE_REGISTERED = "Verified Users"; |
| | 11 | | private const string ALLOW_USERS_TITLE_FRIENDS = "Friends"; |
| | 12 | |
|
| | 13 | | [Header("Prefab References")] |
| | 14 | | [SerializeField] internal ButtonComponentView closeButton; |
| | 15 | | [SerializeField] internal ButtonComponentView joinButton; |
| | 16 | | [SerializeField] internal ButtonComponentView leaveButton; |
| | 17 | | [SerializeField] internal TMP_Text playersText; |
| | 18 | | [SerializeField] internal DropdownComponentView allowUsersDropdown; |
| | 19 | | [SerializeField] internal ToggleComponentView muteAllToggle; |
| | 20 | | [SerializeField] internal GameObject emptyListGameObject; |
| | 21 | | [SerializeField] internal ButtonComponentView goToCrowdButton; |
| | 22 | | [SerializeField] internal VoiceChatPlayerComponentView playerPrefab; |
| | 23 | | [SerializeField] internal Transform usersContainer; |
| | 24 | | [SerializeField] internal UserContextMenu contextMenuPanel; |
| | 25 | |
|
| | 26 | | [Header("Configuration")] |
| | 27 | | [SerializeField] internal VoiceChatWindowComponentModel model; |
| | 28 | |
|
| | 29 | | public event Action OnClose; |
| | 30 | | public event Action<bool> OnJoinVoiceChat; |
| | 31 | | public event Action<string> OnAllowUsersFilterChange; |
| | 32 | | public event Action OnGoToCrowd; |
| | 33 | | public event Action<bool> OnMuteAll; |
| | 34 | | public event Action<string, bool> OnMuteUser; |
| | 35 | |
|
| 0 | 36 | | public RectTransform Transform => (RectTransform)transform; |
| 0 | 37 | | public bool isMuteAllOn => muteAllToggle.isOn; |
| 0 | 38 | | public int numberOfPlayers => currentPlayers.Count; |
| 0 | 39 | | public int numberOfPlayersTalking => usersTalking.Count; |
| | 40 | |
|
| 50 | 41 | | private readonly Queue<VoiceChatPlayerComponentView> playersPool = new Queue<VoiceChatPlayerComponentView>(); |
| 50 | 42 | | internal Dictionary<string, VoiceChatPlayerComponentView> currentPlayers = new Dictionary<string, VoiceChatPlayerCom |
| 50 | 43 | | internal List<string> usersTalking = new List<string>(); |
| | 44 | |
|
| | 45 | | public override void Awake() |
| | 46 | | { |
| 49 | 47 | | base.Awake(); |
| | 48 | |
|
| 49 | 49 | | closeButton.onClick.AddListener(() => OnClose?.Invoke()); |
| 49 | 50 | | joinButton.onClick.AddListener(() => OnJoinVoiceChat?.Invoke(true)); |
| 49 | 51 | | leaveButton.onClick.AddListener(() => OnJoinVoiceChat?.Invoke(false)); |
| 49 | 52 | | goToCrowdButton.onClick.AddListener(() => OnGoToCrowd?.Invoke()); |
| 49 | 53 | | allowUsersDropdown.OnOptionSelectionChanged += AllowUsersOptionChanged; |
| 49 | 54 | | muteAllToggle.OnSelectedChanged += OnMuteAllToggleChanged; |
| | 55 | |
|
| 49 | 56 | | ConfigureAllowUsersFilter(); |
| 49 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Configure(VoiceChatWindowComponentModel newModel) |
| | 60 | | { |
| 1 | 61 | | model = newModel; |
| 1 | 62 | | RefreshControl(); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | public override void RefreshControl() |
| | 66 | | { |
| 1 | 67 | | if (model == null) |
| 0 | 68 | | return; |
| | 69 | |
|
| 1 | 70 | | SetNumberOfPlayers(model.numberOfPlayers); |
| 1 | 71 | | SetAsJoined(model.isJoined); |
| 1 | 72 | | } |
| | 73 | |
|
| 2 | 74 | | public override void Show(bool instant = false) { gameObject.SetActive(true); } |
| | 75 | |
|
| | 76 | | public override void Hide(bool instant = false) |
| | 77 | | { |
| 1 | 78 | | contextMenuPanel.Hide(); |
| 1 | 79 | | gameObject.SetActive(false); |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | public void SetNumberOfPlayers(int numPlayers) |
| | 83 | | { |
| 5 | 84 | | model.numberOfPlayers = numPlayers; |
| | 85 | |
|
| 5 | 86 | | if (playersText != null) |
| | 87 | | { |
| 5 | 88 | | playersText.text = $"PLAYERS ({numPlayers})"; |
| 5 | 89 | | playersText.gameObject.SetActive(numPlayers > 0); |
| | 90 | | } |
| | 91 | |
|
| 5 | 92 | | if (emptyListGameObject != null) |
| 5 | 93 | | emptyListGameObject.SetActive(numPlayers == 0); |
| 5 | 94 | | } |
| | 95 | |
|
| | 96 | | public void SetAsJoined(bool isJoined) |
| | 97 | | { |
| 3 | 98 | | model.isJoined = isJoined; |
| | 99 | |
|
| 3 | 100 | | if (joinButton != null) |
| 3 | 101 | | joinButton.gameObject.SetActive(!isJoined); |
| | 102 | |
|
| 3 | 103 | | if (leaveButton != null) |
| 3 | 104 | | leaveButton.gameObject.SetActive(isJoined); |
| | 105 | |
|
| 3 | 106 | | using (var iterator = currentPlayers.GetEnumerator()) |
| | 107 | | { |
| 12 | 108 | | while (iterator.MoveNext()) |
| | 109 | | { |
| 9 | 110 | | iterator.Current.Value.SetAsJoined(isJoined); |
| | 111 | | } |
| 3 | 112 | | } |
| 3 | 113 | | } |
| | 114 | |
|
| 4 | 115 | | public void SelectAllowUsersOption(int optionIndex) { allowUsersDropdown.GetOption(optionIndex).isOn = true; } |
| | 116 | |
|
| | 117 | | public void SetPlayerMuted(string userId, bool isMuted) |
| | 118 | | { |
| 2 | 119 | | if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView)) |
| 0 | 120 | | return; |
| | 121 | |
|
| 2 | 122 | | elementView.SetAsMuted(isMuted); |
| 2 | 123 | | } |
| | 124 | |
|
| | 125 | | public void SetPlayerRecording(string userId, bool isRecording) |
| | 126 | | { |
| 6 | 127 | | if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView)) |
| 0 | 128 | | return; |
| | 129 | |
|
| 6 | 130 | | elementView.SetAsTalking(isRecording); |
| | 131 | |
|
| 6 | 132 | | if (isRecording) |
| | 133 | | { |
| 3 | 134 | | if (!usersTalking.Contains(userId)) |
| 3 | 135 | | usersTalking.Add(userId); |
| 3 | 136 | | } |
| | 137 | | else |
| | 138 | | { |
| 3 | 139 | | usersTalking.Remove(userId); |
| | 140 | | } |
| 3 | 141 | | } |
| | 142 | |
|
| | 143 | | public void SetPlayerBlocked(string userId, bool isBlocked) |
| | 144 | | { |
| 6 | 145 | | if (currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView)) |
| 6 | 146 | | elementView.SetAsBlocked(isBlocked); |
| 6 | 147 | | } |
| | 148 | |
|
| | 149 | | public void SetPlayerAsFriend(string userId, bool isFriend) |
| | 150 | | { |
| 6 | 151 | | currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView); |
| | 152 | |
|
| 6 | 153 | | if (playerView != null) |
| 6 | 154 | | playerView.SetAsFriend(isFriend); |
| 6 | 155 | | } |
| | 156 | |
|
| | 157 | | public void SetPlayerAsJoined(string userId, bool isJoined) |
| | 158 | | { |
| 6 | 159 | | currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView); |
| | 160 | |
|
| 6 | 161 | | if (playerView != null) |
| 6 | 162 | | playerView.SetAsJoined(isJoined); |
| 6 | 163 | | } |
| | 164 | |
|
| | 165 | | public void AddOrUpdatePlayer(UserProfile otherProfile) |
| | 166 | | { |
| 1 | 167 | | if (currentPlayers.ContainsKey(otherProfile.userId)) |
| 0 | 168 | | return; |
| | 169 | |
|
| 1 | 170 | | VoiceChatPlayerComponentView elementView = null; |
| | 171 | |
|
| 1 | 172 | | if (playersPool.Count > 0) |
| | 173 | | { |
| 0 | 174 | | elementView = playersPool.Dequeue(); |
| 0 | 175 | | } |
| | 176 | | else |
| | 177 | | { |
| 1 | 178 | | elementView = Instantiate(playerPrefab, usersContainer); |
| 1 | 179 | | elementView.OnContextMenuOpen += OpenContextMenu; |
| 1 | 180 | | elementView.OnMuteUser += MuteUser; |
| | 181 | | } |
| | 182 | |
|
| 1 | 183 | | elementView.Configure(new VoiceChatPlayerComponentModel |
| | 184 | | { |
| | 185 | | userId = otherProfile.userId, |
| | 186 | | userImageUrl = otherProfile.face256SnapshotURL, |
| | 187 | | userName = otherProfile.userName, |
| | 188 | | isMuted = false, |
| | 189 | | isTalking = false, |
| | 190 | | isBlocked = false, |
| | 191 | | isFriend = false, |
| | 192 | | isJoined = false, |
| | 193 | | isBackgroundHover = false |
| | 194 | | }); |
| | 195 | |
|
| 1 | 196 | | elementView.SetActive(true); |
| 1 | 197 | | currentPlayers.Add(otherProfile.userId, elementView); |
| 1 | 198 | | SetNumberOfPlayers(currentPlayers.Count); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void RemoveUser(string userId) |
| | 202 | | { |
| 1 | 203 | | if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView)) |
| 0 | 204 | | return; |
| | 205 | |
|
| 1 | 206 | | if (!elementView) |
| 0 | 207 | | return; |
| | 208 | |
|
| 1 | 209 | | playersPool.Enqueue(elementView); |
| 1 | 210 | | currentPlayers.Remove(userId); |
| 1 | 211 | | SetNumberOfPlayers(currentPlayers.Count); |
| | 212 | |
|
| 1 | 213 | | elementView.SetActive(false); |
| 1 | 214 | | } |
| | 215 | |
|
| 0 | 216 | | public string GetUserTalkingByIndex(int index) { return usersTalking[index]; } |
| | 217 | |
|
| | 218 | | public override void Dispose() |
| | 219 | | { |
| 98 | 220 | | closeButton.onClick.RemoveAllListeners(); |
| 98 | 221 | | joinButton.onClick.RemoveAllListeners(); |
| 98 | 222 | | leaveButton.onClick.RemoveAllListeners(); |
| 98 | 223 | | allowUsersDropdown.OnOptionSelectionChanged -= AllowUsersOptionChanged; |
| 98 | 224 | | muteAllToggle.OnSelectedChanged -= OnMuteAllToggleChanged; |
| | 225 | |
|
| 98 | 226 | | currentPlayers.Clear(); |
| 98 | 227 | | playersPool.Clear(); |
| | 228 | |
|
| 98 | 229 | | base.Dispose(); |
| 98 | 230 | | } |
| | 231 | |
|
| | 232 | | internal void ConfigureAllowUsersFilter() |
| | 233 | | { |
| 52 | 234 | | allowUsersDropdown.SetOptions(new List<ToggleComponentModel> |
| | 235 | | { |
| | 236 | | new ToggleComponentModel |
| | 237 | | { |
| | 238 | | isOn = true, |
| | 239 | | id = VoiceChatAllow.ALL_USERS.ToString(), |
| | 240 | | text = ALLOW_USERS_TITLE_ALL, |
| | 241 | | isTextActive = true, |
| | 242 | | changeTextColorOnSelect = true |
| | 243 | | }, |
| | 244 | | new ToggleComponentModel |
| | 245 | | { |
| | 246 | | isOn = false, |
| | 247 | | id = VoiceChatAllow.VERIFIED_ONLY.ToString(), |
| | 248 | | text = ALLOW_USERS_TITLE_REGISTERED, |
| | 249 | | isTextActive = true, |
| | 250 | | changeTextColorOnSelect = true |
| | 251 | | }, |
| | 252 | | new ToggleComponentModel |
| | 253 | | { |
| | 254 | | isOn = false, |
| | 255 | | id = VoiceChatAllow.FRIENDS_ONLY.ToString(), |
| | 256 | | text = ALLOW_USERS_TITLE_FRIENDS, |
| | 257 | | isTextActive = true, |
| | 258 | | changeTextColorOnSelect = true |
| | 259 | | } |
| | 260 | | }); |
| | 261 | |
|
| 52 | 262 | | allowUsersDropdown.SetTitle(ALLOW_USERS_TITLE_ALL); |
| 52 | 263 | | } |
| | 264 | |
|
| | 265 | | internal void AllowUsersOptionChanged(bool isOn, string optionId, string optionName) |
| | 266 | | { |
| 5 | 267 | | if (!isOn) |
| 2 | 268 | | return; |
| | 269 | |
|
| 3 | 270 | | allowUsersDropdown.SetTitle(optionName); |
| 3 | 271 | | OnAllowUsersFilterChange?.Invoke(optionId); |
| 1 | 272 | | } |
| | 273 | |
|
| 4 | 274 | | internal void OnMuteAllToggleChanged(bool isOn, string id, string text) { OnMuteAll?.Invoke(isOn); } |
| | 275 | |
|
| 4 | 276 | | internal void MuteUser(string userId, bool isMute) { OnMuteUser?.Invoke(userId, isMute); } |
| | 277 | |
|
| | 278 | | internal void OpenContextMenu(string userId) |
| | 279 | | { |
| 0 | 280 | | currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView); |
| | 281 | |
|
| 0 | 282 | | if (elementView != null) |
| 0 | 283 | | elementView.DockAndOpenUserContextMenu(contextMenuPanel); |
| 0 | 284 | | } |
| | 285 | |
|
| | 286 | | internal static VoiceChatWindowComponentView Create() |
| | 287 | | { |
| 0 | 288 | | VoiceChatWindowComponentView voiceChatWindowComponentView = Instantiate(Resources.Load<GameObject>("SocialBarV1/ |
| 0 | 289 | | voiceChatWindowComponentView.name = "_VoiceChatHUD"; |
| | 290 | |
|
| 0 | 291 | | return voiceChatWindowComponentView; |
| | 292 | | } |
| | 293 | | } |