GTextField.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. namespace FairyGUI
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class GTextField : GObject, ITextColorGear
  11. {
  12. protected TextField _textField;
  13. protected string _text;
  14. protected bool _ubbEnabled;
  15. protected bool _updatingSize;
  16. protected Dictionary<string, string> _templateVars;
  17. public GTextField()
  18. : base()
  19. {
  20. TextFormat tf = _textField.textFormat;
  21. tf.font = UIConfig.defaultFont;
  22. tf.size = 12;
  23. tf.color = Color.black;
  24. tf.lineSpacing = 3;
  25. tf.letterSpacing = 0;
  26. _textField.textFormat = tf;
  27. _text = string.Empty;
  28. _textField.autoSize = AutoSizeType.Both;
  29. _textField.wordWrap = false;
  30. }
  31. override protected void CreateDisplayObject()
  32. {
  33. _textField = new TextField();
  34. _textField.gOwner = this;
  35. displayObject = _textField;
  36. }
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. override public string text
  41. {
  42. get
  43. {
  44. if (this is GTextInput)
  45. _text = ((GTextInput)this).inputTextField.text;
  46. return _text;
  47. }
  48. set
  49. {
  50. if (value == null)
  51. value = string.Empty;
  52. _text = value;
  53. SetTextFieldText();
  54. UpdateSize();
  55. UpdateGear(6);
  56. }
  57. }
  58. virtual protected void SetTextFieldText()
  59. {
  60. string str = _text;
  61. if (_templateVars != null)
  62. str = ParseTemplate(str);
  63. _textField.maxWidth = maxWidth;
  64. if (_ubbEnabled)
  65. _textField.htmlText = UBBParser.inst.Parse(XMLUtils.EncodeString(str));
  66. else
  67. _textField.text = str;
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. public Dictionary<string, string> templateVars
  73. {
  74. get { return _templateVars; }
  75. set
  76. {
  77. if (_templateVars == null && value == null)
  78. return;
  79. _templateVars = value;
  80. FlushVars();
  81. }
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <param name="name"></param>
  87. /// <param name="value"></param>
  88. /// <returns></returns>
  89. public GTextField SetVar(string name, string value)
  90. {
  91. if (_templateVars == null)
  92. _templateVars = new Dictionary<string, string>();
  93. _templateVars[name] = value;
  94. return this;
  95. }
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. public void FlushVars()
  100. {
  101. SetTextFieldText();
  102. UpdateSize();
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. public bool HasCharacter(char ch)
  108. {
  109. return _textField.HasCharacter(ch);
  110. }
  111. protected string ParseTemplate(string template)
  112. {
  113. int pos1 = 0, pos2 = 0;
  114. int pos3;
  115. string tag;
  116. string value;
  117. StringBuilder buffer = new StringBuilder();
  118. while ((pos2 = template.IndexOf('{', pos1)) != -1)
  119. {
  120. if (pos2 > 0 && template[pos2 - 1] == '\\')
  121. {
  122. buffer.Append(template, pos1, pos2 - pos1 - 1);
  123. buffer.Append('{');
  124. pos1 = pos2 + 1;
  125. continue;
  126. }
  127. buffer.Append(template, pos1, pos2 - pos1);
  128. pos1 = pos2;
  129. pos2 = template.IndexOf('}', pos1);
  130. if (pos2 == -1)
  131. break;
  132. if (pos2 == pos1 + 1)
  133. {
  134. buffer.Append(template, pos1, 2);
  135. pos1 = pos2 + 1;
  136. continue;
  137. }
  138. tag = template.Substring(pos1 + 1, pos2 - pos1 - 1);
  139. pos3 = tag.IndexOf('=');
  140. if (pos3 != -1)
  141. {
  142. if (!_templateVars.TryGetValue(tag.Substring(0, pos3), out value))
  143. value = tag.Substring(pos3 + 1);
  144. }
  145. else
  146. {
  147. if (!_templateVars.TryGetValue(tag, out value))
  148. value = "";
  149. }
  150. buffer.Append(value);
  151. pos1 = pos2 + 1;
  152. }
  153. if (pos1 < template.Length)
  154. buffer.Append(template, pos1, template.Length - pos1);
  155. return buffer.ToString();
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. public TextFormat textFormat
  161. {
  162. get
  163. {
  164. return _textField.textFormat;
  165. }
  166. set
  167. {
  168. _textField.textFormat = value;
  169. if (!underConstruct)
  170. UpdateSize();
  171. }
  172. }
  173. /// <summary>
  174. ///
  175. /// </summary>
  176. public Color color
  177. {
  178. get
  179. {
  180. return _textField.textFormat.color;
  181. }
  182. set
  183. {
  184. if (_textField.textFormat.color != value)
  185. {
  186. TextFormat tf = _textField.textFormat;
  187. tf.color = value;
  188. _textField.textFormat = tf;
  189. UpdateGear(4);
  190. }
  191. }
  192. }
  193. /// <summary>
  194. ///
  195. /// </summary>
  196. public AlignType align
  197. {
  198. get { return _textField.align; }
  199. set { _textField.align = value; }
  200. }
  201. /// <summary>
  202. ///
  203. /// </summary>
  204. public VertAlignType verticalAlign
  205. {
  206. get { return _textField.verticalAlign; }
  207. set { _textField.verticalAlign = value; }
  208. }
  209. /// <summary>
  210. ///
  211. /// </summary>
  212. public bool singleLine
  213. {
  214. get { return _textField.singleLine; }
  215. set { _textField.singleLine = value; }
  216. }
  217. /// <summary>
  218. ///
  219. /// </summary>
  220. public float stroke
  221. {
  222. get { return _textField.stroke; }
  223. set { _textField.stroke = value; }
  224. }
  225. /// <summary>
  226. ///
  227. /// </summary>
  228. public Color strokeColor
  229. {
  230. get { return _textField.strokeColor; }
  231. set
  232. {
  233. _textField.strokeColor = value;
  234. UpdateGear(4);
  235. }
  236. }
  237. /// <summary>
  238. ///
  239. /// </summary>
  240. public Vector2 shadowOffset
  241. {
  242. get { return _textField.shadowOffset; }
  243. set { _textField.shadowOffset = value; }
  244. }
  245. /// <summary>
  246. ///
  247. /// </summary>
  248. public bool UBBEnabled
  249. {
  250. get { return _ubbEnabled; }
  251. set { _ubbEnabled = value; }
  252. }
  253. /// <summary>
  254. ///
  255. /// </summary>
  256. public AutoSizeType autoSize
  257. {
  258. get { return _textField.autoSize; }
  259. set
  260. {
  261. _textField.autoSize = value;
  262. if (value == AutoSizeType.Both)
  263. {
  264. _textField.wordWrap = false;
  265. if (!underConstruct)
  266. this.SetSize(_textField.textWidth, _textField.textHeight);
  267. }
  268. else
  269. {
  270. _textField.wordWrap = true;
  271. if (value == AutoSizeType.Height)
  272. {
  273. if (!underConstruct)
  274. {
  275. displayObject.width = this.width;
  276. this.height = _textField.textHeight;
  277. }
  278. }
  279. else
  280. displayObject.SetSize(this.width, this.height);
  281. }
  282. }
  283. }
  284. /// <summary>
  285. ///
  286. /// </summary>
  287. public float textWidth
  288. {
  289. get { return _textField.textWidth; }
  290. }
  291. /// <summary>
  292. ///
  293. /// </summary>
  294. public float textHeight
  295. {
  296. get { return _textField.textHeight; }
  297. }
  298. protected void UpdateSize()
  299. {
  300. if (_updatingSize)
  301. return;
  302. _updatingSize = true;
  303. if (_textField.autoSize == AutoSizeType.Both)
  304. {
  305. this.size = displayObject.size;
  306. InvalidateBatchingState();
  307. }
  308. else if (_textField.autoSize == AutoSizeType.Height)
  309. {
  310. this.height = displayObject.height;
  311. InvalidateBatchingState();
  312. }
  313. _updatingSize = false;
  314. }
  315. override protected void HandleSizeChanged()
  316. {
  317. if (_updatingSize)
  318. return;
  319. if (underConstruct)
  320. displayObject.SetSize(this.width, this.height);
  321. else if (_textField.autoSize != AutoSizeType.Both)
  322. {
  323. if (_textField.autoSize == AutoSizeType.Height)
  324. {
  325. displayObject.width = this.width;//先调整宽度,让文本重排
  326. if (_text != string.Empty) //文本为空时,1是本来就不需要调整, 2是为了防止改掉文本为空时的默认高度,造成关联错误
  327. SetSizeDirectly(this.width, displayObject.height);
  328. }
  329. else
  330. displayObject.SetSize(this.width, this.height);
  331. }
  332. }
  333. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  334. {
  335. base.Setup_BeforeAdd(buffer, beginPos);
  336. buffer.Seek(beginPos, 5);
  337. TextFormat tf = _textField.textFormat;
  338. tf.font = buffer.ReadS();
  339. tf.size = buffer.ReadShort();
  340. tf.color = buffer.ReadColor();
  341. this.align = (AlignType)buffer.ReadByte();
  342. this.verticalAlign = (VertAlignType)buffer.ReadByte();
  343. tf.lineSpacing = buffer.ReadShort();
  344. tf.letterSpacing = buffer.ReadShort();
  345. _ubbEnabled = buffer.ReadBool();
  346. this.autoSize = (AutoSizeType)buffer.ReadByte();
  347. tf.underline = buffer.ReadBool();
  348. tf.italic = buffer.ReadBool();
  349. tf.bold = buffer.ReadBool();
  350. this.singleLine = buffer.ReadBool();
  351. if (buffer.ReadBool())
  352. {
  353. tf.outlineColor = buffer.ReadColor();
  354. tf.outline = buffer.ReadFloat();
  355. }
  356. if (buffer.ReadBool())
  357. {
  358. tf.shadowColor = buffer.ReadColor();
  359. float f1 = buffer.ReadFloat();
  360. float f2 = buffer.ReadFloat();
  361. tf.shadowOffset = new Vector2(f1, f2);
  362. }
  363. if (buffer.ReadBool())
  364. _templateVars = new Dictionary<string, string>();
  365. if (buffer.version >= 3)
  366. {
  367. tf.strikethrough = buffer.ReadBool();
  368. #if FAIRYGUI_TMPRO
  369. tf.faceDilate = buffer.ReadFloat();
  370. tf.outlineSoftness = buffer.ReadFloat();
  371. tf.underlaySoftness = buffer.ReadFloat();
  372. #else
  373. buffer.Skip(12);
  374. #endif
  375. }
  376. _textField.textFormat = tf;
  377. }
  378. override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  379. {
  380. base.Setup_AfterAdd(buffer, beginPos);
  381. buffer.Seek(beginPos, 6);
  382. string str = buffer.ReadS();
  383. if (str != null)
  384. this.text = str;
  385. }
  386. }
  387. }