| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Events; |
| | 11 | | using UnityEngine.EventSystems; |
| | 12 | | using UnityEngine.UI; |
| | 13 | | using WebGLIMEInput = WebGLSupport.WebGLInput; |
| | 14 | |
|
| | 15 | | namespace DCL.Social.Chat |
| | 16 | | { |
| | 17 | | public class ChatHUDView : BaseComponentView, IChatHUDComponentView |
| | 18 | | { |
| | 19 | | [SerializeField] internal TMP_InputField inputField; |
| | 20 | | [SerializeField] internal RectTransform chatEntriesContainer; |
| | 21 | | [SerializeField] internal ScrollRect scrollRect; |
| | 22 | | [SerializeField] internal GameObject messageHoverPanel; |
| | 23 | | [SerializeField] internal GameObject messageHoverGotoPanel; |
| | 24 | | [SerializeField] internal TextMeshProUGUI messageHoverText; |
| | 25 | | [SerializeField] internal TextMeshProUGUI messageHoverGotoText; |
| | 26 | | [SerializeField] internal UserContextMenu contextMenu; |
| | 27 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 28 | | [SerializeField] internal DefaultChatEntryFactory defaultChatEntryFactory; |
| | 29 | | [SerializeField] internal PoolChatEntryFactory poolChatEntryFactory; |
| | 30 | | [SerializeField] internal InputAction_Trigger nextChatInHistoryInput; |
| | 31 | | [SerializeField] internal InputAction_Trigger previousChatInHistoryInput; |
| | 32 | | [SerializeField] internal InputAction_Trigger nextMentionSuggestionInput; |
| | 33 | | [SerializeField] internal InputAction_Trigger previousMentionSuggestionInput; |
| | 34 | | [SerializeField] internal InputAction_Trigger closeMentionSuggestionsInput; |
| | 35 | | [SerializeField] internal ChatMentionSuggestionComponentView chatMentionSuggestions; |
| | 36 | | [SerializeField] internal WebGLIMEInput webGlImeInput; |
| | 37 | | [SerializeField] internal Button unblockButton; |
| | 38 | | [SerializeField] private Model model; |
| | 39 | |
|
| 38 | 40 | | private readonly Dictionary<string, ChatEntry> entries = new (); |
| 38 | 41 | | private readonly ChatMessage currentMessage = new (); |
| 38 | 42 | | private readonly Dictionary<Action, UnityAction<string>> inputFieldSelectedListeners = new (); |
| 38 | 43 | | private readonly Dictionary<Action, UnityAction<string>> inputFieldUnselectedListeners = new (); |
| | 44 | |
|
| | 45 | | private int updateLayoutDelayedFrames; |
| | 46 | | private bool isSortingDirty; |
| | 47 | |
|
| | 48 | | public event Action<string, int> OnMessageUpdated; |
| | 49 | | public event Action<string> OnOpenedContextMenu; |
| | 50 | |
|
| | 51 | | public event Action OnShowMenu |
| | 52 | | { |
| | 53 | | add |
| | 54 | | { |
| 3 | 55 | | if (contextMenu != null) |
| 3 | 56 | | contextMenu.OnShowMenu += value; |
| 3 | 57 | | } |
| | 58 | |
|
| | 59 | | remove |
| | 60 | | { |
| 6 | 61 | | if (contextMenu != null) |
| 6 | 62 | | contextMenu.OnShowMenu -= value; |
| 6 | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public event Action OnInputFieldSelected |
| | 67 | | { |
| | 68 | | add |
| | 69 | | { |
| | 70 | | void Action(string s) => |
| 0 | 71 | | value.Invoke(); |
| | 72 | |
|
| 3 | 73 | | inputFieldSelectedListeners[value] = Action; |
| 3 | 74 | | inputField.onSelect.AddListener(Action); |
| 3 | 75 | | } |
| | 76 | |
|
| | 77 | | remove |
| | 78 | | { |
| 6 | 79 | | if (!inputFieldSelectedListeners.ContainsKey(value)) |
| 3 | 80 | | return; |
| | 81 | |
|
| 3 | 82 | | inputField.onSelect.RemoveListener(inputFieldSelectedListeners[value]); |
| 3 | 83 | | inputFieldSelectedListeners.Remove(value); |
| 3 | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public event Action OnInputFieldDeselected |
| | 88 | | { |
| | 89 | | add |
| | 90 | | { |
| | 91 | | void Action(string s) => |
| 0 | 92 | | value.Invoke(); |
| | 93 | |
|
| 3 | 94 | | inputFieldUnselectedListeners[value] = Action; |
| 3 | 95 | | inputField.onDeselect.AddListener(Action); |
| 3 | 96 | | } |
| | 97 | |
|
| | 98 | | remove |
| | 99 | | { |
| 6 | 100 | | if (!inputFieldUnselectedListeners.ContainsKey(value)) |
| 3 | 101 | | return; |
| | 102 | |
|
| 3 | 103 | | inputField.onDeselect.RemoveListener(inputFieldUnselectedListeners[value]); |
| 3 | 104 | | inputFieldUnselectedListeners.Remove(value); |
| 3 | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public event Action OnPreviousChatInHistory; |
| | 109 | | public event Action OnNextChatInHistory; |
| | 110 | | public event Action<string> OnMentionSuggestionSelected; |
| | 111 | | public event Action<ChatEntryModel> OnCopyMessageRequested; |
| | 112 | | public event Action<ChatMessage> OnSendMessage; |
| | 113 | | public event Action<string> OnUnblockUser; |
| 0 | 114 | | public int EntryCount => entries.Count; |
| 87 | 115 | | public IChatEntryFactory ChatEntryFactory { get; set; } |
| 8 | 116 | | public IComparer<ChatEntryModel> SortingStrategy { get; set; } |
| 5 | 117 | | public bool UseLegacySorting { private get; set; } |
| | 118 | |
|
| | 119 | | public override void Awake() |
| | 120 | | { |
| 34 | 121 | | base.Awake(); |
| 34 | 122 | | inputField.onSubmit.AddListener(OnInputFieldSubmit); |
| 34 | 123 | | inputField.onSelect.AddListener(OnInputFieldSelect); |
| 34 | 124 | | inputField.onDeselect.AddListener(OnInputFieldDeselect); |
| 34 | 125 | | inputField.onValueChanged.AddListener(str => OnMessageUpdated?.Invoke(str, inputField.stringPosition)); |
| 35 | 126 | | chatMentionSuggestions.OnEntrySubmit += model => OnMentionSuggestionSelected?.Invoke(model.userId); |
| 34 | 127 | | ChatEntryFactory ??= (IChatEntryFactory)poolChatEntryFactory ?? defaultChatEntryFactory; |
| 34 | 128 | | model.enableFadeoutMode = true; |
| 34 | 129 | | contextMenu.SetPassportOpenSource(true); |
| | 130 | |
|
| 34 | 131 | | unblockButton.onClick.AddListener(() => |
| | 132 | | { |
| 0 | 133 | | OnUnblockUser?.Invoke(model.conversationUserId); |
| 0 | 134 | | }); |
| | 135 | |
|
| | 136 | | #if (UNITY_WEBGL && !UNITY_EDITOR) |
| | 137 | | // WebGLInput plugin breaks many features: |
| | 138 | | // @mentions navigation with ARROW keys |
| | 139 | | // @mentions suggestions not hiding when pressing SPACE after @ |
| | 140 | | // chat windows toggling with ENTER key |
| | 141 | | // chat history navigation with ARROW keys |
| | 142 | | // probably more... |
| | 143 | | // Key input events are not triggered anymore after focusing the input field |
| | 144 | | // One of the main reasons is because the plugin replaces the TMP_InputField for a native web input, |
| | 145 | | // overriding input handing, caret position handling, most of the input behaviour.. |
| | 146 | | // To mitigate the issues for most of the non-asian users, |
| | 147 | | // we destroy the component because we assume that users dont need IME input |
| | 148 | | // Users with asian languages will keep experiencing the issues |
| | 149 | | SystemLanguage systemLanguage = Application.systemLanguage; |
| | 150 | |
|
| | 151 | | if (systemLanguage is not (SystemLanguage.Chinese |
| | 152 | | or SystemLanguage.ChineseSimplified |
| | 153 | | or SystemLanguage.ChineseTraditional |
| | 154 | | or SystemLanguage.Korean |
| | 155 | | or SystemLanguage.Japanese)) |
| | 156 | | Destroy(webGlImeInput); |
| | 157 | | #endif |
| 34 | 158 | | } |
| | 159 | |
|
| | 160 | | public override void OnEnable() |
| | 161 | | { |
| 37 | 162 | | base.OnEnable(); |
| 37 | 163 | | UpdateLayout(); |
| 37 | 164 | | nextChatInHistoryInput.OnTriggered += HandleNextChatInHistoryInput; |
| 37 | 165 | | previousChatInHistoryInput.OnTriggered += HandlePreviousChatInHistoryInput; |
| 37 | 166 | | nextMentionSuggestionInput.OnTriggered += HandleNextMentionSuggestionInput; |
| 37 | 167 | | previousMentionSuggestionInput.OnTriggered += HandlePreviousMentionSuggestionInput; |
| 37 | 168 | | closeMentionSuggestionsInput.OnTriggered += HandleCloseMentionSuggestionsInput; |
| 37 | 169 | | } |
| | 170 | |
|
| | 171 | | public override void OnDisable() |
| | 172 | | { |
| 37 | 173 | | base.OnDisable(); |
| 37 | 174 | | nextChatInHistoryInput.OnTriggered -= HandleNextChatInHistoryInput; |
| 37 | 175 | | previousChatInHistoryInput.OnTriggered -= HandlePreviousChatInHistoryInput; |
| 37 | 176 | | nextMentionSuggestionInput.OnTriggered -= HandleNextMentionSuggestionInput; |
| 37 | 177 | | previousMentionSuggestionInput.OnTriggered -= HandlePreviousMentionSuggestionInput; |
| 37 | 178 | | closeMentionSuggestionsInput.OnTriggered -= HandleCloseMentionSuggestionsInput; |
| 37 | 179 | | } |
| | 180 | |
|
| | 181 | | public void Update() |
| | 182 | | { |
| 181 | 183 | | if (updateLayoutDelayedFrames > 0) |
| | 184 | | { |
| 25 | 185 | | updateLayoutDelayedFrames--; |
| | 186 | |
|
| 25 | 187 | | if (updateLayoutDelayedFrames <= 0) |
| 3 | 188 | | chatEntriesContainer.ForceUpdateLayout(delayed: false); |
| | 189 | | } |
| | 190 | |
|
| 181 | 191 | | if (isSortingDirty) |
| 2 | 192 | | SortEntriesImmediate(); |
| | 193 | |
|
| 181 | 194 | | isSortingDirty = false; |
| 181 | 195 | | } |
| | 196 | |
|
| | 197 | | public void ResetInputField(bool loseFocus = false) |
| | 198 | | { |
| 4 | 199 | | inputField.text = string.Empty; |
| 4 | 200 | | inputField.caretColor = Color.white; |
| | 201 | |
|
| 4 | 202 | | if (loseFocus) |
| 0 | 203 | | UnfocusInputField(); |
| 4 | 204 | | } |
| | 205 | |
|
| | 206 | | public override void RefreshControl() |
| | 207 | | { |
| 0 | 208 | | if (model.isInputFieldFocused) |
| 0 | 209 | | FocusInputField(); |
| | 210 | |
|
| 0 | 211 | | SetInputFieldText(model.inputFieldText); |
| 0 | 212 | | SetFadeoutMode(model.enableFadeoutMode); |
| 0 | 213 | | ClearAllEntries(); |
| | 214 | |
|
| 0 | 215 | | foreach (var entry in model.entries) |
| 0 | 216 | | SetEntry(entry); |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public void FocusInputField() |
| | 220 | | { |
| 5 | 221 | | inputField.ActivateInputField(); |
| 5 | 222 | | inputField.Select(); |
| 5 | 223 | | } |
| | 224 | |
|
| | 225 | | public void UnfocusInputField() => |
| 3 | 226 | | EventSystem.current?.SetSelectedGameObject(null); |
| | 227 | |
|
| | 228 | | public void SetInputFieldText(string text) |
| | 229 | | { |
| 4 | 230 | | model.inputFieldText = text; |
| 4 | 231 | | inputField.SetTextWithoutNotify(text); |
| 4 | 232 | | inputField.MoveTextEnd(false); |
| 4 | 233 | | OnMessageUpdated?.Invoke(inputField.text, inputField.stringPosition); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | public void ShowMentionSuggestions() |
| | 237 | | { |
| 3 | 238 | | chatMentionSuggestions.Show(); |
| 3 | 239 | | } |
| | 240 | |
|
| | 241 | | public void SetMentionSuggestions(List<ChatMentionSuggestionModel> suggestions) |
| | 242 | | { |
| 2 | 243 | | chatMentionSuggestions.Clear(); |
| 2 | 244 | | chatMentionSuggestions.Set(suggestions); |
| 2 | 245 | | chatMentionSuggestions.SelectFirstEntry(); |
| 2 | 246 | | } |
| | 247 | |
|
| | 248 | | public void HideMentionSuggestions() |
| | 249 | | { |
| 4 | 250 | | chatMentionSuggestions.Hide(); |
| 4 | 251 | | } |
| | 252 | |
|
| | 253 | | public void SetBlockedStatus(bool blocked) |
| | 254 | | { |
| 2 | 255 | | inputField.gameObject.SetActive(!blocked); |
| 2 | 256 | | unblockButton.gameObject.SetActive(blocked); |
| 2 | 257 | | } |
| | 258 | |
|
| | 259 | | public void AddMentionToInputField(int fromIndex, int length, string userId, string userName) |
| | 260 | | { |
| 2 | 261 | | string message = inputField.text; |
| 2 | 262 | | StringBuilder builder = new (message); |
| | 263 | |
|
| 2 | 264 | | SetInputFieldText(builder.Remove(fromIndex, length) |
| | 265 | | .Insert(fromIndex, @$"@{userName} ") |
| | 266 | | .ToString()); |
| 2 | 267 | | FocusInputField(); |
| 2 | 268 | | } |
| | 269 | |
|
| | 270 | | public void AddTextIntoInputField(string text) |
| | 271 | | { |
| 0 | 272 | | SetInputFieldText(string.IsNullOrEmpty(inputField.text) ? $"{text} " : $"{inputField.text.TrimEnd()} {text} |
| 0 | 273 | | FocusInputField(); |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | public virtual void SetEntry(ChatEntryModel model, bool setScrollPositionToBottom = false) |
| | 277 | | { |
| 5 | 278 | | if (entries.ContainsKey(model.messageId)) |
| | 279 | | { |
| 0 | 280 | | var chatEntry = entries[model.messageId]; |
| 0 | 281 | | Populate(chatEntry, model); |
| 0 | 282 | | SetEntry(model.messageId, chatEntry, setScrollPositionToBottom); |
| | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| 5 | 286 | | var chatEntry = ChatEntryFactory.Create(model); |
| 5 | 287 | | Populate(chatEntry, model); |
| 5 | 288 | | chatEntry.ConfigureMentionLinkDetector(contextMenu); |
| | 289 | |
|
| 5 | 290 | | if (model.subType.Equals(ChatEntryModel.SubType.RECEIVED)) |
| 2 | 291 | | chatEntry.OnUserNameClicked += OnOpenContextMenu; |
| | 292 | |
|
| 5 | 293 | | chatEntry.OnTriggerHover += OnMessageTriggerHover; |
| 5 | 294 | | chatEntry.OnTriggerHoverGoto += OnMessageCoordinatesTriggerHover; |
| 5 | 295 | | chatEntry.OnCancelHover += OnMessageCancelHover; |
| 5 | 296 | | chatEntry.OnCancelGotoHover += OnMessageCancelGotoHover; |
| 5 | 297 | | chatEntry.OnCopyClicked += OnMessageCopy; |
| | 298 | |
|
| 5 | 299 | | SetEntry(model.messageId, chatEntry, setScrollPositionToBottom); |
| | 300 | | } |
| 5 | 301 | | } |
| | 302 | |
|
| | 303 | | public void SetEntry(string messageId, ChatEntry chatEntry, bool setScrollPositionToBottom = false) |
| | 304 | | { |
| 5 | 305 | | Dock(chatEntry); |
| 5 | 306 | | entries[messageId] = chatEntry; |
| | 307 | |
|
| 5 | 308 | | SortEntries(); |
| 5 | 309 | | UpdateLayout(); |
| | 310 | |
|
| 5 | 311 | | if (setScrollPositionToBottom && scrollRect.verticalNormalizedPosition > 0) |
| 0 | 312 | | scrollRect.verticalNormalizedPosition = 0; |
| 5 | 313 | | } |
| | 314 | |
|
| | 315 | | public void RemoveOldestEntry() |
| | 316 | | { |
| 0 | 317 | | if (entries.Count <= 0) return; |
| 0 | 318 | | var firstEntry = GetFirstEntry(); |
| 0 | 319 | | if (!firstEntry) return; |
| 0 | 320 | | entries.Remove(firstEntry.Model.messageId); |
| 0 | 321 | | ChatEntryFactory.Destroy(firstEntry); |
| 0 | 322 | | UpdateLayout(); |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | public override void Hide(bool instant = false) |
| | 326 | | { |
| 0 | 327 | | base.Hide(instant); |
| | 328 | |
|
| 0 | 329 | | if (contextMenu == null) |
| 0 | 330 | | return; |
| | 331 | |
|
| 0 | 332 | | contextMenu.Hide(); |
| 0 | 333 | | confirmationDialog.Hide(); |
| 0 | 334 | | } |
| | 335 | |
|
| | 336 | | public void OnMessageCancelHover() |
| | 337 | | { |
| 0 | 338 | | messageHoverPanel.SetActive(false); |
| 0 | 339 | | messageHoverText.text = string.Empty; |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | public virtual void ClearAllEntries() |
| | 343 | | { |
| 2 | 344 | | foreach (var entry in entries.Values) |
| 0 | 345 | | ChatEntryFactory.Destroy(entry); |
| | 346 | |
|
| 1 | 347 | | entries.Clear(); |
| 1 | 348 | | UpdateLayout(); |
| 1 | 349 | | } |
| | 350 | |
|
| | 351 | | protected void Populate(ChatEntry entry, ChatEntryModel model) |
| | 352 | | { |
| 5 | 353 | | entry.Populate(model); |
| 5 | 354 | | entry.SetFadeout(this.model.enableFadeoutMode); |
| 5 | 355 | | } |
| | 356 | |
|
| | 357 | | public void SetConversationUserId(string userId) |
| | 358 | | { |
| 0 | 359 | | model.conversationUserId = userId; |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | protected void Dock(ChatEntry entry) |
| | 363 | | { |
| 5 | 364 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 5 | 365 | | } |
| | 366 | |
|
| | 367 | | private void SetFadeoutMode(bool enabled) |
| | 368 | | { |
| 0 | 369 | | model.enableFadeoutMode = enabled; |
| | 370 | |
|
| 0 | 371 | | foreach (var entry in entries.Values) |
| 0 | 372 | | entry.SetFadeout(enabled && IsEntryVisible(entry)); |
| | 373 | |
|
| 0 | 374 | | if (enabled) |
| 0 | 375 | | confirmationDialog.Hide(); |
| 0 | 376 | | } |
| | 377 | |
|
| | 378 | | private bool IsEntryVisible(ChatEntry entry) |
| | 379 | | { |
| 0 | 380 | | int visibleCorners = |
| | 381 | | (entry.transform as RectTransform).CountCornersVisibleFrom(scrollRect.viewport.transform as RectTransfor |
| | 382 | |
|
| 0 | 383 | | return visibleCorners > 0; |
| | 384 | | } |
| | 385 | |
|
| | 386 | | private void OnInputFieldSubmit(string message) |
| | 387 | | { |
| 0 | 388 | | if (chatMentionSuggestions.IsVisible) |
| | 389 | | { |
| 0 | 390 | | if (!inputField.wasCanceled) |
| 0 | 391 | | chatMentionSuggestions.SubmitSelectedEntry(); |
| | 392 | |
|
| 0 | 393 | | return; |
| | 394 | | } |
| | 395 | |
|
| 0 | 396 | | currentMessage.body = message; |
| 0 | 397 | | currentMessage.sender = string.Empty; |
| 0 | 398 | | currentMessage.messageType = ChatMessage.Type.NONE; |
| 0 | 399 | | currentMessage.recipient = string.Empty; |
| | 400 | |
|
| | 401 | | // A TMP_InputField is automatically marked as 'wasCanceled' when the ESC key is pressed |
| 0 | 402 | | if (inputField.wasCanceled) |
| 0 | 403 | | currentMessage.body = string.Empty; |
| | 404 | |
|
| | 405 | | // we have to wait one frame to disengage the flow triggered by OnSendMessage |
| | 406 | | // otherwise it crashes the application (WebGL only) due a TextMeshPro bug |
| 0 | 407 | | StartCoroutine(WaitThenTriggerSendMessage()); |
| 0 | 408 | | } |
| | 409 | |
|
| | 410 | | private IEnumerator WaitThenTriggerSendMessage() |
| | 411 | | { |
| 0 | 412 | | yield return null; |
| 0 | 413 | | OnSendMessage?.Invoke(currentMessage); |
| 0 | 414 | | } |
| | 415 | |
|
| | 416 | | private void OnInputFieldSelect(string message) |
| | 417 | | { |
| 0 | 418 | | AudioScriptableObjects.inputFieldFocus.Play(true); |
| 0 | 419 | | } |
| | 420 | |
|
| | 421 | | private void OnInputFieldDeselect(string message) |
| | 422 | | { |
| 0 | 423 | | AudioScriptableObjects.inputFieldUnfocus.Play(true); |
| 0 | 424 | | } |
| | 425 | |
|
| | 426 | | private void OnOpenContextMenu(ChatEntry chatEntry) |
| | 427 | | { |
| 0 | 428 | | OnOpenedContextMenu?.Invoke(chatEntry.Model.senderId); |
| 0 | 429 | | chatEntry.DockContextMenu((RectTransform)contextMenu.transform); |
| 0 | 430 | | contextMenu.transform.parent = transform.parent; |
| 0 | 431 | | contextMenu.transform.SetAsLastSibling(); |
| 0 | 432 | | contextMenu.Show(chatEntry.Model.senderId); |
| 0 | 433 | | } |
| | 434 | |
|
| | 435 | | private void OnMessageTriggerHover(ChatEntry chatEntry) |
| | 436 | | { |
| 0 | 437 | | if (contextMenu == null || contextMenu.isVisible) |
| 0 | 438 | | return; |
| | 439 | |
|
| 0 | 440 | | messageHoverText.text = chatEntry.HoverString; |
| 0 | 441 | | chatEntry.DockHoverPanel((RectTransform)messageHoverPanel.transform); |
| 0 | 442 | | messageHoverPanel.SetActive(true); |
| 0 | 443 | | } |
| | 444 | |
|
| | 445 | | private void OnMessageCoordinatesTriggerHover(ChatEntry chatEntry, ParcelCoordinates parcelCoordinates) |
| | 446 | | { |
| 0 | 447 | | messageHoverGotoText.text = $"{parcelCoordinates} INFO"; |
| 0 | 448 | | chatEntry.DockHoverPanel((RectTransform)messageHoverGotoPanel.transform); |
| 0 | 449 | | messageHoverGotoPanel.SetActive(true); |
| 0 | 450 | | } |
| | 451 | |
|
| | 452 | | private void OnMessageCancelGotoHover() |
| | 453 | | { |
| 0 | 454 | | messageHoverGotoPanel.SetActive(false); |
| 0 | 455 | | messageHoverGotoText.text = string.Empty; |
| 0 | 456 | | } |
| | 457 | |
|
| | 458 | | private void SortEntries() => |
| 5 | 459 | | isSortingDirty = true; |
| | 460 | |
|
| | 461 | | private void SortEntriesImmediate() |
| | 462 | | { |
| 2 | 463 | | if (this.entries.Count <= 0) return; |
| | 464 | |
|
| 2 | 465 | | if (UseLegacySorting) |
| | 466 | | { |
| 0 | 467 | | var entries = this.entries.Values.OrderBy(x => x.Model.timestamp).ToList(); |
| | 468 | |
|
| 0 | 469 | | for (var i = 0; i < entries.Count; i++) |
| | 470 | | { |
| 0 | 471 | | if (entries[i].transform.GetSiblingIndex() != i) |
| 0 | 472 | | entries[i].transform.SetSiblingIndex(i); |
| | 473 | | } |
| | 474 | |
|
| 0 | 475 | | return; |
| | 476 | | } |
| | 477 | |
|
| 2 | 478 | | if (SortingStrategy == null) return; |
| | 479 | |
|
| 19 | 480 | | foreach (var entry in entries.Values.OrderBy(obj => obj.Model, SortingStrategy)) |
| 5 | 481 | | entry.transform.SetAsLastSibling(); |
| 2 | 482 | | } |
| | 483 | |
|
| | 484 | | private void HandleNextChatInHistoryInput(DCLAction_Trigger action) |
| | 485 | | { |
| 0 | 486 | | if (chatMentionSuggestions.IsVisible) return; |
| 0 | 487 | | OnNextChatInHistory?.Invoke(); |
| 0 | 488 | | } |
| | 489 | |
|
| | 490 | | private void HandlePreviousChatInHistoryInput(DCLAction_Trigger action) |
| | 491 | | { |
| 0 | 492 | | if (chatMentionSuggestions.IsVisible) return; |
| 0 | 493 | | OnPreviousChatInHistory?.Invoke(); |
| 0 | 494 | | } |
| | 495 | |
|
| | 496 | | private void HandleNextMentionSuggestionInput(DCLAction_Trigger action) |
| | 497 | | { |
| 0 | 498 | | if (!chatMentionSuggestions.IsVisible) return; |
| 0 | 499 | | chatMentionSuggestions.SelectNextEntry(); |
| 0 | 500 | | } |
| | 501 | |
|
| | 502 | | private void HandlePreviousMentionSuggestionInput(DCLAction_Trigger action) |
| | 503 | | { |
| 0 | 504 | | if (!chatMentionSuggestions.IsVisible) return; |
| 0 | 505 | | chatMentionSuggestions.SelectPreviousEntry(); |
| 0 | 506 | | } |
| | 507 | |
|
| | 508 | | private void HandleCloseMentionSuggestionsInput(DCLAction_Trigger action) |
| | 509 | | { |
| 0 | 510 | | if (!chatMentionSuggestions.IsVisible) return; |
| 0 | 511 | | chatMentionSuggestions.Hide(); |
| 0 | 512 | | } |
| | 513 | |
|
| | 514 | | private void UpdateLayout() |
| | 515 | | { |
| | 516 | | // we have to wait several frames before updating the layout |
| | 517 | | // every message entry waits for 3 frames before updating its own layout |
| | 518 | | // we gotta force the layout updating after that |
| | 519 | | // TODO: simplify this change to a bool when we update to a working TextMeshPro version |
| 43 | 520 | | updateLayoutDelayedFrames = 4; |
| 43 | 521 | | } |
| | 522 | |
|
| | 523 | | private ChatEntry GetFirstEntry() |
| | 524 | | { |
| 0 | 525 | | ChatEntry firstEntry = null; |
| | 526 | |
|
| 0 | 527 | | for (var i = 0; i < chatEntriesContainer.childCount; i++) |
| | 528 | | { |
| 0 | 529 | | var firstChildTransform = chatEntriesContainer.GetChild(i); |
| 0 | 530 | | if (!firstChildTransform) continue; |
| 0 | 531 | | var entry = firstChildTransform.GetComponent<ChatEntry>(); |
| 0 | 532 | | if (!entry) continue; |
| 0 | 533 | | firstEntry = entry; |
| 0 | 534 | | break; |
| | 535 | | } |
| | 536 | |
|
| 0 | 537 | | return firstEntry; |
| | 538 | | } |
| | 539 | |
|
| | 540 | | private void OnMessageCopy(ChatEntry entry) => |
| 0 | 541 | | OnCopyMessageRequested?.Invoke(entry.Model); |
| | 542 | |
|
| | 543 | | [Serializable] |
| | 544 | | private struct Model |
| | 545 | | { |
| | 546 | | public bool isInputFieldFocused; |
| | 547 | | public string inputFieldText; |
| | 548 | | public bool enableFadeoutMode; |
| | 549 | | public ChatEntryModel[] entries; |
| | 550 | | public string conversationUserId; |
| | 551 | | } |
| | 552 | | } |
| | 553 | | } |