summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc/LocalizeTarget_TextMeshPro_Label.cs
blob: 68026424314bc4634c8a874fa1832f7b4856ad27 (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
using System;
using TMPro;
using UnityEngine;

namespace I2.Loc;

public class LocalizeTarget_TextMeshPro_Label : LocalizeTarget<TextMeshPro>
{
	private TextAlignmentOptions mAlignment_RTL = TextAlignmentOptions.Right;

	private TextAlignmentOptions mAlignment_LTR = TextAlignmentOptions.Left;

	private bool mAlignmentWasRTL;

	private bool mInitializeAlignment = true;

	static LocalizeTarget_TextMeshPro_Label()
	{
		AutoRegister();
	}

	[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
	private static void AutoRegister()
	{
		LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<TextMeshPro, LocalizeTarget_TextMeshPro_Label>
		{
			Name = "TextMeshPro Label",
			Priority = 100
		});
	}

	public override eTermType GetPrimaryTermType(Localize cmp)
	{
		return eTermType.Text;
	}

	public override eTermType GetSecondaryTermType(Localize cmp)
	{
		return eTermType.Font;
	}

	public override bool CanUseSecondaryTerm()
	{
		return true;
	}

	public override bool AllowMainTermToBeRTL()
	{
		return true;
	}

	public override bool AllowSecondTermToBeRTL()
	{
		return false;
	}

	public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
	{
		primaryTerm = (mTarget ? mTarget.text : null);
		secondaryTerm = ((mTarget.font != null) ? mTarget.font.name : string.Empty);
	}

	public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
	{
		TMP_FontAsset secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<TMP_FontAsset>(ref mainTranslation, ref secondaryTranslation);
		if (secondaryTranslatedObj != null)
		{
			SetFont(mTarget, secondaryTranslatedObj);
		}
		else
		{
			Material secondaryTranslatedObj2 = cmp.GetSecondaryTranslatedObj<Material>(ref mainTranslation, ref secondaryTranslation);
			if (secondaryTranslatedObj2 != null && mTarget.fontMaterial != secondaryTranslatedObj2)
			{
				if (!secondaryTranslatedObj2.name.StartsWith(mTarget.font.name, StringComparison.Ordinal))
				{
					secondaryTranslatedObj = GetTMPFontFromMaterial(cmp, secondaryTranslation.EndsWith(secondaryTranslatedObj2.name, StringComparison.Ordinal) ? secondaryTranslation : secondaryTranslatedObj2.name);
					if (secondaryTranslatedObj != null)
					{
						SetFont(mTarget, secondaryTranslatedObj);
					}
				}
				SetMaterial(mTarget, secondaryTranslatedObj2);
			}
		}
		if (mInitializeAlignment)
		{
			mInitializeAlignment = false;
			mAlignmentWasRTL = LocalizationManager.IsRight2Left;
			InitAlignment_TMPro(mAlignmentWasRTL, mTarget.alignment, out mAlignment_LTR, out mAlignment_RTL);
		}
		else
		{
			InitAlignment_TMPro(mAlignmentWasRTL, mTarget.alignment, out var alignLTR, out var alignRTL);
			if ((mAlignmentWasRTL && mAlignment_RTL != alignRTL) || (!mAlignmentWasRTL && mAlignment_LTR != alignLTR))
			{
				mAlignment_LTR = alignLTR;
				mAlignment_RTL = alignRTL;
			}
			mAlignmentWasRTL = LocalizationManager.IsRight2Left;
		}
		if (mainTranslation != null && mTarget.text != mainTranslation)
		{
			if (cmp.CorrectAlignmentForRTL)
			{
				mTarget.alignment = (LocalizationManager.IsRight2Left ? mAlignment_RTL : mAlignment_LTR);
			}
			mTarget.isRightToLeftText = LocalizationManager.IsRight2Left;
			if (LocalizationManager.IsRight2Left)
			{
				mainTranslation = I2Utils.ReverseText(mainTranslation);
			}
			mTarget.text = mainTranslation;
		}
	}

	internal static TMP_FontAsset GetTMPFontFromMaterial(Localize cmp, string matName)
	{
		string text = " .\\/-[]()";
		int num = matName.Length - 1;
		while (num > 0)
		{
			while (num > 0 && text.IndexOf(matName[num]) >= 0)
			{
				num--;
			}
			if (num <= 0)
			{
				break;
			}
			string translation = matName.Substring(0, num + 1);
			TMP_FontAsset @object = cmp.GetObject<TMP_FontAsset>(translation);
			if (@object != null)
			{
				return @object;
			}
			while (num > 0 && text.IndexOf(matName[num]) < 0)
			{
				num--;
			}
		}
		return null;
	}

	internal static void InitAlignment_TMPro(bool isRTL, TextAlignmentOptions alignment, out TextAlignmentOptions alignLTR, out TextAlignmentOptions alignRTL)
	{
		alignLTR = (alignRTL = alignment);
		if (isRTL)
		{
			switch (alignment)
			{
			case TextAlignmentOptions.TopRight:
				alignLTR = TextAlignmentOptions.TopLeft;
				break;
			case TextAlignmentOptions.Right:
				alignLTR = TextAlignmentOptions.Left;
				break;
			case TextAlignmentOptions.BottomRight:
				alignLTR = TextAlignmentOptions.BottomLeft;
				break;
			case TextAlignmentOptions.BaselineRight:
				alignLTR = TextAlignmentOptions.BaselineLeft;
				break;
			case TextAlignmentOptions.MidlineRight:
				alignLTR = TextAlignmentOptions.MidlineLeft;
				break;
			case TextAlignmentOptions.CaplineRight:
				alignLTR = TextAlignmentOptions.CaplineLeft;
				break;
			case TextAlignmentOptions.TopLeft:
				alignLTR = TextAlignmentOptions.TopRight;
				break;
			case TextAlignmentOptions.Left:
				alignLTR = TextAlignmentOptions.Right;
				break;
			case TextAlignmentOptions.BottomLeft:
				alignLTR = TextAlignmentOptions.BottomRight;
				break;
			case TextAlignmentOptions.BaselineLeft:
				alignLTR = TextAlignmentOptions.BaselineRight;
				break;
			case TextAlignmentOptions.MidlineLeft:
				alignLTR = TextAlignmentOptions.MidlineRight;
				break;
			case TextAlignmentOptions.CaplineLeft:
				alignLTR = TextAlignmentOptions.CaplineRight;
				break;
			}
		}
		else
		{
			switch (alignment)
			{
			case TextAlignmentOptions.TopRight:
				alignRTL = TextAlignmentOptions.TopLeft;
				break;
			case TextAlignmentOptions.Right:
				alignRTL = TextAlignmentOptions.Left;
				break;
			case TextAlignmentOptions.BottomRight:
				alignRTL = TextAlignmentOptions.BottomLeft;
				break;
			case TextAlignmentOptions.BaselineRight:
				alignRTL = TextAlignmentOptions.BaselineLeft;
				break;
			case TextAlignmentOptions.MidlineRight:
				alignRTL = TextAlignmentOptions.MidlineLeft;
				break;
			case TextAlignmentOptions.CaplineRight:
				alignRTL = TextAlignmentOptions.CaplineLeft;
				break;
			case TextAlignmentOptions.TopLeft:
				alignRTL = TextAlignmentOptions.TopRight;
				break;
			case TextAlignmentOptions.Left:
				alignRTL = TextAlignmentOptions.Right;
				break;
			case TextAlignmentOptions.BottomLeft:
				alignRTL = TextAlignmentOptions.BottomRight;
				break;
			case TextAlignmentOptions.BaselineLeft:
				alignRTL = TextAlignmentOptions.BaselineRight;
				break;
			case TextAlignmentOptions.MidlineLeft:
				alignRTL = TextAlignmentOptions.MidlineRight;
				break;
			case TextAlignmentOptions.CaplineLeft:
				alignRTL = TextAlignmentOptions.CaplineRight;
				break;
			}
		}
	}

	internal static void SetFont(TMP_Text label, TMP_FontAsset newFont)
	{
		if (label.font != newFont)
		{
			label.font = newFont;
		}
		if (label.linkedTextComponent != null)
		{
			SetFont(label.linkedTextComponent, newFont);
		}
	}

	internal static void SetMaterial(TMP_Text label, Material newMat)
	{
		if (label.fontSharedMaterial != newMat)
		{
			label.fontSharedMaterial = newMat;
		}
		if (label.linkedTextComponent != null)
		{
			SetMaterial(label.linkedTextComponent, newMat);
		}
	}
}