/
FlxTextTyper.hx
398 lines (331 loc) · 7.99 KB
/
FlxTextTyper.hx
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
package flixel.addons.text;
import flixel.FlxBasic;
import flixel.FlxG;
import flixel.input.keyboard.FlxKey;
import flixel.util.FlxSignal;
class FlxTextTyper extends FlxBasic
{
/**
* The current state of the typer.
*/
public var state(default, null):FlxTyperState = EMPTY;
/**
* The delay between each character, in seconds.
*/
public var delay:FlxTypeDelay = CONST(0.05);
/**
* The delay between each character erasure, in seconds.
*/
public var eraseDelay:FlxTypeDelay = CONST(0.02);
/**
* Special delay when a specific characgter is typed, has no effect when erasing
*/
public var charDelayScaler:Map<String, Float> = [','=>2.0, '.'=>2.0,'?'=>2.0];
/**
* Set to true to show a blinking cursor at the end of the text.
*/
public var showCursor:Bool = false;
/**
* The character to blink at the end of the text.
*/
public var cursorCharacter:String = "|";
/**
* The speed at which the cursor should blink, if shown at all.
*/
public var cursorBlinkRate:Float = 0.5;
/**
* Text to add at the beginning, without animating.
*/
public var prefix:String = "";
/**
* An array of keys (e.g. `[SPACE, L]`) that will advance the text.
*/
public var skipKeys:Array<FlxKey>;
/**
* Dispatched when the message is done typing.
*/
public var onTypingComplete(default, null):FlxSignal = new FlxSignal();
/**
* Dispatched when the message is done erasing.
*/
public var onErasingComplete(default, null):FlxSignal = new FlxSignal();
/**
* Dispatched whenever the text changes.
*/
public var onChange(default, null):FlxSignal = new FlxSignal();
/**
* Dispatched when a new portion of the text is typed. Helpful for playing sounds.
*/
public var onCharsType(default, null):FlxSignal = new FlxSignal();
/**
* Dispatched when a portion of the text is erased. Helpful for playing sounds.
*/
public var onCharsErase(default, null):FlxSignal = new FlxSignal();
/**
* The text that will ultimately be displayed.
*/
public var finalText(default, null):String;
/**
* The full text to display.
*/
public var text(default, null):String;
/**
* The text that was typed.
*/
var typedText:String = "";
/**
* Used to delay character typing and erasing.
*/
var timer:Float = 0.0;
/**
* Internal tracker for cursor blink time.
*/
var cursorTimer:Float = 0.0;
/**
* Internal tracker for cursor blink time.
*/
var wasCursorShowing:Bool = false;
public function new (prefix = "")
{
if (prefix == null)
prefix = "";
super();
this.prefix = prefix;
text = prefix;
}
/**
* Clears all typed text
*/
public function clear()
{
state = EMPTY;
typedText = "";
}
override function update(elapsed:Float)
{
super.update(elapsed);
// check prefix before typedText changes
final prefixChanged = text.indexOf(prefix + typedText) != 0;
// update typed text
var changed = switch(state)
{
case EMPTY: false;
case TYPING: updateTyping(elapsed);
case FINISHED: false;
case ERASING: updateErasing(elapsed);
}
if (prefixChanged)
changed = true;
// update cursor
cursorTimer += elapsed;
final cursorShowing = isCursorShowing();
if (wasCursorShowing != cursorShowing)
{
changed = true;
wasCursorShowing = cursorShowing;
}
// apply new changes and dispatch change events
if (changed)
{
text = (prefix == null ? "" : prefix) + typedText;
if (cursorShowing)
text += cursorCharacter;
onChange.dispatch();
}
}
/**
* Begins the typing effect.
*
* @param text The text to type. Do not include the prefix
* @param clear If true, all typed text is removed, otherwise, if some of the desired
* message is already typed, it will continue where it left off.
*/
public function startTyping(text:String, clear = true)
{
finalText = text;
// if partial message is already there, and clear is false, continue where we left off
if (clear || (typedText != "" && finalText.indexOf(typedText) != 0))
typedText = "";
else if (typedText == finalText)
{
typingCompleted();
return;
}
timer = delay.random();
state = TYPING;
}
/**
* Begins the typing effect while preserving any text that is already typed.
*
* @param text The text to type. Do not include the prefix
* @param skipCurrent If the type is currently typing or erasing, it will finish
* first before appending the desired text.
*/
public function startAppending(text:String, skipCurrent = true)
{
prefix += typedText;
finalText = text;
if (skipCurrent && (state == TYPING || state == ERASING))
skip();
timer = delay.random();
state = TYPING;
}
public function startErasing(?textOverride:String)
{
if (textOverride != null)
typedText = textOverride;
if (typedText == "")
{
erasingCompleted();
return;
}
timer = eraseDelay.random();
state = ERASING;
}
public function skip()
{
switch(state)
{
case TYPING:
typedText = finalText;
typingCompleted();
case ERASING:
typedText = "";
erasingCompleted();
case EMPTY | FINISHED:
// nothing
}
}
function updateTyping(elapsed:Float):Bool
{
var changed = false;
if (skipPressed())
{
skip();
return true;
}
timer -= elapsed;
while (timer < 0)
{
final char = finalText.charAt(typedText.length);
typedText += char;
timer += getCharTypeDelay(char);
changed = true;
if (typedText == finalText)
{
typingCompleted();
break;
}
}
if (changed)
onCharsType.dispatch();
return changed;
}
function getCharTypeDelay(char:String):Float
{
if (charDelayScaler.exists(char))
return delay.scale(charDelayScaler[char]).random();
return delay.random();
}
function getCharEraseDelay(char:String):Float
{
return eraseDelay.random();
}
public function typingCompleted()
{
state = FINISHED;
onTypingComplete.dispatch();
}
function updateErasing(elapsed:Float):Bool
{
var changed = false;
if (skipPressed())
{
skip();
return true;
}
timer -= elapsed;
while (timer < 0)
{
// remove last typed character
final char = typedText.charAt(typedText.length - 1);
typedText = typedText.substr(0, typedText.length - 1);
timer += getCharEraseDelay(char);
changed = true;
if (typedText == "")
{
erasingCompleted();
break;
}
}
if (changed)
onCharsErase.dispatch();
return changed;
}
public function erasingCompleted()
{
state = EMPTY;
onErasingComplete.dispatch();
}
function skipPressed()
{
return skipKeys != null && FlxG.keys.anyJustPressed(skipKeys);
}
function isCursorShowing()
{
return showCursor && (cursorTimer % cursorBlinkRate) / cursorBlinkRate > 0.5;
}
}
@:using(flixel.addons.text.FlxTextTyper.FlxTypeDelayTools)
enum FlxTypeDelay
{
CONST(delay:Float);
VARIED(delay:Float, variance:Float);
RANGE(min:Float, max:Float);
NORMAL(mean:Float, deviation:Float);
}
class FlxTypeDelayTools
{
public static function random(delay:FlxTypeDelay)
{
return switch(delay)
{
case CONST(amount):
amount;
case RANGE(min, max):
FlxG.random.float(min, max);
case VARIED(base, variance):
base * (1 + FlxG.random.float(-variance, variance));
case NORMAL(mean, deviation):
FlxG.random.floatNormal(mean, deviation);
}
}
public static function scale(delay:FlxTypeDelay, scaler:Float)
{
return switch(delay)
{
case CONST(amount):
CONST(amount * scaler);
case RANGE(min, max):
RANGE(min * scaler, max * scaler);
case VARIED(base, variance):
VARIED(base * scaler, variance * scaler);
case NORMAL(mean, deviation):
NORMAL(mean * scaler, deviation * scaler);
}
}
}
enum FlxTyperState
{
/**
* Text either has not started yet, or has been fully erased.
* Note: if `prefix` is non-null the text will never be empty.
*/
EMPTY;
/** The typer is currently typing */
TYPING;
/** The typer has completed typing, if erasing is enabled it is waiting to erase */
FINISHED;
/** The typer is currently erasing */
ERASING;
}