55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using System;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
// 대사 텍스트의 저작용 단축 표기를 리치 텍스트로 펼친다.
|
|
//
|
|
// 그날 밤 [[피 묻은 칼]]을 봤습니다
|
|
// ↓
|
|
// 그날 밤 <link=glow><color=#FF3B30>피 묻은 칼</color></link>을 봤습니다
|
|
//
|
|
// 왜 단축 표기를 두는가: 그래프의 Talk Text 칸에 <color=#FF3B30>을 손으로 쓰면
|
|
// 오타가 조용히 깨지고, 색을 바꿀 때 모든 대사를 찾아 고쳐야 한다.
|
|
// 색은 TypewriterStyle.EmphasisColor 한 곳에서 관리된다.
|
|
//
|
|
// <link>을 같이 붙이는 이유: TextGlow가 Glyph.linkID로 강조 글자를 식별해 맥동시킨다.
|
|
public static class DialogMarkup
|
|
{
|
|
public const string OPEN = "[[";
|
|
public const string CLOSE = "]]";
|
|
|
|
public static string Expand(string text, TypewriterStyle style)
|
|
{
|
|
if (string.IsNullOrEmpty(text) || text.IndexOf(OPEN, StringComparison.Ordinal) < 0)
|
|
return text;
|
|
|
|
string hex = ColorUtility.ToHtmlStringRGB(style != null ? style.EmphasisColor : Color.red);
|
|
|
|
var sb = new StringBuilder(text.Length + 48);
|
|
int i = 0;
|
|
while (i < text.Length)
|
|
{
|
|
int open = text.IndexOf(OPEN, i, StringComparison.Ordinal);
|
|
if (open < 0)
|
|
{
|
|
sb.Append(text, i, text.Length - i);
|
|
break;
|
|
}
|
|
|
|
int close = text.IndexOf(CLOSE, open + OPEN.Length, StringComparison.Ordinal);
|
|
if (close < 0)
|
|
{
|
|
sb.Append(text, i, text.Length - i); // 닫히지 않은 표기 — 손대지 않고 그대로 둔다
|
|
break;
|
|
}
|
|
|
|
sb.Append(text, i, open - i);
|
|
sb.Append("<link=").Append(TextGlow.LINK_ID).Append("><color=#").Append(hex).Append('>');
|
|
sb.Append(text, open + OPEN.Length, close - open - OPEN.Length);
|
|
sb.Append("</color></link>");
|
|
i = close + CLOSE.Length;
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|