| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using DCL.SettingsCommon; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Events; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | | using UnityEngine.Serialization; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Text.RegularExpressions; |
| | 12 | | using DCL; |
| | 13 | |
|
| | 14 | | public class ChatEntry : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler |
| | 15 | | { |
| | 16 | | private const string COORDINATES_COLOR_PRIVATE = "#4886E3"; |
| | 17 | | private const string COORDINATES_COLOR_PUBLIC = "#62C6FF"; |
| | 18 | |
|
| | 19 | | public struct Model |
| | 20 | | { |
| | 21 | | public enum SubType |
| | 22 | | { |
| | 23 | | NONE, |
| | 24 | | PRIVATE_FROM, |
| | 25 | | PRIVATE_TO |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public ChatMessage.Type messageType; |
| | 29 | | public string bodyText; |
| | 30 | | public string senderId; |
| | 31 | | public string senderName; |
| | 32 | | public string recipientName; |
| | 33 | | public string otherUserId; |
| | 34 | | public ulong timestamp; |
| | 35 | |
|
| | 36 | | public SubType subType; |
| | 37 | | } |
| | 38 | |
|
| 41 | 39 | | [SerializeField] internal float timeToFade = 10f; |
| 41 | 40 | | [SerializeField] internal float fadeDuration = 5f; |
| | 41 | |
|
| | 42 | | [SerializeField] internal TextMeshProUGUI username; |
| | 43 | | [SerializeField] internal TextMeshProUGUI body; |
| | 44 | |
|
| 41 | 45 | | [SerializeField] internal Color worldMessageColor = Color.white; |
| | 46 | |
|
| | 47 | | [FormerlySerializedAs("privateMessageColor")] |
| 41 | 48 | | [SerializeField] internal Color privateToMessageColor = Color.white; |
| | 49 | |
|
| | 50 | | [FormerlySerializedAs("privateMessageColor")] |
| 41 | 51 | | [SerializeField] internal Color privateFromMessageColor = Color.white; |
| | 52 | |
|
| 41 | 53 | | [SerializeField] internal Color systemColor = Color.white; |
| 41 | 54 | | [SerializeField] internal Color playerNameColor = Color.yellow; |
| 41 | 55 | | [SerializeField] internal Color nonPlayerNameColor = Color.white; |
| | 56 | | [SerializeField] CanvasGroup group; |
| 41 | 57 | | [SerializeField] internal float timeToHoverPanel = 1f; |
| 41 | 58 | | [SerializeField] internal float timeToHoverGotoPanel = 1f; |
| | 59 | |
|
| | 60 | | [NonSerialized] public string messageLocalDateTime; |
| | 61 | |
|
| | 62 | | bool fadeEnabled = false; |
| | 63 | | double fadeoutStartTime; |
| | 64 | | float hoverPanelTimer = 0; |
| | 65 | | float hoverGotoPanelTimer = 0; |
| | 66 | | bool isOverCoordinates = false; |
| | 67 | | ParcelCoordinates currentCoordinates; |
| | 68 | |
|
| | 69 | | public RectTransform hoverPanelPositionReference; |
| | 70 | | public RectTransform contextMenuPositionReference; |
| | 71 | |
|
| 0 | 72 | | public Model model { get; private set; } |
| | 73 | |
|
| | 74 | | public event UnityAction<string> OnPress; |
| | 75 | | public event UnityAction<ChatEntry> OnPressRightButton; |
| | 76 | | public event UnityAction<ChatEntry> OnTriggerHover; |
| | 77 | | public event UnityAction<ChatEntry, ParcelCoordinates> OnTriggerHoverGoto; |
| | 78 | | public event UnityAction OnCancelHover; |
| | 79 | | public event UnityAction OnCancelGotoHover; |
| | 80 | |
|
| 41 | 81 | | private List<string> textCoords = new List<string>(); |
| | 82 | |
|
| | 83 | | public void Populate(Model chatEntryModel) |
| | 84 | | { |
| 42 | 85 | | this.model = chatEntryModel; |
| | 86 | |
|
| 42 | 87 | | string userString = GetDefaultSenderString(chatEntryModel.senderName); |
| | 88 | |
|
| 42 | 89 | | if (chatEntryModel.subType == Model.SubType.PRIVATE_FROM) |
| | 90 | | { |
| 6 | 91 | | userString = $"<b>From {chatEntryModel.senderName}:</b>"; |
| 6 | 92 | | } |
| 36 | 93 | | else if (chatEntryModel.subType == Model.SubType.PRIVATE_TO) |
| | 94 | | { |
| 2 | 95 | | userString = $"<b>To {chatEntryModel.recipientName}:</b>"; |
| | 96 | | } |
| | 97 | |
|
| 42 | 98 | | switch (chatEntryModel.messageType) |
| | 99 | | { |
| | 100 | | case ChatMessage.Type.PUBLIC: |
| 27 | 101 | | body.color = worldMessageColor; |
| | 102 | |
|
| 27 | 103 | | if (username != null) |
| 27 | 104 | | username.color = chatEntryModel.senderName == UserProfile.GetOwnUserProfile().userName ? playerNameC |
| 27 | 105 | | break; |
| | 106 | | case ChatMessage.Type.PRIVATE: |
| 14 | 107 | | body.color = worldMessageColor; |
| | 108 | |
|
| 14 | 109 | | if (username != null) |
| | 110 | | { |
| 7 | 111 | | if (model.subType == Model.SubType.PRIVATE_TO) |
| 2 | 112 | | username.color = privateToMessageColor; |
| | 113 | | else |
| 5 | 114 | | username.color = privateFromMessageColor; |
| | 115 | | } |
| | 116 | |
|
| 5 | 117 | | break; |
| | 118 | | case ChatMessage.Type.SYSTEM: |
| 1 | 119 | | body.color = systemColor; |
| | 120 | |
|
| 1 | 121 | | if (username != null) |
| 1 | 122 | | username.color = systemColor; |
| | 123 | | break; |
| | 124 | | } |
| | 125 | |
|
| 42 | 126 | | chatEntryModel.bodyText = RemoveTabs(chatEntryModel.bodyText); |
| | 127 | |
|
| 42 | 128 | | if (username != null && !string.IsNullOrEmpty(userString)) |
| | 129 | | { |
| 35 | 130 | | if (username != null) |
| 35 | 131 | | username.text = userString; |
| | 132 | |
|
| 35 | 133 | | body.text = $"{userString} {chatEntryModel.bodyText}"; |
| 35 | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 7 | 137 | | if (username != null) |
| 0 | 138 | | username.text = ""; |
| | 139 | |
|
| 7 | 140 | | body.text = $"{chatEntryModel.bodyText}"; |
| | 141 | | } |
| | 142 | |
|
| 42 | 143 | | if (CoordinateUtils.HasValidTextCoordinates(body.text)) { |
| 0 | 144 | | List<string> textCoordinates = CoordinateUtils.GetTextCoordinates(body.text); |
| 0 | 145 | | for (int i = 0; i < textCoordinates.Count; i++) |
| | 146 | | { |
| 0 | 147 | | PreloadSceneMetadata(CoordinateUtils.ParseCoordinatesString(textCoordinates[i])); |
| | 148 | | string coordinatesColor; |
| 0 | 149 | | if (chatEntryModel.messageType == ChatMessage.Type.PRIVATE) |
| 0 | 150 | | coordinatesColor = COORDINATES_COLOR_PRIVATE; |
| | 151 | | else |
| 0 | 152 | | coordinatesColor = COORDINATES_COLOR_PUBLIC; |
| | 153 | |
|
| 0 | 154 | | body.text = body.text.Replace(textCoordinates[i], $"<link={textCoordinates[i]}><color={coordinatesColor} |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| 42 | 158 | | messageLocalDateTime = UnixTimeStampToLocalDateTime(chatEntryModel.timestamp).ToString(); |
| | 159 | |
|
| 42 | 160 | | Utils.ForceUpdateLayout(transform as RectTransform); |
| | 161 | |
|
| 42 | 162 | | if (fadeEnabled) |
| 0 | 163 | | group.alpha = 0; |
| | 164 | |
|
| 42 | 165 | | if (HUDAudioHandler.i != null) |
| | 166 | | { |
| | 167 | | // Check whether or not this message is new, and chat sounds are enabled in settings |
| 0 | 168 | | if (chatEntryModel.timestamp > HUDAudioHandler.i.chatLastCheckedTimestamp && Settings.i.audioSettings.Data.c |
| | 169 | | { |
| 0 | 170 | | switch (chatEntryModel.messageType) |
| | 171 | | { |
| | 172 | | case ChatMessage.Type.PUBLIC: |
| | 173 | | // Check whether or not the message was sent by the local player |
| 0 | 174 | | if (chatEntryModel.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 175 | | AudioScriptableObjects.chatSend.Play(true); |
| | 176 | | else |
| 0 | 177 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| 0 | 178 | | break; |
| | 179 | | case ChatMessage.Type.PRIVATE: |
| 0 | 180 | | switch (chatEntryModel.subType) |
| | 181 | | { |
| | 182 | | case Model.SubType.PRIVATE_FROM: |
| 0 | 183 | | AudioScriptableObjects.chatReceivePrivate.Play(true); |
| 0 | 184 | | break; |
| | 185 | | case Model.SubType.PRIVATE_TO: |
| 0 | 186 | | AudioScriptableObjects.chatSend.Play(true); |
| 0 | 187 | | break; |
| | 188 | | default: |
| | 189 | | break; |
| | 190 | | } |
| | 191 | | break; |
| | 192 | | case ChatMessage.Type.SYSTEM: |
| 0 | 193 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| | 194 | | break; |
| | 195 | | default: |
| | 196 | | break; |
| | 197 | | } |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | HUDAudioHandler.i.RefreshChatLastCheckedTimestamp(); |
| | 201 | | } |
| 42 | 202 | | } |
| | 203 | |
|
| | 204 | | private void PreloadSceneMetadata(ParcelCoordinates parcelCoordinates) |
| | 205 | | { |
| 0 | 206 | | if (MinimapMetadata.GetMetadata().GetSceneInfo(parcelCoordinates.x, parcelCoordinates.y) == null) |
| 0 | 207 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(parcelCoordinates.x, parcelCoordinates.y), 2); |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | public void OnPointerClick(PointerEventData pointerEventData) |
| | 211 | | { |
| 0 | 212 | | if (pointerEventData.button == PointerEventData.InputButton.Left) |
| | 213 | | { |
| 0 | 214 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, null); |
| 0 | 215 | | if (linkIndex != -1) |
| | 216 | | { |
| 0 | 217 | | DataStore.i.HUDs.gotoPanelVisible.Set(true); |
| 0 | 218 | | TMP_LinkInfo linkInfo = body.textInfo.linkInfo[linkIndex]; |
| 0 | 219 | | ParcelCoordinates parcelCoordinate = CoordinateUtils.ParseCoordinatesString(linkInfo.GetLinkID().ToStrin |
| 0 | 220 | | DataStore.i.HUDs.gotoPanelCoordinates.Set(parcelCoordinate); |
| | 221 | | } |
| | 222 | |
|
| 0 | 223 | | if (model.messageType != ChatMessage.Type.PRIVATE) |
| 0 | 224 | | return; |
| | 225 | |
|
| 0 | 226 | | OnPress?.Invoke(model.otherUserId); |
| 0 | 227 | | } |
| 0 | 228 | | else if (pointerEventData.button == PointerEventData.InputButton.Right) |
| | 229 | | { |
| 0 | 230 | | if ((model.messageType != ChatMessage.Type.PUBLIC && model.messageType != ChatMessage.Type.PRIVATE) || |
| | 231 | | model.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 232 | | return; |
| | 233 | |
|
| 0 | 234 | | OnPressRightButton?.Invoke(this); |
| | 235 | | } |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | public void OnPointerEnter(PointerEventData pointerEventData) |
| | 239 | | { |
| 0 | 240 | | if (pointerEventData == null) |
| 0 | 241 | | return; |
| | 242 | |
|
| 0 | 243 | | hoverPanelTimer = timeToHoverPanel; |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | public void OnPointerExit(PointerEventData pointerEventData) |
| | 247 | | { |
| 39 | 248 | | if (pointerEventData == null) |
| 39 | 249 | | return; |
| | 250 | |
|
| 0 | 251 | | hoverPanelTimer = 0f; |
| 0 | 252 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, pointerEventData.position, null); |
| 0 | 253 | | if (linkIndex == -1) |
| | 254 | | { |
| 0 | 255 | | isOverCoordinates = false; |
| 0 | 256 | | hoverGotoPanelTimer = 0; |
| 0 | 257 | | OnCancelGotoHover?.Invoke(); |
| | 258 | | } |
| 0 | 259 | | OnCancelHover?.Invoke(); |
| 0 | 260 | | } |
| | 261 | |
|
| 78 | 262 | | void OnDisable() { OnPointerExit(null); } |
| | 263 | |
|
| | 264 | | public void SetFadeout(bool enabled) |
| | 265 | | { |
| 40 | 266 | | if (!enabled) |
| | 267 | | { |
| 40 | 268 | | group.alpha = 1; |
| 40 | 269 | | group.blocksRaycasts = true; |
| 40 | 270 | | group.interactable = true; |
| 40 | 271 | | fadeEnabled = false; |
| 40 | 272 | | return; |
| | 273 | | } |
| | 274 | |
|
| 0 | 275 | | fadeoutStartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| 0 | 276 | | fadeEnabled = true; |
| 0 | 277 | | group.blocksRaycasts = false; |
| 0 | 278 | | group.interactable = false; |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | void Update() |
| | 282 | | { |
| 3 | 283 | | Fade(); |
| 3 | 284 | | CheckHoverCoordinates(); |
| 3 | 285 | | ProcessHoverPanelTimer(); |
| 3 | 286 | | ProcessHoverGotoPanelTimer(); |
| 3 | 287 | | } |
| | 288 | |
|
| | 289 | | private void CheckHoverCoordinates() |
| | 290 | | { |
| 3 | 291 | | if (isOverCoordinates) |
| 0 | 292 | | return; |
| | 293 | |
|
| 3 | 294 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(body, Input.mousePosition, null); |
| | 295 | |
|
| 3 | 296 | | if (linkIndex == -1) |
| 3 | 297 | | return; |
| | 298 | |
|
| 0 | 299 | | isOverCoordinates = true; |
| 0 | 300 | | TMP_LinkInfo linkInfo = body.textInfo.linkInfo[linkIndex]; |
| 0 | 301 | | currentCoordinates = CoordinateUtils.ParseCoordinatesString(linkInfo.GetLinkID().ToString()); |
| 0 | 302 | | hoverGotoPanelTimer = timeToHoverGotoPanel; |
| 0 | 303 | | OnCancelHover?.Invoke(); |
| 0 | 304 | | } |
| | 305 | |
|
| | 306 | | void Fade() |
| | 307 | | { |
| 3 | 308 | | if (!fadeEnabled) |
| 3 | 309 | | return; |
| | 310 | |
|
| | 311 | | //NOTE(Brian): Small offset using normalized Y so we keep the cascade effect |
| 0 | 312 | | double yOffset = (transform as RectTransform).anchoredPosition.y / (double)Screen.height * 2.0; |
| | 313 | |
|
| 0 | 314 | | double fadeTime = Math.Max(model.timestamp / 1000.0, fadeoutStartTime) + timeToFade - yOffset; |
| 0 | 315 | | double currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| | 316 | |
|
| 0 | 317 | | if (currentTime > fadeTime) |
| | 318 | | { |
| 0 | 319 | | double timeSinceFadeTime = currentTime - fadeTime; |
| 0 | 320 | | group.alpha = Mathf.Clamp01(1 - (float)(timeSinceFadeTime / fadeDuration)); |
| 0 | 321 | | } |
| | 322 | | else |
| | 323 | | { |
| 0 | 324 | | group.alpha += (1 - group.alpha) * 0.05f; |
| | 325 | | } |
| 0 | 326 | | } |
| | 327 | |
|
| | 328 | | void ProcessHoverPanelTimer() |
| | 329 | | { |
| 3 | 330 | | if (hoverPanelTimer <= 0f || isOverCoordinates) |
| 3 | 331 | | return; |
| | 332 | |
|
| 0 | 333 | | hoverPanelTimer -= Time.deltaTime; |
| 0 | 334 | | if (hoverPanelTimer <= 0f) |
| | 335 | | { |
| 0 | 336 | | hoverPanelTimer = 0f; |
| | 337 | |
|
| 0 | 338 | | OnTriggerHover?.Invoke(this); |
| | 339 | | } |
| 0 | 340 | | } |
| | 341 | |
|
| | 342 | | void ProcessHoverGotoPanelTimer() |
| | 343 | | { |
| 3 | 344 | | if (hoverGotoPanelTimer <= 0f || !isOverCoordinates) |
| 3 | 345 | | return; |
| | 346 | |
|
| 0 | 347 | | hoverGotoPanelTimer -= Time.deltaTime; |
| 0 | 348 | | if (hoverGotoPanelTimer <= 0f) |
| | 349 | | { |
| 0 | 350 | | hoverGotoPanelTimer = 0f; |
| | 351 | |
|
| 0 | 352 | | OnTriggerHoverGoto?.Invoke(this, currentCoordinates); |
| | 353 | | } |
| 0 | 354 | | } |
| | 355 | |
|
| | 356 | | string RemoveTabs(string text) |
| | 357 | | { |
| 42 | 358 | | if (string.IsNullOrEmpty(text)) |
| 0 | 359 | | return ""; |
| | 360 | |
|
| | 361 | | //NOTE(Brian): ContentSizeFitter doesn't fare well with tabs, so i'm replacing these |
| | 362 | | // with spaces. |
| 42 | 363 | | return text.Replace("\t", " "); |
| | 364 | | } |
| | 365 | |
|
| | 366 | | string GetDefaultSenderString(string sender) |
| | 367 | | { |
| 42 | 368 | | if (!string.IsNullOrEmpty(sender)) |
| 42 | 369 | | return $"<b>{sender}:</b>"; |
| | 370 | |
|
| 0 | 371 | | return ""; |
| | 372 | | } |
| | 373 | |
|
| | 374 | | DateTime UnixTimeStampToLocalDateTime(ulong unixTimeStampMilliseconds) |
| | 375 | | { |
| | 376 | | // TODO see if we can simplify with 'DateTimeOffset.FromUnixTimeMilliseconds' |
| 42 | 377 | | System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); |
| 42 | 378 | | dtDateTime = dtDateTime.AddMilliseconds(unixTimeStampMilliseconds).ToLocalTime(); |
| | 379 | |
|
| 42 | 380 | | return dtDateTime; |
| | 381 | | } |
| | 382 | | } |