RenderCache_TextData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. #pragma warning disable 618
  5. namespace WXB
  6. {
  7. // 缓存渲染元素
  8. public partial class RenderCache
  9. {
  10. public class TextData : BaseData
  11. {
  12. public void Reset(TextNode n, string t, Rect r, Line l)
  13. {
  14. node = n;
  15. text = t;
  16. rect = r;
  17. line = l;
  18. }
  19. protected override void OnRelease()
  20. {
  21. text = null;
  22. }
  23. public override bool isAlignByGeometry
  24. {
  25. get { return true; }
  26. }
  27. public TextNode Node
  28. {
  29. get { return (TextNode)node; }
  30. set { node = value; }
  31. }
  32. public string text;
  33. static TextAnchor SwitchTextAnchor(TextAnchor anchor)
  34. {
  35. switch (anchor)
  36. {
  37. case TextAnchor.UpperLeft:
  38. case TextAnchor.UpperCenter:
  39. case TextAnchor.UpperRight:
  40. return TextAnchor.UpperLeft;
  41. case TextAnchor.MiddleLeft:
  42. case TextAnchor.MiddleCenter:
  43. case TextAnchor.MiddleRight:
  44. return TextAnchor.MiddleCenter;
  45. case TextAnchor.LowerLeft:
  46. case TextAnchor.LowerCenter:
  47. case TextAnchor.LowerRight:
  48. return TextAnchor.LowerLeft;
  49. }
  50. return TextAnchor.MiddleCenter;
  51. }
  52. const float line_height = 2f;
  53. public override void OnLineYCheck(float pixelsPerUnit)
  54. {
  55. if (line == null)
  56. return;
  57. //if (line.y == Node.getHeight())
  58. // return;
  59. var node = Node;
  60. Font font = node.d_font;
  61. FontStyle fs = node.d_fontStyle;
  62. int fontSize = (int)((node.d_fontSize * pixelsPerUnit));
  63. font.RequestCharactersInTexture(text, fontSize, fs);
  64. for (int i = 0; i < text.Length; ++i)
  65. {
  66. if (font.GetCharacterInfo(text[i], out s_Info, fontSize, fs))
  67. {
  68. line.minY = Mathf.Min(s_Info.minY, line.minY);
  69. line.maxY = Mathf.Max(s_Info.maxY, line.maxY);
  70. }
  71. }
  72. }
  73. public override void OnAlignByGeometry(ref Vector2 offset, float pixelsPerUnit)
  74. {
  75. if (line == null)
  76. return;
  77. var node = Node;
  78. Font font = node.d_font;
  79. FontStyle fs = node.d_fontStyle;
  80. int fontSize = (int)((node.d_fontSize * pixelsPerUnit));
  81. font.RequestCharactersInTexture(text, fontSize, fs);
  82. if (rect.x == 0)
  83. {
  84. // 第一个元素
  85. if (font.GetCharacterInfo(text[0], out s_Info, fontSize, fs))
  86. offset.x = s_Info.minX;
  87. }
  88. float y = float.MinValue;
  89. for (int i = 0; i < text.Length; ++i)
  90. {
  91. if (font.GetCharacterInfo(text[i], out s_Info, fontSize, fs))
  92. {
  93. y = Mathf.Max(s_Info.maxY, offset.y);
  94. }
  95. }
  96. offset.y = Mathf.Max(offset.y, line.y - y);
  97. }
  98. static CharacterInfo s_Info;
  99. public override Vector2 GetStartLeftBottom(float unitsPerPixel)
  100. {
  101. Vector2 leftBottomPos = new Vector2(rect.x, rect.y + rect.height);
  102. var la = lineAlignment;
  103. switch (la)
  104. {
  105. case LineAlignment.Top:
  106. leftBottomPos.y = rect.y + line.maxY * unitsPerPixel;
  107. break;
  108. case LineAlignment.Center:
  109. {
  110. leftBottomPos.y = rect.y + line.maxY * unitsPerPixel;
  111. leftBottomPos.y += (line.y - (line.maxY - line.minY) * unitsPerPixel) * 0.5f;
  112. }
  113. break;
  114. case LineAlignment.Bottom:
  115. leftBottomPos.y = rect.y + line.y + line.minY * unitsPerPixel;
  116. break;
  117. }
  118. return leftBottomPos;
  119. }
  120. public override void Render(VertexHelper vh, Rect area, Vector2 offset, float pixelsPerUnit)
  121. {
  122. var node = Node;
  123. Color currentColor = node.currentColor;
  124. if (currentColor.a <= 0.01f)
  125. return;
  126. int start = vh.currentIndexCount;
  127. float unitsPerPixel = 1f / pixelsPerUnit;
  128. Vector2 leftBottomPos = GetStartLeftBottom(unitsPerPixel) + offset;
  129. Tools.LB2LT(ref leftBottomPos, area.height);
  130. // 渲染文本
  131. float minY = float.MaxValue;
  132. float maxY = float.MinValue;
  133. {
  134. //leftPos = Vector2.zero;
  135. Font font = node.d_font;
  136. FontStyle fs = node.d_fontStyle;
  137. int fontSize = (int)((node.d_fontSize * pixelsPerUnit));
  138. font.RequestCharactersInTexture(text, fontSize, fs);
  139. Vector2 startPos = Vector2.zero;
  140. for (int i = 0; i < text.Length; ++i)
  141. {
  142. if (font.GetCharacterInfo(text[i], out s_Info, fontSize, fs))
  143. {
  144. int startVertCount = vh.currentVertCount;
  145. Rect cr = Rect.MinMaxRect(
  146. leftBottomPos.x + ((startPos.x + s_Info.minX) * unitsPerPixel),
  147. leftBottomPos.y + ((startPos.y + s_Info.minY) * unitsPerPixel),
  148. leftBottomPos.x + ((startPos.x + s_Info.maxX) * unitsPerPixel),
  149. leftBottomPos.y + ((startPos.y + s_Info.maxY) * unitsPerPixel));
  150. vh.AddVert(new Vector3(cr.xMin, cr.yMax), currentColor, s_Info.uvTopLeft);
  151. vh.AddVert(new Vector3(cr.xMax, cr.yMax), currentColor, s_Info.uvTopRight);
  152. vh.AddVert(new Vector3(cr.xMax, cr.yMin), currentColor, s_Info.uvBottomRight);
  153. vh.AddVert(new Vector3(cr.xMin, cr.yMin), currentColor, s_Info.uvBottomLeft);
  154. vh.AddTriangle(startVertCount + 0, startVertCount + 1, startVertCount + 2);
  155. vh.AddTriangle(startVertCount + 2, startVertCount + 3, startVertCount + 0);
  156. startPos.x += (s_Info.advance);
  157. minY = Mathf.Min(cr.yMin, minY);
  158. maxY = Mathf.Max(cr.yMax, maxY);
  159. }
  160. }
  161. }
  162. bool isset = false;
  163. if (node.d_bDynUnderline || node.d_bUnderline)
  164. {
  165. isset = true;
  166. node.GetLineCharacterInfo(out s_Info);
  167. Vector2 startpos = new Vector2(leftBottomPos.x, minY);
  168. if (node.d_bDynUnderline)
  169. {
  170. Texture mainTexture = node.d_font.material.mainTexture;
  171. OutlineDraw draw = node.owner.GetDraw(DrawType.Outline, node.keyPrefix + mainTexture.GetInstanceID(), (Draw d, object p)=>
  172. {
  173. OutlineDraw od = d as OutlineDraw;
  174. od.isOpenAlpha = node.d_bBlink;
  175. od.texture = mainTexture;
  176. }) as OutlineDraw;
  177. draw.AddLine(node, startpos, rect.width, line_height * unitsPerPixel, currentColor, s_Info.uv.center, node.d_dynSpeed);
  178. }
  179. else
  180. {
  181. Tools.AddLine(vh, startpos, s_Info.uv.center, rect.width, line_height * unitsPerPixel, currentColor);
  182. }
  183. }
  184. if (node.d_bDynStrickout || node.d_bStrickout)
  185. {
  186. if (!isset)
  187. {
  188. isset = true;
  189. node.GetLineCharacterInfo(out s_Info);
  190. }
  191. Vector2 startpos = new Vector2(leftBottomPos.x, (maxY + minY) * 0.5f + line_height * 0.5f * unitsPerPixel);
  192. if (node.d_bDynStrickout)
  193. {
  194. Texture mainTexture = node.d_font.material.mainTexture;
  195. OutlineDraw draw = node.owner.GetDraw(DrawType.Outline, node.keyPrefix + mainTexture.GetInstanceID(), (Draw d, object p)=>
  196. {
  197. OutlineDraw od = d as OutlineDraw;
  198. od.isOpenAlpha = node.d_bBlink;
  199. od.texture = mainTexture;
  200. }) as OutlineDraw;
  201. draw.AddLine(node, startpos, rect.width, line_height * unitsPerPixel, currentColor, s_Info.uv.center, node.d_dynSpeed);
  202. }
  203. else
  204. {
  205. Tools.AddLine(vh, startpos, s_Info.uv.center, rect.width, line_height * unitsPerPixel, currentColor);
  206. }
  207. }
  208. switch (node.effectType)
  209. {
  210. case EffectType.Outline:
  211. Effect.Outline(vh, start, node.effectColor, node.effectDistance);
  212. break;
  213. case EffectType.Shadow:
  214. Effect.Shadow(vh, start, node.effectColor, node.effectDistance);
  215. break;
  216. }
  217. }
  218. public override void OnMouseEnter()
  219. {
  220. node.onMouseEnter();
  221. }
  222. public override void OnMouseLevel()
  223. {
  224. node.onMouseLeave();
  225. }
  226. }
  227. }
  228. }