| | 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 | |
|
| | 11 | | public class ChatEntry : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler |
| | 12 | | { |
| | 13 | | public struct Model |
| | 14 | | { |
| | 15 | | public enum SubType |
| | 16 | | { |
| | 17 | | NONE, |
| | 18 | | PRIVATE_FROM, |
| | 19 | | PRIVATE_TO |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public ChatMessage.Type messageType; |
| | 23 | | public string bodyText; |
| | 24 | | public string senderId; |
| | 25 | | public string senderName; |
| | 26 | | public string recipientName; |
| | 27 | | public string otherUserId; |
| | 28 | | public ulong timestamp; |
| | 29 | |
|
| | 30 | | public SubType subType; |
| | 31 | | } |
| | 32 | |
|
| 37 | 33 | | [SerializeField] internal float timeToFade = 10f; |
| 37 | 34 | | [SerializeField] internal float fadeDuration = 5f; |
| | 35 | |
|
| | 36 | | [SerializeField] internal TextMeshProUGUI username; |
| | 37 | | [SerializeField] internal TextMeshProUGUI body; |
| | 38 | |
|
| 37 | 39 | | [SerializeField] internal Color worldMessageColor = Color.white; |
| | 40 | |
|
| | 41 | | [FormerlySerializedAs("privateMessageColor")] |
| 37 | 42 | | [SerializeField] internal Color privateToMessageColor = Color.white; |
| | 43 | |
|
| | 44 | | [FormerlySerializedAs("privateMessageColor")] |
| 37 | 45 | | [SerializeField] internal Color privateFromMessageColor = Color.white; |
| | 46 | |
|
| 37 | 47 | | [SerializeField] internal Color systemColor = Color.white; |
| 37 | 48 | | [SerializeField] internal Color playerNameColor = Color.yellow; |
| 37 | 49 | | [SerializeField] internal Color nonPlayerNameColor = Color.white; |
| | 50 | | [SerializeField] CanvasGroup group; |
| 37 | 51 | | [SerializeField] internal float timeToHoverPanel = 1f; |
| | 52 | |
|
| | 53 | | [NonSerialized] public string messageLocalDateTime; |
| | 54 | |
|
| | 55 | | bool fadeEnabled = false; |
| | 56 | | double fadeoutStartTime; |
| | 57 | | float hoverPanelTimer = 0; |
| | 58 | |
|
| | 59 | | public RectTransform hoverPanelPositionReference; |
| | 60 | | public RectTransform contextMenuPositionReference; |
| | 61 | |
|
| 0 | 62 | | public Model model { get; private set; } |
| | 63 | |
|
| | 64 | | public event UnityAction<string> OnPress; |
| | 65 | | public event UnityAction<ChatEntry> OnPressRightButton; |
| | 66 | | public event UnityAction<ChatEntry> OnTriggerHover; |
| | 67 | | public event UnityAction OnCancelHover; |
| | 68 | |
|
| | 69 | | public void Populate(Model chatEntryModel) |
| | 70 | | { |
| 32 | 71 | | this.model = chatEntryModel; |
| | 72 | |
|
| 32 | 73 | | string userString = GetDefaultSenderString(chatEntryModel.senderName); |
| | 74 | |
|
| 32 | 75 | | if (chatEntryModel.subType == Model.SubType.PRIVATE_FROM) |
| | 76 | | { |
| 6 | 77 | | userString = $"<b>From {chatEntryModel.senderName}:</b>"; |
| 6 | 78 | | } |
| 26 | 79 | | else if (chatEntryModel.subType == Model.SubType.PRIVATE_TO) |
| | 80 | | { |
| 2 | 81 | | userString = $"<b>To {chatEntryModel.recipientName}:</b>"; |
| | 82 | | } |
| | 83 | |
|
| 32 | 84 | | switch (chatEntryModel.messageType) |
| | 85 | | { |
| | 86 | | case ChatMessage.Type.PUBLIC: |
| 18 | 87 | | body.color = worldMessageColor; |
| | 88 | |
|
| 18 | 89 | | if (username != null) |
| 18 | 90 | | username.color = chatEntryModel.senderName == UserProfile.GetOwnUserProfile().userName ? playerNameC |
| 18 | 91 | | break; |
| | 92 | | case ChatMessage.Type.PRIVATE: |
| 13 | 93 | | body.color = worldMessageColor; |
| | 94 | |
|
| 13 | 95 | | if (username != null) |
| | 96 | | { |
| 6 | 97 | | if (model.subType == Model.SubType.PRIVATE_TO) |
| 2 | 98 | | username.color = privateToMessageColor; |
| | 99 | | else |
| 4 | 100 | | username.color = privateFromMessageColor; |
| | 101 | | } |
| | 102 | |
|
| 4 | 103 | | break; |
| | 104 | | case ChatMessage.Type.SYSTEM: |
| 1 | 105 | | body.color = systemColor; |
| | 106 | |
|
| 1 | 107 | | if (username != null) |
| 1 | 108 | | username.color = systemColor; |
| | 109 | | break; |
| | 110 | | } |
| | 111 | |
|
| 32 | 112 | | chatEntryModel.bodyText = RemoveTabs(chatEntryModel.bodyText); |
| | 113 | |
|
| 32 | 114 | | if (username != null && !string.IsNullOrEmpty(userString)) |
| | 115 | | { |
| 25 | 116 | | if (username != null) |
| 25 | 117 | | username.text = userString; |
| | 118 | |
|
| 25 | 119 | | body.text = $"{userString} {chatEntryModel.bodyText}"; |
| 25 | 120 | | } |
| | 121 | | else |
| | 122 | | { |
| 7 | 123 | | if (username != null) |
| 0 | 124 | | username.text = ""; |
| | 125 | |
|
| 7 | 126 | | body.text = $"{chatEntryModel.bodyText}"; |
| | 127 | | } |
| | 128 | |
|
| 32 | 129 | | messageLocalDateTime = UnixTimeStampToLocalDateTime(chatEntryModel.timestamp).ToString(); |
| | 130 | |
|
| 32 | 131 | | Utils.ForceUpdateLayout(transform as RectTransform); |
| | 132 | |
|
| 32 | 133 | | if (fadeEnabled) |
| 0 | 134 | | group.alpha = 0; |
| | 135 | |
|
| 32 | 136 | | if (HUDAudioHandler.i != null) |
| | 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 && Settings.i.audioSettings.Data.c |
| | 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 Model.SubType.PRIVATE_FROM: |
| 0 | 154 | | AudioScriptableObjects.chatReceivePrivate.Play(true); |
| 0 | 155 | | break; |
| | 156 | | case Model.SubType.PRIVATE_TO: |
| 0 | 157 | | AudioScriptableObjects.chatSend.Play(true); |
| 0 | 158 | | break; |
| | 159 | | default: |
| | 160 | | break; |
| | 161 | | } |
| | 162 | | break; |
| | 163 | | case ChatMessage.Type.SYSTEM: |
| 0 | 164 | | AudioScriptableObjects.chatReceiveGlobal.Play(true); |
| | 165 | | break; |
| | 166 | | default: |
| | 167 | | break; |
| | 168 | | } |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | HUDAudioHandler.i.RefreshChatLastCheckedTimestamp(); |
| | 172 | | } |
| 32 | 173 | | } |
| | 174 | |
|
| | 175 | | public void OnPointerClick(PointerEventData pointerEventData) |
| | 176 | | { |
| 0 | 177 | | if (pointerEventData.button == PointerEventData.InputButton.Left) |
| | 178 | | { |
| 0 | 179 | | if (model.messageType != ChatMessage.Type.PRIVATE) |
| 0 | 180 | | return; |
| | 181 | |
|
| 0 | 182 | | OnPress?.Invoke(model.otherUserId); |
| 0 | 183 | | } |
| 0 | 184 | | else if (pointerEventData.button == PointerEventData.InputButton.Right) |
| | 185 | | { |
| 0 | 186 | | if ((model.messageType != ChatMessage.Type.PUBLIC && model.messageType != ChatMessage.Type.PRIVATE) || |
| | 187 | | model.senderId == UserProfile.GetOwnUserProfile().userId) |
| 0 | 188 | | return; |
| | 189 | |
|
| 0 | 190 | | OnPressRightButton?.Invoke(this); |
| | 191 | | } |
| 0 | 192 | | } |
| | 193 | |
|
| 0 | 194 | | public void OnPointerEnter(PointerEventData pointerEventData) { hoverPanelTimer = timeToHoverPanel; } |
| | 195 | |
|
| | 196 | | public void OnPointerExit(PointerEventData pointerEventData) |
| | 197 | | { |
| 29 | 198 | | hoverPanelTimer = 0f; |
| | 199 | |
|
| 29 | 200 | | OnCancelHover?.Invoke(); |
| 28 | 201 | | } |
| | 202 | |
|
| 58 | 203 | | void OnDisable() { OnPointerExit(null); } |
| | 204 | |
|
| | 205 | | public void SetFadeout(bool enabled) |
| | 206 | | { |
| 29 | 207 | | if (!enabled) |
| | 208 | | { |
| 29 | 209 | | group.alpha = 1; |
| 29 | 210 | | group.blocksRaycasts = true; |
| 29 | 211 | | group.interactable = true; |
| 29 | 212 | | fadeEnabled = false; |
| 29 | 213 | | return; |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | fadeoutStartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| 0 | 217 | | fadeEnabled = true; |
| 0 | 218 | | group.blocksRaycasts = false; |
| 0 | 219 | | group.interactable = false; |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | void Update() |
| | 223 | | { |
| 3 | 224 | | Fade(); |
| | 225 | |
|
| 3 | 226 | | ProcessHoverPanelTimer(); |
| 3 | 227 | | } |
| | 228 | |
|
| | 229 | | void Fade() |
| | 230 | | { |
| 3 | 231 | | if (!fadeEnabled) |
| 3 | 232 | | return; |
| | 233 | |
|
| | 234 | | //NOTE(Brian): Small offset using normalized Y so we keep the cascade effect |
| 0 | 235 | | double yOffset = (transform as RectTransform).anchoredPosition.y / (double)Screen.height * 2.0; |
| | 236 | |
|
| 0 | 237 | | double fadeTime = Math.Max(model.timestamp / 1000.0, fadeoutStartTime) + timeToFade - yOffset; |
| 0 | 238 | | double currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| | 239 | |
|
| 0 | 240 | | if (currentTime > fadeTime) |
| | 241 | | { |
| 0 | 242 | | double timeSinceFadeTime = currentTime - fadeTime; |
| 0 | 243 | | group.alpha = Mathf.Clamp01(1 - (float)(timeSinceFadeTime / fadeDuration)); |
| 0 | 244 | | } |
| | 245 | | else |
| | 246 | | { |
| 0 | 247 | | group.alpha += (1 - group.alpha) * 0.05f; |
| | 248 | | } |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | void ProcessHoverPanelTimer() |
| | 252 | | { |
| 3 | 253 | | if (hoverPanelTimer <= 0f) |
| 3 | 254 | | return; |
| | 255 | |
|
| 0 | 256 | | hoverPanelTimer -= Time.deltaTime; |
| 0 | 257 | | if (hoverPanelTimer <= 0f) |
| | 258 | | { |
| 0 | 259 | | hoverPanelTimer = 0f; |
| | 260 | |
|
| 0 | 261 | | OnTriggerHover?.Invoke(this); |
| | 262 | | } |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | string RemoveTabs(string text) |
| | 266 | | { |
| 32 | 267 | | if (string.IsNullOrEmpty(text)) |
| 0 | 268 | | return ""; |
| | 269 | |
|
| | 270 | | //NOTE(Brian): ContentSizeFitter doesn't fare well with tabs, so i'm replacing these |
| | 271 | | // with spaces. |
| 32 | 272 | | return text.Replace("\t", " "); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | string GetDefaultSenderString(string sender) |
| | 276 | | { |
| 32 | 277 | | if (!string.IsNullOrEmpty(sender)) |
| 32 | 278 | | return $"<b>{sender}:</b>"; |
| | 279 | |
|
| 0 | 280 | | return ""; |
| | 281 | | } |
| | 282 | |
|
| | 283 | | DateTime UnixTimeStampToLocalDateTime(ulong unixTimeStampMilliseconds) |
| | 284 | | { |
| | 285 | | // TODO see if we can simplify with 'DateTimeOffset.FromUnixTimeMilliseconds' |
| 32 | 286 | | System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); |
| 32 | 287 | | dtDateTime = dtDateTime.AddMilliseconds(unixTimeStampMilliseconds).ToLocalTime(); |
| | 288 | |
|
| 32 | 289 | | return dtDateTime; |
| | 290 | | } |
| | 291 | | } |