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