TextRender.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Text;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace WXB
  5. {
  6. public partial class TextNode : NodeBase
  7. {
  8. struct Helper
  9. {
  10. public RenderCache cache;
  11. public float x;
  12. public uint yline;
  13. public List<Line> lines;
  14. public Anchor xFormatting;
  15. public float offsetX;
  16. public float offsetY;
  17. public StringBuilder sb;
  18. public Helper(float maxWidth, RenderCache cache, float x, uint yline, List<Line> lines, Anchor xFormatting, float offsetX, float offsetY, StringBuilder sb)
  19. {
  20. this.maxWidth = maxWidth;
  21. this.cache = cache;
  22. this.x = x;
  23. this.yline = yline;
  24. this.lines = lines;
  25. this.xFormatting = xFormatting;
  26. this.offsetX = offsetX;
  27. this.offsetY = offsetY;
  28. pixelsPerUnit = 1f;
  29. alignedX = 0;
  30. pt = Vector2.zero;
  31. node = null;
  32. fHeight = 0f;
  33. this.sb = sb;
  34. }
  35. public float pixelsPerUnit;
  36. Vector2 pt;
  37. float alignedX;
  38. TextNode node;
  39. float maxWidth;
  40. float fHeight;
  41. void DrawCurrent(bool isnewLine, Around around, float lineX)
  42. {
  43. if (sb.Length != 0)
  44. {
  45. var line = lines[(int)yline];
  46. Rect area_rect = new Rect(pt.x + alignedX, pt.y, x - pt.x + offsetX, node.getHeight());
  47. cache.cacheText(line, node, sb.ToString(), area_rect);
  48. sb.Remove(0, sb.Length);
  49. }
  50. if (isnewLine)
  51. {
  52. // 再换行
  53. yline++;
  54. x = lineX;
  55. pt.x = offsetX;
  56. pt.y = offsetY;
  57. for (int n = 0; n < yline; ++n)
  58. pt.y += lines[n].y;
  59. if (yline >= lines.Count)
  60. {
  61. --yline;
  62. //Debug.LogError("yline >= vLineSize.Count!yline:" + yline + " vLineSize:" + lines.Count);
  63. }
  64. alignedX = AlignedFormatting(node.owner, xFormatting, maxWidth, lines[(int)(yline)].x, lineX);
  65. float newx;
  66. if (!around.isContain(pt.x + alignedX, pt.y, 1, node.getHeight(), out newx))
  67. {
  68. pt.x = newx - alignedX;
  69. x = pt.x;
  70. }
  71. }
  72. }
  73. public void Draw(TextNode n, float lineX)
  74. {
  75. node = n;
  76. pt = new Vector2(x + offsetX, offsetY);
  77. for (int i = 0; i < yline; ++i)
  78. pt.y += lines[i].y;
  79. if (maxWidth == 0)
  80. return;
  81. alignedX = AlignedFormatting(n.owner, xFormatting, maxWidth, lines[(int)(yline)].x, 0);
  82. fHeight = node.getHeight();
  83. sb.Remove(0, sb.Length);
  84. Around around = n.owner.around;
  85. int textindex = 0;
  86. float newx = 0f;
  87. for (int k = 0; k < node.d_widthList.Count; ++k)
  88. {
  89. Element e = node.d_widthList[k];
  90. float totalwidth = e.totalwidth;
  91. if ((x + totalwidth) > maxWidth)
  92. {
  93. if (x != 0f)
  94. {
  95. DrawCurrent(true, around, lineX);
  96. }
  97. if (e.widths == null)
  98. {
  99. if ((x + e.totalwidth > maxWidth))
  100. {
  101. DrawCurrent(true, around, lineX);
  102. }
  103. else
  104. {
  105. x += e.totalwidth;
  106. sb.Append(node.d_text[textindex++]);
  107. }
  108. }
  109. else
  110. {
  111. for (int m = 0; m < e.widths.Count;)
  112. {
  113. if (x != 0 && x + e.widths[m] > maxWidth)
  114. {
  115. DrawCurrent(true, around, lineX);
  116. }
  117. else
  118. {
  119. x += e.widths[m];
  120. sb.Append(node.d_text[textindex++]);
  121. ++m;
  122. }
  123. }
  124. }
  125. }
  126. else if (!around.isContain(x, pt.y, totalwidth, fHeight, out newx))
  127. {
  128. DrawCurrent(false, around, lineX);
  129. x = newx;
  130. pt.x = newx;
  131. --k;
  132. }
  133. else
  134. {
  135. int ec = e.count;
  136. sb.Append(node.d_text.Substring(textindex, ec));
  137. textindex += ec;
  138. x += totalwidth;
  139. }
  140. }
  141. if (sb.Length != 0)
  142. {
  143. DrawCurrent(false, around, lineX);
  144. }
  145. if (node.d_bNewLine == true)
  146. {
  147. yline++;
  148. x = lineX;
  149. }
  150. }
  151. }
  152. }
  153. }