123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- 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<UILabel>();
- }
- 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<UITextList.Paragraph> mParagraphs = new BetterList<UITextList.Paragraph>();
- 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;
- }
- }
|