SymbolTextImp.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace WXB
  5. {
  6. public partial class SymbolText
  7. {
  8. public override float preferredWidth
  9. {
  10. get
  11. {
  12. UpdateByDirty();
  13. return getNodeWidth();
  14. }
  15. }
  16. public override float preferredHeight
  17. {
  18. get
  19. {
  20. UpdateByDirty();
  21. return getNodeHeight();
  22. }
  23. }
  24. public override void SetAllDirty()
  25. {
  26. base.SetAllDirty();
  27. SetTextDirty();
  28. }
  29. void UpdateByDirty()
  30. {
  31. if (m_textDirty)
  32. {
  33. UpdateByTextDirty();
  34. m_textDirty = false;
  35. UpdateTextHeight();
  36. m_layoutDirty = false;
  37. UpdateRenderElement();
  38. m_renderNodeDirty = false;
  39. }
  40. if (m_layoutDirty)
  41. {
  42. UpdateTextHeight();
  43. m_layoutDirty = false;
  44. UpdateRenderElement();
  45. m_renderNodeDirty = false;
  46. }
  47. if (m_renderNodeDirty)
  48. {
  49. UpdateRenderElement();
  50. m_renderNodeDirty = false;
  51. }
  52. }
  53. public override void Rebuild(CanvasUpdate update)
  54. {
  55. if (canvasRenderer.cull)
  56. return;
  57. if (pixelsPerUnit >= 10f)
  58. return;
  59. switch (update)
  60. {
  61. case CanvasUpdate.PreRender:
  62. {
  63. UpdateByDirty();
  64. base.Rebuild(update);
  65. }
  66. break;
  67. }
  68. }
  69. protected virtual void SetTextDirty()
  70. {
  71. m_textDirty = true;
  72. m_renderNodeDirty = true;
  73. SetMaterialDirty();
  74. FreeDraws();
  75. if (isActiveAndEnabled)
  76. CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
  77. }
  78. protected override void UpdateMaterial()
  79. {
  80. if (!IsActive())
  81. return;
  82. if (m_UsedDraws.Count == 0)
  83. {
  84. return;
  85. }
  86. var components = ListPool<Component>.Get();
  87. GetComponents(typeof(IMaterialModifier), components);
  88. for (int i = 0; i < m_UsedDraws.Count; ++i)
  89. {
  90. Draw draw = m_UsedDraws[i];
  91. if (draw.srcMat == null)
  92. draw.srcMat = material;
  93. Material currentMat = draw.srcMat;
  94. for (var m = 0; m < components.Count; m++)
  95. currentMat = (components[m] as IMaterialModifier).GetModifiedMaterial(currentMat);
  96. draw.UpdateMaterial(currentMat);
  97. }
  98. ListPool<Component>.Release(components);
  99. }
  100. Around d_Around = new Around();
  101. public Around around { get { return d_Around; } }
  102. public ElementSegment elementSegment
  103. {
  104. get
  105. {
  106. if (string.IsNullOrEmpty(m_ElementSegment))
  107. return null;
  108. return ESFactory.Get(m_ElementSegment);
  109. }
  110. }
  111. protected static List<NodeBase> s_nodebases = new List<NodeBase>();
  112. // 得到外部结点
  113. public System.Func<TagAttributes, IExternalNode> getExternalNode { get; set; }
  114. // 根据新文本,解析结点
  115. public void UpdateByTextDirty()
  116. {
  117. Clear();
  118. s_nodebases.Clear();
  119. Parser.parser(this, text, CreateConfig(), s_nodebases, getExternalNode);
  120. s_nodebases.ForEach((NodeBase nb) =>
  121. {
  122. mNodeList.AddLast(nb);
  123. });
  124. s_nodebases.Clear();
  125. }
  126. public override void SetVerticesDirty()
  127. {
  128. base.SetVerticesDirty();
  129. SetMaterialDirty();
  130. }
  131. Vector2 last_size = new Vector2(-1000f, -1000f);
  132. protected override void OnRectTransformDimensionsChange()
  133. {
  134. if (gameObject.activeInHierarchy)
  135. {
  136. // prevent double dirtying...
  137. if (CanvasUpdateRegistry.IsRebuildingLayout())
  138. {
  139. if (last_size == rectTransform.rect.size)
  140. return;
  141. SetLayoutDirty();
  142. }
  143. else
  144. {
  145. if (last_size != rectTransform.rect.size)
  146. SetVerticesDirty();
  147. SetLayoutDirty();
  148. }
  149. }
  150. }
  151. public override void SetLayoutDirty()
  152. {
  153. base.SetLayoutDirty();
  154. SetMaterialDirty();
  155. SetRenderDirty();
  156. m_textDirty = true;
  157. m_layoutDirty = true;
  158. }
  159. public void UpdateTextHeight()
  160. {
  161. if (pixelsPerUnit <= 0f)
  162. return;
  163. renderCache.Release();
  164. float w = rectTransform.rect.size.x /** pixelsPerUnit*/;
  165. mLines.Clear();
  166. if (w <= 0f)
  167. return;
  168. d_Around.Clear();
  169. foreach (NodeBase node in mNodeList)
  170. {
  171. if (node is RectSpriteNode)
  172. {
  173. RectSpriteNode rsn = node as RectSpriteNode;
  174. d_Around.Add(rsn.rect);
  175. }
  176. }
  177. mLines.Add(new Line(Vector2.zero));
  178. Vector2 currentpos = Vector2.zero;
  179. float scale = pixelsPerUnit;
  180. foreach (NodeBase node in mNodeList)
  181. node.fill(ref currentpos, mLines, w, scale);
  182. for (int i = 0; i < mLines.Count; ++i)
  183. {
  184. mLines[i].y = Mathf.Max(mLines[i].y, m_MinLineHeight);
  185. }
  186. }
  187. // 更新渲染的文本
  188. void UpdateRenderElement()
  189. {
  190. if (pixelsPerUnit <= 0f)
  191. return;
  192. FreeDraws();
  193. renderCache.Release();
  194. Rect inputRect = rectTransform.rect;
  195. float w = inputRect.size.x/* * pixelsPerUnit*/;
  196. if (w <= 0f)
  197. return;
  198. float x = 0;
  199. uint yline = 0;
  200. LinkedListNode<NodeBase> itor = mNodeList.First;
  201. while (itor != null)
  202. {
  203. itor.Value.render(
  204. w,
  205. renderCache,
  206. ref x,
  207. ref yline,
  208. mLines,
  209. 0f,
  210. 0f);
  211. itor = itor.Next;
  212. }
  213. }
  214. public void FontTextureChangedOther()
  215. {
  216. // Only invoke if we are not destroyed.
  217. if (!this)
  218. {
  219. return;
  220. }
  221. if (m_DisableFontTextureRebuiltCallback)
  222. return;
  223. if (!IsActive())
  224. return;
  225. // this is a bit hacky, but it is currently the
  226. // cleanest solution....
  227. // if we detect the font texture has changed and are in a rebuild loop
  228. // we just regenerate the verts for the new UV's
  229. if (CanvasUpdateRegistry.IsRebuildingGraphics() || CanvasUpdateRegistry.IsRebuildingLayout())
  230. UpdateGeometry();
  231. else
  232. SetAllDirty();
  233. }
  234. }
  235. }