RichTextBox.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using UnityEngine;
  2. using CommonUI.Cell.Game;
  3. using CommonUI.Display;
  4. using UnityImage = CommonUI_Unity3D.Impl.UnityImage;
  5. using AttributedString = CommonUI.Display.Text.AttributedString;
  6. using BaseRichTextLayer = CommonUI.Display.Text.BaseRichTextLayer;
  7. using TextAttribute = CommonUI.Display.Text.TextAttribute;
  8. using CommonUI.Display.Text;
  9. using UnityEngine.EventSystems;
  10. using System;
  11. using CommonUI.Data;
  12. namespace CommonUnity3D.UGUI
  13. {
  14. //----------------------------------------------------------------------------------------
  15. /// <summary>
  16. /// 可滚动富文本
  17. /// </summary>
  18. public class RichTextBox : ScrollablePanel, ITextComponent
  19. {
  20. private readonly UGUIRichTextLayer mLayer;
  21. private bool mIsNeedScroll;
  22. private bool mScrollable = true;
  23. protected readonly bool mUseBitmapFont;
  24. public DisplayNode Binding { get { return mContent; } }
  25. public UGUIRichTextLayer RichTextLayer
  26. {
  27. get { return mLayer; }
  28. }
  29. public AttributedString AText
  30. {
  31. get { return mLayer.GetText(); }
  32. set
  33. {
  34. if (IsDispose) return;
  35. mLayer.SetString(value);
  36. }
  37. }
  38. public string XmlText
  39. {
  40. set
  41. {
  42. if (IsDispose) return;
  43. mLayer.XmlText = value;
  44. }
  45. }
  46. public string UnityRichText
  47. {
  48. set
  49. {
  50. if (IsDispose) return;
  51. this.XmlText = UIUtils.UnityRichTextToXmlText(value);
  52. }
  53. }
  54. public bool IsNeedScroll
  55. {
  56. get { return mIsNeedScroll; }
  57. }
  58. public bool Scrollable
  59. {
  60. get { return mScrollable; }
  61. set { this.mScrollable = value; }
  62. }
  63. public RichTextBox(string name = "", bool use_bitmap = false) : base(name)
  64. {
  65. this.mUseBitmapFont = use_bitmap;
  66. this.mLayer = UIFactory.Instance.CreateRichTextLayer(mContent, mUseBitmapFont);
  67. this.mMaskScrollRect.enabled = false;
  68. this.mMaskScrollRect.horizontal = false;
  69. this.mMaskScrollRect.vertical = true;
  70. this.mMaskScrollRect.AutoUpdateContentSize = false;
  71. this.mRectMask.enabled = true;
  72. this.mMaskGraphics.enabled = false;
  73. // this.mMask.enabled = false;
  74. this.mContent.EnableChildren = false;
  75. }
  76. public bool TestClick(Vector2 point, out RichTextClickInfo info)
  77. {
  78. Rect scroll_rect = this.ScrollRect2D;
  79. return RichTextLayer.Click(point.x + scroll_rect.x, point.y + scroll_rect.y, out info);
  80. }
  81. protected override void OnDispose()
  82. {
  83. mLayer.Dispose();
  84. base.OnDispose();
  85. }
  86. protected override void OnUpdateContentSize()
  87. {
  88. Vector2 bsize = this.Size2D;
  89. this.mLayer.SetWidth(bsize.x);
  90. Rect scroll_rect = this.ScrollRect2D;
  91. this.mContent.Size2D = new Vector2(bsize.x, mLayer.ContentHeight);
  92. this.mIsNeedScroll = mScrollable && (mLayer.ContentHeight > bsize.y);
  93. this.mMaskScrollRect.enabled = mIsNeedScroll;
  94. //this.mRectMask.enabled = mIsNeedScroll;
  95. this.mMaskGraphics.enabled = mIsNeedScroll;
  96. this.EnableChildren = mIsNeedScroll;
  97. if (!mIsNeedScroll)
  98. {
  99. switch (this.Anchor)
  100. {
  101. case CommonUI.Data.TextAnchor.L_T:
  102. case CommonUI.Data.TextAnchor.C_T:
  103. case CommonUI.Data.TextAnchor.R_T:
  104. break;
  105. case CommonUI.Data.TextAnchor.L_C:
  106. case CommonUI.Data.TextAnchor.C_C:
  107. case CommonUI.Data.TextAnchor.R_C:
  108. scroll_rect.y = -(bsize.y - this.mLayer.ContentHeight) * 0.5f;
  109. this.Container.Position2D = -scroll_rect.position;
  110. break;
  111. case CommonUI.Data.TextAnchor.L_B:
  112. case CommonUI.Data.TextAnchor.C_B:
  113. case CommonUI.Data.TextAnchor.R_B:
  114. scroll_rect.y = -(bsize.y - this.mLayer.ContentHeight);
  115. this.Container.Position2D = -scroll_rect.position;
  116. break;
  117. }
  118. }
  119. this.mLayer.Render(NullGraphics.SimpleGraphics, 0, 0, bsize.x, bsize.y, scroll_rect.x, scroll_rect.y);
  120. }
  121. protected override void OnSizeChanged(Vector2 size)
  122. {
  123. base.OnSizeChanged(size);
  124. this.mLayer.SetWidth(size.x);
  125. }
  126. #region ITextComponent
  127. public string Text
  128. {
  129. get { return mLayer.Text; }
  130. set
  131. {
  132. if (IsDispose) return;
  133. mLayer.Text = value;
  134. }
  135. }
  136. public int FontSize
  137. {
  138. get { return mLayer.FontSize; }
  139. set { mLayer.FontSize = value; }
  140. }
  141. public UnityEngine.Color FontColor
  142. {
  143. get { return mLayer.FontColor; }
  144. set { mLayer.FontColor = value; }
  145. }
  146. public CommonUI.Data.FontStyle Style
  147. {
  148. get { return mLayer.Style; }
  149. set { mLayer.Style = value; }
  150. }
  151. public bool IsUnderline
  152. {
  153. get { return mLayer.IsUnderline; }
  154. set { mLayer.IsUnderline = value; }
  155. }
  156. public Vector2 TextOffset
  157. {
  158. get { return mLayer.TextOffset; }
  159. set { mLayer.TextOffset = value; }
  160. }
  161. public CommonUI.Data.TextAnchor Anchor
  162. {
  163. get { return mLayer.Anchor; }
  164. set { mLayer.Anchor = value; }
  165. }
  166. public Vector2 PreferredSize
  167. {
  168. get { return mLayer.PreferredSize; }
  169. }
  170. public Rect LastCaretPosition
  171. {
  172. get
  173. {
  174. var bounds = mLayer.LastCaretPosition;
  175. bounds.position += mContent.Position2D;
  176. return bounds;
  177. }
  178. }
  179. public void SetBorder(UnityEngine.Color bc, Vector2 distance)
  180. {
  181. mLayer.SetBorder(bc, distance);
  182. }
  183. public void SetShadow(UnityEngine.Color bc, Vector2 distance)
  184. {
  185. mLayer.SetShadow(bc, distance);
  186. }
  187. public void SetFont(Font font)
  188. {
  189. mLayer.SetFont(font);
  190. }
  191. #endregion
  192. }
  193. //----------------------------------------------------------------------------------------
  194. /// <summary>
  195. /// 不可滚动富文本
  196. /// </summary>
  197. public class RichTextPan : DisplayText
  198. {
  199. private readonly UGUIRichTextLayer mLayer;
  200. private bool mScrollToCaret = false;
  201. private Rect mScrollRect = new Rect();
  202. protected readonly bool mUseBitmapFont;
  203. public UGUIRichTextLayer RichTextLayer
  204. {
  205. get { return mLayer; }
  206. }
  207. public AttributedString AText
  208. {
  209. get { return mLayer.GetText(); }
  210. set { mLayer.SetString(value); }
  211. }
  212. public string XmlText
  213. {
  214. set { mLayer.XmlText = value; }
  215. }
  216. public string UnityRichText
  217. {
  218. set { this.XmlText = UIUtils.UnityRichTextToXmlText(value); }
  219. }
  220. public bool AutoScrollToCaret
  221. {
  222. get { return mScrollToCaret; }
  223. set { mScrollToCaret = value; }
  224. }
  225. public Vector2 ContentSize
  226. {
  227. get
  228. {
  229. return new Vector2(mLayer.ContentWidth, mLayer.ContentHeight);
  230. }
  231. }
  232. public RichTextPan(bool use_bitmap, string name = "") : base(name)
  233. {
  234. this.mUseBitmapFont = use_bitmap;
  235. this.mLayer = UIFactory.Instance.CreateRichTextLayer(this, use_bitmap);
  236. this.mLayer.SetWidth(this.Width);
  237. }
  238. public bool TestClick(Vector2 point, out RichTextClickInfo info)
  239. {
  240. return RichTextLayer.Click(point.x, point.y, out info);
  241. }
  242. protected override void OnDispose()
  243. {
  244. mLayer.Dispose();
  245. base.OnDispose();
  246. }
  247. protected override void OnUpdate()
  248. {
  249. base.OnUpdate();
  250. this.mScrollRect.size = this.Size2D;
  251. this.mScrollRect.position = Vector2.zero;
  252. this.mLayer.SetWidth(mScrollRect.width);
  253. if (mScrollToCaret)
  254. {
  255. // todo
  256. float dh = mLayer.ContentHeight - Binding.Height;
  257. if (dh > 0)
  258. {
  259. mScrollRect.y += dh;
  260. }
  261. }
  262. else
  263. {
  264. switch (this.Anchor)
  265. {
  266. case CommonUI.Data.TextAnchor.L_T:
  267. case CommonUI.Data.TextAnchor.C_T:
  268. case CommonUI.Data.TextAnchor.R_T:
  269. break;
  270. case CommonUI.Data.TextAnchor.L_C:
  271. case CommonUI.Data.TextAnchor.C_C:
  272. case CommonUI.Data.TextAnchor.R_C:
  273. mScrollRect.y = -(mScrollRect.height - this.mLayer.ContentHeight) * 0.5f;
  274. break;
  275. case CommonUI.Data.TextAnchor.L_B:
  276. case CommonUI.Data.TextAnchor.C_B:
  277. case CommonUI.Data.TextAnchor.R_B:
  278. mScrollRect.y = -(mScrollRect.height - this.mLayer.ContentHeight);
  279. break;
  280. }
  281. }
  282. this.mLayer.Render(NullGraphics.SimpleGraphics, -mScrollRect.x, -mScrollRect.y, mScrollRect.width, mScrollRect.height, mScrollRect.x, mScrollRect.y);
  283. }
  284. #region ITextComponent
  285. public override string Text
  286. {
  287. get { return mLayer.Text; }
  288. set { mLayer.Text = value; }
  289. }
  290. public override int FontSize
  291. {
  292. get { return mLayer.FontSize; }
  293. set { mLayer.FontSize = value; }
  294. }
  295. public override UnityEngine.Color FontColor
  296. {
  297. get { return mLayer.FontColor; }
  298. set { mLayer.FontColor = value; }
  299. }
  300. public override CommonUI.Data.FontStyle Style
  301. {
  302. get { return mLayer.Style; }
  303. set { mLayer.Style = value; }
  304. }
  305. public override bool IsUnderline
  306. {
  307. get { return mLayer.IsUnderline; }
  308. set { mLayer.IsUnderline = value; }
  309. }
  310. public override Vector2 TextOffset
  311. {
  312. get { return mLayer.TextOffset; }
  313. set { mLayer.TextOffset = value; }
  314. }
  315. public override CommonUI.Data.TextAnchor Anchor
  316. {
  317. get { return mLayer.Anchor; }
  318. set { mLayer.Anchor = value; }
  319. }
  320. public override Vector2 PreferredSize
  321. {
  322. get { return mLayer.PreferredSize; }
  323. }
  324. public override Rect LastCaretPosition
  325. {
  326. get
  327. {
  328. var bounds = mLayer.LastCaretPosition;
  329. bounds.position -= mScrollRect.position;
  330. return bounds;
  331. }
  332. }
  333. public override void SetBorder(UnityEngine.Color bc, Vector2 distance)
  334. {
  335. mLayer.SetBorder(bc, distance);
  336. }
  337. public override void SetShadow(UnityEngine.Color bc, Vector2 distance)
  338. {
  339. mLayer.SetShadow(bc, distance);
  340. }
  341. public override void SetFont(Font font)
  342. {
  343. mLayer.SetFont(font);
  344. }
  345. #endregion
  346. }
  347. //----------------------------------------------------------------------------------------
  348. }