| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using TMPro; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Events; |
| | 10 | | using UnityEngine.EventSystems; |
| | 11 | | using UnityEngine.UI; |
| | 12 | |
|
| | 13 | | public class ChatHUDView : BaseComponentView, IChatHUDComponentView |
| | 14 | | { |
| | 15 | | private const string VIEW_PATH = "SocialBarV1/Chat"; |
| | 16 | |
|
| | 17 | | public TMP_InputField inputField; |
| | 18 | | public RectTransform chatEntriesContainer; |
| | 19 | | public ScrollRect scrollRect; |
| | 20 | | public GameObject messageHoverPanel; |
| | 21 | | public GameObject messageHoverGotoPanel; |
| | 22 | | public TextMeshProUGUI messageHoverText; |
| | 23 | | public TextMeshProUGUI messageHoverGotoText; |
| | 24 | | public UserContextMenu contextMenu; |
| | 25 | | public UserContextConfirmationDialog confirmationDialog; |
| | 26 | | [SerializeField] private DefaultChatEntryFactory defaultChatEntryFactory; |
| | 27 | | [SerializeField] private Model model; |
| | 28 | | [SerializeField] private InputAction_Trigger nextChatInHistoryInput; |
| | 29 | | [SerializeField] private InputAction_Trigger previousChatInHistoryInput; |
| | 30 | |
|
| 31 | 31 | | private readonly Dictionary<string, ChatEntry> entries = new Dictionary<string, ChatEntry>(); |
| 31 | 32 | | private readonly ChatMessage currentMessage = new ChatMessage(); |
| | 33 | |
|
| 31 | 34 | | private readonly Dictionary<Action, UnityAction<string>> inputFieldSelectedListeners = |
| | 35 | | new Dictionary<Action, UnityAction<string>>(); |
| | 36 | |
|
| 31 | 37 | | private readonly Dictionary<Action, UnityAction<string>> inputFieldUnselectedListeners = |
| | 38 | | new Dictionary<Action, UnityAction<string>>(); |
| | 39 | |
|
| | 40 | | private int updateLayoutDelayedFrames; |
| | 41 | | private bool isSortingDirty; |
| | 42 | |
|
| 0 | 43 | | protected bool IsFadeoutModeEnabled => model.enableFadeoutMode; |
| | 44 | |
|
| | 45 | | public event Action<string> OnMessageUpdated; |
| | 46 | |
|
| | 47 | | public event Action OnShowMenu |
| | 48 | | { |
| | 49 | | add |
| | 50 | | { |
| 2 | 51 | | if (contextMenu != null) |
| 2 | 52 | | contextMenu.OnShowMenu += value; |
| 2 | 53 | | } |
| | 54 | | remove |
| | 55 | | { |
| 2 | 56 | | if (contextMenu != null) |
| 2 | 57 | | contextMenu.OnShowMenu -= value; |
| 2 | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public event Action OnInputFieldSelected |
| | 62 | | { |
| | 63 | | add |
| | 64 | | { |
| 0 | 65 | | void Action(string s) => value.Invoke(); |
| 2 | 66 | | inputFieldSelectedListeners[value] = Action; |
| 2 | 67 | | inputField.onSelect.AddListener(Action); |
| 2 | 68 | | } |
| | 69 | | remove |
| | 70 | | { |
| 4 | 71 | | if (!inputFieldSelectedListeners.ContainsKey(value)) return; |
| 0 | 72 | | inputField.onSelect.RemoveListener(inputFieldSelectedListeners[value]); |
| 0 | 73 | | inputFieldSelectedListeners.Remove(value); |
| 0 | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public event Action OnInputFieldDeselected |
| | 78 | | { |
| | 79 | | add |
| | 80 | | { |
| 0 | 81 | | void Action(string s) => value.Invoke(); |
| 2 | 82 | | inputFieldUnselectedListeners[value] = Action; |
| 2 | 83 | | inputField.onDeselect.AddListener(Action); |
| 2 | 84 | | } |
| | 85 | | remove |
| | 86 | | { |
| 4 | 87 | | if (!inputFieldUnselectedListeners.ContainsKey(value)) return; |
| 0 | 88 | | inputField.onDeselect.RemoveListener(inputFieldUnselectedListeners[value]); |
| 0 | 89 | | inputFieldUnselectedListeners.Remove(value); |
| 0 | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public event Action OnPreviousChatInHistory; |
| | 94 | | public event Action OnNextChatInHistory; |
| | 95 | |
|
| | 96 | | public event Action<ChatMessage> OnSendMessage; |
| | 97 | |
|
| 0 | 98 | | public int EntryCount => entries.Count; |
| 0 | 99 | | public IChatEntryFactory ChatEntryFactory { get; set; } |
| 1 | 100 | | public bool IsInputFieldSelected => inputField.isFocused; |
| | 101 | |
|
| | 102 | | public static ChatHUDView Create() |
| | 103 | | { |
| 0 | 104 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<ChatHUDView>(); |
| 0 | 105 | | return view; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public override void Awake() |
| | 109 | | { |
| 28 | 110 | | base.Awake(); |
| 28 | 111 | | inputField.onSubmit.AddListener(OnInputFieldSubmit); |
| 28 | 112 | | inputField.onSelect.AddListener(OnInputFieldSelect); |
| 28 | 113 | | inputField.onDeselect.AddListener(OnInputFieldDeselect); |
| 28 | 114 | | inputField.onValueChanged.AddListener(str => OnMessageUpdated?.Invoke(str)); |
| 28 | 115 | | ChatEntryFactory ??= defaultChatEntryFactory; |
| 28 | 116 | | model.enableFadeoutMode = true; |
| 28 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void OnEnable() |
| | 120 | | { |
| 29 | 121 | | base.OnEnable(); |
| 29 | 122 | | UpdateLayout(); |
| 29 | 123 | | nextChatInHistoryInput.OnTriggered += HandleNextChatInHistoryInput; |
| 29 | 124 | | previousChatInHistoryInput.OnTriggered += HandlePreviousChatInHistoryInput; |
| 29 | 125 | | } |
| | 126 | |
|
| | 127 | | public override void OnDisable() |
| | 128 | | { |
| 29 | 129 | | base.OnDisable(); |
| 29 | 130 | | nextChatInHistoryInput.OnTriggered -= HandleNextChatInHistoryInput; |
| 29 | 131 | | previousChatInHistoryInput.OnTriggered -= HandlePreviousChatInHistoryInput; |
| 29 | 132 | | } |
| | 133 | |
|
| | 134 | | public override void Update() |
| | 135 | | { |
| 897 | 136 | | base.Update(); |
| | 137 | |
|
| 897 | 138 | | if (updateLayoutDelayedFrames > 0) |
| | 139 | | { |
| 18 | 140 | | updateLayoutDelayedFrames--; |
| | 141 | |
|
| 18 | 142 | | if (updateLayoutDelayedFrames <= 0) |
| 4 | 143 | | chatEntriesContainer.ForceUpdateLayout(delayed: false); |
| | 144 | | } |
| | 145 | |
|
| 897 | 146 | | if (isSortingDirty) |
| 0 | 147 | | SortEntriesImmediate(); |
| 897 | 148 | | isSortingDirty = false; |
| 897 | 149 | | } |
| | 150 | |
|
| | 151 | | public void ResetInputField(bool loseFocus = false) |
| | 152 | | { |
| 0 | 153 | | inputField.text = string.Empty; |
| 0 | 154 | | inputField.caretColor = Color.white; |
| 0 | 155 | | if (loseFocus) |
| 0 | 156 | | UnfocusInputField(); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public override void RefreshControl() |
| | 160 | | { |
| 0 | 161 | | if (model.isInputFieldFocused) |
| 0 | 162 | | FocusInputField(); |
| 0 | 163 | | SetInputFieldText(model.inputFieldText); |
| 0 | 164 | | SetFadeoutMode(model.enableFadeoutMode); |
| 0 | 165 | | if (model.isInPreviewMode) |
| 0 | 166 | | ActivatePreview(); |
| | 167 | | else |
| 0 | 168 | | DeactivatePreview(); |
| 0 | 169 | | ClearAllEntries(); |
| 0 | 170 | | foreach (var entry in model.entries) |
| 0 | 171 | | AddEntry(entry); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | public void FocusInputField() |
| | 175 | | { |
| 1 | 176 | | inputField.ActivateInputField(); |
| 1 | 177 | | inputField.Select(); |
| 1 | 178 | | } |
| | 179 | |
|
| 1 | 180 | | public void UnfocusInputField() => EventSystem.current?.SetSelectedGameObject(null); |
| | 181 | |
|
| | 182 | | public void SetInputFieldText(string text) |
| | 183 | | { |
| 0 | 184 | | model.inputFieldText = text; |
| 0 | 185 | | inputField.text = text; |
| 0 | 186 | | inputField.MoveTextEnd(false); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public void ActivatePreview() |
| | 190 | | { |
| 2 | 191 | | model.isInPreviewMode = true; |
| | 192 | |
|
| 4 | 193 | | foreach (var entry in entries.Values) |
| 0 | 194 | | entry.ActivatePreview(); |
| 2 | 195 | | } |
| | 196 | |
|
| | 197 | | public void DeactivatePreview() |
| | 198 | | { |
| 0 | 199 | | model.isInPreviewMode = false; |
| | 200 | |
|
| 0 | 201 | | foreach (var entry in entries.Values) |
| 0 | 202 | | entry.DeactivatePreview(); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | public void FadeOutMessages() |
| | 206 | | { |
| 4 | 207 | | foreach (var entry in entries.Values) |
| 0 | 208 | | entry.FadeOut(); |
| 2 | 209 | | } |
| | 210 | |
|
| | 211 | | private void SetFadeoutMode(bool enabled) |
| | 212 | | { |
| 0 | 213 | | model.enableFadeoutMode = enabled; |
| | 214 | |
|
| 0 | 215 | | foreach (var entry in entries.Values) |
| 0 | 216 | | entry.SetFadeout(enabled && IsEntryVisible(entry)); |
| | 217 | |
|
| 0 | 218 | | if (enabled) |
| 0 | 219 | | confirmationDialog.Hide(); |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | public virtual void AddEntry(ChatEntryModel model, bool setScrollPositionToBottom = false) |
| | 223 | | { |
| 0 | 224 | | if (entries.ContainsKey(model.messageId)) |
| | 225 | | { |
| 0 | 226 | | var chatEntry = entries[model.messageId]; |
| 0 | 227 | | chatEntry.SetFadeout(this.model.enableFadeoutMode); |
| 0 | 228 | | chatEntry.Populate(model); |
| | 229 | |
|
| 0 | 230 | | SetEntry(model.messageId, chatEntry, setScrollPositionToBottom); |
| 0 | 231 | | } |
| | 232 | | else |
| | 233 | | { |
| 0 | 234 | | var chatEntry = ChatEntryFactory.Create(model); |
| 0 | 235 | | chatEntry.SetFadeout(this.model.enableFadeoutMode); |
| 0 | 236 | | chatEntry.Populate(model); |
| | 237 | |
|
| 0 | 238 | | if (model.messageType == ChatMessage.Type.PUBLIC |
| | 239 | | || model.messageType == ChatMessage.Type.PRIVATE) |
| 0 | 240 | | chatEntry.OnPressRightButton += OnOpenContextMenu; |
| | 241 | |
|
| 0 | 242 | | chatEntry.OnTriggerHover += OnMessageTriggerHover; |
| 0 | 243 | | chatEntry.OnTriggerHoverGoto += OnMessageCoordinatesTriggerHover; |
| 0 | 244 | | chatEntry.OnCancelHover += OnMessageCancelHover; |
| 0 | 245 | | chatEntry.OnCancelGotoHover += OnMessageCancelGotoHover; |
| | 246 | |
|
| 0 | 247 | | SetEntry(model.messageId, chatEntry, setScrollPositionToBottom); |
| | 248 | | } |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | public virtual void SetEntry(string messageId, ChatEntry chatEntry, bool setScrollPositionToBottom = false) |
| | 252 | | { |
| 0 | 253 | | chatEntry.transform.SetParent(chatEntriesContainer, false); |
| 0 | 254 | | entries[messageId] = chatEntry; |
| | 255 | |
|
| 0 | 256 | | if (model.isInPreviewMode) |
| 0 | 257 | | chatEntry.ActivatePreviewInstantly(); |
| | 258 | |
|
| 0 | 259 | | SortEntries(); |
| 0 | 260 | | UpdateLayout(); |
| | 261 | |
|
| 0 | 262 | | if (setScrollPositionToBottom && scrollRect.verticalNormalizedPosition > 0) |
| 0 | 263 | | scrollRect.verticalNormalizedPosition = 0; |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | public void RemoveFirstEntry() |
| | 267 | | { |
| 0 | 268 | | if (entries.Count <= 0) return; |
| 0 | 269 | | var firstEntry = GetFirstEntry(); |
| 0 | 270 | | if (firstEntry == null) return; |
| 0 | 271 | | Destroy(firstEntry.gameObject); |
| 0 | 272 | | entries.Remove(firstEntry.Model.messageId); |
| 0 | 273 | | } |
| | 274 | |
|
| | 275 | | public override void Hide(bool instant = false) |
| | 276 | | { |
| 0 | 277 | | base.Hide(instant); |
| 0 | 278 | | if (contextMenu == null) return; |
| 0 | 279 | | contextMenu.Hide(); |
| 0 | 280 | | confirmationDialog.Hide(); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | public void OnMessageCancelHover() |
| | 284 | | { |
| 0 | 285 | | messageHoverPanel.SetActive(false); |
| 0 | 286 | | messageHoverText.text = string.Empty; |
| 0 | 287 | | } |
| | 288 | |
|
| | 289 | | public virtual void ClearAllEntries() |
| | 290 | | { |
| 0 | 291 | | foreach (var entry in entries.Values) |
| 0 | 292 | | Destroy(entry.gameObject); |
| 0 | 293 | | entries.Clear(); |
| 0 | 294 | | } |
| | 295 | |
|
| | 296 | | private bool IsEntryVisible(ChatEntry entry) |
| | 297 | | { |
| 0 | 298 | | int visibleCorners = |
| | 299 | | (entry.transform as RectTransform).CountCornersVisibleFrom(scrollRect.viewport.transform as RectTransform); |
| 0 | 300 | | return visibleCorners > 0; |
| | 301 | | } |
| | 302 | |
|
| | 303 | | private void OnInputFieldSubmit(string message) |
| | 304 | | { |
| 0 | 305 | | currentMessage.body = message; |
| 0 | 306 | | currentMessage.sender = string.Empty; |
| 0 | 307 | | currentMessage.messageType = ChatMessage.Type.NONE; |
| 0 | 308 | | currentMessage.recipient = string.Empty; |
| | 309 | |
|
| | 310 | | // A TMP_InputField is automatically marked as 'wasCanceled' when the ESC key is pressed |
| 0 | 311 | | if (inputField.wasCanceled) |
| 0 | 312 | | currentMessage.body = string.Empty; |
| | 313 | |
|
| | 314 | | // we have to wait one frame to disengage the flow triggered by OnSendMessage |
| | 315 | | // otherwise it crashes the application (WebGL only) due a TextMeshPro bug |
| 0 | 316 | | StartCoroutine(WaitThenTriggerSendMessage()); |
| 0 | 317 | | } |
| | 318 | |
|
| | 319 | | private IEnumerator WaitThenTriggerSendMessage() |
| | 320 | | { |
| 0 | 321 | | yield return null; |
| 0 | 322 | | OnSendMessage?.Invoke(currentMessage); |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | private void OnInputFieldSelect(string message) |
| | 326 | | { |
| 0 | 327 | | AudioScriptableObjects.inputFieldFocus.Play(true); |
| 0 | 328 | | } |
| | 329 | |
|
| | 330 | | private void OnInputFieldDeselect(string message) |
| | 331 | | { |
| 0 | 332 | | AudioScriptableObjects.inputFieldUnfocus.Play(true); |
| 0 | 333 | | } |
| | 334 | |
|
| | 335 | | private void OnOpenContextMenu(DefaultChatEntry chatEntry) |
| | 336 | | { |
| 0 | 337 | | chatEntry.DockContextMenu((RectTransform) contextMenu.transform); |
| 0 | 338 | | contextMenu.transform.parent = transform; |
| 0 | 339 | | contextMenu.Show(chatEntry.Model.senderId); |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | protected virtual void OnMessageTriggerHover(DefaultChatEntry chatEntry) |
| | 343 | | { |
| 0 | 344 | | if (contextMenu == null || contextMenu.isVisible) |
| 0 | 345 | | return; |
| | 346 | |
|
| 0 | 347 | | messageHoverText.text = chatEntry.messageLocalDateTime; |
| 0 | 348 | | chatEntry.DockHoverPanel((RectTransform) messageHoverPanel.transform); |
| 0 | 349 | | messageHoverPanel.SetActive(true); |
| 0 | 350 | | } |
| | 351 | |
|
| | 352 | | private void OnMessageCoordinatesTriggerHover(DefaultChatEntry chatEntry, ParcelCoordinates parcelCoordinates) |
| | 353 | | { |
| 0 | 354 | | messageHoverGotoText.text = $"{parcelCoordinates} INFO"; |
| 0 | 355 | | chatEntry.DockHoverPanel((RectTransform) messageHoverGotoPanel.transform); |
| 0 | 356 | | messageHoverGotoPanel.SetActive(true); |
| 0 | 357 | | } |
| | 358 | |
|
| | 359 | | private void OnMessageCancelGotoHover() |
| | 360 | | { |
| 0 | 361 | | messageHoverGotoPanel.SetActive(false); |
| 0 | 362 | | messageHoverGotoText.text = string.Empty; |
| 0 | 363 | | } |
| | 364 | |
|
| 0 | 365 | | private void SortEntries() => isSortingDirty = true; |
| | 366 | |
|
| | 367 | | private void SortEntriesImmediate() |
| | 368 | | { |
| 0 | 369 | | if (this.entries.Count <= 0) return; |
| | 370 | |
|
| 0 | 371 | | var entries = this.entries.Values.OrderBy(x => x.Model.timestamp).ToList(); |
| | 372 | |
|
| 0 | 373 | | for (var i = 0; i < entries.Count; i++) |
| | 374 | | { |
| 0 | 375 | | if (entries[i].transform.GetSiblingIndex() != i) |
| 0 | 376 | | entries[i].transform.SetSiblingIndex(i); |
| | 377 | | } |
| 0 | 378 | | } |
| | 379 | |
|
| 0 | 380 | | private void HandleNextChatInHistoryInput(DCLAction_Trigger action) => OnNextChatInHistory?.Invoke(); |
| | 381 | |
|
| 0 | 382 | | private void HandlePreviousChatInHistoryInput(DCLAction_Trigger action) => OnPreviousChatInHistory?.Invoke(); |
| | 383 | |
|
| | 384 | | private void UpdateLayout() |
| | 385 | | { |
| | 386 | | // we have to wait several frames before updating the layout |
| | 387 | | // every message entry waits for 3 frames before updating its own layout |
| | 388 | | // we gotta force the layout updating after that |
| | 389 | | // TODO: simplify this change to a bool when we update to a working TextMeshPro version |
| 0 | 390 | | updateLayoutDelayedFrames = 4; |
| 0 | 391 | | } |
| | 392 | |
|
| | 393 | | private ChatEntry GetFirstEntry() |
| | 394 | | { |
| 0 | 395 | | ChatEntry firstEntry = null; |
| | 396 | |
|
| 0 | 397 | | for (var i = 0; i < chatEntriesContainer.childCount; i++) |
| | 398 | | { |
| 0 | 399 | | var firstChildTransform = chatEntriesContainer.GetChild(i); |
| 0 | 400 | | var entry = firstChildTransform.GetComponent<ChatEntry>(); |
| 0 | 401 | | if (entry == null) continue; |
| 0 | 402 | | firstEntry = entry; |
| 0 | 403 | | break; |
| | 404 | | } |
| | 405 | |
|
| 0 | 406 | | return firstEntry; |
| | 407 | | } |
| | 408 | |
|
| | 409 | | [Serializable] |
| | 410 | | private struct Model |
| | 411 | | { |
| | 412 | | public bool isInPreviewMode; |
| | 413 | | public bool isInputFieldFocused; |
| | 414 | | public string inputFieldText; |
| | 415 | | public bool enableFadeoutMode; |
| | 416 | | public ChatEntryModel[] entries; |
| | 417 | | } |
| | 418 | | } |