| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Chat.Mentions |
| | 10 | | { |
| | 11 | | public class MentionLinkDetector : MonoBehaviour, IPointerClickHandler |
| | 12 | | { |
| | 13 | | private const string MENTION_URL_PREFIX = "mention://"; |
| | 14 | |
|
| | 15 | | public event Action OnPlayerMentioned; |
| | 16 | |
|
| | 17 | | [SerializeField] internal TMP_Text textComponent; |
| | 18 | |
|
| 43 | 19 | | internal bool isMentionsFeatureEnabled = true; |
| | 20 | | internal string currentText; |
| | 21 | | internal bool hasNoParseLabel; |
| | 22 | | private UserContextMenu contextMenu; |
| 43 | 23 | | private readonly CancellationTokenSource cancellationToken = new (); |
| | 24 | |
|
| | 25 | | private void Awake() |
| | 26 | | { |
| 27 | 27 | | if (textComponent == null) |
| 0 | 28 | | return; |
| | 29 | |
|
| 27 | 30 | | textComponent.OnPreRenderText += OnTextComponentPreRenderText; |
| 27 | 31 | | } |
| | 32 | |
|
| | 33 | | private void Start() |
| | 34 | | { |
| 17 | 35 | | isMentionsFeatureEnabled = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("chat_mentions_enabled"); |
| 17 | 36 | | DataStore.i.featureFlags.flags.OnChange += OnFeatureFlagsChanged; |
| 17 | 37 | | } |
| | 38 | |
|
| | 39 | | private void OnDestroy() |
| | 40 | | { |
| 27 | 41 | | if (textComponent == null) |
| 0 | 42 | | return; |
| | 43 | |
|
| 27 | 44 | | textComponent.OnPreRenderText -= OnTextComponentPreRenderText; |
| 27 | 45 | | DataStore.i.featureFlags.flags.OnChange -= OnFeatureFlagsChanged; |
| | 46 | |
|
| 27 | 47 | | cancellationToken.SafeCancelAndDispose(); |
| 27 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnPointerClick(PointerEventData eventData) |
| | 51 | | { |
| 0 | 52 | | if (!isMentionsFeatureEnabled) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | if (eventData.button != PointerEventData.InputButton.Left) |
| 0 | 56 | | return; |
| | 57 | |
|
| 0 | 58 | | string userName = GetUserNameByPointerPosition(eventData.position); |
| 0 | 59 | | if (string.IsNullOrEmpty(userName)) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | ShowContextMenu(userName); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void SetContextMenu(UserContextMenu userContextMenu) |
| | 66 | | { |
| 5 | 67 | | this.contextMenu = userContextMenu; |
| 5 | 68 | | } |
| | 69 | |
|
| | 70 | | private string GetUserNameByPointerPosition(Vector2 pointerPosition) |
| | 71 | | { |
| 0 | 72 | | if (textComponent == null || textComponent.canvas == null) |
| 0 | 73 | | return null; |
| | 74 | |
|
| 0 | 75 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(textComponent, pointerPosition, textComponent.canvas. |
| 0 | 76 | | if (linkIndex == -1) |
| 0 | 77 | | return null; |
| | 78 | |
|
| 0 | 79 | | TMP_LinkInfo linkInfo = textComponent.textInfo.linkInfo[linkIndex]; |
| | 80 | |
|
| 0 | 81 | | string mentionText = linkInfo.GetLinkText(); |
| 0 | 82 | | string mentionLink = linkInfo.GetLinkID(); |
| | 83 | |
|
| 0 | 84 | | return !MentionsUtils.IsAMention(mentionText) |
| | 85 | | ? null |
| | 86 | | : mentionLink.Replace(MENTION_URL_PREFIX, string.Empty); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private void ShowContextMenu(string userName) |
| | 90 | | { |
| 0 | 91 | | if (contextMenu == null) |
| 0 | 92 | | return; |
| | 93 | |
|
| 0 | 94 | | var menuTransform = (RectTransform)contextMenu.transform; |
| 0 | 95 | | menuTransform.position = textComponent.transform.position; |
| 0 | 96 | | contextMenu.ShowByUserName(userName); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private void OnTextComponentPreRenderText(TMP_TextInfo textInfo) |
| | 100 | | { |
| 19 | 101 | | if (!isMentionsFeatureEnabled) |
| 19 | 102 | | return; |
| | 103 | |
|
| 0 | 104 | | if (textInfo.textComponent.text == currentText) |
| 0 | 105 | | return; |
| | 106 | |
|
| 0 | 107 | | hasNoParseLabel = textInfo.textComponent.text.Contains("<noparse>", StringComparison.OrdinalIgnoreCase); |
| 0 | 108 | | RefreshMentionPatterns(cancellationToken.Token).Forget(); |
| 0 | 109 | | CheckMentionAsync(textInfo.textComponent, cancellationToken.Token).Forget(); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private async UniTask RefreshMentionPatterns(CancellationToken cancellationToken) |
| | 113 | | { |
| 0 | 114 | | await UniTask.WaitForEndOfFrame(this, cancellationToken); |
| | 115 | |
|
| 0 | 116 | | textComponent.text = MentionsUtils.ReplaceMentionPattern(textComponent.text, mention => |
| | 117 | | { |
| 0 | 118 | | string mentionWithoutSymbol = mention[1..]; |
| | 119 | |
|
| 0 | 120 | | return hasNoParseLabel |
| | 121 | | ? $"</noparse><link={MENTION_URL_PREFIX}{mentionWithoutSymbol}><color=#4886E3><u>{mention}</u></colo |
| | 122 | | : $"<link={MENTION_URL_PREFIX}{mentionWithoutSymbol}><color=#4886E3><u>{mention}</u></color></link>" |
| | 123 | | }); |
| | 124 | |
|
| 0 | 125 | | currentText = textComponent.text; |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private async UniTask CheckMentionAsync(TMP_Text textComp, CancellationToken ct) |
| | 129 | | { |
| 0 | 130 | | await UniTask.WaitForEndOfFrame(this, ct); |
| | 131 | |
|
| 0 | 132 | | if (MentionsUtils.TextContainsMention(textComp.text)) |
| 0 | 133 | | OnPlayerMentioned?.Invoke(); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | private void OnFeatureFlagsChanged(FeatureFlag current, FeatureFlag previous) => |
| 0 | 137 | | isMentionsFeatureEnabled = current.IsFeatureEnabled("chat_mentions_enabled"); |
| | 138 | | } |
| | 139 | | } |