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