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
|
using System;
using I2.Loc;
using TMPro;
using UnityEngine;
[RequireComponent(typeof(TextMeshProUGUI))]
public class TFUITextButton : ThronefallUIElement
{
[Serializable]
public class Style
{
public Color color = Color.white;
public float fontSize = 30f;
public string prefix;
public string suffix;
public TMP_FontAsset font;
public AnimationCurve animationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
public float animationDuration = 0.5f;
public Style(Color color, float fontSize, TMP_FontAsset font, AnimationCurve animationCurve, float animationDuration)
{
this.color = color;
this.fontSize = fontSize;
this.font = font;
this.animationCurve = animationCurve;
this.animationDuration = animationDuration;
}
}
public class Animation
{
public TFUITextButton target;
public Style startStyle;
public Style endStyle;
public float clock;
public Animation(Style startStyle, Style endStyle, TFUITextButton target)
{
this.startStyle = startStyle;
this.endStyle = endStyle;
this.target = target;
target.ApplyStyle(startStyle);
target.targetText.font = endStyle.font;
target.targetText.text = endStyle.prefix + target.originalString + endStyle.suffix;
target.currentAnimation = this;
}
public void Tick()
{
clock += Time.unscaledDeltaTime;
float num = Mathf.InverseLerp(0f, endStyle.animationDuration, clock);
float t = endStyle.animationCurve.Evaluate(num);
target.targetText.color = Color.Lerp(startStyle.color, endStyle.color, t);
target.targetText.fontSize = Mathf.LerpUnclamped(startStyle.fontSize, endStyle.fontSize, t);
if (num >= 1f)
{
target.ApplyStyle(endStyle);
target.currentAnimation = null;
}
}
}
private Style defaultStyle;
private bool defaultStyleInitialized;
public AnimationCurve defaultAnimationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
public float defaultAnimationTime = 0.3f;
public Style focussedStyle;
public Style selectedStyle;
public Style focussedAndSelectedStyle;
public Style overrideStyle;
public bool applyOverrideStyle;
private TextMeshProUGUI targetText;
private string originalString;
private Animation currentAnimation;
private Localize locComponent;
private void Start()
{
locComponent = GetComponent<Localize>();
if (locComponent != null)
{
locComponent.LocalizeEvent.AddListener(UpdateOriginalStringLocalized);
}
}
private void Update()
{
if (currentAnimation != null)
{
currentAnimation.Tick();
}
}
protected Style GetStyle(SelectionState state)
{
if (!defaultStyleInitialized)
{
InitializeDefaultStyle();
}
return state switch
{
SelectionState.Default => defaultStyle,
SelectionState.Focussed => focussedStyle,
SelectionState.Selected => selectedStyle,
SelectionState.FocussedAndSelected => focussedAndSelectedStyle,
_ => defaultStyle,
};
}
protected override void OnApply()
{
}
protected override void OnClear()
{
new Animation(GetStyle(previousState), applyOverrideStyle ? overrideStyle : defaultStyle, this);
}
protected override void OnFocus()
{
new Animation(GetStyle(previousState), applyOverrideStyle ? overrideStyle : focussedStyle, this);
}
protected override void OnSelect()
{
new Animation(GetStyle(previousState), applyOverrideStyle ? overrideStyle : selectedStyle, this);
}
protected override void OnFocusAndSelect()
{
new Animation(GetStyle(previousState), applyOverrideStyle ? overrideStyle : focussedAndSelectedStyle, this);
}
protected override void OnHardStateSet(SelectionState selectionState)
{
currentAnimation = null;
ApplyStyle(GetStyle(selectionState));
}
private void ApplyStyle(Style style)
{
targetText.color = style.color;
targetText.fontSize = style.fontSize;
targetText.font = style.font;
targetText.text = style.prefix + originalString + style.suffix;
}
private void InitializeDefaultStyle()
{
if (!defaultStyleInitialized)
{
targetText = GetComponent<TextMeshProUGUI>();
UpdateOriginalStringLocalized();
defaultStyle = new Style(targetText.color, targetText.fontSize, targetText.font, defaultAnimationCurve, defaultAnimationTime);
defaultStyleInitialized = true;
}
}
private void UpdateOriginalStringLocalized()
{
if (locComponent == null)
{
locComponent = GetComponent<Localize>();
}
if (locComponent == null)
{
originalString = targetText.text;
}
else
{
originalString = LocalizationManager.GetTranslation(locComponent.Term);
}
}
}
|