summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/TextRenderer.cs
blob: e0893f1dea3d39861c6e9ee69aa3cb3ab9dab572 (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class TextRenderer : MonoBehaviour
{
	public float Width { get; private set; }

	public float Height { get; private set; }

	public Vector3 CursorPos
	{
		get
		{
			return new Vector3(this.cursorLocation.x / 100f * this.scale, this.cursorLocation.y / 100f * this.scale, -0.001f);
		}
	}

	public TextAsset FontData;

	public float scale = 1f;

	public float TabWidth = 0.5f;

	public bool Centered;

	public bool RightAligned;

	public TextLink textLinkPrefab;

	[HideInInspector]
	private Mesh mesh;

	[HideInInspector]
	private MeshRenderer render;

	[Multiline]
	public string Text;

	private string lastText;

	public Color Color = Color.white;

	private Color lastColor = Color.white;

	public Color OutlineColor = Color.black;

	private Color lastOutlineColor = Color.white;

	public float maxWidth = -1f;

	public bool scaleToFit;

	public bool paragraphSpacing;

	private Vector2 cursorLocation;

	public void Start()
	{
		this.render = base.GetComponent<MeshRenderer>();
		MeshFilter component = base.GetComponent<MeshFilter>();
		if (!component.mesh)
		{
			this.mesh = new Mesh();
			this.mesh.name = "Text" + base.name;
			component.mesh = this.mesh;
			this.render.material.SetColor("_OutlineColor", this.OutlineColor);
			return;
		}
		this.mesh = component.mesh;
	}

	[ContextMenu("Generate Mesh")]
	public void GenerateMesh()
	{
		this.render = base.GetComponent<MeshRenderer>();
		MeshFilter component = base.GetComponent<MeshFilter>();
		if (!component.sharedMesh)
		{
			this.mesh = new Mesh();
			this.mesh.name = "Text" + base.name;
			component.mesh = this.mesh;
		}
		else
		{
			this.mesh = component.sharedMesh;
		}
		this.lastText = null;
		this.lastOutlineColor = this.OutlineColor;
		this.Update();
	}

	private void Update()
	{
		if (this.lastOutlineColor != this.OutlineColor)
		{
			this.lastOutlineColor = this.OutlineColor;
			this.render.material.SetColor("_OutlineColor", this.OutlineColor);
		}
		if (this.lastText != this.Text || this.lastColor != this.Color)
		{
			this.RefreshMesh();
		}
	}

	public void RefreshMesh()
	{
		if (this.render == null)
		{
			this.Start();
		}
		if (this.Text != null)
		{
			if (this.Text.Any((char c) => c > '✐'))
			{
				FontCache.Instance.SetFont(this, "Korean");
			}
		}
		FontData fontData = FontCache.Instance.LoadFont(this.FontData);
		this.lastText = this.Text;
		this.lastColor = this.Color;
		float num = this.scale;
		if (this.scaleToFit)
		{
			num = Mathf.Min(this.scale, this.maxWidth / this.GetMaxWidth(fontData, this.lastText));
		}
		else if (this.maxWidth > 0f)
		{
			this.lastText = (this.Text = TextRenderer.WrapText(fontData, this.lastText, this.maxWidth));
		}
		List<Vector3> list = new List<Vector3>(this.lastText.Length * 4);
		List<Vector2> list2 = new List<Vector2>(this.lastText.Length * 4);
		List<Vector4> list3 = new List<Vector4>(this.lastText.Length * 4);
		List<Color> list4 = new List<Color>(this.lastText.Length * 4);
		int[] array = new int[this.lastText.Length * 6];
		this.Width = 0f;
		this.cursorLocation.x = (this.cursorLocation.y = 0f);
		int num2 = -1;
		Vector2 from = default(Vector2);
		string text = null;
		int lineStart = 0;
		int num3 = 0;
		Color item = this.Color;
		int? num4 = null;
		int i = 0;
		while (i < this.lastText.Length)
		{
			int num5 = (int)this.lastText[i];
			if (num5 == 91)
			{
				if (num2 != 91)
				{
					if (this.lastText[i + 1] == '[')
					{
						goto IL_422;
					}
					num4 = new int?(0);
					num2 = num5;
				}
			}
			else
			{
				if (num4 == null)
				{
					goto IL_422;
				}
				if (num5 == 93)
				{
					if (num2 != 91)
					{
						int? num6 = num4;
						byte r = (byte)((num6 != null) ? new int?(num6.GetValueOrDefault() >> 24 & 255) : null).Value;
						int? num7 = num4;
						byte g = (byte)((num7 != null) ? new int?(num7.GetValueOrDefault() >> 16 & 255) : null).Value;
						int? num8 = num4;
						item = new Color32(r, g, (byte)((num8 != null) ? new int?(num8.GetValueOrDefault() >> 8 & 255) : null).Value, (byte)(num4 & 255).Value);
						item.a *= this.Color.a;
					}
					else
					{
						item = this.Color;
					}
					num4 = null;
					if (text != null)
					{
						TextLink textLink = UnityEngine.Object.Instantiate<TextLink>(this.textLinkPrefab, base.transform);
						textLink.transform.localScale = Vector3.one;
						Vector3 v = list.Last<Vector3>();
						textLink.Set(from, v, text);
						text = null;
					}
				}
				else if (num5 == 104)
				{
					int num9 = this.lastText.IndexOf(']', i);
					text = this.lastText.Substring(i, num9 - i);
					from = list[list.Count - 2];
					item = new Color(0.5f, 0.5f, 1f);
					num4 = null;
					i = num9;
				}
				else
				{
					num4 = (num4 << 4 | this.CharToInt(num5));
				}
				num2 = num5;
			}
			IL_81F:
			i++;
			continue;
			IL_422:
			if (num5 == 13)
			{
				goto IL_81F;
			}
			if (num5 == 10)
			{
				if (this.Centered)
				{
					this.CenterVerts(list, this.cursorLocation.x, lineStart, num);
				}
				else if (this.RightAligned)
				{
					this.RightAlignVerts(list, this.cursorLocation.x, lineStart, num);
				}
				bool flag = this.cursorLocation.x == 0f;
				this.cursorLocation.x = 0f;
				if (flag)
				{
					this.cursorLocation.y = this.cursorLocation.y - fontData.LineHeight / 2f;
				}
				else
				{
					this.cursorLocation.y = this.cursorLocation.y - fontData.LineHeight;
				}
				lineStart = list.Count;
				goto IL_81F;
			}
			if (num5 == 9)
			{
				float num10 = this.cursorLocation.x / 100f;
				num10 = Mathf.Ceil(num10 / this.TabWidth) * this.TabWidth;
				this.cursorLocation.x = num10 * 100f;
				goto IL_81F;
			}
			int index;
			if (!fontData.charMap.TryGetValue(num5, out index))
			{
				Debug.Log("Missing char :" + num5);
				num5 = -1;
				index = fontData.charMap[-1];
			}
			Vector4 vector = fontData.bounds[index];
			Vector2 textureSize = fontData.TextureSize;
			Vector3 vector2 = fontData.offsets[index];
			float kerning = fontData.GetKerning(num2, num5);
			float num11 = this.cursorLocation.x + vector2.x + kerning;
			float num12 = this.cursorLocation.y - vector2.y;
			list.Add(new Vector3(num11, num12 - vector.w) / 100f * num);
			list.Add(new Vector3(num11, num12) / 100f * num);
			list.Add(new Vector3(num11 + vector.z, num12) / 100f * num);
			list.Add(new Vector3(num11 + vector.z, num12 - vector.w) / 100f * num);
			list4.Add(item);
			list4.Add(item);
			list4.Add(item);
			list4.Add(item);
			list2.Add(new Vector2(vector.x / textureSize.x, 1f - (vector.y + vector.w) / textureSize.y));
			list2.Add(new Vector2(vector.x / textureSize.x, 1f - vector.y / textureSize.y));
			list2.Add(new Vector2((vector.x + vector.z) / textureSize.x, 1f - vector.y / textureSize.y));
			list2.Add(new Vector2((vector.x + vector.z) / textureSize.x, 1f - (vector.y + vector.w) / textureSize.y));
			Vector4 item2 = fontData.Channels[index];
			list3.Add(item2);
			list3.Add(item2);
			list3.Add(item2);
			list3.Add(item2);
			array[num3 * 6] = num3 * 4;
			array[num3 * 6 + 1] = num3 * 4 + 1;
			array[num3 * 6 + 2] = num3 * 4 + 2;
			array[num3 * 6 + 3] = num3 * 4;
			array[num3 * 6 + 4] = num3 * 4 + 2;
			array[num3 * 6 + 5] = num3 * 4 + 3;
			this.cursorLocation.x = this.cursorLocation.x + (vector2.z + kerning);
			float num13 = this.cursorLocation.x / 100f * num;
			if (this.Width < num13)
			{
				this.Width = num13;
			}
			num2 = num5;
			num3++;
			goto IL_81F;
		}
		if (this.Centered)
		{
			this.CenterVerts(list, this.cursorLocation.x, lineStart, num);
			this.cursorLocation.x = this.cursorLocation.x / 2f;
			this.Width /= 2f;
		}
		else if (this.RightAligned)
		{
			this.RightAlignVerts(list, this.cursorLocation.x, lineStart, num);
		}
		this.Height = -(this.cursorLocation.y - fontData.LineHeight) / 100f * num;
		this.mesh.Clear();
		if (list.Count > 0)
		{
			this.mesh.SetVertices(list);
			this.mesh.SetColors(list4);
			this.mesh.SetUVs(0, list2);
			this.mesh.SetUVs(1, list3);
			this.mesh.SetIndices(array, MeshTopology.Triangles, 0);
		}
	}

	private float GetMaxWidth(FontData data, string lastText)
	{
		float num = 0f;
		float num2 = 0f;
		int last = -1;
		bool flag = false;
		int num3 = 0;
		int num4 = 0;
		while (num4 < lastText.Length && num3++ <= 1000)
		{
			int num5 = (int)lastText[num4];
			if (num5 == 91)
			{
				flag = true;
				goto IL_4D;
			}
			if (num5 != 93)
			{
				goto IL_4D;
			}
			flag = false;
			IL_100:
			num4++;
			continue;
			IL_4D:
			if (flag || num5 == 13)
			{
				goto IL_100;
			}
			if (num5 == 10)
			{
				last = -1;
				num2 = 0f;
				goto IL_100;
			}
			if (num5 == 9)
			{
				num2 = Mathf.Ceil(num2 / 100f / 0.5f) * 0.5f * 100f;
				goto IL_100;
			}
			int index;
			if (!data.charMap.TryGetValue(num5, out index))
			{
				Debug.Log("Missing char :" + num5);
				num5 = -1;
				index = data.charMap[-1];
			}
			Vector3 vector = data.offsets[index];
			num2 += vector.z + data.GetKerning(last, num5);
			if (num2 > num)
			{
				num = num2;
			}
			last = num5;
			goto IL_100;
		}
		return num / 100f;
	}

	private void RightAlignVerts(List<Vector3> verts, float baseX, int lineStart, float scale)
	{
		for (int i = lineStart; i < verts.Count; i++)
		{
			Vector3 value = verts[i];
			value.x -= baseX / 100f * scale;
			verts[i] = value;
		}
	}

	private void CenterVerts(List<Vector3> verts, float baseX, int lineStart, float scale)
	{
		for (int i = lineStart; i < verts.Count; i++)
		{
			Vector3 value = verts[i];
			value.x -= baseX / 200f * scale;
			verts[i] = value;
		}
	}

	private int CharToInt(int c)
	{
		if (c < 65)
		{
			return c - 48;
		}
		if (c < 97)
		{
			return 10 + (c - 65);
		}
		return 10 + (c - 97);
	}

	public static string WrapText(FontData data, string displayTxt, float maxWidth)
	{
		float num = 0f;
		int num2 = -1;
		int last = -1;
		bool flag = false;
		int num3 = 0;
		int num4 = 0;
		while (num4 < displayTxt.Length && num3++ <= 1000)
		{
			int num5 = (int)displayTxt[num4];
			if (num5 == 91)
			{
				flag = true;
				goto IL_49;
			}
			if (num5 != 93)
			{
				goto IL_49;
			}
			flag = false;
			IL_155:
			num4++;
			continue;
			IL_49:
			if (flag || num5 == 13)
			{
				goto IL_155;
			}
			if (num5 == 10)
			{
				num2 = -1;
				last = -1;
				num = 0f;
				goto IL_155;
			}
			if (num5 == 9)
			{
				num = Mathf.Ceil(num / 100f / 0.5f) * 0.5f * 100f;
				goto IL_155;
			}
			int index;
			if (!data.charMap.TryGetValue(num5, out index))
			{
				Debug.Log("Missing char :" + num5);
				num5 = -1;
				index = data.charMap[-1];
			}
			if (num5 == 32)
			{
				num2 = num4;
			}
			Vector3 vector = data.offsets[index];
			num += vector.z + data.GetKerning(last, num5);
			if (num > maxWidth * 100f)
			{
				if (num2 != -1)
				{
					displayTxt = displayTxt.Substring(0, num2) + "\n" + displayTxt.Substring(num2 + 1);
					num4 = num2;
				}
				else
				{
					displayTxt = displayTxt.Substring(0, num4) + "\n" + displayTxt.Substring(num4);
				}
				num2 = -1;
				num = 0f;
			}
			last = num5;
			goto IL_155;
		}
		return displayTxt;
	}
}