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