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