| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.SettingsData; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | internal class UsersAroundListHUDListView : MonoBehaviour, IUsersAroundListHUDListView |
| | 9 | | { |
| | 10 | | public event Action<string, bool> OnRequestMuteUser; |
| | 11 | | public event Action<bool> OnRequestMuteGlobal; |
| | 12 | | public event Action OnGoToCrowdPressed; |
| | 13 | | public event Action OnOpen; |
| | 14 | |
|
| | 15 | | [SerializeField] private UsersAroundListHUDListElementView listElementView; |
| | 16 | | [SerializeField] private ShowHideAnimator showHideAnimator; |
| | 17 | | [SerializeField] internal TMPro.TextMeshProUGUI textPlayersTitle; |
| | 18 | | [SerializeField] internal Transform contentPlayers; |
| | 19 | | [SerializeField] internal Toggle muteAllToggle; |
| | 20 | | [SerializeField] internal UserContextMenu contextMenu; |
| | 21 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 22 | | [SerializeField] internal GameObject emptyListGameObject; |
| | 23 | | [SerializeField] internal Button gotoCrowdButton; |
| | 24 | | [SerializeField] internal SpinBoxPresetted voiceChatSpinBox; |
| | 25 | |
|
| | 26 | | internal Queue<UsersAroundListHUDListElementView> availableElements; |
| | 27 | | internal Dictionary<string, UsersAroundListHUDListElementView> userElementDictionary; |
| | 28 | |
|
| | 29 | | private string playersTextPattern; |
| | 30 | | private bool isGameObjectDestroyed = false; |
| | 31 | |
|
| | 32 | | private void Awake() |
| | 33 | | { |
| 4 | 34 | | availableElements = new Queue<UsersAroundListHUDListElementView>(); |
| 4 | 35 | | userElementDictionary = new Dictionary<string, UsersAroundListHUDListElementView>(); |
| | 36 | |
|
| 4 | 37 | | playersTextPattern = textPlayersTitle.text; |
| 4 | 38 | | textPlayersTitle.text = string.Format(playersTextPattern, userElementDictionary?.Count ?? 0); |
| | 39 | |
|
| 4 | 40 | | muteAllToggle.onValueChanged.AddListener(OnMuteGlobal); |
| 4 | 41 | | gotoCrowdButton.onClick.AddListener(() => OnGoToCrowdPressed?.Invoke()); |
| 4 | 42 | | voiceChatSpinBox.onValueChanged.AddListener(OnVoiceChatSettingChanged); |
| | 43 | |
|
| 4 | 44 | | listElementView.OnMuteUser += OnMuteUser; |
| 4 | 45 | | listElementView.OnShowUserContexMenu += OnUserContextMenu; |
| 4 | 46 | | listElementView.OnPoolRelease(); |
| 4 | 47 | | availableElements.Enqueue(listElementView); |
| | 48 | |
|
| 4 | 49 | | Settings.i.OnGeneralSettingsChanged += OnSettingsChanged; |
| 4 | 50 | | } |
| | 51 | |
|
| | 52 | | void OnDestroy() |
| | 53 | | { |
| 4 | 54 | | isGameObjectDestroyed = true; |
| 4 | 55 | | Settings.i.OnGeneralSettingsChanged -= OnSettingsChanged; |
| 4 | 56 | | } |
| | 57 | |
|
| | 58 | | void IUsersAroundListHUDListView.AddOrUpdatePlayer(Player player) |
| | 59 | | { |
| 3 | 60 | | if (userElementDictionary.ContainsKey(player.id)) |
| | 61 | | { |
| 0 | 62 | | return; |
| | 63 | | } |
| | 64 | |
|
| 3 | 65 | | var profile = UserProfileController.userProfilesCatalog.Get(player.id); |
| | 66 | |
|
| 3 | 67 | | if (profile == null) |
| 0 | 68 | | return; |
| | 69 | |
|
| 3 | 70 | | UsersAroundListHUDListElementView view = null; |
| 3 | 71 | | if (availableElements.Count > 0) |
| | 72 | | { |
| 2 | 73 | | view = availableElements.Dequeue(); |
| 2 | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 1 | 77 | | view = Instantiate(listElementView, contentPlayers); |
| 1 | 78 | | view.OnMuteUser += OnMuteUser; |
| 1 | 79 | | view.OnShowUserContexMenu += OnUserContextMenu; |
| | 80 | | } |
| | 81 | |
|
| 3 | 82 | | view.OnPoolGet(); |
| 3 | 83 | | view.SetUserProfile(profile); |
| 3 | 84 | | userElementDictionary.Add(player.id, view); |
| 3 | 85 | | OnModifyListCount(); |
| 3 | 86 | | CheckListEmptyState(); |
| 3 | 87 | | } |
| | 88 | |
|
| | 89 | | void IUsersAroundListHUDListView.RemoveUser(string userId) |
| | 90 | | { |
| 2 | 91 | | if (!userElementDictionary.TryGetValue(userId, out UsersAroundListHUDListElementView elementView)) |
| | 92 | | { |
| 0 | 93 | | return; |
| | 94 | | } |
| 2 | 95 | | if (!elementView) |
| | 96 | | { |
| 0 | 97 | | return; |
| | 98 | | } |
| | 99 | |
|
| 2 | 100 | | PoolElementView(elementView); |
| 2 | 101 | | userElementDictionary.Remove(userId); |
| 2 | 102 | | OnModifyListCount(); |
| 2 | 103 | | CheckListEmptyState(); |
| 2 | 104 | | } |
| | 105 | |
|
| | 106 | | void IUsersAroundListHUDListView.SetUserRecording(string userId, bool isRecording) |
| | 107 | | { |
| 0 | 108 | | if (!userElementDictionary.TryGetValue(userId, out UsersAroundListHUDListElementView elementView)) |
| | 109 | | { |
| 0 | 110 | | return; |
| | 111 | | } |
| 0 | 112 | | elementView.SetRecording(isRecording); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | void IUsersAroundListHUDListView.SetUserMuted(string userId, bool isMuted) |
| | 116 | | { |
| 3 | 117 | | if (!userElementDictionary.TryGetValue(userId, out UsersAroundListHUDListElementView elementView)) |
| | 118 | | { |
| 0 | 119 | | return; |
| | 120 | | } |
| 3 | 121 | | elementView.SetMuted(isMuted); |
| 3 | 122 | | } |
| | 123 | |
|
| | 124 | | void IUsersAroundListHUDListView.SetUserBlocked(string userId, bool blocked) |
| | 125 | | { |
| 3 | 126 | | if (!userElementDictionary.TryGetValue(userId, out UsersAroundListHUDListElementView elementView)) |
| | 127 | | { |
| 0 | 128 | | return; |
| | 129 | | } |
| 3 | 130 | | elementView.SetBlocked(blocked); |
| 3 | 131 | | } |
| | 132 | |
|
| | 133 | | void IUsersAroundListHUDListView.SetVisibility(bool visible) |
| | 134 | | { |
| 1 | 135 | | if (visible) |
| | 136 | | { |
| 1 | 137 | | if (!gameObject.activeSelf) |
| | 138 | | { |
| 1 | 139 | | gameObject.SetActive(true); |
| | 140 | | } |
| | 141 | |
|
| 1 | 142 | | showHideAnimator.Show(); |
| 1 | 143 | | CheckListEmptyState(); |
| 1 | 144 | | OnOpen?.Invoke(); |
| 1 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 0 | 148 | | showHideAnimator.Hide(); |
| 0 | 149 | | contextMenu.Hide(); |
| 0 | 150 | | confirmationDialog.Hide(); |
| | 151 | | } |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | void IUsersAroundListHUDListView.Dispose() |
| | 155 | | { |
| 4 | 156 | | userElementDictionary.Clear(); |
| 4 | 157 | | availableElements.Clear(); |
| | 158 | |
|
| 4 | 159 | | if (!isGameObjectDestroyed) |
| | 160 | | { |
| 4 | 161 | | Destroy(gameObject); |
| | 162 | | } |
| 4 | 163 | | } |
| | 164 | |
|
| 0 | 165 | | void OnMuteUser(string userId, bool mute) { OnRequestMuteUser?.Invoke(userId, mute); } |
| | 166 | |
|
| 0 | 167 | | void OnMuteGlobal(bool mute) { OnRequestMuteGlobal?.Invoke(mute); } |
| | 168 | |
|
| | 169 | | void OnUserContextMenu(Vector3 position, string userId) |
| | 170 | | { |
| 0 | 171 | | contextMenu.transform.position = position; |
| 0 | 172 | | contextMenu.Show(userId); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | void PoolElementView(UsersAroundListHUDListElementView element) |
| | 176 | | { |
| 2 | 177 | | element.OnPoolRelease(); |
| 2 | 178 | | availableElements.Enqueue(element); |
| 2 | 179 | | } |
| | 180 | |
|
| 10 | 181 | | void OnModifyListCount() { textPlayersTitle.text = string.Format(playersTextPattern, userElementDictionary.Count); } |
| | 182 | |
|
| | 183 | | void CheckListEmptyState() |
| | 184 | | { |
| 6 | 185 | | bool isEmpty = userElementDictionary.Count == 0; |
| 6 | 186 | | emptyListGameObject.SetActive(isEmpty); |
| 6 | 187 | | } |
| | 188 | |
|
| | 189 | | void OnVoiceChatSettingChanged(int index) |
| | 190 | | { |
| 0 | 191 | | var newSettings = Settings.i.generalSettings; |
| 0 | 192 | | newSettings.voiceChatAllow = (GeneralSettings.VoiceChatAllow)index; |
| 0 | 193 | | Settings.i.ApplyGeneralSettings(newSettings); |
| 0 | 194 | | } |
| | 195 | |
|
| 0 | 196 | | void OnSettingsChanged(GeneralSettings settings) { voiceChatSpinBox.SetValue((int)settings.voiceChatAllow, false); } |
| | 197 | | } |