| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Events; |
| | 9 | | using UnityEngine.UI; |
| | 10 | | using System.Text.RegularExpressions; |
| | 11 | | using UnityEngine.EventSystems; |
| | 12 | | using DCL; |
| | 13 | |
|
| | 14 | | public class ChatHUDView : MonoBehaviour |
| | 15 | | { |
| 1 | 16 | | static string VIEW_PATH = "Chat Widget"; |
| 35 | 17 | | string ENTRY_PATH = "Chat Entry"; |
| | 18 | | private const int MAX_CONTINUOUS_MESSAGES = 6; |
| | 19 | | private const int MIN_MILLISECONDS_BETWEEN_MESSAGES = 1500; |
| | 20 | | private const int TEMPORARILY_MUTE_MINUTES = 10; |
| | 21 | |
|
| 35 | 22 | | public bool detectWhisper = true; |
| | 23 | | public TMP_InputField inputField; |
| | 24 | | public RectTransform chatEntriesContainer; |
| | 25 | |
|
| | 26 | | public ScrollRect scrollRect; |
| | 27 | | public ChatHUDController controller; |
| | 28 | | public GameObject messageHoverPanel; |
| | 29 | | public GameObject messageHoverGotoPanel; |
| | 30 | | public TextMeshProUGUI messageHoverText; |
| | 31 | | public TextMeshProUGUI messageHoverGotoText; |
| | 32 | | public UserContextMenu contextMenu; |
| | 33 | | public UserContextConfirmationDialog confirmationDialog; |
| | 34 | |
|
| 35 | 35 | | [NonSerialized] public List<ChatEntry> entries = new List<ChatEntry>(); |
| 35 | 36 | | [NonSerialized] public List<DateSeparatorEntry> dateSeparators = new List<DateSeparatorEntry>(); |
| | 37 | |
|
| 35 | 38 | | ChatMessage currentMessage = new ChatMessage(); |
| 35 | 39 | | Regex whisperRegex = new Regex(@"(?i)^\/(whisper|w) (\S+)( *)(.*)"); |
| | 40 | | Match whisperRegexMatch; |
| 35 | 41 | | private List<ChatEntry.Model> lastMessages = new List<ChatEntry.Model>(); |
| 35 | 42 | | private Dictionary<string, ulong> temporarilyMutedSenders = new Dictionary<string, ulong>(); |
| | 43 | |
|
| | 44 | | public event UnityAction<string> OnPressPrivateMessage; |
| | 45 | | public event UnityAction<ChatMessage> OnSendMessage; |
| | 46 | |
|
| | 47 | | public static ChatHUDView Create() |
| | 48 | | { |
| 14 | 49 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<ChatHUDView>(); |
| 14 | 50 | | return view; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public void Initialize(ChatHUDController controller, UnityAction<ChatMessage> OnSendMessage) |
| | 54 | | { |
| 32 | 55 | | this.controller = controller; |
| 32 | 56 | | this.OnSendMessage += OnSendMessage; |
| 32 | 57 | | inputField.onSubmit.AddListener(OnInputFieldSubmit); |
| 32 | 58 | | inputField.onSelect.AddListener(OnInputFieldSelect); |
| 32 | 59 | | inputField.onDeselect.AddListener(OnInputFieldDeselect); |
| 32 | 60 | | } |
| | 61 | |
|
| | 62 | | private void OnInputFieldSubmit(string message) |
| | 63 | | { |
| 4 | 64 | | currentMessage.body = message; |
| 4 | 65 | | currentMessage.sender = UserProfile.GetOwnUserProfile().userId; |
| 4 | 66 | | currentMessage.messageType = ChatMessage.Type.NONE; |
| 4 | 67 | | currentMessage.recipient = string.Empty; |
| | 68 | |
|
| 4 | 69 | | if (detectWhisper && !string.IsNullOrWhiteSpace(message)) |
| | 70 | | { |
| 4 | 71 | | whisperRegexMatch = whisperRegex.Match(message); |
| | 72 | |
|
| 4 | 73 | | if (whisperRegexMatch.Success) |
| | 74 | | { |
| 2 | 75 | | currentMessage.messageType = ChatMessage.Type.PRIVATE; |
| 2 | 76 | | currentMessage.recipient = whisperRegexMatch.Groups[2].Value; |
| 2 | 77 | | currentMessage.body = whisperRegexMatch.Groups[4].Value; |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // A TMP_InputField is automatically marked as 'wasCanceled' when the ESC key is pressed |
| 4 | 82 | | if (inputField.wasCanceled) |
| 1 | 83 | | currentMessage.body = string.Empty; |
| | 84 | |
|
| 4 | 85 | | OnSendMessage?.Invoke(currentMessage); |
| 4 | 86 | | } |
| | 87 | |
|
| 0 | 88 | | private void OnInputFieldSelect(string message) { AudioScriptableObjects.inputFieldFocus.Play(true); } |
| | 89 | |
|
| 0 | 90 | | private void OnInputFieldDeselect(string message) { AudioScriptableObjects.inputFieldUnfocus.Play(true); } |
| | 91 | |
|
| | 92 | | public void ResetInputField() |
| | 93 | | { |
| 0 | 94 | | inputField.text = string.Empty; |
| 0 | 95 | | inputField.caretColor = Color.white; |
| 0 | 96 | | } |
| | 97 | |
|
| 80 | 98 | | void OnEnable() { Utils.ForceUpdateLayout(transform as RectTransform); } |
| | 99 | |
|
| | 100 | | public void FocusInputField() |
| | 101 | | { |
| 5 | 102 | | inputField.ActivateInputField(); |
| 5 | 103 | | inputField.Select(); |
| 5 | 104 | | } |
| | 105 | |
|
| | 106 | | bool enableFadeoutMode = false; |
| | 107 | |
|
| | 108 | | bool EntryIsVisible(ChatEntry entry) |
| | 109 | | { |
| 1 | 110 | | int visibleCorners = |
| | 111 | | (entry.transform as RectTransform).CountCornersVisibleFrom(scrollRect.viewport.transform as RectTransform); |
| 1 | 112 | | return visibleCorners > 0; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public void SetFadeoutMode(bool enabled) |
| | 116 | | { |
| 9 | 117 | | enableFadeoutMode = enabled; |
| | 118 | |
|
| 22 | 119 | | for (int i = 0; i < entries.Count; i++) |
| | 120 | | { |
| 2 | 121 | | ChatEntry entry = entries[i]; |
| | 122 | |
|
| 2 | 123 | | if (enabled) |
| | 124 | | { |
| 0 | 125 | | entry.SetFadeout(EntryIsVisible(entry)); |
| 0 | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 2 | 129 | | entry.SetFadeout(false); |
| | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 9 | 133 | | if (enabled) |
| | 134 | | { |
| 3 | 135 | | confirmationDialog.Hide(); |
| | 136 | | } |
| 9 | 137 | | } |
| | 138 | |
|
| | 139 | | public virtual void AddEntry(ChatEntry.Model chatEntryModel, bool setScrollPositionToBottom = false) |
| | 140 | | { |
| 31 | 141 | | if (IsSpamming(chatEntryModel.senderName)) |
| 0 | 142 | | return; |
| | 143 | |
|
| 31 | 144 | | var chatEntryGO = Instantiate(Resources.Load(ENTRY_PATH) as GameObject, chatEntriesContainer); |
| 31 | 145 | | ChatEntry chatEntry = chatEntryGO.GetComponent<ChatEntry>(); |
| | 146 | |
|
| 31 | 147 | | if (enableFadeoutMode && EntryIsVisible(chatEntry)) |
| 0 | 148 | | chatEntry.SetFadeout(true); |
| | 149 | | else |
| 31 | 150 | | chatEntry.SetFadeout(false); |
| | 151 | |
|
| 31 | 152 | | chatEntry.Populate(chatEntryModel); |
| | 153 | |
|
| 31 | 154 | | if (chatEntryModel.messageType == ChatMessage.Type.PRIVATE) |
| 5 | 155 | | chatEntry.OnPress += OnPressPrivateMessage; |
| | 156 | |
|
| 31 | 157 | | if (chatEntryModel.messageType == ChatMessage.Type.PUBLIC || chatEntryModel.messageType == ChatMessage.Type.PRIV |
| 31 | 158 | | chatEntry.OnPressRightButton += OnOpenContextMenu; |
| | 159 | |
|
| 31 | 160 | | chatEntry.OnTriggerHover += OnMessageTriggerHover; |
| 31 | 161 | | chatEntry.OnTriggerHoverGoto += OnMessageCoordinatesTriggerHover; |
| 31 | 162 | | chatEntry.OnCancelHover += OnMessageCancelHover; |
| 31 | 163 | | chatEntry.OnCancelGotoHover += OnMessageCancelGotoHover; |
| | 164 | |
|
| 31 | 165 | | entries.Add(chatEntry); |
| | 166 | |
|
| 31 | 167 | | SortEntries(); |
| | 168 | |
|
| 31 | 169 | | Utils.ForceUpdateLayout(chatEntry.transform as RectTransform, delayed: false); |
| | 170 | |
|
| 31 | 171 | | if (setScrollPositionToBottom && scrollRect.verticalNormalizedPosition > 0) |
| 0 | 172 | | scrollRect.verticalNormalizedPosition = 0; |
| | 173 | |
|
| 31 | 174 | | if (string.IsNullOrEmpty(chatEntryModel.senderId)) |
| 26 | 175 | | return; |
| | 176 | |
|
| 5 | 177 | | if (lastMessages.Count == 0) |
| | 178 | | { |
| 4 | 179 | | lastMessages.Add(chatEntryModel); |
| 4 | 180 | | } |
| 1 | 181 | | else if(lastMessages[lastMessages.Count-1].senderName == chatEntryModel.senderName) |
| | 182 | | { |
| 0 | 183 | | if (MessagesSentTooFast(lastMessages[lastMessages.Count - 1].timestamp, chatEntryModel.timestamp)) |
| | 184 | | { |
| 0 | 185 | | lastMessages.Add(chatEntryModel); |
| | 186 | |
|
| 0 | 187 | | if (lastMessages.Count == MAX_CONTINUOUS_MESSAGES) |
| | 188 | | { |
| 0 | 189 | | temporarilyMutedSenders.Add(chatEntryModel.senderName, chatEntryModel.timestamp); |
| 0 | 190 | | lastMessages.Clear(); |
| | 191 | | } |
| 0 | 192 | | } |
| | 193 | | else |
| | 194 | | { |
| 0 | 195 | | lastMessages.Clear(); |
| | 196 | | } |
| 0 | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| 1 | 200 | | lastMessages.Clear(); |
| | 201 | | } |
| 1 | 202 | | } |
| | 203 | |
|
| | 204 | | bool MessagesSentTooFast(ulong oldMessageTimeStamp, ulong newMessageTimeStamp) |
| | 205 | | { |
| 0 | 206 | | System.DateTime oldDateTime = CreateBaseDateTime().AddMilliseconds(oldMessageTimeStamp).ToLocalTime(); |
| 0 | 207 | | System.DateTime newDateTime = CreateBaseDateTime().AddMilliseconds(newMessageTimeStamp).ToLocalTime(); |
| | 208 | |
|
| 0 | 209 | | return (newDateTime - oldDateTime).TotalMilliseconds < MIN_MILLISECONDS_BETWEEN_MESSAGES; |
| | 210 | | } |
| | 211 | |
|
| | 212 | | private bool IsSpamming(string senderName) |
| | 213 | | { |
| 31 | 214 | | if (string.IsNullOrEmpty(senderName)) |
| 0 | 215 | | return false; |
| | 216 | |
|
| 31 | 217 | | bool isSpamming = false; |
| | 218 | |
|
| 31 | 219 | | if (temporarilyMutedSenders.ContainsKey(senderName)) |
| | 220 | | { |
| 0 | 221 | | System.DateTime muteTimestamp = CreateBaseDateTime().AddMilliseconds(temporarilyMutedSenders[senderName]).To |
| 0 | 222 | | if ((System.DateTime.Now - muteTimestamp).Minutes < TEMPORARILY_MUTE_MINUTES) |
| 0 | 223 | | isSpamming = true; |
| | 224 | | else |
| 0 | 225 | | temporarilyMutedSenders.Remove(senderName); |
| | 226 | | } |
| | 227 | |
|
| 31 | 228 | | return isSpamming; |
| | 229 | | } |
| | 230 | |
|
| | 231 | | private System.DateTime CreateBaseDateTime() |
| | 232 | | { |
| 0 | 233 | | return new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); |
| | 234 | | } |
| | 235 | |
|
| | 236 | | private void OnOpenContextMenu(ChatEntry chatEntry) |
| | 237 | | { |
| 0 | 238 | | contextMenu.transform.position = chatEntry.contextMenuPositionReference.position; |
| 0 | 239 | | contextMenu.transform.parent = this.transform; |
| 0 | 240 | | contextMenu.Show(chatEntry.model.senderId); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | protected virtual void OnMessageTriggerHover(ChatEntry chatEntry) |
| | 244 | | { |
| 0 | 245 | | if (contextMenu == null || contextMenu.isVisible) |
| 0 | 246 | | return; |
| | 247 | |
|
| 0 | 248 | | messageHoverText.text = chatEntry.messageLocalDateTime; |
| 0 | 249 | | messageHoverPanel.transform.position = chatEntry.hoverPanelPositionReference.position; |
| 0 | 250 | | messageHoverPanel.SetActive(true); |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | protected virtual void OnMessageCoordinatesTriggerHover(ChatEntry chatEntry, ParcelCoordinates parcelCoordinates) |
| | 254 | | { |
| 0 | 255 | | messageHoverGotoText.text = $"{parcelCoordinates.ToString()} INFO"; |
| 0 | 256 | | messageHoverGotoPanel.transform.position = new Vector3(Input.mousePosition.x, chatEntry.hoverPanelPositionRefere |
| 0 | 257 | | messageHoverGotoPanel.SetActive(true); |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | public void OnMessageCancelHover() |
| | 261 | | { |
| 0 | 262 | | messageHoverPanel.SetActive(false); |
| 0 | 263 | | messageHoverText.text = string.Empty; |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | public void OnMessageCancelGotoHover() |
| | 267 | | { |
| 0 | 268 | | messageHoverGotoPanel.SetActive(false); |
| 0 | 269 | | messageHoverGotoText.text = string.Empty; |
| 0 | 270 | | } |
| | 271 | |
|
| | 272 | | public void SortEntries() |
| | 273 | | { |
| 158 | 274 | | entries = entries.OrderBy(x => x.model.timestamp).ToList(); |
| | 275 | |
|
| 31 | 276 | | int count = entries.Count; |
| 316 | 277 | | for (int i = 0; i < count; i++) |
| | 278 | | { |
| 127 | 279 | | if (entries[i].transform.GetSiblingIndex() != i) |
| | 280 | | { |
| 44 | 281 | | entries[i].transform.SetSiblingIndex(i); |
| 44 | 282 | | Utils.ForceUpdateLayout(entries[i].transform as RectTransform, delayed: false); |
| | 283 | | } |
| | 284 | | } |
| 31 | 285 | | } |
| | 286 | |
|
| | 287 | | public void CleanAllEntries() |
| | 288 | | { |
| 18 | 289 | | foreach (var entry in entries) |
| | 290 | | { |
| 1 | 291 | | Destroy(entry.gameObject); |
| | 292 | | } |
| | 293 | |
|
| 8 | 294 | | entries.Clear(); |
| | 295 | |
|
| 16 | 296 | | foreach (DateSeparatorEntry separator in dateSeparators) |
| | 297 | | { |
| 0 | 298 | | Destroy(separator.gameObject); |
| | 299 | | } |
| | 300 | |
|
| 8 | 301 | | dateSeparators.Clear(); |
| 8 | 302 | | } |
| | 303 | |
|
| | 304 | | public void SetGotoPanelStatus(bool isActive) |
| | 305 | | { |
| 6 | 306 | | DataStore.i.HUDs.gotoPanelVisible.Set(isActive); |
| 6 | 307 | | } |
| | 308 | | } |