| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using DCL.Interface; |
| | 9 | | using DCL.SettingsCommon; |
| | 10 | | using TMPro; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.EventSystems; |
| | 13 | | using UnityEngine.UI; |
| | 14 | |
|
| | 15 | | public class DefaultChatEntry : ChatEntry, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler |
| | 16 | | { |
| | 17 | | [SerializeField] internal TextMeshProUGUI body; |
| 27 | 18 | | [SerializeField] internal float timeToHoverPanel = 1f; |
| 27 | 19 | | [SerializeField] internal float timeToHoverGotoPanel = 1f; |
| 27 | 20 | | [SerializeField] internal bool showUserName = true; |
| | 21 | | [SerializeField] private RectTransform hoverPanelPositionReference; |
| | 22 | | [SerializeField] private RectTransform contextMenuPositionReference; |
| | 23 | | [NonSerialized] public string messageLocalDateTime; |
| | 24 | |
|
| | 25 | | [Header("Preview Mode")] [SerializeField] |
| | 26 | | internal Image previewBackgroundImage; |
| | 27 | |
|
| | 28 | | [SerializeField] internal Color previewBackgroundColor; |
| | 29 | | [SerializeField] internal Color previewFontColor; |
| | 30 | |
|
| | 31 | | private float hoverPanelTimer; |
| | 32 | | private float hoverGotoPanelTimer; |
| | 33 | | private bool isOverCoordinates; |
| | 34 | | private ParcelCoordinates currentCoordinates; |
| | 35 | | private ChatEntryModel model; |
| | 36 | |
|
| | 37 | | private Color originalBackgroundColor; |
| | 38 | | private Color originalFontColor; |
| 27 | 39 | | private readonly CancellationTokenSource populationTaskCancellationTokenSource = new CancellationTokenSource(); |
| | 40 | |
|
| 0 | 41 | | public override ChatEntryModel Model => model; |
| | 42 | |
|
| | 43 | | public event Action<string> OnPress; |
| | 44 | | public event Action<DefaultChatEntry> OnPressRightButton; |
| | 45 | | public event Action<DefaultChatEntry> OnTriggerHover; |
| | 46 | | public event Action<DefaultChatEntry, ParcelCoordinates> OnTriggerHoverGoto; |
| | 47 | | public event Action OnCancelHover; |
| | 48 | | public event Action OnCancelGotoHover; |
| | 49 | |
|
| | 50 | | private void Awake() |
| | 51 | | { |
| 12 | 52 | | originalBackgroundColor = previewBackgroundImage.color; |
| 12 | 53 | | originalFontColor = body.color; |
| 12 | 54 | | } |
| | 55 | |
|
| | 56 | | public override void Populate(ChatEntryModel chatEntryModel) => |
| 12 | 57 | | PopulateTask(chatEntryModel, populationTaskCancellationTokenSource.Token).Forget(); |
| | 58 | |
|
| | 59 | | private async UniTask PopulateTask(ChatEntryModel chatEntryModel, CancellationToken cancellationToken) |
| | 60 | | { |
| 12 | 61 | | model = chatEntryModel; |
| | 62 | |
|
| 12 | 63 | | chatEntryModel.bodyText = RemoveTabs(chatEntryModel.bodyText); |
| 12 | 64 | | var userString = GetUserString(chatEntryModel); |
| | 65 | |
|
| | 66 | | // Due to a TMPro bug in Unity 2020 LTS we have to wait several frames before setting the body.text to avoid a |
| | 67 | | // client crash. More info at https://github.com/decentraland/unity-renderer/pull/2345#issuecomment-1155753538 |
| | 68 | | // TODO: Remove hack in a newer Unity/TMPro version |
| 36 | 69 | | await UniTask.NextFrame(cancellationToken); |
| 36 | 70 | | await UniTask.NextFrame(cancellationToken); |
| 36 | 71 | | await UniTask.NextFrame(cancellationToken); |
| | 72 | |
|
| 12 | 73 | | if (!string.IsNullOrEmpty(userString) && showUserName) |
| 9 | 74 | | body.text = $"{userString} {chatEntryModel.bodyText}"; |
| | 75 | | else |
| 3 | 76 | | body.text = chatEntryModel.bodyText; |
| | 77 | |
|
| 12 | 78 | | body.text = GetCoordinatesLink(body.text); |
| | 79 | |
|
| 12 | 80 | | messageLocalDateTime = UnixTimeStampToLocalDateTime(chatEntryModel.timestamp).ToString(); |
| | 81 | |
|
| 12 | 82 | | (transform as RectTransform).ForceUpdateLayout(); |
| | 83 | |
|
| 12 | 84 | | PlaySfx(chatEntryModel); |
| 12 | 85 | | } |
| | 86 | |
|
| | 87 | | private string GetUserString(ChatEntryModel chatEntryModel) |
| | 88 | | { |
| 12 | 89 | | var userString = GetDefaultSenderString(chatEntryModel.senderName); |
| 12 | 90 | | switch (chatEntryModel.messageType) |
| | 91 | | { |
| | 92 | | case ChatMessage.Type.PUBLIC: |
| 2 | 93 | | userString = chatEntryModel.subType switch |
| | 94 | | { |
| | 95 | |
|
| | 96 | | ChatEntryModel.SubType.RECEIVED => userString, |
| | 97 | | ChatEntryModel.SubType.SENT => $"<b>You:</b>", |
| | 98 | | _ => userString |
| | 99 | | }; |
| 2 | 100 | | break; |
| | 101 | | case ChatMessage.Type.PRIVATE: |
| 9 | 102 | | userString = chatEntryModel.subType switch |
| | 103 | | { |
| | 104 | |
|
| | 105 | | ChatEntryModel.SubType.RECEIVED => $"<b><color=#5EBD3D>From {chatEntryModel.senderName}:</color></b> |
| | 106 | | ChatEntryModel.SubType.SENT => $"<b>To {chatEntryModel.recipientName}:</b>", |
| | 107 | | _ => userString |
| | 108 | | }; |
| | 109 | | break; |
| | 110 | | } |
| | 111 | |
|
| 12 | 112 | | return userString; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | private string GetCoordinatesLink(string body) |
| | 116 | | { |
| 12 | 117 | | if (!CoordinateUtils.HasValidTextCoordinates(body)) |
| 9 | 118 | | return body; |
| 3 | 119 | | var textCoordinates = CoordinateUtils.GetTextCoordinates(body); |
| | 120 | |
|
| 16 | 121 | | for (var i = 0; i < textCoordinates.Count; i++) |
| | 122 | | { |
| | 123 | | // TODO: the preload should not be here |
| 5 | 124 | | PreloadSceneMetadata(CoordinateUtils.ParseCoordinatesString(textCoordinates[i])); |
| | 125 | |
|
| 5 | 126 | | body = body.Replace(textCoordinates[i], |
| | 127 | | $"</noparse><link={textCoordinates[i]}><color=#4886E3><u>{textCoordinates[i]}</u></color></link><noparse |
| | 128 | | } |
| | 129 | |
|
| 3 | 130 | | return body; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | private void PlaySfx(ChatEntryModel chatEntryModel) |
| | 134 | | { |
| 12 | 135 | | if (HUDAudioHandler.i == null) |
| 12 | 136 | | return; |
| | 137 | |
|
| | 138 | | // Check whether or not this message is new, and chat sounds are enabled in settings |
| 0 | 139 | | if (chatEntryModel.timestamp > HUDAudioHandler.i.chatLastCheckedTimestamp && |
| | 140 | | Settings.i.audioSettings.Data.chatSFXEnabled) |
| | 141 | | { |
| 0 | 142 | | switch (chatEntryModel.messageType) |
| | 143 | | { |
| | 144 | | case ChatMessage.Type.PUBLIC: |
| | 145 | | // Check whether or not the message was sent by the local player |
| 0 | 146 | | if (chatEntryModel.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 147 | | AudioScriptableObjects.chatSend.Play(true); |
| | 148 | | else |
| 0 | 149 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| 0 | 150 | | break; |
| | 151 | | case ChatMessage.Type.PRIVATE: |
| 0 | 152 | | switch (chatEntryModel.subType) |
| | 153 | | { |
| | 154 | | case ChatEntryModel.SubType.RECEIVED: |
| 0 | 155 | | AudioScriptableObjects.chatReceivePrivate.Play(true); |
| 0 | 156 | | break; |
| | 157 | | case ChatEntryModel.SubType.SENT: |
| 0 | 158 | | AudioScriptableObjects.chatSend.Play(true); |
| 0 | 159 | | break; |
| | 160 | | default: |
| | 161 | | break; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | break; |
| | 165 | | case ChatMessage.Type.SYSTEM: |
| 0 | 166 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| | 167 | | break; |
| | 168 | | default: |
| | 169 | | break; |
| | 170 | | } |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | HUDAudioHandler.i.RefreshChatLastCheckedTimestamp(); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | private void PreloadSceneMetadata(ParcelCoordinates parcelCoordinates) |
| | 177 | | { |
| 5 | 178 | | if (MinimapMetadata.GetMetadata().GetSceneInfo(parcelCoordinates.x, parcelCoordinates.y) == null) |
| 5 | 179 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(parcelCoordinates.x, parcelCoordinates.y), 2); |
| 5 | 180 | | } |
| | 181 | |
|
| | 182 | | public void OnPointerClick(PointerEventData pointerEventData) |
| | 183 | | { |
| 0 | 184 | | if (pointerEventData.button == PointerEventData.InputButton.Left) |
| | 185 | | { |
| 0 | 186 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, DataStore.i.camera.h |
| 0 | 187 | | if (linkIndex != -1) |
| | 188 | | { |
| 0 | 189 | | DataStore.i.HUDs.gotoPanelVisible.Set(true); |
| 0 | 190 | | TMP_LinkInfo linkInfo = body.textInfo.linkInfo[linkIndex]; |
| 0 | 191 | | ParcelCoordinates parcelCoordinate = |
| | 192 | | CoordinateUtils.ParseCoordinatesString(linkInfo.GetLinkID().ToString()); |
| 0 | 193 | | DataStore.i.HUDs.gotoPanelCoordinates.Set(parcelCoordinate); |
| | 194 | | } |
| | 195 | |
|
| 0 | 196 | | if (Model.messageType != ChatMessage.Type.PRIVATE) |
| 0 | 197 | | return; |
| | 198 | |
|
| 0 | 199 | | OnPress?.Invoke(Model.otherUserId); |
| 0 | 200 | | } |
| 0 | 201 | | else if (pointerEventData.button == PointerEventData.InputButton.Right) |
| | 202 | | { |
| 0 | 203 | | if ((Model.messageType != ChatMessage.Type.PUBLIC && Model.messageType != ChatMessage.Type.PRIVATE) || |
| | 204 | | Model.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 205 | | return; |
| | 206 | |
|
| 0 | 207 | | OnPressRightButton?.Invoke(this); |
| | 208 | | } |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void OnPointerEnter(PointerEventData pointerEventData) |
| | 212 | | { |
| 0 | 213 | | if (pointerEventData == null) |
| 0 | 214 | | return; |
| | 215 | |
|
| 0 | 216 | | hoverPanelTimer = timeToHoverPanel; |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public void OnPointerExit(PointerEventData pointerEventData) |
| | 220 | | { |
| 12 | 221 | | if (pointerEventData == null) |
| 12 | 222 | | return; |
| | 223 | |
|
| 0 | 224 | | hoverPanelTimer = 0f; |
| 0 | 225 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, DataStore.i.camera.hudsC |
| 0 | 226 | | if (linkIndex == -1) |
| | 227 | | { |
| 0 | 228 | | isOverCoordinates = false; |
| 0 | 229 | | hoverGotoPanelTimer = 0; |
| 0 | 230 | | OnCancelGotoHover?.Invoke(); |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | OnCancelHover?.Invoke(); |
| 0 | 234 | | } |
| | 235 | |
|
| 24 | 236 | | private void OnDisable() { OnPointerExit(null); } |
| | 237 | |
|
| 24 | 238 | | private void OnDestroy() { populationTaskCancellationTokenSource.Cancel(); } |
| | 239 | |
|
| | 240 | | public override void SetFadeout(bool enabled) |
| | 241 | | { |
| 0 | 242 | | if (!enabled) |
| | 243 | | { |
| 0 | 244 | | group.alpha = 1; |
| 0 | 245 | | fadeEnabled = false; |
| 0 | 246 | | return; |
| | 247 | | } |
| | 248 | |
|
| 0 | 249 | | fadeEnabled = true; |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | public override void DeactivatePreview() |
| | 253 | | { |
| 0 | 254 | | if (!gameObject.activeInHierarchy) |
| | 255 | | { |
| 0 | 256 | | previewBackgroundImage.color = originalBackgroundColor; |
| 0 | 257 | | body.color = originalFontColor; |
| 0 | 258 | | return; |
| | 259 | | } |
| | 260 | |
|
| 0 | 261 | | if (previewInterpolationRoutine != null) |
| 0 | 262 | | StopCoroutine(previewInterpolationRoutine); |
| | 263 | |
|
| 0 | 264 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 265 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 266 | |
|
| 0 | 267 | | group.alpha = 1; |
| 0 | 268 | | previewInterpolationRoutine = |
| | 269 | | StartCoroutine(InterpolatePreviewColor(originalBackgroundColor, originalFontColor, 0.5f)); |
| 0 | 270 | | } |
| | 271 | |
|
| | 272 | | public override void FadeOut() |
| | 273 | | { |
| 0 | 274 | | if (!gameObject.activeInHierarchy) |
| | 275 | | { |
| 0 | 276 | | group.alpha = 0; |
| 0 | 277 | | return; |
| | 278 | | } |
| | 279 | |
|
| 0 | 280 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 281 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 282 | |
|
| 0 | 283 | | previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(0, 0.5f)); |
| 0 | 284 | | } |
| | 285 | |
|
| | 286 | | public override void ActivatePreview() |
| | 287 | | { |
| 0 | 288 | | if (!gameObject.activeInHierarchy) |
| | 289 | | { |
| 0 | 290 | | ActivatePreviewInstantly(); |
| 0 | 291 | | return; |
| | 292 | | } |
| | 293 | |
|
| 0 | 294 | | if (previewInterpolationRoutine != null) |
| 0 | 295 | | StopCoroutine(previewInterpolationRoutine); |
| | 296 | |
|
| 0 | 297 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 298 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 299 | |
|
| 0 | 300 | | previewInterpolationRoutine = |
| | 301 | | StartCoroutine(InterpolatePreviewColor(previewBackgroundColor, previewFontColor, 0.5f)); |
| | 302 | |
|
| 0 | 303 | | previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(1, 0.5f)); |
| 0 | 304 | | } |
| | 305 | |
|
| | 306 | | public override void ActivatePreviewInstantly() |
| | 307 | | { |
| 0 | 308 | | if (!gameObject.activeInHierarchy) |
| 0 | 309 | | return; |
| | 310 | |
|
| 0 | 311 | | if (previewInterpolationRoutine != null) |
| 0 | 312 | | StopCoroutine(previewInterpolationRoutine); |
| | 313 | |
|
| 0 | 314 | | previewBackgroundImage.color = previewBackgroundColor; |
| 0 | 315 | | body.color = previewFontColor; |
| 0 | 316 | | group.alpha = 1; |
| | 317 | |
|
| 0 | 318 | | if (previewInterpolationAlphaRoutine != null) |
| 0 | 319 | | StopCoroutine(previewInterpolationAlphaRoutine); |
| | 320 | |
|
| 0 | 321 | | previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(1, 0.5f)); |
| 0 | 322 | | } |
| | 323 | |
|
| | 324 | | public override void DeactivatePreviewInstantly() |
| | 325 | | { |
| 0 | 326 | | if (previewInterpolationRoutine != null) |
| 0 | 327 | | StopCoroutine(previewInterpolationRoutine); |
| | 328 | |
|
| 0 | 329 | | previewBackgroundImage.color = originalBackgroundColor; |
| 0 | 330 | | body.color = originalFontColor; |
| 0 | 331 | | } |
| | 332 | |
|
| | 333 | | public void DockContextMenu(RectTransform panel) |
| | 334 | | { |
| 0 | 335 | | panel.pivot = contextMenuPositionReference.pivot; |
| 0 | 336 | | panel.position = contextMenuPositionReference.position; |
| 0 | 337 | | } |
| | 338 | |
|
| | 339 | | public void DockHoverPanel(RectTransform panel) |
| | 340 | | { |
| 0 | 341 | | panel.pivot = hoverPanelPositionReference.pivot; |
| 0 | 342 | | panel.position = hoverPanelPositionReference.position; |
| 0 | 343 | | } |
| | 344 | |
|
| | 345 | | private void Update() |
| | 346 | | { |
| 48 | 347 | | CheckHoverCoordinates(); |
| 48 | 348 | | ProcessHoverPanelTimer(); |
| 48 | 349 | | ProcessHoverGotoPanelTimer(); |
| 48 | 350 | | } |
| | 351 | |
|
| | 352 | | private void CheckHoverCoordinates() |
| | 353 | | { |
| 48 | 354 | | if (isOverCoordinates) |
| 0 | 355 | | return; |
| | 356 | |
|
| 48 | 357 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, Input.mousePosition, DataStore.i.camera.hudsCamera. |
| | 358 | |
|
| 48 | 359 | | if (linkIndex == -1) |
| 48 | 360 | | return; |
| | 361 | |
|
| 0 | 362 | | isOverCoordinates = true; |
| 0 | 363 | | TMP_LinkInfo linkInfo = body.textInfo.linkInfo[linkIndex]; |
| 0 | 364 | | currentCoordinates = CoordinateUtils.ParseCoordinatesString(linkInfo.GetLinkID().ToString()); |
| 0 | 365 | | hoverGotoPanelTimer = timeToHoverGotoPanel; |
| 0 | 366 | | OnCancelHover?.Invoke(); |
| 0 | 367 | | } |
| | 368 | |
|
| | 369 | | private void ProcessHoverPanelTimer() |
| | 370 | | { |
| 48 | 371 | | if (hoverPanelTimer <= 0f || isOverCoordinates) |
| 48 | 372 | | return; |
| | 373 | |
|
| 0 | 374 | | hoverPanelTimer -= Time.deltaTime; |
| 0 | 375 | | if (hoverPanelTimer <= 0f) |
| | 376 | | { |
| 0 | 377 | | hoverPanelTimer = 0f; |
| 0 | 378 | | OnTriggerHover?.Invoke(this); |
| | 379 | | } |
| 0 | 380 | | } |
| | 381 | |
|
| | 382 | | private void ProcessHoverGotoPanelTimer() |
| | 383 | | { |
| 48 | 384 | | if (hoverGotoPanelTimer <= 0f || !isOverCoordinates) |
| 48 | 385 | | return; |
| | 386 | |
|
| 0 | 387 | | hoverGotoPanelTimer -= Time.deltaTime; |
| 0 | 388 | | if (hoverGotoPanelTimer <= 0f) |
| | 389 | | { |
| 0 | 390 | | hoverGotoPanelTimer = 0f; |
| 0 | 391 | | OnTriggerHoverGoto?.Invoke(this, currentCoordinates); |
| | 392 | | } |
| 0 | 393 | | } |
| | 394 | |
|
| | 395 | | private string RemoveTabs(string text) |
| | 396 | | { |
| 12 | 397 | | if (string.IsNullOrEmpty(text)) |
| 0 | 398 | | return ""; |
| | 399 | |
|
| | 400 | | //NOTE(Brian): ContentSizeFitter doesn't fare well with tabs, so i'm replacing these |
| | 401 | | // with spaces. |
| 12 | 402 | | return text.Replace("\t", " "); |
| | 403 | | } |
| | 404 | |
|
| | 405 | | private static string GetDefaultSenderString(string sender) |
| | 406 | | { |
| 12 | 407 | | if (!string.IsNullOrEmpty(sender)) |
| 12 | 408 | | return $"<b>{sender}:</b>"; |
| 0 | 409 | | return ""; |
| | 410 | | } |
| | 411 | |
|
| | 412 | | private static DateTime UnixTimeStampToLocalDateTime(ulong unixTimeStampMilliseconds) |
| | 413 | | { |
| | 414 | | // TODO see if we can simplify with 'DateTimeOffset.FromUnixTimeMilliseconds' |
| 12 | 415 | | DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); |
| 12 | 416 | | dtDateTime = dtDateTime.AddMilliseconds(unixTimeStampMilliseconds).ToLocalTime(); |
| 12 | 417 | | return dtDateTime; |
| | 418 | | } |
| | 419 | |
|
| | 420 | | private IEnumerator InterpolatePreviewColor(Color backgroundColor, Color fontColor, float duration) |
| | 421 | | { |
| 0 | 422 | | var t = 0f; |
| | 423 | |
|
| 0 | 424 | | while (t < duration) |
| | 425 | | { |
| 0 | 426 | | t += Time.deltaTime; |
| | 427 | |
|
| 0 | 428 | | previewBackgroundImage.color = Color.Lerp(previewBackgroundImage.color, backgroundColor, t / duration); |
| 0 | 429 | | body.color = Color.Lerp(body.color, fontColor, t / duration); |
| | 430 | |
|
| 0 | 431 | | yield return null; |
| | 432 | | } |
| | 433 | |
|
| 0 | 434 | | previewBackgroundImage.color = backgroundColor; |
| 0 | 435 | | body.color = fontColor; |
| 0 | 436 | | } |
| | 437 | |
|
| | 438 | | } |