ImageFontGraphics.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using CommonUI.Data;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace CommonUnity3D.UGUI
  7. { //---------------------------------------------------------------------------------------------------
  8. public class ImageFontGraphics : UnityEngine.UI.MaskableGraphic, ITextComponent
  9. {
  10. private DisplayNode mBinding;
  11. [SerializeField]
  12. private string m_Text;
  13. [SerializeField]
  14. private CommonUI.Data.TextAnchor m_Anchor = CommonUI.Data.TextAnchor.L_T;
  15. [SerializeField]
  16. private Color m_FontColor = Color.white;
  17. [SerializeField]
  18. private Vector2 m_TextOffset = Vector3.zero;
  19. private CommonUI_Unity3D.Impl.UnityImage mSrc;
  20. private CommonUI.Cell.CPJAtlas mAtlas;
  21. private Texture2D mTexture;
  22. private Vector2 mPreferredSize = Vector2.zero;
  23. private Rect mLastCaretPosition = new Rect(0, 0, 0, 0);
  24. public override Texture mainTexture { get { return mTexture; } }
  25. public DisplayNode Binding { get { return mBinding; } }
  26. public CommonUI.Cell.CPJAtlas Atlas
  27. {
  28. get { return mAtlas; }
  29. set
  30. {
  31. mAtlas = value;
  32. mSrc = mAtlas.GetTile(0) as CommonUI_Unity3D.Impl.UnityImage;
  33. if (mSrc != null)
  34. {
  35. this.mTexture = mSrc.Texture2D;
  36. this.material = mSrc.TextureMaterial;
  37. base.SetAllDirty();
  38. }
  39. }
  40. }
  41. public string Text
  42. {
  43. get { return m_Text; }
  44. set
  45. {
  46. if (value == null) value = "";
  47. if (value != m_Text)
  48. {
  49. this.m_Text = value;
  50. base.SetVerticesDirty();
  51. if (mAtlas != null)
  52. {
  53. mPreferredSize = GetImageFontPreferredSize(mAtlas, m_Text);
  54. }
  55. }
  56. }
  57. }
  58. public CommonUI.Data.TextAnchor Anchor
  59. {
  60. get { return m_Anchor; }
  61. set
  62. {
  63. if (value != m_Anchor)
  64. {
  65. this.m_Anchor = value;
  66. base.SetVerticesDirty();
  67. }
  68. }
  69. }
  70. public int FontSize
  71. {
  72. get { return 1; }
  73. set { }
  74. }
  75. public CommonUI.Data.FontStyle Style
  76. {
  77. get { return CommonUI.Data.FontStyle.Normal; }
  78. set { }
  79. }
  80. public Color FontColor
  81. {
  82. get { return m_FontColor; }
  83. set
  84. {
  85. if (m_FontColor != value)
  86. {
  87. m_FontColor = value;
  88. value.a = base.color.a * value.a;
  89. base.color = value;
  90. }
  91. }
  92. }
  93. public Vector2 TextOffset
  94. {
  95. get { return m_TextOffset; }
  96. set
  97. {
  98. if (value != m_TextOffset)
  99. {
  100. this.m_TextOffset = value;
  101. base.SetVerticesDirty();
  102. }
  103. }
  104. }
  105. public bool IsUnderline
  106. {
  107. get { return false; }
  108. set { }
  109. }
  110. public void SetBorder(Color bc, Vector2 distance)
  111. {
  112. //Do nothing.
  113. }
  114. public void SetShadow(Color bc, Vector2 distance)
  115. {
  116. }
  117. public void SetFont(UnityEngine.Font font)
  118. {
  119. }
  120. public virtual Vector2 PreferredSize
  121. {
  122. get { return mPreferredSize; }
  123. }
  124. public virtual Rect LastCaretPosition
  125. {
  126. get { return mLastCaretPosition; }
  127. }
  128. protected override void Start()
  129. {
  130. this.mBinding = DisplayNode.AsDisplayNode(gameObject);
  131. base.Start();
  132. }
  133. protected override void OnPopulateMesh(VertexHelper vh)
  134. {
  135. vh.Clear();
  136. if (mAtlas != null)
  137. {
  138. //mPreferredSize = GetImageFontPreferredSize(mAtlas, m_Text);
  139. mLastCaretPosition.size = UIFactory.Instance.DefaultCaretSize;
  140. mLastCaretPosition.y = 0;
  141. mLastCaretPosition.x = 0;
  142. if (mSrc != null)
  143. {
  144. float sw = 0, sh = 0;
  145. Rect bounds = new Rect(0, 0, mPreferredSize.x, mPreferredSize.y);
  146. UIUtils.AdjustAnchor(m_Anchor, rectTransform.sizeDelta, ref bounds);
  147. Vector2 offset = bounds.position;
  148. offset.x += m_TextOffset.x;
  149. offset.y += m_TextOffset.y;
  150. for (int i = 0; i < m_Text.Length; i++)
  151. {
  152. char ch = m_Text[i];
  153. int index = mAtlas.GetIndexByKey(ch.ToString());
  154. if (index >= 0)
  155. {
  156. CommonUI.Gemo.Rectangle2D rect = mAtlas.GetAtlasRegion(index);
  157. if (rect != null)
  158. {
  159. UIUtils.CreateVertexQuard(mSrc, color, rect.x, rect.y, sw + offset.x, offset.y, rect.width, rect.height, vh);
  160. sw += rect.width;
  161. sh = Math.Max(rect.height, sh);
  162. }
  163. }
  164. }
  165. mLastCaretPosition.x = sw;
  166. mLastCaretPosition.height = sh;
  167. }
  168. }
  169. else
  170. {
  171. mPreferredSize = Vector3.zero;
  172. mLastCaretPosition.size = UIFactory.Instance.DefaultCaretSize;
  173. }
  174. }
  175. public virtual void CalculateLayoutInputHorizontal()
  176. {
  177. }
  178. public virtual void CalculateLayoutInputVertical()
  179. {
  180. }
  181. public override bool Raycast(Vector2 sp, Camera eventCamera)
  182. {
  183. if (mBinding.Enable && mBinding.EnableTouchInParents)
  184. {
  185. return base.Raycast(sp, eventCamera);
  186. }
  187. return false;
  188. }
  189. public static Vector2 GetImageFontPreferredSize(CommonUI.Cell.CPJAtlas atlas, string text)
  190. {
  191. float sw = 0, sh = 0;
  192. CommonUI_Unity3D.Impl.UnityImage src = atlas.GetTile(0) as CommonUI_Unity3D.Impl.UnityImage;
  193. if (src != null)
  194. {
  195. for (int i = 0; i < text.Length; i++)
  196. {
  197. char ch = text[i];
  198. int index = atlas.GetIndexByKey(ch.ToString());
  199. if (index >= 0)
  200. {
  201. Vector2 size = new Vector2(atlas.getWidth(index), atlas.getHeight(index));
  202. if (size.x > 0)
  203. {
  204. sw += size.x;
  205. sh = Math.Max(size.y, sh);
  206. }
  207. }
  208. }
  209. }
  210. return new Vector2(sw, sh);
  211. }
  212. private void ResetTexture()
  213. {
  214. if (mSrc != null && mSrc.Texture2D != mTexture)
  215. {
  216. this.mTexture = mSrc.Texture2D;
  217. this.material = mSrc.TextureMaterial;
  218. base.SetAllDirty();
  219. }
  220. }
  221. protected override void OnEnable()
  222. {
  223. base.OnEnable();
  224. ResetTexture();
  225. }
  226. void Update()
  227. {
  228. ResetTexture();
  229. }
  230. }
  231. }