summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc/LocalizeTarget_UnityUI_Text.cs
blob: ab26e0d16962e5d9467a93c811af248e92757f74 (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
using UnityEngine;
using UnityEngine.UI;

namespace I2.Loc;

public class LocalizeTarget_UnityUI_Text : LocalizeTarget<Text>
{
	private TextAnchor mAlignment_RTL = TextAnchor.UpperRight;

	private TextAnchor mAlignment_LTR;

	private bool mAlignmentWasRTL;

	private bool mInitializeAlignment = true;

	static LocalizeTarget_UnityUI_Text()
	{
		AutoRegister();
	}

	[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
	private static void AutoRegister()
	{
		LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Text, LocalizeTarget_UnityUI_Text>
		{
			Name = "Text",
			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)
	{
		Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
		if (secondaryTranslatedObj != null && secondaryTranslatedObj != mTarget.font)
		{
			mTarget.font = secondaryTranslatedObj;
		}
		if (mInitializeAlignment)
		{
			mInitializeAlignment = false;
			mAlignmentWasRTL = LocalizationManager.IsRight2Left;
			InitAlignment(mAlignmentWasRTL, mTarget.alignment, out mAlignment_LTR, out mAlignment_RTL);
		}
		else
		{
			InitAlignment(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.text = mainTranslation;
			mTarget.SetVerticesDirty();
		}
	}

	private void InitAlignment(bool isRTL, TextAnchor alignment, out TextAnchor alignLTR, out TextAnchor alignRTL)
	{
		alignLTR = (alignRTL = alignment);
		if (isRTL)
		{
			switch (alignment)
			{
			case TextAnchor.UpperRight:
				alignLTR = TextAnchor.UpperLeft;
				break;
			case TextAnchor.MiddleRight:
				alignLTR = TextAnchor.MiddleLeft;
				break;
			case TextAnchor.LowerRight:
				alignLTR = TextAnchor.LowerLeft;
				break;
			case TextAnchor.UpperLeft:
				alignLTR = TextAnchor.UpperRight;
				break;
			case TextAnchor.MiddleLeft:
				alignLTR = TextAnchor.MiddleRight;
				break;
			case TextAnchor.LowerLeft:
				alignLTR = TextAnchor.LowerRight;
				break;
			case TextAnchor.UpperCenter:
			case TextAnchor.MiddleCenter:
			case TextAnchor.LowerCenter:
				break;
			}
		}
		else
		{
			switch (alignment)
			{
			case TextAnchor.UpperRight:
				alignRTL = TextAnchor.UpperLeft;
				break;
			case TextAnchor.MiddleRight:
				alignRTL = TextAnchor.MiddleLeft;
				break;
			case TextAnchor.LowerRight:
				alignRTL = TextAnchor.LowerLeft;
				break;
			case TextAnchor.UpperLeft:
				alignRTL = TextAnchor.UpperRight;
				break;
			case TextAnchor.MiddleLeft:
				alignRTL = TextAnchor.MiddleRight;
				break;
			case TextAnchor.LowerLeft:
				alignRTL = TextAnchor.LowerRight;
				break;
			case TextAnchor.UpperCenter:
			case TextAnchor.MiddleCenter:
			case TextAnchor.LowerCenter:
				break;
			}
		}
	}
}