TextNode.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace WXB
  6. {
  7. public partial class TextNode : NodeBase
  8. {
  9. public bool isFontSame(TextNode n)
  10. {
  11. if (d_font == n.d_font && d_fontSize == n.d_fontSize && d_fontStyle == n.d_fontStyle)
  12. return true;
  13. return false;
  14. }
  15. public override void Reset(Owner o, Anchor hf)
  16. {
  17. base.Reset(o, hf);
  18. d_font = null;
  19. d_bUnderline = false;
  20. d_bStrickout = false;
  21. }
  22. protected virtual bool isUnderLine()
  23. {
  24. return d_bUnderline;
  25. }
  26. public virtual Color currentColor
  27. {
  28. get { return d_color; }
  29. }
  30. public override float getHeight()
  31. {
  32. return size.y;
  33. }
  34. public override float getWidth()
  35. {
  36. return size.x;
  37. }
  38. Vector2 size = Vector2.zero;
  39. List<Element> d_widthList = new List<Element>();
  40. protected override void UpdateWidthList(out List<Element> widths, float pixelsPerUnit)
  41. {
  42. widths = d_widthList;
  43. d_widthList.Clear();
  44. if (d_text.Length == 0)
  45. return;
  46. float unitsPerPixel = 1f / pixelsPerUnit;
  47. int fontsize = (int)(d_fontSize * pixelsPerUnit);
  48. size.x = 0;
  49. size.y = FontCache.GetLineHeight(d_font, fontsize, d_fontStyle) * unitsPerPixel;
  50. Func<char, float> fontwidth = (char code) => { return FontCache.GetAdvance(d_font, fontsize, d_fontStyle, code) * unitsPerPixel; };
  51. ElementSegment es = owner.elementSegment;
  52. if (es == null)
  53. {
  54. for (int i = 0; i < d_text.Length; ++i)
  55. {
  56. var e = new Element(fontwidth(d_text[i]));
  57. #if UNITY_EDITOR
  58. e.text = "" + d_text[i];
  59. #endif
  60. widths.Add(e);
  61. }
  62. }
  63. else
  64. {
  65. es.Segment(d_text, widths, fontwidth);
  66. }
  67. for (int i = 0; i < d_widthList.Count; ++i)
  68. size.x += d_widthList[i].totalwidth;
  69. //size.x *= pixelsPerUnit;
  70. //size.y *= pixelsPerUnit;
  71. }
  72. public virtual bool IsHyText()
  73. {
  74. return false;
  75. }
  76. public override void render(float maxWidth, RenderCache cache, ref float x, ref uint yline, List<Line> lines, float offsetX, float offsetY)
  77. {
  78. if (d_font == null)
  79. return;
  80. using (PD<StringBuilder> psb = Pool.GetSB())
  81. {
  82. Helper helper = new Helper(maxWidth, cache, x, yline, lines, formatting, offsetX, offsetY, psb.value);
  83. helper.Draw(this, NextLineX);
  84. x = helper.x;
  85. yline = helper.yline;
  86. }
  87. }
  88. public string d_text;
  89. public Font d_font;
  90. public int d_fontSize;
  91. public FontStyle d_fontStyle;
  92. public bool d_bUnderline;
  93. public bool d_bStrickout;
  94. public bool d_bDynUnderline;
  95. public bool d_bDynStrickout;
  96. public int d_dynSpeed;
  97. public EffectType effectType;
  98. public Color effectColor;
  99. public Vector2 effectDistance;
  100. public new long keyPrefix
  101. {
  102. get
  103. {
  104. long key = base.keyPrefix;
  105. if (d_bDynStrickout)
  106. key += 1 << 45;
  107. if (d_bDynUnderline)
  108. key += 1 << 45;
  109. return key;
  110. }
  111. }
  112. public override void SetConfig(TextParser.Config c)
  113. {
  114. base.SetConfig(c);
  115. d_font = c.font;
  116. d_bUnderline = c.isUnderline;
  117. d_fontSize = c.fontSize;
  118. d_fontStyle = c.fontStyle;
  119. d_bStrickout = c.isStrickout;
  120. d_bDynUnderline = c.isDyncUnderline;
  121. d_bDynStrickout = c.isDyncStrickout;
  122. d_dynSpeed = c.dyncSpeed;
  123. effectType = c.effectType;
  124. effectColor = c.effectColor;
  125. effectDistance = c.effectDistance;
  126. }
  127. public void GetLineCharacterInfo(out CharacterInfo info)
  128. {
  129. if (!d_font.GetCharacterInfo('_', out info, 20, FontStyle.Bold))
  130. {
  131. d_font.RequestCharactersInTexture("_", 20, FontStyle.Bold);
  132. d_font.GetCharacterInfo('_', out info, 20, FontStyle.Bold);
  133. }
  134. }
  135. public override void Release()
  136. {
  137. base.Release();
  138. d_text = null;
  139. d_font = null;
  140. d_fontSize = 0;
  141. d_bUnderline = false;
  142. d_bDynUnderline = false;
  143. d_bDynStrickout = false;
  144. d_dynSpeed = 0;
  145. }
  146. };
  147. }