BitmapTextGraphics.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using CommonUI.Data;
  2. using CommonUI_Unity3D.Impl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace CommonUnity3D.UGUI
  10. {
  11. //---------------------------------------------------------------------------------------------------
  12. public class BitmapTextGraphics : UnityEngine.UI.MaskableGraphic, ITextComponent
  13. {
  14. private readonly UnityTextLayer mTextLayer;
  15. private DisplayNode mBinding;
  16. private UnityImage mSrc;
  17. private Texture2D mTexture;
  18. private bool m_IsRefresh = true;
  19. private Vector2 mPreferredSize = Vector2.zero;
  20. private Rect mLastCaretPosition = new Rect(0, 0, 0, 0);
  21. private bool mScrollToCaret = false;
  22. [SerializeField]
  23. private string m_Text;
  24. [SerializeField]
  25. private CommonUI.Data.TextAnchor m_Anchor = CommonUI.Data.TextAnchor.L_T;
  26. [SerializeField]
  27. private Vector2 m_TextOffset = Vector3.zero;
  28. [SerializeField]
  29. private int m_FontSize;
  30. [SerializeField]
  31. private CommonUI.Data.FontStyle m_FontStyle = CommonUI.Data.FontStyle.Normal;
  32. [SerializeField]
  33. private Color m_BorderColor = Color.black;
  34. [SerializeField]
  35. private Color m_FontColor = Color.white;
  36. [SerializeField]
  37. private CommonUI.Data.TextBorderCount m_BorderTimes = TextBorderCount.Null;
  38. [SerializeField]
  39. private bool m_IsUnderline = false;
  40. public override Texture mainTexture { get { return mTexture; } }
  41. public DisplayNode Binding { get { return mBinding; } }
  42. public string Text
  43. {
  44. get { return m_Text; }
  45. set
  46. {
  47. if (value == null) value = "";
  48. if (value != m_Text)
  49. {
  50. m_Text = value;
  51. m_IsRefresh = true;
  52. }
  53. }
  54. }
  55. public int FontSize
  56. {
  57. get { return m_FontSize; }
  58. set
  59. {
  60. if (value != m_FontSize)
  61. {
  62. m_FontSize = value;
  63. m_IsRefresh = true;
  64. }
  65. }
  66. }
  67. public UnityEngine.Color FontColor
  68. {
  69. get { return m_FontColor; }
  70. set
  71. {
  72. if (value != m_FontColor)
  73. {
  74. m_FontColor = value;
  75. m_IsRefresh = true;
  76. }
  77. }
  78. }
  79. public CommonUI.Data.FontStyle Style
  80. {
  81. get { return m_FontStyle; }
  82. set
  83. {
  84. if (value != m_FontStyle)
  85. {
  86. m_FontStyle = value;
  87. m_IsRefresh = true;
  88. }
  89. }
  90. }
  91. public bool IsUnderline
  92. {
  93. get { return m_IsUnderline; }
  94. set
  95. {
  96. if (value != m_IsUnderline)
  97. {
  98. m_IsUnderline = value;
  99. m_IsRefresh = true;
  100. }
  101. }
  102. }
  103. public CommonUI.Display.FontStyle LayerFontStyle
  104. {
  105. get { return mTextLayer.TextFontStyle; }
  106. set
  107. {
  108. if (value != mTextLayer.TextFontStyle)
  109. {
  110. m_FontStyle = UIUtils.ToFontStyle(value, out m_IsUnderline);
  111. m_IsRefresh = true;
  112. }
  113. }
  114. }
  115. public CommonUI.Data.TextBorderCount BorderTime
  116. {
  117. get { return m_BorderTimes; }
  118. set
  119. {
  120. if (value != m_BorderTimes)
  121. {
  122. m_BorderTimes = value;
  123. m_IsRefresh = true;
  124. }
  125. }
  126. }
  127. public Color BorderColor
  128. {
  129. get { return m_BorderColor; }
  130. set
  131. {
  132. if (value != m_BorderColor)
  133. {
  134. m_BorderColor = value;
  135. m_IsRefresh = true;
  136. }
  137. }
  138. }
  139. public CommonUI.Data.TextAnchor Anchor
  140. {
  141. get { return m_Anchor; }
  142. set
  143. {
  144. if (value != m_Anchor)
  145. {
  146. this.m_Anchor = value;
  147. base.SetVerticesDirty();
  148. }
  149. }
  150. }
  151. public Vector2 TextOffset
  152. {
  153. get { return m_TextOffset; }
  154. set
  155. {
  156. if (value != m_TextOffset)
  157. {
  158. this.m_TextOffset = value;
  159. base.SetVerticesDirty();
  160. }
  161. }
  162. }
  163. public void SetBorder(Color bc, Vector2 distance)
  164. {
  165. if (m_BorderTimes != TextBorderCount.Border || bc != m_BorderColor)
  166. {
  167. m_BorderColor = bc;
  168. m_BorderTimes = TextBorderCount.Border;
  169. m_IsRefresh = true;
  170. }
  171. }
  172. public void SetShadow(Color bc, Vector2 distance)
  173. {
  174. var value = UIUtils.ToTextShadowCount(distance);
  175. if (m_BorderTimes != value || bc != m_BorderColor)
  176. {
  177. m_BorderColor = bc;
  178. m_BorderTimes = value;
  179. m_IsRefresh = true;
  180. }
  181. }
  182. public void SetFont(Font font)
  183. {
  184. }
  185. public virtual Vector2 PreferredSize
  186. {
  187. get { return mPreferredSize; }
  188. }
  189. public virtual Rect LastCaretPosition
  190. {
  191. get { return mLastCaretPosition; }
  192. }
  193. public virtual bool AutoScrollToCaret
  194. {
  195. get { return mScrollToCaret; }
  196. set
  197. {
  198. if (mScrollToCaret != value)
  199. {
  200. mScrollToCaret = value;
  201. this.SetVerticesDirty();
  202. }
  203. }
  204. }
  205. public BitmapTextGraphics()
  206. {
  207. this.m_Text = "";
  208. this.m_FontSize = 16;
  209. this.mTextLayer = new UnityTextLayer("", CommonUI.Display.FontStyle.STYLE_PLAIN, m_FontSize);
  210. this.mTextLayer.FontColor = CommonUI.Display.Color.COLOR_WHITE;
  211. this.mTextLayer.BorderColor = CommonUI.Display.Color.COLOR_BLACK;
  212. this.mTextLayer.BorderTime = 0;
  213. this.material = null;
  214. }
  215. protected override void OnDestroy()
  216. {
  217. mTextLayer.Dispose();
  218. base.OnDestroy();
  219. }
  220. public void Apply()
  221. {
  222. m_IsRefresh = true;
  223. }
  224. private void Refresh()
  225. {
  226. mTextLayer.Text = m_Text;
  227. mTextLayer.FontSize = m_FontSize;
  228. mTextLayer.TextFontStyle = UIUtils.ToTextLayerFontStyle(m_FontStyle, m_IsUnderline);
  229. mTextLayer.FontColor = UIUtils.Color_To_UInt32_RGBA(m_FontColor);
  230. mTextLayer.BorderColor = UIUtils.Color_To_UInt32_RGBA(m_BorderColor);
  231. mTextLayer.BorderTime = (int)m_BorderTimes;
  232. this.mSrc = this.mTextLayer.GetBuffer() as UnityImage;
  233. if (mSrc != null)
  234. {
  235. this.mPreferredSize = new Vector2(this.mSrc.Width, this.mSrc.Height);
  236. this.mTexture = this.mSrc.Texture2D;
  237. this.material = this.mSrc.TextureMaterial;
  238. }
  239. else
  240. {
  241. this.mPreferredSize = Vector2.zero;
  242. this.mTexture = null;
  243. this.material = null;
  244. }
  245. SetAllDirty();
  246. }
  247. protected override void Start()
  248. {
  249. this.mBinding = DisplayNode.AsDisplayNode(gameObject);
  250. base.Start();
  251. if (m_IsRefresh)
  252. {
  253. m_IsRefresh = false;
  254. Refresh();
  255. }
  256. }
  257. protected virtual void Update()
  258. {
  259. if (mSrc != null && mSrc.Texture2D != mTexture)
  260. {
  261. m_IsRefresh = true;
  262. }
  263. if (m_IsRefresh)
  264. {
  265. m_IsRefresh = false;
  266. Refresh();
  267. }
  268. }
  269. protected override void OnPopulateMesh(VertexHelper vh)
  270. {
  271. vh.Clear();
  272. Rect bounds = new Rect(0, 0, mPreferredSize.x, mPreferredSize.y);
  273. UIUtils.AdjustAnchor(m_Anchor, rectTransform.sizeDelta, ref bounds);
  274. bounds.position += m_TextOffset;
  275. if (mSrc != null)
  276. {
  277. Vector2 src_pos = new Vector2(0, 0);
  278. if (mScrollToCaret)
  279. {
  280. float dw = bounds.width - Binding.Width;
  281. if (dw > 0)
  282. {
  283. src_pos.x += dw;
  284. bounds.width -= dw;
  285. }
  286. }
  287. UIUtils.CreateVertexQuard(this.mSrc, color, src_pos.x, src_pos.y, bounds.x, bounds.y, bounds.width, bounds.height, vh);
  288. this.mLastCaretPosition.height = bounds.height;
  289. this.mLastCaretPosition.width = UIFactory.Instance.DefaultCaretSize.x;
  290. this.mLastCaretPosition.y = 0;
  291. this.mLastCaretPosition.x = bounds.x + bounds.width;
  292. }
  293. else
  294. {
  295. this.mLastCaretPosition.y = 0;
  296. this.mLastCaretPosition.x = bounds.x;
  297. this.mLastCaretPosition.size = UIFactory.Instance.DefaultCaretSize;
  298. }
  299. }
  300. public virtual void CalculateLayoutInputHorizontal()
  301. {
  302. }
  303. public virtual void CalculateLayoutInputVertical()
  304. {
  305. }
  306. public override bool Raycast(Vector2 sp, Camera eventCamera)
  307. {
  308. if (mBinding.Enable && mBinding.EnableTouchInParents)
  309. {
  310. return base.Raycast(sp, eventCamera);
  311. }
  312. return false;
  313. }
  314. }
  315. }