UITextList.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/UI/Text List")]
  5. public class UITextList : MonoBehaviour
  6. {
  7. public bool isValid
  8. {
  9. get
  10. {
  11. return this.textLabel != null && this.textLabel.ambigiousFont != null;
  12. }
  13. }
  14. public float scrollValue
  15. {
  16. get
  17. {
  18. return this.mScroll;
  19. }
  20. set
  21. {
  22. value = Mathf.Clamp01(value);
  23. if (this.isValid && this.mScroll != value)
  24. {
  25. if (this.scrollBar != null)
  26. {
  27. this.scrollBar.value = value;
  28. }
  29. else
  30. {
  31. this.mScroll = value;
  32. this.UpdateVisibleText();
  33. }
  34. }
  35. }
  36. }
  37. protected float lineHeight
  38. {
  39. get
  40. {
  41. return (!(this.textLabel != null)) ? 20f : ((float)this.textLabel.fontSize + this.textLabel.effectiveSpacingY);
  42. }
  43. }
  44. protected int scrollHeight
  45. {
  46. get
  47. {
  48. if (!this.isValid)
  49. {
  50. return 0;
  51. }
  52. int num = Mathf.FloorToInt((float)this.textLabel.height / this.lineHeight);
  53. return Mathf.Max(0, this.mTotalLines - num);
  54. }
  55. }
  56. public void Clear()
  57. {
  58. this.mParagraphs.Clear();
  59. this.UpdateVisibleText();
  60. }
  61. private void Start()
  62. {
  63. if (this.textLabel == null)
  64. {
  65. this.textLabel = base.GetComponentInChildren<UILabel>();
  66. }
  67. if (this.scrollBar != null)
  68. {
  69. EventDelegate.Add(this.scrollBar.onChange, new EventDelegate.Callback(this.OnScrollBar));
  70. }
  71. this.textLabel.overflowMethod = UILabel.Overflow.ClampContent;
  72. if (this.style == UITextList.Style.Chat)
  73. {
  74. this.textLabel.pivot = UIWidget.Pivot.BottomLeft;
  75. this.scrollValue = 1f;
  76. }
  77. else
  78. {
  79. this.textLabel.pivot = UIWidget.Pivot.TopLeft;
  80. this.scrollValue = 0f;
  81. }
  82. }
  83. private void Update()
  84. {
  85. if (this.isValid && (this.textLabel.width != this.mLastWidth || this.textLabel.height != this.mLastHeight))
  86. {
  87. this.mLastWidth = this.textLabel.width;
  88. this.mLastHeight = this.textLabel.height;
  89. this.Rebuild();
  90. }
  91. }
  92. public void OnScroll(float val)
  93. {
  94. int scrollHeight = this.scrollHeight;
  95. if (scrollHeight != 0)
  96. {
  97. val *= this.lineHeight;
  98. this.scrollValue = this.mScroll - val / (float)scrollHeight;
  99. }
  100. }
  101. public void OnDrag(Vector2 delta)
  102. {
  103. int scrollHeight = this.scrollHeight;
  104. if (scrollHeight != 0)
  105. {
  106. float num = delta.y / this.lineHeight;
  107. this.scrollValue = this.mScroll + num / (float)scrollHeight;
  108. }
  109. }
  110. private void OnScrollBar()
  111. {
  112. this.mScroll = UIProgressBar.current.value;
  113. this.UpdateVisibleText();
  114. }
  115. public void Add(string text)
  116. {
  117. this.Add(text, true);
  118. }
  119. protected void Add(string text, bool updateVisible)
  120. {
  121. UITextList.Paragraph paragraph;
  122. if (this.mParagraphs.size < this.paragraphHistory)
  123. {
  124. paragraph = new UITextList.Paragraph();
  125. }
  126. else
  127. {
  128. paragraph = this.mParagraphs[0];
  129. this.mParagraphs.RemoveAt(0);
  130. }
  131. paragraph.text = text;
  132. this.mParagraphs.Add(paragraph);
  133. this.Rebuild();
  134. }
  135. protected void Rebuild()
  136. {
  137. if (this.isValid)
  138. {
  139. this.textLabel.UpdateNGUIText();
  140. NGUIText.rectHeight = 1000000;
  141. this.mTotalLines = 0;
  142. for (int i = 0; i < this.mParagraphs.size; i++)
  143. {
  144. UITextList.Paragraph paragraph = this.mParagraphs.buffer[i];
  145. string text;
  146. NGUIText.WrapText(paragraph.text, out text);
  147. paragraph.lines = text.Split(new char[]
  148. {
  149. '\n'
  150. });
  151. this.mTotalLines += paragraph.lines.Length;
  152. }
  153. this.mTotalLines = 0;
  154. int j = 0;
  155. int size = this.mParagraphs.size;
  156. while (j < size)
  157. {
  158. this.mTotalLines += this.mParagraphs.buffer[j].lines.Length;
  159. j++;
  160. }
  161. if (this.scrollBar != null)
  162. {
  163. UIScrollBar uiscrollBar = this.scrollBar as UIScrollBar;
  164. if (uiscrollBar != null)
  165. {
  166. uiscrollBar.barSize = ((this.mTotalLines != 0) ? (1f - (float)this.scrollHeight / (float)this.mTotalLines) : 1f);
  167. }
  168. }
  169. this.UpdateVisibleText();
  170. }
  171. }
  172. protected void UpdateVisibleText()
  173. {
  174. if (this.isValid)
  175. {
  176. if (this.mTotalLines == 0)
  177. {
  178. this.textLabel.text = string.Empty;
  179. return;
  180. }
  181. int num = Mathf.FloorToInt((float)this.textLabel.height / this.lineHeight);
  182. int num2 = Mathf.Max(0, this.mTotalLines - num);
  183. int num3 = Mathf.RoundToInt(this.mScroll * (float)num2);
  184. if (num3 < 0)
  185. {
  186. num3 = 0;
  187. }
  188. StringBuilder stringBuilder = new StringBuilder();
  189. int num4 = 0;
  190. int size = this.mParagraphs.size;
  191. while (num > 0 && num4 < size)
  192. {
  193. UITextList.Paragraph paragraph = this.mParagraphs.buffer[num4];
  194. int num5 = 0;
  195. int num6 = paragraph.lines.Length;
  196. while (num > 0 && num5 < num6)
  197. {
  198. string value = paragraph.lines[num5];
  199. if (num3 > 0)
  200. {
  201. num3--;
  202. }
  203. else
  204. {
  205. if (stringBuilder.Length > 0)
  206. {
  207. stringBuilder.Append("\n");
  208. }
  209. stringBuilder.Append(value);
  210. num--;
  211. }
  212. num5++;
  213. }
  214. num4++;
  215. }
  216. this.textLabel.text = stringBuilder.ToString();
  217. }
  218. }
  219. public UILabel textLabel;
  220. public UIProgressBar scrollBar;
  221. public UITextList.Style style;
  222. public int paragraphHistory = 50;
  223. protected char[] mSeparator = new char[]
  224. {
  225. '\n'
  226. };
  227. protected BetterList<UITextList.Paragraph> mParagraphs = new BetterList<UITextList.Paragraph>();
  228. protected float mScroll;
  229. protected int mTotalLines;
  230. protected int mLastWidth;
  231. protected int mLastHeight;
  232. public enum Style
  233. {
  234. Text,
  235. Chat
  236. }
  237. protected class Paragraph
  238. {
  239. public string text;
  240. public string[] lines;
  241. }
  242. }