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