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