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