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