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