Files
test/Assets/PerfectWorld/Prefab/Task/TaskTrace/StyledTaskTraceText.cs
2026-01-29 17:42:34 +07:00

144 lines
4.5 KiB
C#

using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;
using BrewMonster.Scripts.Task.UI;
using System.Collections.Generic;
using BrewMonster.UI;
namespace BrewMonster
{
public class StyledTaskTraceText : MonoBehaviour, IPointerMoveHandler, IPointerExitHandler,IPointerClickHandler
{
public enum LinkType
{
coord,
npc,
monster,
item,
target
}
public TMP_Text tmp;
private List<LinkCommand> linkCommands =new List<LinkCommand>();
int hoveredLink = -1;
LinkType linkType = LinkType.coord;
Color32 normalColor = new Color32(77, 166, 255, 255);
Color32 hoverColor = new Color32(255, 200, 80, 255);
public void SetLinkType( LinkType linkType)
{
this.linkType = linkType;
}
private void Start()
{
tmp.raycastTarget = true;
ResetLinkColor();
tmp.ForceMeshUpdate();
}
public void OnPointerMove(PointerEventData eventData)
{
int linkIndex = TMP_TextUtilities.FindIntersectingLink(
tmp,
eventData.position,
eventData.pressEventCamera
);
if (linkIndex != hoveredLink)
{
ResetLinkColor();
hoveredLink = linkIndex;
if (hoveredLink != -1)
SetLinkColor(hoveredLink, hoverColor);
}
}
public void Set(string text)
{
tmp.text = text;
}
public void OnPointerExit(PointerEventData eventData)
{
ResetLinkColor();
hoveredLink = -1;
}
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)
{
if (hoveredLink == -1) return;
TMP_LinkInfo linkInfo = tmp.textInfo.linkInfo[hoveredLink];
string linkText = linkInfo.GetLinkText();
string entityID = linkInfo.GetLinkID();
switch (linkType)
{
case LinkType.coord:
Debug.Log($"Clicked coord link: ID={entityID} Type={linkType} Text={linkText}");
linkCommands[hoveredLink].Execute(tmp);
//Debug.Log($"[StyledTaskTraceText] OnPointerClick: linkCommands[hoveredLink].Execute(tmp) => {linkCommands[hoveredLink].Execute(tmp)}");
break;
case LinkType.npc:
Debug.Log($"Clicked npc link: ID={entityID} Type={linkType} Text={linkText}");
break;
case LinkType.monster:
Debug.Log($"Clicked monster link: ID={entityID} Type={linkType} Text={linkText}");
break;
}
ResetLinkColor();
hoveredLink = -1;
}
void ResetLinkColor()
{
if (hoveredLink == -1) return;
SetLinkColor(hoveredLink, normalColor);
}
}
}