| | 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"; |
| 41 | 15 | | string ENTRY_PATH = "Chat Entry"; |
| | 16 | |
|
| 41 | 17 | | public bool detectWhisper = true; |
| | 18 | | public TMP_InputField inputField; |
| | 19 | | public RectTransform chatEntriesContainer; |
| | 20 | |
|
| | 21 | | public ScrollRect scrollRect; |
| | 22 | | public ChatHUDController controller; |
| | 23 | | public GameObject messageHoverPanel; |
| | 24 | | public TextMeshProUGUI messageHoverText; |
| | 25 | | public UserContextMenu contextMenu; |
| | 26 | | public UserContextConfirmationDialog confirmationDialog; |
| | 27 | |
|
| 41 | 28 | | [NonSerialized] public List<ChatEntry> entries = new List<ChatEntry>(); |
| 41 | 29 | | [NonSerialized] public List<DateSeparatorEntry> dateSeparators = new List<DateSeparatorEntry>(); |
| | 30 | |
|
| 41 | 31 | | ChatMessage currentMessage = new ChatMessage(); |
| 41 | 32 | | Regex whisperRegex = new Regex(@"(?i)^\/(whisper|w) (\S+)( *)(.*)"); |
| | 33 | | Match whisperRegexMatch; |
| | 34 | |
|
| | 35 | | public event UnityAction<string> OnPressPrivateMessage; |
| | 36 | | public event UnityAction<ChatMessage> OnSendMessage; |
| | 37 | |
|
| | 38 | | public static ChatHUDView Create() |
| | 39 | | { |
| 4 | 40 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<ChatHUDView>(); |
| 4 | 41 | | return view; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public void Initialize(ChatHUDController controller, UnityAction<ChatMessage> OnSendMessage) |
| | 45 | | { |
| 22 | 46 | | this.controller = controller; |
| 22 | 47 | | this.OnSendMessage += OnSendMessage; |
| 22 | 48 | | inputField.onSubmit.AddListener(OnInputFieldSubmit); |
| 22 | 49 | | inputField.onSelect.AddListener(OnInputFieldSelect); |
| 22 | 50 | | inputField.onDeselect.AddListener(OnInputFieldDeselect); |
| 22 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnInputFieldSubmit(string message) |
| | 54 | | { |
| 4 | 55 | | currentMessage.body = message; |
| 4 | 56 | | currentMessage.sender = UserProfile.GetOwnUserProfile().userId; |
| 4 | 57 | | currentMessage.messageType = ChatMessage.Type.NONE; |
| 4 | 58 | | currentMessage.recipient = string.Empty; |
| | 59 | |
|
| 4 | 60 | | if (detectWhisper && !string.IsNullOrWhiteSpace(message)) |
| | 61 | | { |
| 4 | 62 | | whisperRegexMatch = whisperRegex.Match(message); |
| | 63 | |
|
| 4 | 64 | | if (whisperRegexMatch.Success) |
| | 65 | | { |
| 2 | 66 | | currentMessage.messageType = ChatMessage.Type.PRIVATE; |
| 2 | 67 | | currentMessage.recipient = whisperRegexMatch.Groups[2].Value; |
| 2 | 68 | | currentMessage.body = whisperRegexMatch.Groups[4].Value; |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| | 72 | | // A TMP_InputField is automatically marked as 'wasCanceled' when the ESC key is pressed |
| 4 | 73 | | if (inputField.wasCanceled) |
| 1 | 74 | | currentMessage.body = string.Empty; |
| | 75 | |
|
| 4 | 76 | | OnSendMessage?.Invoke(currentMessage); |
| 4 | 77 | | } |
| | 78 | |
|
| 8 | 79 | | private void OnInputFieldSelect(string message) { AudioScriptableObjects.inputFieldFocus.Play(true); } |
| | 80 | |
|
| 4 | 81 | | private void OnInputFieldDeselect(string message) { AudioScriptableObjects.inputFieldUnfocus.Play(true); } |
| | 82 | |
|
| | 83 | | public void ResetInputField() |
| | 84 | | { |
| 0 | 85 | | inputField.text = string.Empty; |
| 0 | 86 | | inputField.caretColor = Color.white; |
| 0 | 87 | | } |
| | 88 | |
|
| 60 | 89 | | void OnEnable() { Utils.ForceUpdateLayout(transform as RectTransform); } |
| | 90 | |
|
| | 91 | | public void FocusInputField() |
| | 92 | | { |
| 4 | 93 | | inputField.ActivateInputField(); |
| 4 | 94 | | inputField.Select(); |
| 4 | 95 | | } |
| | 96 | |
|
| | 97 | | bool enableFadeoutMode = false; |
| | 98 | |
|
| | 99 | | bool EntryIsVisible(ChatEntry entry) |
| | 100 | | { |
| 1 | 101 | | int visibleCorners = |
| | 102 | | (entry.transform as RectTransform).CountCornersVisibleFrom(scrollRect.viewport.transform as RectTransform); |
| 1 | 103 | | return visibleCorners > 0; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public void SetFadeoutMode(bool enabled) |
| | 107 | | { |
| 8 | 108 | | enableFadeoutMode = enabled; |
| | 109 | |
|
| 18 | 110 | | for (int i = 0; i < entries.Count; i++) |
| | 111 | | { |
| 1 | 112 | | ChatEntry entry = entries[i]; |
| | 113 | |
|
| 1 | 114 | | if (enabled) |
| | 115 | | { |
| 0 | 116 | | entry.SetFadeout(EntryIsVisible(entry)); |
| 0 | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 1 | 120 | | entry.SetFadeout(false); |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| 8 | 124 | | if (enabled) |
| | 125 | | { |
| 3 | 126 | | confirmationDialog.Hide(); |
| | 127 | | } |
| 8 | 128 | | } |
| | 129 | |
|
| | 130 | | public virtual void AddEntry(ChatEntry.Model chatEntryModel, bool setScrollPositionToBottom = false) |
| | 131 | | { |
| 21 | 132 | | var chatEntryGO = Instantiate(Resources.Load(ENTRY_PATH) as GameObject, chatEntriesContainer); |
| 21 | 133 | | ChatEntry chatEntry = chatEntryGO.GetComponent<ChatEntry>(); |
| | 134 | |
|
| 21 | 135 | | if (enableFadeoutMode && EntryIsVisible(chatEntry)) |
| 0 | 136 | | chatEntry.SetFadeout(true); |
| | 137 | | else |
| 21 | 138 | | chatEntry.SetFadeout(false); |
| | 139 | |
|
| 21 | 140 | | chatEntry.Populate(chatEntryModel); |
| | 141 | |
|
| 21 | 142 | | if (chatEntryModel.messageType == ChatMessage.Type.PRIVATE) |
| 4 | 143 | | chatEntry.OnPress += OnPressPrivateMessage; |
| | 144 | |
|
| 21 | 145 | | if (chatEntryModel.messageType == ChatMessage.Type.PUBLIC || chatEntryModel.messageType == ChatMessage.Type.PRIV |
| 21 | 146 | | chatEntry.OnPressRightButton += OnOpenContextMenu; |
| | 147 | |
|
| 21 | 148 | | chatEntry.OnTriggerHover += OnMessageTriggerHover; |
| 21 | 149 | | chatEntry.OnCancelHover += OnMessageCancelHover; |
| | 150 | |
|
| 21 | 151 | | entries.Add(chatEntry); |
| | 152 | |
|
| 21 | 153 | | SortEntries(); |
| | 154 | |
|
| 21 | 155 | | Utils.ForceUpdateLayout(chatEntry.transform as RectTransform, delayed: false); |
| | 156 | |
|
| 21 | 157 | | if (setScrollPositionToBottom && scrollRect.verticalNormalizedPosition > 0) |
| 0 | 158 | | scrollRect.verticalNormalizedPosition = 0; |
| 21 | 159 | | } |
| | 160 | |
|
| | 161 | | private void OnOpenContextMenu(ChatEntry chatEntry) |
| | 162 | | { |
| 0 | 163 | | contextMenu.transform.position = chatEntry.contextMenuPositionReference.position; |
| 0 | 164 | | contextMenu.transform.parent = this.transform; |
| 0 | 165 | | contextMenu.Show(chatEntry.model.senderId); |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | protected virtual void OnMessageTriggerHover(ChatEntry chatEntry) |
| | 169 | | { |
| 0 | 170 | | if (contextMenu == null || contextMenu.isVisible) |
| 0 | 171 | | return; |
| | 172 | |
|
| 0 | 173 | | messageHoverText.text = chatEntry.messageLocalDateTime; |
| 0 | 174 | | messageHoverPanel.transform.position = chatEntry.hoverPanelPositionReference.position; |
| 0 | 175 | | messageHoverPanel.SetActive(true); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | public void OnMessageCancelHover() |
| | 179 | | { |
| 28 | 180 | | messageHoverPanel.SetActive(false); |
| 28 | 181 | | messageHoverText.text = string.Empty; |
| 28 | 182 | | } |
| | 183 | |
|
| | 184 | | public void SortEntries() |
| | 185 | | { |
| 138 | 186 | | entries = entries.OrderBy(x => x.model.timestamp).ToList(); |
| | 187 | |
|
| 21 | 188 | | int count = entries.Count; |
| 276 | 189 | | for (int i = 0; i < count; i++) |
| | 190 | | { |
| 117 | 191 | | if (entries[i].transform.GetSiblingIndex() != i) |
| | 192 | | { |
| 44 | 193 | | entries[i].transform.SetSiblingIndex(i); |
| 44 | 194 | | Utils.ForceUpdateLayout(entries[i].transform as RectTransform, delayed: false); |
| | 195 | | } |
| | 196 | | } |
| 21 | 197 | | } |
| | 198 | |
|
| | 199 | | public void CleanAllEntries() |
| | 200 | | { |
| 18 | 201 | | foreach (var entry in entries) |
| | 202 | | { |
| 1 | 203 | | Destroy(entry.gameObject); |
| | 204 | | } |
| | 205 | |
|
| 8 | 206 | | entries.Clear(); |
| | 207 | |
|
| 16 | 208 | | foreach (DateSeparatorEntry separator in dateSeparators) |
| | 209 | | { |
| 0 | 210 | | Destroy(separator.gameObject); |
| | 211 | | } |
| | 212 | |
|
| 8 | 213 | | dateSeparators.Clear(); |
| 8 | 214 | | } |
| | 215 | | } |