-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpeechBubble.asc
More file actions
1210 lines (1076 loc) · 38.7 KB
/
Copy pathSpeechBubble.asc
File metadata and controls
1210 lines (1076 loc) · 38.7 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SPEECH BUBBLE MODULE - Script *
* by Gunnar Harboe (Snarky), v0.9.0 *
* *
* Copyright (c) 2017, 2025 Gunnar Harboe *
* *
* This code is offered under the MIT License *
* https://opensource.org/licenses/MIT *
* *
* It is also licensed under a Creative Commons Attribution 4.0 International License. *
* https://creativecommons.org/licenses/by/4.0/ *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Change this function to call a different Say() function
void SB_sayImpl(this Character*, String message)
{
this.Say(message);
}
// If you want to use another function to animate background speech (e.g. to do lip-sync),
// call it here and return true
bool SB_sayBackgroundAnimateImpl(this Character*, String message)
{
return false;
}
// If you're using another function to animate background speech, replace this with the
// appropriate test to see if the background speech animation is running
/*
bool SB_isSpeakingBackground(this Character*)
{
SpeechBubble* sb = this.GetSpeechBubble();
return (sb != null && sb.IsBackgroundSpeech);
}*/
#region Variable declarations
// Variables for the SpeecBubble attributes
GUI* _defaultGui;
int _backgroundColor;
int _borderColor;
int _backgroundTransparency;
int _borderTransparency;
int _backgroundSpeechTint;
int _borderSpeechTint;
int _textTransparency;
int _textOutlineColor;
int _textOutlineSpeechTint;
int _textOutlineWidth;
TextOutlineStyle _textOutlineStyle;
int _maxTextWidth;
int _heightOverHead;
int _paddingTop;
int _paddingBottom;
int _paddingLeft;
int _paddingRight;
int _cornerRoundingRadius;
String _talkTail[];
String _thinkTail[];
int _talkTailHeight;
int _talkTailWidth;
int _thinkTailHeight;
int _thinkTailWidth;
GUI* _bubbleGui;
GUI* _textGui;
FontType _invisibleFont;
Alignment _textAlign;
// Internal script variables
Character* _bubbleChars[];
SpeechBubble* _charBubbles[]; // Array to store bubbles for all characters
String _bubbleMessages[];
DynamicSprite* _bubbleSprites[];
Overlay* _bubbleOverlays[];
GUI* _bubbleGuis[];
int _bubbleCount;
#endregion
#region Helper Functions
// Waits until the conditions for skipping a speech line are met, according to current settings
// If forceTimeOut is set, function will always time out, even if current settings don't skip by timeout
int _waitSpeech(int loops, SkipSpeechStyle style)
{
if(style == eSkipNone)
style = Speech.SkipStyle;
switch(style)
{
case eSkipKey:
while(true)
{
int result = WaitKey();
if(Speech.SkipKey == eKeyNone || (result & eKeyCodeMask) == Speech.SkipKey)
return result;
}
case eSkipKeyMouse:
while(true)
{
int result = WaitMouseKey();
if(Speech.SkipKey == eKeyNone || (result & eInputAny) == eInputMouse || (result & eKeyCodeMask) == Speech.SkipKey)
return result;
}
case eSkipKeyMouseTime:
for(int i=0; i<loops; i++)
{
int result = WaitMouseKey(1);
if(result != 0 && (Speech.SkipKey == eKeyNone
|| (result & eInputAny) == eInputMouse
|| (result & eKeyCodeMask) == Speech.SkipKey) )
return result;
}
return 0;
case eSkipKeyTime:
for(int i=0; i<loops; i++)
{
int result = WaitKey(1);
if(result != 0 && (Speech.SkipKey == eKeyNone
|| (result & eKeyCodeMask) == Speech.SkipKey) )
return result;
}
return 0;
case eSkipMouse:
return WaitMouse();
case eSkipMouseTime:
return WaitMouse(loops);
case eSkipTime:
Wait(loops);
return 0;
default:
return 0;
}
}
int WaitSpeech(int loops, bool forceTimeOut)
{
if(forceTimeOut)
{
switch(Speech.SkipStyle)
{
case eSkipKey:
return _waitSpeech(loops, eSkipKeyTime);
case eSkipKeyMouse:
return _waitSpeech(loops, eSkipKeyMouseTime);
case eSkipMouse:
return _waitSpeech(loops, eSkipMouseTime);
default:
return _waitSpeech(loops, Speech.SkipStyle);
}
}
else return _waitSpeech(loops, Speech.SkipStyle);
}
int _maxInt(int a, int b)
{
if(a > b) return a;
return b;
}
int _minInt(int a, int b)
{
if(a < b) return a;
return b;
}
int _clampInt(int value, int minRange, int maxRange)
{
if(value<minRange) return minRange;
if(value>maxRange) return maxRange;
return value;
}
// Calculate the color that comes closest to a mix of c1 and c2 (at 'mix' percent c1)
int mixColors(int c1, int c2, int mix)
{
if(mix==0) return c2;
if(mix==100) return c1;
int r1, g1, b1;
int r2, g2, b2;
// Extract the c1 channels
if(c1 < 32)
{
r1 = (palette[c1].r << 2) + (palette[c1].r >> 4);
g1 = (palette[c1].g << 2) + (palette[c1].g >> 4);
b1 = (palette[c1].b << 2) + (palette[c1].b >> 4);
}
else
{
r1 = (c1 & 63488) >> 11; // 63488 = binary 11111-000000-00000
g1 = (c1 & 2016) >> 5; // 2016 = binary 00000-111111-00000
b1 = (c1 & 31); // 31 = binary 00000-000000-11111
r1 = (r1 << 3) + (r1 >> 2);
g1 = (g1 << 2) + (g1 >> 4);
b1 = (b1 << 3) + (b1 >> 2);
}
// Extract the c2 channels
if(c2 < 32)
{
r2 = (palette[c2].r << 2) + (palette[c2].r >> 4);
g2 = (palette[c2].g << 2) + (palette[c2].g >> 4);
b2 = (palette[c2].b << 2) + (palette[c2].b >> 4);
}
else
{
r2 = (c2 & 63488) >> 11; // 63488 = binary 11111-000000-00000
g2 = (c2 & 2016) >> 5; // 2016 = binary 00000-111111-00000
b2 = (c2 & 31); // 31 = binary 00000-000000-11111
r2 = (r2 << 3) + (r2 >> 2);
g2 = (g2 << 2) + (g2 >> 4);
b2 = (b2 << 3) + (b2 >> 2);
}
// Calculate the mix
int r = (r1 * mix + r2*(100-mix) + 50) / 100; // +50 to round up to nearest
int g = (g1 * mix + g2*(100-mix) + 50) / 100;
int b = (b1 * mix + b2*(100-mix) + 50) / 100;
// Convert back to AGS color num
r = r >> 3;
g = g >> 2;
b = b >> 3;
r = r << 11;
g = g << 5;
int c = r+g+b;
if(c < 32)
c += 65536;
return c;
}
#endregion
// Attribute accessors
#region SpeechBubble Attribute accessors
#region static accessors
GUI* get_DefaultGui(static SpeechBubble)
{ return _defaultGui; }
void set_DefaultGui(static SpeechBubble, GUI* value)
{ _defaultGui = value; }
int get_BackgroundColor(static SpeechBubble)
{ return _backgroundColor; }
void set_BackgroundColor(static SpeechBubble, int value)
{ _backgroundColor = value; }
int get_BorderColor(static SpeechBubble)
{ return _borderColor; }
void set_BorderColor(static SpeechBubble, int value)
{ _borderColor = value; }
int get_BackgroundTransparency(static SpeechBubble)
{ return _backgroundTransparency; }
void set_BackgroundTransparency(static SpeechBubble, int value)
{ _backgroundTransparency = _clampInt(value, 0, 100); }
int get_BorderTransparency(static SpeechBubble)
{ return _borderTransparency; }
void set_BorderTransparency(static SpeechBubble, int value)
{ _borderTransparency = _clampInt(value, 0, 100); }
int get_BackgroundSpeechTint(static SpeechBubble)
{ return _backgroundSpeechTint; }
void set_BackgroundSpeechTint(static SpeechBubble, int value)
{ _backgroundSpeechTint = _clampInt(value, 0, 100); }
int get_BorderSpeechTint(static SpeechBubble)
{ return _borderSpeechTint; }
void set_BorderSpeechTint(static SpeechBubble, int value)
{ _borderSpeechTint = _clampInt(value, 0, 100); }
int get_TextTransparency(static SpeechBubble)
{ return _textTransparency; }
void set_TextTransparency(static SpeechBubble, int value)
{ _textTransparency = _clampInt(value, 0, 100); }
int get_TextOutlineColor(static SpeechBubble)
{ return _textOutlineColor; }
void set_TextOutlineColor(static SpeechBubble, int value)
{ _textOutlineColor = value; }
int get_TextOutlineSpeechTint(static SpeechBubble)
{ return _textOutlineSpeechTint; }
void set_TextOutlineSpeechTint(static SpeechBubble, int value)
{ _textOutlineSpeechTint = _clampInt(value, 0, 100); }
int get_TextOutlineWidth(static SpeechBubble)
{ return _textOutlineWidth; }
void set_TextOutlineWidth(static SpeechBubble, int value)
{ _textOutlineWidth = value; }
TextOutlineStyle get_TextOutlineStyle(static SpeechBubble)
{ return _textOutlineStyle; }
void set_TextOutlineStyle(static SpeechBubble, TextOutlineStyle value)
{ _textOutlineStyle = value; }
int get_MaxTextWidth(static SpeechBubble)
{ return _maxTextWidth; }
void set_MaxTextWidth(static SpeechBubble, int value)
{ _maxTextWidth = value; }
int get_HeightOverHead(static SpeechBubble)
{ return _heightOverHead; }
void set_HeightOverHead(static SpeechBubble, int value)
{ _heightOverHead = value; }
int get_PaddingTop(static SpeechBubble)
{ return _paddingTop; }
void set_PaddingTop(static SpeechBubble, int value)
{ _paddingTop = value; }
int get_PaddingBottom(static SpeechBubble)
{ return _paddingBottom; }
void set_PaddingBottom(static SpeechBubble, int value)
{ _paddingBottom = value; }
int get_PaddingLeft(static SpeechBubble)
{ return _paddingLeft; }
void set_PaddingLeft(static SpeechBubble, int value)
{ _paddingLeft = value; }
int get_PaddingRight(static SpeechBubble)
{ return _paddingRight; }
void set_PaddingRight(static SpeechBubble, int value)
{ _paddingRight = value; }
int get_CornerRoundingRadius(static SpeechBubble)
{ return _cornerRoundingRadius; }
void set_CornerRoundingRadius(static SpeechBubble, int value)
{ _cornerRoundingRadius = _maxInt(0, value); }
// Helper function to set tail width and height
int[] measureTail(String tail[])
{
int dim[] = new int[2];
if(tail == null)
{
dim[0] = -1;
dim[1] = -1;
return dim;
}
dim[0] = 0;
int i;
for(i=0; tail[i] != null; i++)
{
dim[0] = _maxInt(dim[0], tail[i].Length);
}
dim[1] = i;
return dim;
}
String[] GetTalkTail(static SpeechBubble)
{ return _talkTail; }
void SetTalkTail(static SpeechBubble, String value[])
{
_talkTail = value;
int dim[] = measureTail(value);
_talkTailWidth = dim[0];
_talkTailHeight = dim[1];
}
String[] GetThinkTail(static SpeechBubble)
{ return _thinkTail; }
void SetThinkTail(static SpeechBubble, String value[])
{
_thinkTail = value;
int dim[] = measureTail(value);
_thinkTailWidth = dim[0];
_thinkTailHeight = dim[1];
}
int get_TalkTailHeight(static SpeechBubble)
{ return _talkTailHeight; }
int get_TalkTailWidth(static SpeechBubble)
{ return _talkTailWidth; }
int get_ThinkTailHeight(static SpeechBubble)
{ return _thinkTailHeight; }
int get_ThinkTailWidth(static SpeechBubble)
{ return _thinkTailWidth; }
FontType get_InvisibleFont(static SpeechBubble)
{ return _invisibleFont; }
void set_InvisibleFont(static SpeechBubble, FontType value)
{ _invisibleFont = value; }
Alignment get_TextAlign(static SpeechBubble)
{ return _textAlign; }
void set_TextAlign(static SpeechBubble, Alignment value)
{ _textAlign = value; }
#endregion
#region instance accessors
Character* get_OwningCharacter(this SpeechBubble*)
{
if(this._id == -1) return null;
else return character[this._id];
}
bool get_Valid(this SpeechBubble*)
{ return this._valid; }
bool get_IsBackgroundSpeech(this SpeechBubble*)
{ return this._isBackgroundSpeech; }
bool get_IsThinking(this SpeechBubble*)
{ return this._isThinking; }
bool get_IsAnimating(this SpeechBubble*)
{ return this._isAnimating; }
bool get_UsesGUI(this SpeechBubble*)
{ return this._usesGui; }
String get_Text(this SpeechBubble*)
{ return _bubbleMessages[this._id]; }
DynamicSprite* get_BubbleSprite(this SpeechBubble*)
{ return _bubbleSprites[this._id]; }
Overlay* get_BubbleOverlay(this SpeechBubble*)
{ return _bubbleOverlays[this._id]; }
GUI* get_BubbleGUI(this SpeechBubble*)
{ return _bubbleGuis[this._id]; }
int get_TotalDuration(this SpeechBubble*)
{ return this._totalDuration; }
int get_ElapsedDuration(this SpeechBubble*)
{ return this._elapsedDuration; }
int get_X(this SpeechBubble*)
{ return this._x; }
void set_X(this SpeechBubble*, int value)
{
this._x = value;
if(!this._valid) return;
if(this.get_UsesGUI())
{
GUI* g = this.get_BubbleGUI();
g.X = value;
}
else
{
Overlay* o = _bubbleOverlays[this._id];
o.Remove();
DynamicSprite* bs = this.get_BubbleSprite();
_bubbleOverlays[this._id] = Overlay.CreateGraphical(this._x, this._y, bs.Graphic, true);
}
}
int get_Y(this SpeechBubble*)
{ return this._y; }
int set_Y(this SpeechBubble*, int value)
{
this._y = value;
if(!this._valid) return;
if(this.get_UsesGUI())
{
GUI* g = this.get_BubbleGUI();
g.Y = value;
}
else
{
Overlay* o = _bubbleOverlays[this._id];
o.Remove();
DynamicSprite* bs = this.get_BubbleSprite();
_bubbleOverlays[this._id] = Overlay.CreateGraphical(this._x, this._y, bs.Graphic, true);
}
}
#endregion
// "Protected" setters: used internally to setup SpeechBubble instances
void setOwningCharacter(this SpeechBubble*, Character* value)
{
if(value == null)
this._id = -1;
else
this._id = value.ID;
}
void setValid(this SpeechBubble*, bool value)
{ this._valid = value; }
void setBackgroundSpeech(this SpeechBubble*, bool value)
{ this._isBackgroundSpeech = value; }
void setThinking(this SpeechBubble*, bool value)
{ this._isThinking = value; }
void setAnimating(this SpeechBubble*, bool value)
{ this._isAnimating = value; }
void setTotalDuration(this SpeechBubble*, int value)
{ this._totalDuration = value; }
void setElapsedDuration(this SpeechBubble*, int value)
{ this._elapsedDuration = value; }
void setX(this SpeechBubble*, int value)
{ this._x = value; }
void setY(this SpeechBubble*, int value)
{ this._y = value; }
SpeechBubble* Create(static SpeechBubble, Character* owner, String message, DynamicSprite* bubbleSprite, GUI* bubbleGui, Overlay* bubbleOverlay)
{
SpeechBubble* sb = new SpeechBubble;
sb.setOwningCharacter(owner);
sb.setValid(true);
int id = owner.ID;
_charBubbles[id] = sb;
_bubbleMessages[id] = message;
_bubbleSprites[id] = bubbleSprite;
_bubbleOverlays[id] = bubbleOverlay;
_bubbleGuis[id] = bubbleGui;
return sb;
}
void _addBubbleChar(Character* c)
{
_bubbleChars[_bubbleCount] = c;
_bubbleCount++;
}
bool _removeBubbleChar(Character* c)
{
if(c == null)
return false;
for(int i=0; i<_bubbleCount; i++)
{
if(_bubbleChars[i] == c)
{
_bubbleCount--;
_bubbleChars[i] = _bubbleChars[_bubbleCount];
_bubbleChars[_bubbleCount] = null;
return true;
}
}
return false;
}
void Remove(this SpeechBubble*)
{
SpeechBubble* _this = this;
int id = this._id;
if(id != -1)
{
_removeBubbleChar(this.get_OwningCharacter());
_charBubbles[id] = null;
_bubbleMessages[id] = null;
if(_bubbleSprites[id] != null)
_bubbleSprites[id].Delete();
_bubbleSprites[id] = null;
if(_bubbleOverlays[id] != null)
_bubbleOverlays[id].Remove();
_bubbleOverlays[id] = null;
if(_bubbleGuis[id] != null)
_bubbleGuis[id].Visible = false;
_bubbleGuis[id] = null;
}
this._valid = false;
}
#endregion
// Calculate the height of the character at current scaling (due to rounding, believe this could be 1 pixel off)
int GetHeight(this Character*)
{
ViewFrame* frame = Game.GetViewFrame(this.View, this.Loop, this.Frame);
// TODO: Check that Z value is correctly calculated
return ((Game.SpriteHeight[frame.Graphic] + this.z) * this.Scaling)/100;
}
// Whether the character is speaking in a speech bubble (if includeBackground, count background speech as speech)
bool IsSpeakingBubble(this Character*, bool includeBackground)
{
SpeechBubble* bubble = _charBubbles[this.ID];
return (bubble != null && (includeBackground || !bubble.get_IsBackgroundSpeech()));
}
/// Interrupt the character if they are speaking in the background
bool StopBackgroundBubble(this Character*)
{
SpeechBubble* bubble = _charBubbles[this.ID];
if(bubble != null && bubble.get_Valid() && bubble.get_IsBackgroundSpeech())
{
if(bubble.get_IsAnimating())
this.UnlockView();
bubble.Remove();
return true;
}
return false;
}
void _stopAllBackgroundBubbles()
{
for(int i=_bubbleCount-1; i >= 0; i--)
{
int id = _bubbleChars[i].ID;
if(_charBubbles[id].get_IsBackgroundSpeech())
_bubbleChars[i].StopBackgroundBubble();
}
}
/// The speech bubble used by the character (null if none)
SpeechBubble* GetSpeechBubble(this Character*)
{
return _charBubbles[this.ID];
}
// Initializations
void initSpeechBubble()
{
// Initialize arrays
_bubbleChars = new Character[Game.CharacterCount];
_charBubbles = new SpeechBubble[Game.CharacterCount];
_bubbleMessages = new String[Game.CharacterCount];
_bubbleSprites = new DynamicSprite[Game.CharacterCount];
_bubbleOverlays= new Overlay[Game.CharacterCount];
_bubbleGuis = new GUI[Game.CharacterCount];
// Set default values
SpeechBubble.set_InvisibleFont(-1);
SpeechBubble.set_TextAlign(eAlignCenter);
SpeechBubble.set_BackgroundColor(15);
SpeechBubble.set_BorderColor(0);
SpeechBubble.set_BackgroundTransparency(0);
SpeechBubble.set_BorderTransparency(0);
SpeechBubble.set_MaxTextWidth(-1);
SpeechBubble.set_CornerRoundingRadius(8);
SpeechBubble.set_PaddingTop(10);
SpeechBubble.set_PaddingBottom(10);
SpeechBubble.set_PaddingLeft(20);
SpeechBubble.set_PaddingRight(20);
_talkTail = new String[10];
_talkTail[0] = "OOOOOOOO";
_talkTail[1] = "XOOOOOXX";
_talkTail[2] = "XOOOOX ";
_talkTail[3] = "XOOOOX ";
_talkTail[4] = " XOOOX ";
_talkTail[5] = " XOOOX ";
_talkTail[6] = " XOOOX ";
_talkTail[7] = " XOOOX";
_talkTail[8] = " XXX ";
_talkTail[9] = null;
SpeechBubble.SetTalkTail(_talkTail); // Just to set height/width
_thinkTail = new String[14];
_thinkTail[00] = "XXXXXXXX";
_thinkTail[01] = " ";
_thinkTail[02] = " XX ";
_thinkTail[03] = " XOOX ";
_thinkTail[04] = "XOOOOX ";
_thinkTail[05] = "XOOOOX ";
_thinkTail[06] = " XOOX ";
_thinkTail[07] = " XX ";
_thinkTail[08] = " ";
_thinkTail[09] = " XX ";
_thinkTail[10] = " XOOX ";
_thinkTail[11] = " XOOX ";
_thinkTail[12] = " XX ";
_thinkTail[13] = null;
SpeechBubble.SetThinkTail(_thinkTail); // Just to set height/width
}
function game_start()
{
initSpeechBubble();
}
// To work around the AGS bug where antialiasing "pokes holes" in semi-transparent canvases
void drawStringWrappedAA(this DrawingSurface*, int x, int y, int width, FontType font, Alignment alignment, String message, int transparency)
{
DynamicSprite* textSprite = DynamicSprite.Create(this.Width, this.Height, true);
DrawingSurface* textSurface = textSprite.GetDrawingSurface();
textSurface.DrawingColor = this.DrawingColor;
textSurface.DrawStringWrapped(x, y, width, font, alignment, message);
textSurface.Release();
this.DrawImage(0, 0, textSprite.Graphic, transparency);
textSprite.Delete();
}
// Draw a string with outline (make sure the canvas has at least outlineWidth pixels on each side of the string)
void DrawStringWrappedOutline(this DrawingSurface*, int x, int y, int width, TextOutlineStyle outlineStyle, FontType font, Alignment alignment, String message, int transparency, int outlineColor, int outlineWidth)
{
// This is what we draw on (because we might need to copy with transparency)
DynamicSprite* outlineSprite = DynamicSprite.Create(this.Width, this.Height, true);
DrawingSurface* outlineSurface = outlineSprite.GetDrawingSurface();
// This holds multiple horizontal copies of the text
// We copy it multiple times (shifted vertically) onto the outlineSprite to create the outline
DynamicSprite* outlineStripSprite = DynamicSprite.Create(this.Width, this.Height, true);
DrawingSurface* outlineStripSurface = outlineStripSprite.GetDrawingSurface();
// This is our "text stamp" that we use to draw the outline, we copy it onto outlineStripSprite
DynamicSprite* textSprite = DynamicSprite.Create(this.Width, this.Height, true);
DrawingSurface* textSurface = textSprite.GetDrawingSurface();
// Draw our text stamp
textSurface.DrawingColor = outlineColor;
textSurface.DrawStringWrapped(x, y, width, font, alignment, message);
textSurface.Release();
switch(outlineStyle)
{
case eTextOutlineRounded:
{
// Draw Circular outline
int maxSquare = outlineWidth*outlineWidth+1; // Add 1 for rounding purposes, to avoid "pointy corners"
int maxWidth = 0;
outlineStripSurface.DrawImage(0, 0, textSprite.Graphic);
// We loop from top and bottom to the middle, making the outline wider and wider, to form circular outline
for(int i = outlineWidth; i > 0; i--)
{
// Here's the circular calculation...
while(i*i + maxWidth*maxWidth <= maxSquare)
{
// Increase width of the outline if necessary
maxWidth++;
outlineStripSurface.DrawImage(-maxWidth, 0, textSprite.Graphic);
outlineStripSurface.DrawImage(maxWidth, 0, textSprite.Graphic);
outlineStripSurface.Release();
outlineStripSurface = outlineStripSprite.GetDrawingSurface();
}
// Draw outline strip above and below
outlineSurface.DrawImage(0, -i, outlineStripSprite.Graphic);
outlineSurface.DrawImage(0, i, outlineStripSprite.Graphic);
}
// Finally the middle strip
outlineSurface.DrawImage(0, 0, outlineStripSprite.Graphic);
break;
}
case eTextOutlineSquare:
{
// Draw square block outline
// Just draw the full outline width onto the strip
for(int i = -outlineWidth; i <= outlineWidth; i++)
outlineStripSurface.DrawImage(i, 0, textSprite.Graphic);
outlineStripSurface.Release();
// Draw the full outline height
for(int j = -outlineWidth; j <= outlineWidth; j++)
outlineSurface.DrawImage(0, j, outlineStripSprite.Graphic);
break;
}
}
textSprite.Delete();
outlineStripSurface.Release();
outlineStripSprite.Delete();
/// Now draw the text itself on top of the outline
outlineSurface.DrawingColor = this.DrawingColor;
outlineSurface.drawStringWrappedAA(x, y, width, font, alignment, message, 0);
outlineSurface.Release();
// ... And copy it onto our canvas
this.DrawImage(0, 0, outlineSprite.Graphic, transparency);
outlineSprite.Delete();
}
// Draw a "bitmap" stored as an array of Strings
void drawPixelArray(this DrawingSurface*, String array[], int x, int y, int w, int h, char p, bool flipH, bool flipV)
{
for(int i=0; i<h; i++)
{
for(int j=0; j<array[i].Length; j++)
{
char c = array[i].Chars[j];
if(c == p)
{
int px; int py;
if(flipH)
px = x+w-j;
else
px = x+j;
if(flipV)
py = y+h-i;
else
py = y+i;
this.DrawPixel(px, py);
}
}
}
}
// Round off the corners of the bubble by erasing to transparent and drawing border
void drawRoundedCorners32(DrawingSurface* background, DrawingSurface* border, int borderColor, int top, int bottom)
{
// Uses Bresenham's circle formula, found online
int r = _cornerRoundingRadius;
int x = 0;
int y = r;
int p = 3 - 2 * r;
while (y >= x) // only formulate 1/8 of circle
{
// Erase background corners
// Top Left
background.DrawingColor = COLOR_TRANSPARENT;
background.DrawLine(r - x, top+r - y, r - x, top);
background.DrawLine(r - y, top+r - x, r - y, top);
// Top Right
background.DrawLine(border.Width-r-1 + y, top+r - x, border.Width-r-1 + y, top);
background.DrawLine(border.Width-r-1 + x, top+r - y, border.Width-r-1 + x, top);
background.DrawingColor = COLOR_TRANSPARENT;
// Bottom Left
background.DrawLine(r - x, bottom-r + y, r - x, bottom);
background.DrawLine(r - y, bottom-r + x, r - y, bottom);
// Bottom Right
background.DrawLine(border.Width-r-1 + y, bottom-r + x, border.Width-r-1 + y, bottom);
background.DrawLine(border.Width-r-1 + x, bottom-r + y, border.Width-r-1 + x, bottom);
// Draw border
// Top Left
border.DrawingColor = borderColor;
border.DrawPixel(r - x, top+r - y);//upper left left
border.DrawPixel(r - y, top+r - x);//upper upper left
// Top Right
border.DrawPixel(border.Width-r-1 + y, top+r - x);//upper upper right
border.DrawPixel(border.Width-r-1 + x, top+r - y);//upper right right
// Bottom Left
border.DrawPixel(r - x, bottom-r + y);//lower left left
border.DrawPixel(r - y, bottom-r + x);//lower lower left
// Bottom Right
border.DrawPixel(border.Width-r-1 + y, bottom-r + x);//lower lower right
border.DrawPixel(border.Width-r-1 + x, bottom-r + y);//lower right right
if (p < 0)
{
p += 4*x + 6;
x++;
}
else
{
p += 4*(x - y) + 10;
x++;
y--;
}
}
}
// Find the actual width of a text that has been linewrapped to maxTextWidth
int calculateExactTextWidth(String message, FontType font, int maxTextWidth, int maxHeight)
{
// Binary search to find the minimum width, by trying the midpoint (rounding down)
// between the smallest width we know to be working and the biggest we know to be too small,
// until there's only a 1 pixel difference
int cut = maxTextWidth;
int height = maxHeight;
while(cut>1)
{
cut = (cut+1) >> 1; // Subtract half as much as we tried last time, rounding up
height = GetTextHeight(message, font, maxTextWidth-cut);
if(height == maxHeight)
maxTextWidth -= cut;
}
// ... one last time
height = GetTextHeight(message, font, maxTextWidth-1);
if(height == maxHeight)
return maxTextWidth-1;
else
return maxTextWidth;
}
// AGS's formula for character speech width
int calculateDefaultTextWidth(Character* c)
{
int w = Screen.Width * 2/3;
if(c.x - Game.Camera.X <= Screen.Width/4 || c.x - Game.Camera.X >= Screen.Width * 3/4)
w -= Screen.Width/5;
return w;
}
// Draw a speech bubble in 32-bit (using transparency)
DynamicSprite* renderBubble32(this Character*, String message, bool talkTail)
{
// Calculate text dimensions
int textWidth = _maxTextWidth;
if(textWidth <= 0)
textWidth = calculateDefaultTextWidth(this);
textWidth = _minInt(textWidth, Screen.Width - _paddingLeft - _paddingRight);
int textHeight = GetTextHeight(message, Game.SpeechFont, textWidth);
textWidth = calculateExactTextWidth(message, Game.SpeechFont, textWidth, textHeight);
// Calculate bubble dimensions
int totalWidth = textWidth + _paddingLeft + _paddingRight;
int bubbleHeight = textHeight + _paddingTop + _paddingBottom;
int totalHeight;
if(talkTail)
totalHeight = bubbleHeight + _talkTailHeight;
else
totalHeight = bubbleHeight + _thinkTailHeight;
// Set up the canvases
DynamicSprite* bubbleSprite = DynamicSprite.Create(totalWidth, totalHeight, true);
DrawingSurface* bubbleSurface = bubbleSprite.GetDrawingSurface();
//bubbleSurface.Clear();
DynamicSprite* bgSprite; DrawingSurface* bgSurface;
DynamicSprite* borderSprite; DrawingSurface* borderSurface;
if(_backgroundTransparency == 0)
{
bgSprite = bubbleSprite;
bgSurface = bubbleSurface;
}
else
{
bgSprite = DynamicSprite.Create(totalWidth, totalHeight, true);
bgSurface = bgSprite.GetDrawingSurface();
}
if(_borderTransparency == 0)
{
borderSprite = bubbleSprite;
borderSurface = bubbleSurface;
}
else
{
borderSprite = DynamicSprite.Create(totalWidth, totalHeight, true);
borderSurface = borderSprite.GetDrawingSurface();
}
int bgColor = mixColors(this.SpeechColor, _backgroundColor, _backgroundSpeechTint);
int borderColor = mixColors(this.SpeechColor, _borderColor, _borderSpeechTint);
// Draw!
bgSurface.DrawingColor = bgColor;
bgSurface.DrawRectangle(1, 1, totalWidth-2, bubbleHeight-1);
drawRoundedCorners32(bgSurface, borderSurface, borderColor, 0, bubbleHeight);
String tail[]; int tailWidth; int tailHeight;
if(talkTail)
{
tail = _talkTail; tailWidth = _talkTailWidth; tailHeight = _talkTailHeight;
}
else
{
tail = _thinkTail; tailWidth = _thinkTailWidth; tailHeight = _thinkTailHeight;
}
bgSurface.DrawingColor = bgColor;
bgSurface.drawPixelArray(tail, totalWidth/2-tailWidth, bubbleHeight, tailWidth, tailHeight, 'O', false, false);
borderSurface.DrawingColor = borderColor;
borderSurface.drawPixelArray(tail, totalWidth/2-tailWidth, bubbleHeight, tailWidth, tailHeight, 'X', false, false);
borderSurface.DrawLine(_cornerRoundingRadius, 0, totalWidth - _cornerRoundingRadius, 0);
// Left Line
borderSurface.DrawLine(0, _cornerRoundingRadius, 0, bubbleHeight - _cornerRoundingRadius);
// Right Line
borderSurface.DrawLine(totalWidth-1, _cornerRoundingRadius, totalWidth-1, bubbleHeight - _cornerRoundingRadius);
// Bottom Lines
borderSurface.DrawLine(_cornerRoundingRadius, bubbleHeight, totalWidth/2 - tailWidth - 1, bubbleHeight);
borderSurface.DrawLine(totalWidth/2, bubbleHeight, totalWidth - _cornerRoundingRadius, bubbleHeight);
if(_backgroundTransparency != 0)
{
bgSurface.Release();
bubbleSurface.DrawImage(0, 0, bgSprite.Graphic, _backgroundTransparency);
bgSprite.Delete();
}
if(_borderTransparency != 0)
{
borderSurface.Release();
bubbleSurface.DrawImage(0, 0, borderSprite.Graphic, _borderTransparency);
borderSprite.Delete();
}
bubbleSurface.DrawingColor = this.SpeechColor;
int outlineColor = mixColors(this.SpeechColor, _textOutlineColor, _textOutlineSpeechTint);
if(_textOutlineWidth > 0)
bubbleSurface.DrawStringWrappedOutline(_paddingLeft, _paddingTop, textWidth, _textOutlineStyle, Game.SpeechFont, _textAlign, message, _textTransparency, outlineColor, _textOutlineWidth);
else
bubbleSurface.drawStringWrappedAA(_paddingLeft, _paddingTop, textWidth, Game.SpeechFont, _textAlign, message, _textTransparency);
bubbleSurface.Release();
return bubbleSprite;
}
// Whether a speech string has a voice clip ID
bool hasVoiceClip(String message)
{
return (message != null && message.Length>1 && message.Chars[0] == '&' && message.Chars[1] >= '0' && message.Chars[1] <= '9');
}
// Get the voice clip ID (speech line number) of a speech string, in the "&123" string format
String getLineNumber(String message)
{
if(hasVoiceClip(message))
{
String s = message.Substring(1, message.Length-1);
int n = s.AsInt;
return String.Format("&%d", n);
}
return null;
}
int calculateDuration(String message)
{