/
fancy.asc
1270 lines (1089 loc) · 35.8 KB
/
fancy.asc
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
// fancy module script
// Define maximum text tokens and stack size
#define MAX_TXTTK 1024
#define MAX_LINES 16
#define MAX_TAG_NEST 6
#define MAX_FANCY_BUTTON 32
// internal
struct FancyButtonData {
int ID;
DynamicSprite* Normal;
DynamicSprite* Over;
DynamicSprite* Pushed;
};
FancyButtonData _fancybtn[MAX_FANCY_BUTTON];
int _fancybtn_count;
Dictionary* _tvdict;
FancyConfig* _default_cfg;
Fancy9Piece* _default_f9p;
Fancy9Piece* _default_default_f9p;
FancyTypedText _sayft;
DynamicSprite* _sayspr;
Overlay* _say_last_speech;
Overlay* _say_ovr_tmp;
bool _say_started;
bool _say_delay_end;
int _say_char_head_y;
bool _say_is_room_coords;
int _clamp_i(int v, int max, int min)
{
if(v > max) return max;
if(v < min) return min;
return v;
}
int _get_character_height(Character* c)
{
ViewFrame* vf = Game.GetViewFrame(c.View, c.Loop, c.Frame);
if(vf == null) return 0;
return ((Game.SpriteHeight[vf.Graphic] + c.z) * c.Scaling)/100;
}
int _adjust_speech_y(int y, int height) // meant to avoid occluding player head if possible
{
y = _clamp_i(y, _clamp_i(_say_char_head_y-_sayspr.Height, Screen.Height, _sayspr.Height + 5), 0);
return _clamp_i(_clamp_i(y + height, Screen.Height-5, 5) - height, Screen.Height-5, 5);
}
int _adjust_speech_x(int x, int width) // avoids text going outside screen
{
return _clamp_i(_clamp_i(x + width, Screen.Width-5, 5) - width, Screen.Width-5, 5);
}
void _fancy_say_set_overlay(Overlay* curSpeech)
{
curSpeech.Graphic = _sayspr.Graphic;
int screen_x = curSpeech.X;
int screen_y = curSpeech.Y;
screen_x = _adjust_speech_x(screen_x, curSpeech.Width);
screen_y = _adjust_speech_y(screen_y, curSpeech.Height);
// there's a bug in AGS we will workaround : https://github.com/adventuregamestudio/ags/issues/1180
// The text overlay may be using room coordinates if Speech Style is lucasarts or is Sierra but the
// but the character has no speech view! We do this check in _fancy_say function.
int x = screen_x;
int y = screen_y;
if(_say_is_room_coords)
{
Point* p = Screen.Viewport.ScreenToRoomPoint(x, y, false);
x = p.x;
y = p.y;
}
curSpeech.Y = y;
curSpeech.X = x;
}
int _get_character_top_head_y(Character* c)
{
int y = 0;
Point* p = Screen.Viewport.RoomToScreenPoint(c.x, c.y, false);
if(p)
y = p.y;
y -= _get_character_height(c);
return y;
}
int _get_btn_real_id(Button* btn)
{
return btn.ID + btn.OwningGUI.ID * 64;
}
int _get_fancybtn_index(int id) {
for(int i=0; i<_fancybtn_count; i++) {
if(id == _fancybtn[i].ID) return i;
}
return -1;
}
int _get_new_fancybtn_index(int id) {
int index = _get_fancybtn_index(id);
if(index != -1) return index;
if(_fancybtn_count >= MAX_FANCY_BUTTON) return -1; // max capacity
index = _fancybtn_count;
_fancybtn_count++;
return index;
}
void _set_fancybtn(int index, int id, DynamicSprite* normal, DynamicSprite* over, DynamicSprite* pushed)
{
_fancybtn[index].ID = id;
_fancybtn[index].Normal = normal;
_fancybtn[index].Over = over;
_fancybtn[index].Pushed = pushed;
}
void _remove_fancybtn(int id)
{
int index = _get_fancybtn_index(id);
if(index != -1) return; // already removed
for(int i=index; i<_fancybtn_count-1; i++) {
_set_fancybtn(i, _fancybtn[i+1].ID, _fancybtn[i+1].Normal, _fancybtn[i+1].Over, _fancybtn[i+1].Pushed);
}
_set_fancybtn(_fancybtn_count-1, -1, null, null, null);
_fancybtn_count--;
}
#region TEXT_TOKEN_REG
managed struct TokenState {
int color;
int outline_color;
FontType font;
};
enum TkType
{
eTkText = 0,
eTkLinebreak,
eTkBlank,
eTkGraphic
};
FancyTextToken* _newLineBreakTxtToken()
{
FancyTextToken* tk = new FancyTextToken;
tk.type = eTkLinebreak;
return tk;
}
FancyTextToken* _newSpriteTxtToken(int sprite)
{
FancyTextToken* tk = new FancyTextToken;
tk.type = eTkGraphic;
tk.font = sprite;
tk.width = Game.SpriteWidth[sprite];
tk.height = Game.SpriteHeight[sprite];
return tk;
}
FancyTextToken* _newTxtToken(String text, int text_begin, int text_length, TokenState* ts)
{
FancyTextToken* tk = new FancyTextToken;
tk.text_begin = text_begin;
tk.text_length = text_length;
tk.color = ts.color;
tk.outline_color = ts.outline_color;
tk.font = ts.font;
tk.width = GetTextWidth(text, ts.font);
tk.height = GetFontHeight(ts.font);
return tk;
}
int _AppendTxtTok(FancyTextToken* txttk_arr[], int txttk_count, FancyTextToken* tk)
{
int i = txttk_count;
txttk_arr[i] = tk;
txttk_count++;
return txttk_count;
}
int _InserTxtTok(FancyTextToken* txttk_arr[], int txttk_count, FancyTextToken* tk, int index)
{
txttk_count++;
for(int i = txttk_count; i>index; i--)
{
txttk_arr[i] = txttk_arr[i-1];
}
txttk_arr[index] = tk;
return txttk_count;
}
FancyTextToken* [] _NewTxtTok()
{
FancyTextToken* txttk_arr[];
txttk_arr = new FancyTextToken [MAX_TXTTK];
return txttk_arr;
}
#endregion // TEXT_TOKEN_REG
enum TagType
{
eTagNone = 0,
eTagColor,
eTagOutlineColor,
eTagFont,
eTagSprite
};
#region STACK_OF_INT_REG
struct StackOfInt
{
int items[MAX_TAG_NEST];
int index;
import void Push(int number);
import int Pop();
};
void StackOfInt::Push(int number)
{
this.items[this.index] = number;
this.index++;
}
int StackOfInt::Pop()
{
this.index--;
return this.items[this.index];
}
#endregion // STACK_OF_INT_REG
FancyState* NewState() {
FancyState* fs = new FancyState;
return fs;
}
// Get the type of tag in format [>tag_type<:data]
TagType _get_tag_type(String tag)
{
int c = tag.Chars[0];
switch (c) {
case 'c': return eTagColor;
case 'o': return eTagOutlineColor;
case 'f': return eTagFont;
case 's': return eTagSprite;
}
return eTagNone;
}
// Get the data from tag in format [tag_type:>data<]
int _get_tag_data(String tag, TagType tag_type)
{
if (tag_type == eTagNone) return 0;
int p = tag.IndexOf(":") + 1;
if (p == 0) return -1;
String data = tag.Substring(p, tag.Length-p);
if (data.Chars[0] >= '0' && data.Chars[0] <= '9') return data.AsInt;
if (_tvdict.Contains(data)) {
String sval = _tvdict.Get(data);
return sval.AsInt;
}
return 0;
}
// Parse the input text for tags and generate text tokens
int _parse_text(FancyTextToken* txttk_arr[], String text, FancyConfig* cfg)
{
TokenState* ts = new TokenState;
int len = text.Length;
ts.color = cfg.TextColor;
ts.outline_color = cfg.OutlineColor;
ts.font = cfg.Font;
String token = "";
int token_start = 0;
int token_length = 0;
int txttk_count = 0;
StackOfInt stk_font;
StackOfInt stk_color;
StackOfInt stk_outcolor;
for (int i = 0; i < len; i++) {
int c = text.Chars[i];
if (c == '[') {
if (token.Length > 0) {
txttk_count = _AppendTxtTok(txttk_arr, txttk_count, _newTxtToken(token, token_start, token_length, ts));
token = "";
token_start = i - token_length;
token_length = 0;
}
i++;
bool is_closing = false;
if (i < len && text.Chars[i] == '/') {
is_closing = true;
i++;
}
int j = i;
while (j <= len && text.Chars[j] != ']') j++;
int delta = j - i;
String strtag = text.Substring(i, delta);
TagType tag = _get_tag_type(strtag);
TagType tdata = tag;
if (is_closing) tdata = eTagNone;
int data = _get_tag_data(strtag, tdata);
switch (tag) {
case eTagColor:
if (is_closing) {
ts.color = stk_color.Pop();
} else {
stk_color.Push(ts.color);
ts.color = data;
}
break;
case eTagOutlineColor:
if (is_closing) {
ts.outline_color = stk_outcolor.Pop();
} else {
stk_outcolor.Push(ts.outline_color);
ts.outline_color = data;
}
break;
case eTagFont:
if (is_closing) {
ts.font = stk_font.Pop();
} else {
stk_font.Push(ts.font);
ts.font = data;
}
break;
// following tags have no closing elements
case eTagSprite:
txttk_count = _AppendTxtTok(txttk_arr, txttk_count, _newSpriteTxtToken(data));
break;
}
i += delta + is_closing - 1;
if (is_closing) {
token_start = i;
}
} else if (c == ']') {
// skip
} else if (c == ' ' || c == '\n') {
if (token.Length > 0) {
txttk_count = _AppendTxtTok(txttk_arr, txttk_count, _newTxtToken(token, token_start, token_length, ts));
token = "";
token_start = i - token_length;
token_length = 0;
}
txttk_count = _AppendTxtTok(txttk_arr, txttk_count, _newTxtToken(" ", i, 1, ts));
} else {
if (token.Length == 0) {
token_start = i;
}
token = token.AppendChar(c);
token_length++;
}
}
if (token_length > 0) {
txttk_count = _AppendTxtTok(txttk_arr, txttk_count, _newTxtToken(token, token_start, token_length, ts));
}
_AppendTxtTok(txttk_arr, txttk_count, null);
return txttk_count;
}
int _do_word_wrapping(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text)
{
int line_height[MAX_LINES];
int line_width[MAX_LINES];
int line = 0;
int w = 0;
int width = fs.MaxTextWidth;
for (int i = 0; i < tk_count; i++) {
FancyTextToken* t = tk_arr[i];
String word = "";
if(t.type == eTkText) word = text.Substring(t.text_begin, t.text_length);
bool is_wrap_linebreak = w + t.width > width;
bool is_char_linebreak = word == "\n";
if (word == " ") {
t.type = eTkBlank; // Optimize for the draw step
}
if (is_wrap_linebreak || is_char_linebreak) {
line++;
w = 0;
if (is_char_linebreak) {
t.width = 0;
t.type = eTkLinebreak;
} else if (is_wrap_linebreak) {
tk_count = _InserTxtTok(tk_arr, tk_count, _newLineBreakTxtToken(), i);
i++; // advance i due to insertion
}
}
if (t.height > line_height[line]) {
line_height[line] = t.height;
}
w += t.width;
line_width[line] = w;
}
line++;
int max_width = 0;
int text_height = 0;
for (int i = 0; i < line; i++) {
if (max_width < line_width[i]) {
max_width = line_width[i];
}
text_height += line_height[i];
}
line = 0;
fs.TextWidth = max_width;
fs.TextHeight = text_height;
for (int i = 0; i < tk_count; i++) {
FancyTextToken* t = tk_arr[i];
t.line_height = line_height[line];
t.line_width = line_width[line];
// System.Log(eLogInfo, "(%d) bw %d lw %d tw %d lh %d, %d : %s", i, t.text_width, t.line_width, t.width, t.line_height, t.type, text.Substring(t.text_begin, t.text_length));
// System.Log(eLogInfo, "(%d) len %d type %d : %s", i, t.text_length, t.type, text.Substring(t.text_begin, t.text_length));
if (t.type == eTkLinebreak) {
line++;
t.line_width = line_width[line];
}
}
return tk_count;
}
String _get_as_plain_text(FancyTextToken* tk_arr[], int tk_count, String text)
{
String s = "";
for (int i = 0; i < tk_count; i++) {
FancyTextToken* t = tk_arr[i];
switch (t.type) {
case eTkBlank:
s = s.Append(" ");
break;
case eTkLinebreak:
s = s.Append("\n");
break;
case eTkText:
if(t.text_length > 0) {
s = s.Append(text.Substring(t.text_begin, t.text_length));
}
break;
case eTkGraphic:
// skip
break;
}
}
return s;
}
void _adjust_state_sizes_to_config(FancyState* fs, FancyConfig* cfg)
{
int adjust = cfg.OutlineWidth + cfg.Padding;
fs.TextWidth += adjust*2;
fs.TextHeight += adjust*2;
fs.X += adjust;
fs.Y += adjust;
}
void _draw_string_outline(this DrawingSurface*, int x, int y, FontType font, String text, int outline_color, int outline_width)
{
this.DrawingColor = outline_color;
for(int i = -outline_width; i <= outline_width; i++) {
for(int j = -outline_width; j <= outline_width; j++) {
this.DrawString(x+i, y+j, font, text);
}
}
}
void _draw_text(DrawingSurface* surf, int x, int y, int color, int outline_color, int outline_width, FontType font, const string text)
{
if(outline_color != COLOR_TRANSPARENT && outline_width > 0) {
surf._draw_string_outline(x, y, font, text, outline_color, outline_width);
}
surf.DrawingColor = color;
surf.DrawString(x, y, font, text);
}
void _draw_sprite(DrawingSurface* surf, int x, int y, int graphic)
{
surf.DrawImage(x, y, graphic);
}
enum VerticalAlignment {
eVAlignTop = 1,
eVAlignMiddle = 2,
eVAlignBottom = 4
};
int _get_align_x(HorizontalAlignment halign, int x, FancyTextToken* t, int text_width)
{
switch(halign) {
case eAlignRight: return x + text_width - t.line_width;
case eAlignCenter: return x + text_width/2 - t.line_width/2;
default: return x; break;
}
}
int _get_align_y(VerticalAlignment valign, int y, FancyTextToken* t)
{
switch(valign) {
case eVAlignTop: return y; // do nothing
case eVAlignMiddle: return y + t.line_height/2 - t.height/2; break;
default: return y + t.line_height - t.height; break; // bottom by default
}
}
// Write text tokens on a surface
void _draw_tokens(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, DrawingSurface* surf, String text, int partial, FancyConfig* cfg)
{
if(tk_count <= 0) return;
if(fs == null) return;
int x = fs.X;
int y = fs.Y;
int r_x = x;
int r_y = y;
VerticalAlignment valign = ((cfg.TextAlign & eAlignHasTop) && true) * eVAlignTop + ((cfg.TextAlign & eAlignHasVerCenter ) && true) * eVAlignMiddle;
HorizontalAlignment halign = ((cfg.TextAlign & eAlignHasRight) && true) * eAlignRight + ((cfg.TextAlign & eAlignHasHorCenter ) && true) * eAlignCenter;
int text_width = fs.TextWidth;
r_x = _get_align_x(halign, x, tk_arr[0], text_width);
int tk_len;
for (int i = 0; i < tk_count; i++) {
FancyTextToken* t = tk_arr[i];
int line_height = t.line_height;
int draw_y = _get_align_y(valign, r_y, t);
switch (t.type) {
case eTkLinebreak:
r_x = _get_align_x(halign, x, t, text_width);
r_y += line_height;
continue; // this will skip to next line and token
case eTkText:
tk_len = t.text_length;
if(partial >= 0 && i == tk_count-1) tk_len = partial;
_draw_text(surf, r_x, draw_y, t.color, t.outline_color, cfg.OutlineWidth, t.font, text.Substring(t.text_begin, tk_len));
break;
case eTkGraphic:
_draw_sprite(surf, r_x, draw_y, t.font);
break;
}
r_x += t.width;
}
}
int _i_max3(int v1, int v2, int v3)
{
if(v1 > v2 && v1 > v3) return v1;
if(v2 > v1 && v2 > v3) return v2;
return v3;
}
int _i_min3(int v1, int v2, int v3)
{
if(v1 < v2 && v1 < v3) return v1;
if(v2 < v1 && v2 < v3) return v2;
return v3;
}
int _f9p_get_left_border(Fancy9Piece* f9p) { return _i_max3(f9p.TL_w, f9p.BL_w, f9p.L_w); }
int _f9p_get_right_border(Fancy9Piece* f9p) { return _i_max3(f9p.TR_w, f9p.BR_w, f9p.R_w); }
int _f9p_get_top_border(Fancy9Piece* f9p) { return _i_max3(f9p.TL_h, f9p.TR_h, f9p.T_h); }
int _f9p_get_bottom_border(Fancy9Piece* f9p) { return _i_max3(f9p.BL_h, f9p.BR_h, f9p.B_h); }
DynamicSprite* _create_textbox(int internal_width, int internal_height, Fancy9Piece* f9p)
{
int left_border = f9p.BorderLeft;
int right_border = f9p.BorderRight;
int top_border = f9p.BorderTop;
int bottom_border = f9p.BorderBottom;
int w = left_border + internal_width + right_border;
int h = top_border + internal_height + bottom_border;
DynamicSprite* spr = DynamicSprite.Create(w, h, true);
DrawingSurface* surf = spr.GetDrawingSurface();
int bg_color = f9p.CBG_Color;
if(f9p.CBG) surf.DrawImage(left_border, top_border, f9p.CBG, 0, internal_width, internal_height); //draw background
else if(bg_color && bg_color != COLOR_TRANSPARENT) {
surf.DrawingColor = bg_color;
surf.DrawRectangle(left_border, top_border, left_border + internal_width, top_border + internal_height);
}
if(f9p.TL) surf.DrawImage(0, 0, f9p.TL); // Top Left Corner
if(f9p.TR) surf.DrawImage(w - f9p.TR_w, 0, f9p.TR); // Top Right Corner
if(f9p.BL) surf.DrawImage(0, h - f9p.BL_h, f9p.BL); // Bottom Left Corner
if(f9p.BR) surf.DrawImage(w - f9p.BR_w, h - f9p.BR_h, f9p.BR); // Bottom Right Corner
if(f9p.T) surf.DrawImage(f9p.TL_w, 0, f9p.T, 0, w - f9p.TL_w - f9p.TR_w, f9p.T_h); // Top Edge
if(f9p.L) surf.DrawImage(0, f9p.TL_h, f9p.L, 0, f9p.L_w, h - f9p.TL_h - f9p.BL_h); // Left Edge
if(f9p.R) surf.DrawImage(w - f9p.R_w, f9p.TR_h, f9p.R, 0, f9p.R_w, h - f9p.TR_h - f9p.BR_h); // Right Edge
if(f9p.B) surf.DrawImage(f9p.BL_w, h - f9p.B_h, f9p.B, 0, w - f9p.BL_w - f9p.BR_w, f9p.B_h); // Bottom Edge
return spr;
}
DynamicSprite* _create_textbox_from_textgraphic(Fancy9Piece* f9p, int internal_width, int internal_height, int text_graphic)
{
if(f9p == null) return null;
DynamicSprite* textbox = _create_textbox(internal_width, internal_height, f9p);
DrawingSurface* surf = textbox.GetDrawingSurface();
surf.DrawImage(f9p.BorderLeft, f9p.BorderTop, text_graphic);
surf.Release();
return textbox;
}
int _get_speech_width(Character* c, int width)
{
int portrait_added_width = 0;
if(Speech.Style == eSpeechSierra || Speech.Style == eSpeechSierraWithBackground)
{
ViewFrame* vf = null;
int speech_view = c.SpeechView;
if(speech_view != 0) {
vf = Game.GetViewFrame(speech_view, 0, 0);
}
if(vf != null) {
portrait_added_width = Game.SpriteWidth[vf.Frame];
}
}
portrait_added_width += 35;
width = _clamp_i(width, Screen.Width - portrait_added_width, 0);
return width;
}
DynamicSprite* _create_textbox_from_text_spr(DynamicSprite* text_spr, Fancy9Piece* f9p)
{
if(f9p == null)
return text_spr;
return _create_textbox_from_textgraphic(f9p, text_spr.Width, text_spr.Height, text_spr.Graphic);
}
DynamicSprite* _create_text_sprite(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text, int typed_token_len, FancyConfig* cfg)
{
DynamicSprite* text_spr = DynamicSprite.Create(fs.TextWidth, fs.TextHeight, true);
DrawingSurface* surf = text_spr.GetDrawingSurface();
_draw_tokens(tk_arr, tk_count, fs, surf, text, typed_token_len, cfg);
surf.Release();
return text_spr;
}
int _process_fancy_string(FancyTextToken* tk_arr[], FancyState* fs, String text, FancyConfig* config)
{
if(String.IsNullOrEmpty(text))
return 0;
int tk_count = _parse_text(tk_arr, text, config);
tk_count = _do_word_wrapping(tk_arr, tk_count, fs, text);
_adjust_state_sizes_to_config(fs, config); // fix outline and margin
return tk_count;
}
// ----------------------------------------------------------------------
// ---------------- fancy module public interface -----------------------
// ----------------------------------------------------------------------
static FancyConfig* FancyConfig::Create(FontType font, int color, int outline_color, int outline_width, Alignment align, int line_spacing)
{
FancyConfig* cfg = new FancyConfig;
cfg.Font = font;
cfg.LineSpacing = line_spacing;
cfg.OutlineColor = outline_color;
cfg.OutlineWidth = outline_width;
cfg.TextAlign = align;
cfg.TextColor = color;
return cfg;
}
void FancyConfig::Set(FancyConfig* config)
{
this.Font = config.Font;
this.LineSpacing = config.LineSpacing;
this.OutlineColor = config.OutlineColor;
this.OutlineWidth = config.OutlineWidth;
this.Padding = config.Padding;
this.TextAlign = config.TextAlign;
this.TextColor = config.TextColor;
}
FancyConfig* FancyConfig::Clone()
{
FancyConfig* cfg = new FancyConfig;
cfg.Set(this);
return cfg;
}
FancyConfig* _get_speech_config(Character* c, FancyConfig* config)
{
if(config == null) {
config = _default_cfg.Clone();
config.Font = Game.SpeechFont;
config.TextColor = c.SpeechColor;
config.OutlineWidth = 0;
config.OutlineColor = COLOR_TRANSPARENT;
config.Padding = 1;
}
return config;
}
static void Fancy::AddAlias(String key, int value)
{
_tvdict.Set(key, String.Format("%d", value));
}
static FancyConfig* Fancy::get_FancyConfig()
{
if(_default_cfg == null) {
_default_cfg = FancyConfig.Create();
}
return _default_cfg;
}
static void Fancy::set_FancyConfig(FancyConfig* value)
{
if(value == null) {
value = FancyConfig.Create();
}
if(_default_cfg == null) {
_default_cfg = FancyConfig.Create();
}
_default_cfg.Set(value);
}
void Fancy9Piece::SetAll(int top, int bottom, int left, int right, int top_left, int top_right, int bottom_left, int bottom_right, int background, int bg_color)
{
this.T = top;
this.B = bottom;
this.L = left;
this.R = right;
this.TL = top_left;
this.TR = top_right;
this.BL = bottom_left;
this.BR = bottom_right;
this.CBG = background;
this.CBG_Color = bg_color;
if(this.T > 0) { this.T_w = Game.SpriteWidth[this.T]; this.T_h = Game.SpriteHeight[this.T]; } else {this.T_w = 0; this.T_h = 0;}
if(this.B > 0) { this.B_w = Game.SpriteWidth[this.B]; this.B_h = Game.SpriteHeight[this.B]; } else {this.B_w = 0; this.B_h = 0;}
if(this.L > 0) { this.L_w = Game.SpriteWidth[this.L]; this.L_h = Game.SpriteHeight[this.L]; } else {this.L_w = 0; this.L_h = 0;}
if(this.R > 0) { this.R_w = Game.SpriteWidth[this.R]; this.R_h = Game.SpriteHeight[this.R]; } else {this.R_w = 0; this.R_h = 0;}
if(this.TL > 0) { this.TL_w = Game.SpriteWidth[this.TL]; this.TL_h = Game.SpriteHeight[this.TL]; } else {this.TL_w = 0; this.TL_h = 0;}
if(this.TR > 0) { this.TR_w = Game.SpriteWidth[this.TR]; this.TR_h = Game.SpriteHeight[this.TR]; } else {this.TR_w = 0; this.TR_h = 0;}
if(this.BL > 0) { this.BL_w = Game.SpriteWidth[this.BL]; this.BL_h = Game.SpriteHeight[this.BL]; } else {this.BL_w = 0; this.BL_h = 0;}
if(this.BR > 0) { this.BR_w = Game.SpriteWidth[this.BR]; this.BR_h = Game.SpriteHeight[this.BR]; } else {this.BR_w = 0; this.BR_h = 0;}
if(this.CBG > 0) { this.CBG_w = Game.SpriteWidth[this.CBG]; this.CBG_h = Game.SpriteHeight[this.CBG]; } else {this.CBG_w = 0; this.CBG_h = 0;}
this.BorderLeft = _f9p_get_left_border(this);
this.BorderRight = _f9p_get_right_border(this);
this.BorderTop = _f9p_get_top_border(this);
this.BorderBottom = _f9p_get_bottom_border(this);
}
static Fancy9Piece* Fancy9Piece::CreateFrom9Sprites(int top, int bottom, int left, int right, int top_left, int top_right, int bottom_left, int bottom_right, int background, int bg_color)
{
Fancy9Piece* f9p = new Fancy9Piece;
f9p.SetAll(top, bottom, left, right, top_left, top_right, bottom_left, bottom_right, background, bg_color);
return f9p;
}
static Fancy9Piece* Fancy9Piece::CreateFromTextWindowGui(GUI* text_window_gui)
{
TextWindowGUI* twg = null;
if(text_window_gui != null) {
twg = text_window_gui.AsTextWindow;
}
if(twg == null) {
if(game.speech_text_gui >= 0 && Speech.Style != eSpeechLucasarts) {
twg = gui[game.speech_text_gui].AsTextWindow;
}
if(twg == null)
return null;
}
Button* btn_top_left = twg.Controls[0].AsButton;
Button* btn_bot_left = twg.Controls[1].AsButton;
Button* btn_top_right = twg.Controls[2].AsButton;
Button* btn_bot_right = twg.Controls[3].AsButton;
Button* btn_left = twg.Controls[4].AsButton;
Button* btn_right = twg.Controls[5].AsButton;
Button* btn_top = twg.Controls[6].AsButton;
Button* btn_bot = twg.Controls[7].AsButton;
int sprite_bg = twg.BackgroundGraphic;
int bg_color = twg.BackgroundColor;
return Fancy9Piece.CreateFrom9Sprites(btn_top.NormalGraphic, btn_bot.NormalGraphic, btn_left.NormalGraphic, btn_right.NormalGraphic,
btn_top_left.NormalGraphic, btn_top_right.NormalGraphic, btn_bot_left.NormalGraphic, btn_bot_right.NormalGraphic,
sprite_bg, bg_color);
}
void Fancy9Piece::Set(Fancy9Piece* v)
{
if(v == null) v = Fancy9Piece.CreateFrom9Sprites(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
this.B = v.B; this.B_h = v.B_h; this.B_w = v.B_w;
this.L = v.L; this.L_h = v.L_h; this.L_w = v.L_w;
this.R = v.R; this.R_h = v.R_h; this.R_w = v.R_w;
this.T = v.T; this.T_h = v.T_h; this.T_w = v.T_w;
this.TL = v.TL; this.TL_h = v.TL_h; this.TL_w = v.TL_w;
this.TR = v.TR; this.TR_h = v.TR_h; this.TR_w = v.TR_w;
this.BL = v.BL; this.BL_h = v.BL_h; this.BL_w = v.BL_w;
this.BR = v.BR; this.BR_h = v.BR_h; this.BR_w = v.BR_w;
this.CBG = v.CBG; this.CBG_Color = v.CBG_Color; this.CBG_h = v.CBG_h; this.CBG_w = v.CBG_w;
this.BorderBottom = v.BorderBottom; this.BorderLeft = v.BorderLeft; this.BorderRight = v.BorderRight; this.BorderTop = v.BorderTop;
}
Fancy9Piece* Fancy9Piece::Clone()
{
Fancy9Piece* f9p = new Fancy9Piece;
f9p.Set(this);
return f9p;
}
static Fancy9Piece* Fancy::get_Fancy9Piece()
{
return _default_f9p;
}
static void Fancy::set_Fancy9Piece(Fancy9Piece* value)
{
if(value == null) {
value = _default_default_f9p.Clone();
}
if(_default_f9p == null) {
_default_f9p = value.Clone();
}
_default_f9p.Set(value);
}
void DrawFancyString(this DrawingSurface*, int x, int y, const string text, FancyConfig* config, int width)
{
FancyTextToken* tk_arr[] = _NewTxtTok();
FancyState* fs = NewState();
if(config == null)
config = _default_cfg.Clone();
fs.X = x; fs.Y = y; fs.MaxTextWidth = width;
int tk_count = _process_fancy_string(tk_arr, fs, text, config);
if(tk_count <= 0) return;
_draw_tokens(tk_arr, tk_count, fs, this, text, -1, config);
}
DynamicSprite* CreateFromFancyString(static DynamicSprite, const string text, FancyConfig* config, int width)
{
FancyTextToken* tk_arr[] = _NewTxtTok();
FancyState* fs = NewState();
if(config == null)
config = _default_cfg.Clone();
fs.MaxTextWidth = width;
int tk_count = _process_fancy_string(tk_arr, fs, text, config);
if(tk_count <= 0) return null;
return _create_text_sprite(tk_arr, tk_count, fs, text, -1, config);
}
DynamicSprite* CreateFromFancyTextBox(static DynamicSprite, const string text, FancyConfig* config, int width, Fancy9Piece* f9p)
{
if(String.IsNullOrEmpty(text)) return null;
if(config == null) config = _default_cfg.Clone();
DynamicSprite* text_spr = DynamicSprite.CreateFromFancyString(text, config, width);
return _create_textbox_from_text_spr(text_spr, f9p);
}
void Fancify(this Button*, Fancy9Piece* normal9p, Fancy9Piece* mouse_over9p, Fancy9Piece* pushed9p)
{
if(String.IsNullOrEmpty(this.Text))
return;
DynamicSprite* spr_normal, spr_over, spr_pushed;
int id = _get_btn_real_id(this);
int index = _get_new_fancybtn_index(id);
if(index < 0) AbortGame("Maximum Fancy Button capacity reached");
int border_left, border_right, border_top, border_bottom;
if(normal9p != null) {
border_left = normal9p.BorderLeft;
border_right = normal9p.BorderRight;
border_top = normal9p.BorderTop;
border_bottom = normal9p.BorderBottom;
}
int w = this.Width - border_left - border_right;
int h = this.Height - border_top - border_bottom;
String text = this.Text;
int color = this.TextColor;
int font = this.Font;
FancyConfig* config = FancyConfig.Create(font, color);
DynamicSprite* text_spr = DynamicSprite.CreateFromFancyString(text, config, w);
if(text_spr.Height > h) {
h = text_spr.Height;
}
spr_normal = _create_textbox_from_textgraphic(normal9p, w, h, text_spr.Graphic);
if(spr_normal == null) {
spr_normal = text_spr;
} else {
spr_over = _create_textbox_from_textgraphic(mouse_over9p, w, h, text_spr.Graphic);
spr_pushed = _create_textbox_from_textgraphic(pushed9p, w, h, text_spr.Graphic);
}
_set_fancybtn(index, id, spr_normal, spr_over, spr_pushed);
if(spr_normal != null) this.NormalGraphic = spr_normal.Graphic;
if(spr_over != null) this.MouseOverGraphic = spr_over.Graphic;
if(spr_pushed!= null) this.PushedGraphic = spr_pushed.Graphic;
this.Text = "";
}
void UnFancify(this Button*)
{
int id = _get_btn_real_id(this);
this.NormalGraphic = 0;
this.MouseOverGraphic = 0;
this.PushedGraphic = 0;
_remove_fancybtn(id);
}
FancyConfig* FancyTextBase::get_FancyConfig()
{
if(this._cfg == null) {
this._cfg = _default_cfg.Clone();
}
return this._cfg;
}
void FancyTextBase::set_FancyConfig(FancyConfig* value)
{
if(value == null) {
value = _default_cfg.Clone();
}
if(this._cfg == null) {
this._cfg = _default_cfg.Clone();
}
this._cfg.Set(value);
}
void FancyTextBase::SetDrawingArea(int x, int y, int width)
{
if(this._fs == null)
this._fs = NewState();
this._fs.X = x;
this._fs.Y = y;
this._fs.MaxTextWidth = width;
}
// FIX-ME: reduce code duplication
protected void FancyTextBase::_set_text(String text)
{
if(this._cfg == null) { this._cfg = _default_cfg.Clone(); }
if(this._fs == null) { this._fs = NewState(); }
if(String.IsNullOrEmpty(text)) {
this._text = "";
this._tk_arr = null;
this._tk_count = 0;
return;
}
int font = this._cfg.Font;
int color = this._cfg.TextColor;
if(this._fs.MaxTextWidth <= 0) { this._fs.MaxTextWidth = FANCY_INFINITE_WIDTH; }
if(this._f9p != null) {
this._fs.MaxTextWidth -= (this._f9p.BorderLeft + this._f9p.BorderRight);
}
FancyTextToken* tk_arr[] = _NewTxtTok();
int tk_count = _process_fancy_string(tk_arr, this._fs, text, this._cfg);
this._tk_arr = tk_arr;