TextParser_Hy.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using UnityEngine;
  2. using System.Text;
  3. // 文本解析
  4. namespace WXB
  5. {
  6. public partial class TextParser
  7. {
  8. class HyConfig
  9. {
  10. public string text = "";
  11. public HyperlinkNode node = null;
  12. public int startPos = 0;
  13. public int lenght = 0;
  14. StringBuilder sb;
  15. public TextParser parser;
  16. delegate void OnFunHy(string text);
  17. OnFunHy[] OnFunHys = null;
  18. public HyConfig(TextParser p)
  19. {
  20. parser = p;
  21. OnFunHys = new OnFunHy[128];
  22. // 特定的颜色
  23. OnFunHys['R'] = ParserSureColor;
  24. OnFunHys['G'] = ParserSureColor;
  25. OnFunHys['B'] = ParserSureColor;
  26. OnFunHys['K'] = ParserSureColor;
  27. OnFunHys['Y'] = ParserSureColor;
  28. OnFunHys['W'] = ParserSureColor;
  29. OnFunHys['P'] = ParserSureColor;
  30. OnFunHys['c'] = ParserFontColor;
  31. OnFunHys['n'] = ParserRestore;
  32. OnFunHys['s'] = ParserFontSize;
  33. OnFunHys['f'] = ParserFont;
  34. OnFunHys['#'] = ParserOutputChar;
  35. OnFunHys['u'] = (string text) =>
  36. {
  37. node.d_bUnderline = !node.d_bUnderline; // 下划线
  38. startPos++;
  39. };
  40. OnFunHys['e'] = (string text) =>
  41. {
  42. node.d_bStrickout = !node.d_bStrickout; // 下划线
  43. startPos++;
  44. };
  45. }
  46. void ParserOutputChar(string text)
  47. {
  48. sb.Append("#");
  49. ++startPos;
  50. }
  51. void ParserSureColor(string text)
  52. {
  53. node.d_color = GetColour(text[startPos]);
  54. ++startPos;
  55. }
  56. void ParserFontColor(string text)
  57. {
  58. node.d_color = Tools.ParserColorName(text, ref startPos, node.d_color);
  59. }
  60. void ParserRestore(string text)
  61. {
  62. node.SetConfig(parser.startConfig);
  63. }
  64. void ParserFontSize(string text)
  65. {
  66. float size = 1f;
  67. if (!ParserFloat(ref startPos, text, ref size))
  68. return;
  69. node.d_fontSize = (int)size;
  70. }
  71. void ParserFont(string text)
  72. {
  73. ++startPos;
  74. // 字体
  75. Font pFont = Tools.ParserFontName(text, ref startPos);
  76. if (pFont != null)
  77. {
  78. node.d_font = pFont;
  79. }
  80. else
  81. {
  82. --startPos;
  83. }
  84. }
  85. public void Clear()
  86. {
  87. text = null;
  88. node = null;
  89. sb = null;
  90. startPos = 0;
  91. }
  92. public void BeginParser(StringBuilder s)
  93. {
  94. sb = s;
  95. bool bBegin = false;
  96. while (lenght > startPos)
  97. {
  98. if (bBegin == false)
  99. {
  100. if (text[startPos] == '#')
  101. {
  102. // 未遇到功能字符,开始功能字符的解析
  103. bBegin = true;
  104. startPos++;
  105. }
  106. else
  107. {
  108. sb.Append(text[startPos]);
  109. startPos++;
  110. }
  111. }
  112. else
  113. {
  114. char c = text[startPos];
  115. OnFunHy fun = null;
  116. if (c < 128 && ((fun = OnFunHys[c]) != null))
  117. {
  118. fun(text);
  119. }
  120. else
  121. {
  122. sb.Append(text[startPos]);
  123. startPos++;
  124. }
  125. bBegin = false;
  126. }
  127. }
  128. node.d_text = sb.ToString();
  129. node.hoveColor = node.d_color;
  130. }
  131. }
  132. static HyConfig hyConfig = null;
  133. void ParseHyText(string text, HyperlinkNode data)
  134. {
  135. if (hyConfig == null)
  136. hyConfig = new HyConfig(this);
  137. // 初始化数据
  138. {
  139. hyConfig.text = text;
  140. hyConfig.node = data;
  141. hyConfig.startPos = 0;
  142. hyConfig.lenght = text.Length;
  143. }
  144. using (PD<StringBuilder> psb = Pool.GetSB())
  145. {
  146. hyConfig.BeginParser(psb.value);
  147. hyConfig.Clear();
  148. }
  149. }
  150. }
  151. }