using UnityEngine; using TMPro; using UnityEngine.EventSystems; using BrewMonster.Scripts.Task.UI; using System.Collections.Generic; using BrewMonster.UI; using System.Text; using System.Text.RegularExpressions; namespace BrewMonster { public class StyledTaskTraceText : MonoBehaviour, IPointerMoveHandler,IPointerClickHandler { public TMP_Text tmp; private List linkCommands =new List(); int hoveredLink = -1; private void Start() { tmp.raycastTarget = true; tmp.ForceMeshUpdate(); } public void OnPointerMove(PointerEventData eventData) { int linkIndex = TMP_TextUtilities.FindIntersectingLink( tmp, eventData.position, eventData.pressEventCamera ); if (linkIndex != hoveredLink) { hoveredLink = linkIndex; } } public void Set(string text) { tmp.text = FormatForTextMeshPro(text); } public static string FormatForTextMeshPro(string text) { if (string.IsNullOrEmpty(text)) return string.Empty; StringBuilder result = new StringBuilder(text); result.Replace("\\r", "\n"); return ProcessColorCodes(result); } public static string ProcessColorCodes(StringBuilder text) { string pattern = @"\^([0-9A-Fa-f]{6})"; return Regex.Replace(text.ToString(), pattern, match => { string hexColor = match.Groups[1].Value; return $""; }, RegexOptions.None); } public void AddToFirstPosition(LinkCommand linkCommand) { linkCommands.Insert(0, linkCommand); } public void AddToLastPosition(LinkCommand linkCommand) { linkCommands.Add(linkCommand); } public void AddLinkCommand(LinkCommand linkCommand) { linkCommands.Add(linkCommand); } public void RemoveLinkCommand(int linkIndex) { linkCommands.RemoveAt(linkIndex); } public void RefreshLinkCommands() { tmp.text = ""; linkCommands.Clear(); } void SetLinkColor(int linkIndex, Color32 color) { TMP_LinkInfo linkInfo = tmp.textInfo.linkInfo[linkIndex]; for (int i = 0; i < linkInfo.linkTextLength; i++) { int charIndex = linkInfo.linkTextfirstCharacterIndex + i; var charInfo = tmp.textInfo.characterInfo[charIndex]; if (!charInfo.isVisible) continue; int meshIndex = charInfo.materialReferenceIndex; int vertexIndex = charInfo.vertexIndex; var colors = tmp.textInfo.meshInfo[meshIndex].colors32; colors[vertexIndex + 0] = color; colors[vertexIndex + 1] = color; colors[vertexIndex + 2] = color; colors[vertexIndex + 3] = color; } tmp.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32); } public void OnPointerClick(PointerEventData eventData) { TMP_LinkInfo linkInfo = tmp.textInfo.linkInfo[hoveredLink]; string linkText = linkInfo.GetLinkText(); string entityID = linkInfo.GetLinkID(); switch (entityID) { case "coord": linkCommands[hoveredLink].Execute(tmp); //Debug.Log($"[StyledTaskTraceText] OnPointerClick: linkCommands[hoveredLink].Execute(tmp) => {linkCommands[hoveredLink].Execute(tmp)}"); break; case "npc": linkCommands[hoveredLink].Execute(tmp); break; case "monster": break; default: Debug.Log($"Clicked unknown link: ID={entityID} Text={linkText}"); break; } hoveredLink = -1; } } }