| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.SettingsCommon; |
| | 5 | | using System; |
| | 6 | | using System.Threading; |
| | 7 | | using TMPro; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.EventSystems; |
| | 10 | |
|
| | 11 | | namespace DCL.Chat.HUD |
| | 12 | | { |
| | 13 | | public class DefaultChatEntry : ChatEntry, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler |
| | 14 | | { |
| | 15 | | [SerializeField] internal TextMeshProUGUI body; |
| 33 | 16 | | [SerializeField] internal float timeToHoverPanel = 1f; |
| 33 | 17 | | [SerializeField] internal float timeToHoverGotoPanel = 1f; |
| 33 | 18 | | [SerializeField] internal bool showUserName = true; |
| | 19 | | [SerializeField] private RectTransform hoverPanelPositionReference; |
| | 20 | | [SerializeField] private RectTransform contextMenuPositionReference; |
| | 21 | |
|
| | 22 | | private float hoverPanelTimer; |
| | 23 | | private float hoverGotoPanelTimer; |
| | 24 | | private bool isOverCoordinates; |
| | 25 | | private bool isShowingPreview; |
| | 26 | | private ParcelCoordinates currentCoordinates; |
| | 27 | | private ChatEntryModel model; |
| | 28 | |
|
| 33 | 29 | | private readonly CancellationTokenSource populationTaskCancellationTokenSource = new CancellationTokenSource(); |
| | 30 | |
|
| 0 | 31 | | public override ChatEntryModel Model => model; |
| | 32 | |
|
| | 33 | | public override string DateString => |
| 0 | 34 | | DateTimeOffset.FromUnixTimeMilliseconds((long) Model.timestamp) |
| | 35 | | .ToLocalTime() |
| | 36 | | .ToString("MM/dd/yyyy h:mm:ss tt"); |
| | 37 | | public override event Action<ChatEntry> OnUserNameClicked; |
| | 38 | | public override event Action<ChatEntry> OnTriggerHover; |
| | 39 | | public override event Action<ChatEntry, ParcelCoordinates> OnTriggerHoverGoto; |
| | 40 | | public override event Action OnCancelHover; |
| | 41 | | public override event Action OnCancelGotoHover; |
| | 42 | |
|
| | 43 | | public override void Populate(ChatEntryModel chatEntryModel) => |
| 12 | 44 | | PopulateTask(chatEntryModel, populationTaskCancellationTokenSource.Token).Forget(); |
| | 45 | |
|
| | 46 | | private async UniTask PopulateTask(ChatEntryModel chatEntryModel, CancellationToken cancellationToken) |
| | 47 | | { |
| 12 | 48 | | model = chatEntryModel; |
| | 49 | |
|
| 12 | 50 | | chatEntryModel.bodyText = ReplaceNonSupportedCharacteres(body.font, chatEntryModel.bodyText, '?'); |
| 12 | 51 | | chatEntryModel.bodyText = RemoveTabs(chatEntryModel.bodyText); |
| 12 | 52 | | var userString = GetUserString(chatEntryModel); |
| | 53 | |
|
| | 54 | | // Due to a TMPro bug in Unity 2020 LTS we have to wait several frames before setting the body.text to avoid |
| | 55 | | // client crash. More info at https://github.com/decentraland/unity-renderer/pull/2345#issuecomment-11557535 |
| | 56 | | // TODO: Remove hack in a newer Unity/TMPro version |
| 36 | 57 | | await UniTask.NextFrame(cancellationToken); |
| 36 | 58 | | await UniTask.NextFrame(cancellationToken); |
| 36 | 59 | | await UniTask.NextFrame(cancellationToken); |
| | 60 | |
|
| 12 | 61 | | if (!string.IsNullOrEmpty(userString) && showUserName) |
| 9 | 62 | | body.text = $"{userString} {chatEntryModel.bodyText}"; |
| | 63 | | else |
| 3 | 64 | | body.text = chatEntryModel.bodyText; |
| | 65 | |
|
| 12 | 66 | | body.text = GetCoordinatesLink(body.text); |
| | 67 | |
|
| 12 | 68 | | (transform as RectTransform).ForceUpdateLayout(); |
| | 69 | |
|
| 12 | 70 | | PlaySfx(chatEntryModel); |
| 12 | 71 | | } |
| | 72 | |
|
| | 73 | | private string GetUserString(ChatEntryModel chatEntryModel) |
| | 74 | | { |
| 12 | 75 | | if (string.IsNullOrEmpty(model.senderName)) return ""; |
| | 76 | |
|
| 12 | 77 | | var baseName = model.senderName; |
| | 78 | |
|
| 12 | 79 | | switch (chatEntryModel.subType) |
| | 80 | | { |
| | 81 | | case ChatEntryModel.SubType.SENT: |
| 7 | 82 | | switch (chatEntryModel.messageType) |
| | 83 | | { |
| | 84 | | case ChatMessage.Type.PUBLIC: |
| 6 | 85 | | case ChatMessage.Type.PRIVATE when chatEntryModel.isChannelMessage: |
| 1 | 86 | | baseName = "You"; |
| 1 | 87 | | break; |
| | 88 | | case ChatMessage.Type.PRIVATE: |
| 6 | 89 | | baseName = $"To {chatEntryModel.recipientName}"; |
| 6 | 90 | | break; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | break; |
| | 94 | | case ChatEntryModel.SubType.RECEIVED: |
| 5 | 95 | | switch (chatEntryModel.messageType) |
| | 96 | | { |
| | 97 | | case ChatMessage.Type.PRIVATE: |
| 3 | 98 | | baseName = $"<color=#5EBD3D>From <link=username://{baseName}>{baseName}</link></color>"; |
| 3 | 99 | | break; |
| | 100 | | case ChatMessage.Type.PUBLIC: |
| 1 | 101 | | baseName = $"<link=username://{baseName}>{baseName}</link>"; |
| | 102 | | break; |
| | 103 | | } |
| | 104 | | break; |
| | 105 | | } |
| | 106 | |
|
| 12 | 107 | | baseName = $"<b>{baseName}:</b>"; |
| | 108 | |
|
| 12 | 109 | | return baseName; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private string GetCoordinatesLink(string body) |
| | 113 | | { |
| 12 | 114 | | if (!CoordinateUtils.HasValidTextCoordinates(body)) |
| 9 | 115 | | return body; |
| 3 | 116 | | var textCoordinates = CoordinateUtils.GetTextCoordinates(body); |
| | 117 | |
|
| 16 | 118 | | for (var i = 0; i < textCoordinates.Count; i++) |
| | 119 | | { |
| | 120 | | // TODO: the preload should not be here |
| 5 | 121 | | PreloadSceneMetadata(CoordinateUtils.ParseCoordinatesString(textCoordinates[i])); |
| | 122 | |
|
| 5 | 123 | | body = body.Replace(textCoordinates[i], |
| | 124 | | $"</noparse><link={textCoordinates[i]}><color=#4886E3><u>{textCoordinates[i]}</u></color></link><nop |
| | 125 | | } |
| | 126 | |
|
| 3 | 127 | | return body; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | private void PlaySfx(ChatEntryModel chatEntryModel) |
| | 131 | | { |
| 12 | 132 | | if (HUDAudioHandler.i == null) |
| 12 | 133 | | return; |
| | 134 | |
|
| 0 | 135 | | if (IsRecentMessage(chatEntryModel) && Settings.i.audioSettings.Data.chatSFXEnabled) |
| | 136 | | { |
| 0 | 137 | | switch (chatEntryModel.messageType) |
| | 138 | | { |
| | 139 | | case ChatMessage.Type.PUBLIC: |
| | 140 | | // Check whether or not the message was sent by the local player |
| 0 | 141 | | if (chatEntryModel.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 142 | | AudioScriptableObjects.chatSend.Play(true); |
| | 143 | | else |
| 0 | 144 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| 0 | 145 | | break; |
| | 146 | | case ChatMessage.Type.PRIVATE: |
| 0 | 147 | | switch (chatEntryModel.subType) |
| | 148 | | { |
| | 149 | | case ChatEntryModel.SubType.RECEIVED: |
| 0 | 150 | | AudioScriptableObjects.chatReceivePrivate.Play(true); |
| 0 | 151 | | break; |
| | 152 | | case ChatEntryModel.SubType.SENT: |
| 0 | 153 | | AudioScriptableObjects.chatSend.Play(true); |
| 0 | 154 | | break; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | break; |
| | 158 | | case ChatMessage.Type.SYSTEM: |
| 0 | 159 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| | 160 | | break; |
| | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | HUDAudioHandler.i.RefreshChatLastCheckedTimestamp(); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private bool IsRecentMessage(ChatEntryModel chatEntryModel) |
| | 168 | | { |
| 0 | 169 | | return chatEntryModel.timestamp > HUDAudioHandler.i.chatLastCheckedTimestamp |
| | 170 | | && (DateTimeOffset.UtcNow - DateTimeOffset.FromUnixTimeMilliseconds((long) chatEntryModel.timestamp)) |
| | 171 | | .TotalSeconds < 30; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | private void PreloadSceneMetadata(ParcelCoordinates parcelCoordinates) |
| | 175 | | { |
| 5 | 176 | | if (MinimapMetadata.GetMetadata().GetSceneInfo(parcelCoordinates.x, parcelCoordinates.y) == null) |
| 5 | 177 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(parcelCoordinates.x, parcelCoordinates.y), 2); |
| 5 | 178 | | } |
| | 179 | |
|
| | 180 | | public void OnPointerClick(PointerEventData pointerEventData) |
| | 181 | | { |
| 0 | 182 | | if (pointerEventData.button == PointerEventData.InputButton.Left) |
| | 183 | | { |
| 0 | 184 | | var linkIndex = |
| | 185 | | TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, body.canvas.worldCamera); |
| 0 | 186 | | if (linkIndex == -1) return; |
| | 187 | |
|
| 0 | 188 | | var link = body.textInfo.linkInfo[linkIndex].GetLinkID(); |
| | 189 | |
|
| 0 | 190 | | if (CoordinateUtils.HasValidTextCoordinates(link)) |
| | 191 | | { |
| 0 | 192 | | DataStore.i.HUDs.gotoPanelVisible.Set(true); |
| 0 | 193 | | var parcelCoordinate = CoordinateUtils.ParseCoordinatesString(link); |
| 0 | 194 | | DataStore.i.HUDs.gotoPanelCoordinates.Set(parcelCoordinate); |
| 0 | 195 | | } |
| 0 | 196 | | else if (link.StartsWith("username://")) |
| 0 | 197 | | OnUserNameClicked?.Invoke(this); |
| | 198 | | } |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | public void OnPointerEnter(PointerEventData pointerEventData) |
| | 202 | | { |
| 0 | 203 | | if (pointerEventData == null) |
| 0 | 204 | | return; |
| | 205 | |
|
| 0 | 206 | | hoverPanelTimer = timeToHoverPanel; |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | public void OnPointerExit(PointerEventData pointerEventData) |
| | 210 | | { |
| 12 | 211 | | if (pointerEventData == null) |
| 12 | 212 | | return; |
| | 213 | |
|
| 0 | 214 | | hoverPanelTimer = 0f; |
| 0 | 215 | | var linkIndex = |
| | 216 | | TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, |
| | 217 | | DataStore.i.camera.hudsCamera.Get()); |
| 0 | 218 | | if (linkIndex == -1) |
| | 219 | | { |
| 0 | 220 | | isOverCoordinates = false; |
| 0 | 221 | | hoverGotoPanelTimer = 0; |
| 0 | 222 | | OnCancelGotoHover?.Invoke(); |
| | 223 | | } |
| | 224 | |
|
| 0 | 225 | | OnCancelHover?.Invoke(); |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private void OnDisable() |
| | 229 | | { |
| 12 | 230 | | OnPointerExit(null); |
| 12 | 231 | | } |
| | 232 | |
|
| | 233 | | private void OnDestroy() |
| | 234 | | { |
| 12 | 235 | | populationTaskCancellationTokenSource.Cancel(); |
| 12 | 236 | | } |
| | 237 | |
|
| | 238 | | public override void SetFadeout(bool enabled) |
| | 239 | | { |
| 0 | 240 | | if (enabled) return; |
| 0 | 241 | | group.alpha = 1; |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | public override void DockContextMenu(RectTransform panel) |
| | 245 | | { |
| 0 | 246 | | panel.pivot = new Vector2(0, 0); |
| 0 | 247 | | panel.position = contextMenuPositionReference.position; |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | public override void DockHoverPanel(RectTransform panel) |
| | 251 | | { |
| 0 | 252 | | panel.pivot = hoverPanelPositionReference.pivot; |
| 0 | 253 | | panel.position = hoverPanelPositionReference.position; |
| 0 | 254 | | } |
| | 255 | |
|
| | 256 | | private void Update() |
| | 257 | | { |
| | 258 | | // TODO: why it needs to be in an update? what about OnPointerEnter/OnPointerExit? |
| 48 | 259 | | CheckHoverCoordinates(); |
| 48 | 260 | | ProcessHoverPanelTimer(); |
| 48 | 261 | | ProcessHoverGotoPanelTimer(); |
| 48 | 262 | | } |
| | 263 | |
|
| | 264 | | private void CheckHoverCoordinates() |
| | 265 | | { |
| 48 | 266 | | if (isOverCoordinates) |
| 0 | 267 | | return; |
| | 268 | |
|
| 48 | 269 | | var linkIndex = |
| | 270 | | TMP_TextUtilities.FindIntersectingLink(body, Input.mousePosition, DataStore.i.camera.hudsCamera.Get()); |
| | 271 | |
|
| 48 | 272 | | if (linkIndex == -1) |
| 48 | 273 | | return; |
| | 274 | |
|
| 0 | 275 | | var link = body.textInfo.linkInfo[linkIndex].GetLinkID(); |
| 0 | 276 | | if (!CoordinateUtils.HasValidTextCoordinates(link)) return; |
| | 277 | |
|
| 0 | 278 | | isOverCoordinates = true; |
| 0 | 279 | | currentCoordinates = CoordinateUtils.ParseCoordinatesString(link); |
| 0 | 280 | | hoverGotoPanelTimer = timeToHoverGotoPanel; |
| 0 | 281 | | OnCancelHover?.Invoke(); |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | private void ProcessHoverPanelTimer() |
| | 285 | | { |
| 48 | 286 | | if (hoverPanelTimer <= 0f || isOverCoordinates) |
| 48 | 287 | | return; |
| | 288 | |
|
| 0 | 289 | | hoverPanelTimer -= Time.deltaTime; |
| 0 | 290 | | if (hoverPanelTimer <= 0f) |
| | 291 | | { |
| 0 | 292 | | hoverPanelTimer = 0f; |
| 0 | 293 | | OnTriggerHover?.Invoke(this); |
| | 294 | | } |
| 0 | 295 | | } |
| | 296 | |
|
| | 297 | | private void ProcessHoverGotoPanelTimer() |
| | 298 | | { |
| 48 | 299 | | if (hoverGotoPanelTimer <= 0f || !isOverCoordinates) |
| 48 | 300 | | return; |
| | 301 | |
|
| 0 | 302 | | hoverGotoPanelTimer -= Time.deltaTime; |
| 0 | 303 | | if (hoverGotoPanelTimer <= 0f) |
| | 304 | | { |
| 0 | 305 | | hoverGotoPanelTimer = 0f; |
| 0 | 306 | | OnTriggerHoverGoto?.Invoke(this, currentCoordinates); |
| | 307 | | } |
| 0 | 308 | | } |
| | 309 | |
|
| | 310 | | private string RemoveTabs(string text) |
| | 311 | | { |
| 12 | 312 | | if (string.IsNullOrEmpty(text)) |
| 0 | 313 | | return ""; |
| | 314 | |
|
| | 315 | | //NOTE(Brian): ContentSizeFitter doesn't fare well with tabs, so i'm replacing these |
| | 316 | | // with spaces. |
| 12 | 317 | | return text.Replace("\t", " "); |
| | 318 | | } |
| | 319 | |
|
| | 320 | | private string ReplaceNonSupportedCharacteres(TMP_FontAsset font, string bodyText, char replacementChar) |
| | 321 | | { |
| 12 | 322 | | bool isBodyFormatSupported = font.HasCharacters(bodyText, out uint[] missing, searchFallbacks: true, tryAddC |
| | 323 | |
|
| 12 | 324 | | if (!isBodyFormatSupported) |
| | 325 | | { |
| 0 | 326 | | if (missing != null && missing.Length > 0) |
| | 327 | | { |
| 0 | 328 | | for (int i = 0; i < missing.Length; i++) |
| | 329 | | { |
| 0 | 330 | | bodyText = bodyText.Replace(((char)missing[i]), replacementChar); |
| | 331 | | } |
| | 332 | | } |
| | 333 | | } |
| | 334 | |
|
| 12 | 335 | | return bodyText; |
| | 336 | | } |
| | 337 | | } |
| | 338 | | } |