UETextBox.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using CommonLang.Xml;
  2. using CommonUnity3D.UGUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using UnityEngine;
  9. using CommonUI.Data;
  10. using TextAnchor = CommonUI.Data.TextAnchor;
  11. using FontStyle = CommonUI.Data.FontStyle;
  12. namespace CommonUnity3D.UGUIEditor.UI
  13. {
  14. public abstract class UETextBoxBase : UIComponent
  15. {
  16. protected readonly RichTextBox mTextSprite;
  17. protected UETextBoxBase(bool use_bitmap)
  18. {
  19. this.mTextSprite = new RichTextBox("text", use_bitmap);
  20. this.AddChild(mTextSprite);
  21. this.Scrollable = false;
  22. }
  23. //----------------------------------------------------------------------
  24. public string Text
  25. {
  26. get { return mTextSprite.Text; }
  27. set
  28. {
  29. if (IsDispose) return;
  30. mTextSprite.Text = value;
  31. }
  32. }
  33. public int FontSize
  34. {
  35. get { return mTextSprite.FontSize; }
  36. set { mTextSprite.FontSize = value; }
  37. }
  38. public UnityEngine.Color FontColor
  39. {
  40. get { return mTextSprite.FontColor; }
  41. set { mTextSprite.FontColor = value; }
  42. }
  43. public ITextComponent TextComponent
  44. {
  45. get { return mTextSprite; }
  46. }
  47. public ScrollRectInteractive Scroll
  48. {
  49. get { return mTextSprite.Scroll; }
  50. }
  51. public bool Scrollable
  52. {
  53. get { return mTextSprite.Scrollable; }
  54. set { this.mTextSprite.Scrollable = value; }
  55. }
  56. public CommonUI.Display.Text.AttributedString AText
  57. {
  58. get { return mTextSprite.AText; }
  59. set { mTextSprite.AText = value; }
  60. }
  61. public string XmlText
  62. {
  63. set { mTextSprite.RichTextLayer.XmlText = value; }
  64. }
  65. public string UnityRichText
  66. {
  67. set { this.XmlText = UIUtils.UnityRichTextToXmlText(value); }
  68. }
  69. //----------------------------------------------------------------------
  70. private void ResetSize()
  71. {
  72. Vector2 bsize = this.Size2D;
  73. if (this.Layout != null)
  74. {
  75. mTextSprite.Position2D = new Vector2(
  76. Layout.ClipSize,
  77. Layout.ClipSize);
  78. mTextSprite.Size2D = new Vector2(
  79. bsize.x - Layout.ClipSize2,
  80. bsize.y - Layout.ClipSize2);
  81. }
  82. else
  83. {
  84. mTextSprite.Size2D = bsize;
  85. }
  86. this.EnableChildren = mTextSprite.IsNeedScroll;
  87. }
  88. protected override void OnSizeChanged(Vector2 size)
  89. {
  90. base.OnSizeChanged(size);
  91. this.ResetSize();
  92. }
  93. protected override void OnUpdate()
  94. {
  95. base.OnUpdate();
  96. this.ResetSize();
  97. }
  98. protected override void DecodeBegin(UIEditor.Decoder editor, UIComponentMeta e)
  99. {
  100. base.DecodeBegin(editor, e);
  101. }
  102. protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  103. {
  104. base.DecodeEnd(editor, e);
  105. this.Decode_Text(editor, e as UETextBoxBaseMeta);
  106. this.Enable = false;
  107. }
  108. private void Decode_Text(UIEditor.Decoder editor, UETextBoxBaseMeta e)
  109. {
  110. this.FontColor = UIUtils.UInt32_ARGB_To_Color(e.textColor);
  111. if (e.text_size > 0)
  112. {
  113. this.FontSize = e.text_size;
  114. }
  115. if (e.text_shadow_alpha > 0)
  116. {
  117. Color shadow_color = UIUtils.UInt32_ARGB_To_Color(e.text_shadow_dcolor);
  118. shadow_color.a = e.text_shadow_alpha;
  119. mTextSprite.SetShadow(shadow_color, new Vector2(e.text_shadow_x, e.text_shadow_y));
  120. }
  121. }
  122. }
  123. //----------------------------------------------------------------------
  124. public class UETextBox : UETextBoxBase
  125. {
  126. public UETextBox(bool use_bitmap) : base(use_bitmap)
  127. {
  128. }
  129. public UETextBox() : this(UIEditor.GlobalUseBitmapText)
  130. {
  131. }
  132. protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  133. {
  134. base.DecodeEnd(editor, e);
  135. this.Text = (e as UETextBoxMeta).Text;
  136. }
  137. }
  138. public class UETextBoxHtml : UETextBoxBase
  139. {
  140. public UETextBoxHtml(bool use_bitmap) : base(use_bitmap)
  141. {
  142. }
  143. public UETextBoxHtml() : this(UIEditor.GlobalUseBitmapText)
  144. {
  145. }
  146. protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  147. {
  148. base.DecodeEnd(editor, e);
  149. var xmltext = (e as UETextBoxHtmlMeta).HtmlText;
  150. if (!string.IsNullOrEmpty(xmltext))
  151. {
  152. this.UnityRichText = xmltext;
  153. }
  154. }
  155. }
  156. }