using System; using System.Text; using UnityEngine; [AddComponentMenu("NGUI/UI/Text List")] public class UITextList : MonoBehaviour { public bool isValid { get { return this.textLabel != null && this.textLabel.ambigiousFont != null; } } public float scrollValue { get { return this.mScroll; } set { value = Mathf.Clamp01(value); if (this.isValid && this.mScroll != value) { if (this.scrollBar != null) { this.scrollBar.value = value; } else { this.mScroll = value; this.UpdateVisibleText(); } } } } protected float lineHeight { get { return (!(this.textLabel != null)) ? 20f : ((float)this.textLabel.fontSize + this.textLabel.effectiveSpacingY); } } protected int scrollHeight { get { if (!this.isValid) { return 0; } int num = Mathf.FloorToInt((float)this.textLabel.height / this.lineHeight); return Mathf.Max(0, this.mTotalLines - num); } } public void Clear() { this.mParagraphs.Clear(); this.UpdateVisibleText(); } private void Start() { if (this.textLabel == null) { this.textLabel = base.GetComponentInChildren(); } if (this.scrollBar != null) { EventDelegate.Add(this.scrollBar.onChange, new EventDelegate.Callback(this.OnScrollBar)); } this.textLabel.overflowMethod = UILabel.Overflow.ClampContent; if (this.style == UITextList.Style.Chat) { this.textLabel.pivot = UIWidget.Pivot.BottomLeft; this.scrollValue = 1f; } else { this.textLabel.pivot = UIWidget.Pivot.TopLeft; this.scrollValue = 0f; } } private void Update() { if (this.isValid && (this.textLabel.width != this.mLastWidth || this.textLabel.height != this.mLastHeight)) { this.mLastWidth = this.textLabel.width; this.mLastHeight = this.textLabel.height; this.Rebuild(); } } public void OnScroll(float val) { int scrollHeight = this.scrollHeight; if (scrollHeight != 0) { val *= this.lineHeight; this.scrollValue = this.mScroll - val / (float)scrollHeight; } } public void OnDrag(Vector2 delta) { int scrollHeight = this.scrollHeight; if (scrollHeight != 0) { float num = delta.y / this.lineHeight; this.scrollValue = this.mScroll + num / (float)scrollHeight; } } private void OnScrollBar() { this.mScroll = UIProgressBar.current.value; this.UpdateVisibleText(); } public void Add(string text) { this.Add(text, true); } protected void Add(string text, bool updateVisible) { UITextList.Paragraph paragraph; if (this.mParagraphs.size < this.paragraphHistory) { paragraph = new UITextList.Paragraph(); } else { paragraph = this.mParagraphs[0]; this.mParagraphs.RemoveAt(0); } paragraph.text = text; this.mParagraphs.Add(paragraph); this.Rebuild(); } protected void Rebuild() { if (this.isValid) { this.textLabel.UpdateNGUIText(); NGUIText.rectHeight = 1000000; this.mTotalLines = 0; for (int i = 0; i < this.mParagraphs.size; i++) { UITextList.Paragraph paragraph = this.mParagraphs.buffer[i]; string text; NGUIText.WrapText(paragraph.text, out text); paragraph.lines = text.Split(new char[] { '\n' }); this.mTotalLines += paragraph.lines.Length; } this.mTotalLines = 0; int j = 0; int size = this.mParagraphs.size; while (j < size) { this.mTotalLines += this.mParagraphs.buffer[j].lines.Length; j++; } if (this.scrollBar != null) { UIScrollBar uiscrollBar = this.scrollBar as UIScrollBar; if (uiscrollBar != null) { uiscrollBar.barSize = ((this.mTotalLines != 0) ? (1f - (float)this.scrollHeight / (float)this.mTotalLines) : 1f); } } this.UpdateVisibleText(); } } protected void UpdateVisibleText() { if (this.isValid) { if (this.mTotalLines == 0) { this.textLabel.text = string.Empty; return; } int num = Mathf.FloorToInt((float)this.textLabel.height / this.lineHeight); int num2 = Mathf.Max(0, this.mTotalLines - num); int num3 = Mathf.RoundToInt(this.mScroll * (float)num2); if (num3 < 0) { num3 = 0; } StringBuilder stringBuilder = new StringBuilder(); int num4 = 0; int size = this.mParagraphs.size; while (num > 0 && num4 < size) { UITextList.Paragraph paragraph = this.mParagraphs.buffer[num4]; int num5 = 0; int num6 = paragraph.lines.Length; while (num > 0 && num5 < num6) { string value = paragraph.lines[num5]; if (num3 > 0) { num3--; } else { if (stringBuilder.Length > 0) { stringBuilder.Append("\n"); } stringBuilder.Append(value); num--; } num5++; } num4++; } this.textLabel.text = stringBuilder.ToString(); } } public UILabel textLabel; public UIProgressBar scrollBar; public UITextList.Style style; public int paragraphHistory = 50; protected char[] mSeparator = new char[] { '\n' }; protected BetterList mParagraphs = new BetterList(); protected float mScroll; protected int mTotalLines; protected int mLastWidth; protected int mLastHeight; public enum Style { Text, Chat } protected class Paragraph { public string text; public string[] lines; } }