-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedLabel.cs
More file actions
264 lines (230 loc) · 8.15 KB
/
AnimatedLabel.cs
File metadata and controls
264 lines (230 loc) · 8.15 KB
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
using System;
using Godot;
using System.Linq;
using Godot.Collections;
namespace AnimatedLabel;
#if TOOLS
[Tool, Icon("res://assets/ui/misc/AnimatedLabel.svg")]
#endif
[GlobalClass] public partial class AnimatedLabel : ReferenceRect
{
[Export(PropertyHint.MultilineText)] public string Text
{
get => _text;
set
{
_text = value;
_letterArray = GetLetterArray();
UpdateText();
}
}
[Export(PropertyHint.File)] public AnimatedFont AnimatedFont
{
get => _animatedFont;
set
{
_animatedFont = value;
UpdateText();
}
}
[ExportGroup("Animation"), Export(PropertyHint.Enum)]
private AnimationStyles AnimationStyles
{
get => _animationStyles;
set
{
_animationStyles = value;
UpdateText();
}
}
[Export] public float SyncFrameSpeed
{
get => _syncFrameSpeed;
set
{
_syncFrameSpeed = value;
_syncFrameDuration = 1 / _syncFrameSpeed;
}
}
[ExportGroup("Style"), Export] public float Separation
{
get => _separation;
set
{
_separation = value;
UpdateText();
}
}
[Export] public float FontSize
{
get => _fontSize;
set
{
_fontSize = value;
UpdateText();
}
}
[Export] public HorizontalAlignment HorizontalAlignment
{
get => _horizontalAlignment;
set
{
_horizontalAlignment = value;
UpdateText();
}
}
[Export] public VerticalAlignment VerticalAlignment
{
get => _verticalAlignment;
set
{
_verticalAlignment = value;
UpdateText();
}
}
private string _text;
private AnimatedFont _animatedFont;
private AnimationStyles _animationStyles = AnimationStyles.Synchronized;
private float _separation = 24f;
private float _fontSize = 24f;
private HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Left;
private VerticalAlignment _verticalAlignment = VerticalAlignment.Top;
private AnimatedLetter[] _letterArray;
private float _syncFrameSpeed = 24f;
private float _syncFrameLength;
private int _syncFrameIndex;
private float _syncFrameDuration;
private float _timePassed;
public override void _Ready()
{
UpdateText();
}
private AnimatedLetter[] GetLetterArray()
{
string[] splitText = Text.Select(x => x.ToString()).ToArray();
AnimatedLetter[] letterArray = new AnimatedLetter[splitText.Length];
for (int i = 0; i < splitText.Length; i++)
{
string letter = splitText[i];
AnimatedLetter animatedLetter = new AnimatedLetter(letter);
letterArray[i] = animatedLetter;
}
return letterArray;
}
private void UpdateLetterPositions(bool redraw = true)
{
for (int i = 0; i < _letterArray.Length; i++)
{
AnimatedLetter animLetter = _letterArray[i];
// fallback rect for spaces or texture-less letters
Vector2 position = new Vector2(FontSize + Separation * i, 0);
Vector2 size = new Vector2(FontSize, FontSize);
// figure out the actual position and scale if it's a valid letter
if (animLetter.Texture != null && animLetter.Texture[0] != null)
{
Vector2 texSize = animLetter.Texture[0].GetSize();
float texRatio = CalculateTextureRatio(texSize);
size = new Vector2(texSize.X * texRatio, texSize.Y * texRatio);
position = new Vector2(texSize.X*i, FontSize - size.Y);
}
animLetter.Rect = new Rect2(position, size);
}
CustomMinimumSize = new Vector2(Separation * _letterArray.Length + FontSize, FontSize);
if (redraw)
QueueRedraw();
}
private float CalculateTextureRatio(Vector2 texSize)
{
float widthRatio = FontSize / texSize.X;
float heightRatio = FontSize / texSize.Y;
float ratio = Math.Min(widthRatio, heightRatio);
return ratio;
}
private void UpdateText()
{
foreach (AnimatedLetter animLetter in _letterArray)
{
if (AnimatedFont == null || AnimatedFont.SpriteFrames == null)
{
GD.PrintErr("The provided AnimatedFont is null");
return;
}
if (!AnimatedFont.SpriteFrames.HasAnimation(animLetter.Letter))
continue;
//GD.Print(animLetter.Letter);
int frameCount = AnimatedFont.SpriteFrames.GetFrameCount(animLetter.Letter);
//GD.Print($"Frame count {frameCount}");
animLetter.Texture = new Texture2D[frameCount];
UpdateLetterPositions(false);
if((frameCount > 1 && frameCount < _syncFrameLength) || _syncFrameLength <= 0)
_syncFrameLength = frameCount;
for (int frame = 0; frame < frameCount; frame++)
{
Texture2D frameTexture = AnimatedFont.SpriteFrames.GetFrameTexture(animLetter.Letter, frame);
animLetter.Texture[frame] = frameTexture;
if (AnimationStyles == AnimationStyles.InstantLoop)
animLetter.FrameSpeed = (float)AnimatedFont.SpriteFrames.GetAnimationSpeed(animLetter.Letter);
//GD.Print($"Texture {frameTexture} for frame {frame} of letter {animLetter.Letter}");
/*if (frameTexture is AtlasTexture atlasTexture)
{
animatedLetter.SourceRect[frame] = atlasTexture.Region;
}*/
}
//GD.Print($"Done setting {SpriteFrames.GetFrameCount(animLetter.Letter)} textures for letter: {animLetter.Letter}");
}
QueueRedraw();
}
public override void _Draw()
{
foreach (AnimatedLetter animLetter in _letterArray)
{
int finalIndex = AnimationStyles == AnimationStyles.Synchronized ? _syncFrameIndex : animLetter.FrameIndex;
if (animLetter.Texture != null && animLetter.Texture[finalIndex] != null)
{
// note to self:
// this doesn't take in account letters without texture so that might be an issue
Texture2D frameTexture = animLetter.Texture[finalIndex];
if (frameTexture is AtlasTexture letterAtlas)
{
//GD.Print($"drawing AtlasTexture letter {animLetter.Letter}");
// AtlasTexture drawing
Texture2D atlas = letterAtlas.Atlas;
Rect2 sourceRect = letterAtlas.Region;
DrawTextureRectRegion(atlas,
animLetter.Rect,
sourceRect,
Modulate
);
continue;
}
//GD.Print($"drawing Texture2D letter {animLetter.Letter}");
// Drawing other Texture2D derivatives
DrawTextureRect(frameTexture,
animLetter.Rect,
false,
Modulate);
}
}
}
public override void _Process(double delta)
{
if (AnimationStyles != AnimationStyles.Synchronized)
return;
_timePassed += (float)delta;
if (_syncFrameIndex > _syncFrameLength)
{
_syncFrameIndex = 0;
_timePassed = 0;
}
_syncFrameIndex = GetCurrentFrame();
}
public int GetCurrentFrame()
{
int timeToFrame = (int)(_timePassed / _syncFrameDuration);
GD.Print("frame: "+timeToFrame);
GD.Print("length: "+_syncFrameLength);
GD.Print("passed: "+_timePassed);
GD.Print("duration: "+_syncFrameDuration);
return timeToFrame;
}
}