forked from seleb/bitsy-hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextbox-styler.js
1367 lines (1211 loc) · 53.9 KB
/
textbox-styler.js
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
/**
📐
@file textbox styler
@summary customize the style and properties of the textbox
@license MIT
@version 13.3.6
@requires Bitsy Version: 6.1
@author Dana Holdampf & Sean S. LeBlanc
@description
This hack lets you edit the appearance, position, and other properties of the textbox.
It lets you draw a border, replace the continue arrow, resize or reposition the box, recolor text or backgrounds, etc.
You can also use dialog tags to switch styles, or redefine style properties, from a dialog.
NOTE: This hack re-implements the functionality of the Long Dialog hack.
Since they modify the same code, don't use them both to avoid conflicts!
Like the Long Dialog hack, it also includes the paragraph break "(p)" dialog tag.
HOW TO USE:
1. Copy-paste this script into a script tag after the bitsy source
2. Edit hackOptions below, to define the default textbox style
3. Optionally, add additional alternate styles to the dialogStyles section
You can define custom style properties for the textbox in the hackOptions below.
- The topmost options are the default style properties, which are applied to the textbox initially.
- The dialogStyles section is used for defining alternate styles you can switch between.
- Use the dialog commands below to switch between styles, or change individual properties.
The Dialog Tags for switching textbox styles are as follows:
============================================================
-- SWITCH TEXTBOX STYLE, WITH UNDEFINED PROPERTIES BEING IGNORED ---------------
{textStyle "dialogStyle"}
{textStyleNow "dialogStyle"}
Information:
- Replaces current textbox style with a new set of style properties, as defined in the dialogStyles below.
- Only changes style properties that are defined in the dialogStyle. Undefined properties are left unchanged.
Parameters:
- dialogStyle: id/name of a dialogStyle, as defined in the hackOptions below. (ex. "vanilla", "centered", "inverted", etc.)
-- SWITCH TEXTBOX STYLE, WITH UNDEFINED PROPERTIES REVERTING TO DEFAULTS -------
{setTextStyle "dialogStyle"}
{setTextStyleNow "dialogStyle"}
Information:
- As above, but any undefined style properties in the new dialogStyle revert to the default style properties.
Parameters:
- dialogStyle: id/name of a dialog style, as defined in the hackOptions below. (ex. "vanilla", "centered", "inverted", etc.)
-- CHANGE AN INDIVIDUAL TEXTBOX STYLE PROPERTY ---------------------------------
{textProperty "styleProperty, styleValue"}
{textPropertyNow "styleProperty, styleValue"}
Information:
- Sets an individual textbox style property, to a given value. See the hackOptions below for valid properties and values.
Parameters:
- styleProperty: id/name of a style property, as defined in the hackOptions below. (ex. "borderColor", "textboxWidth", "textSpeed", etc.)
- styleValue: value to assign to the styleProperty, as defined in the hackOptions below. (ex. "[128,128,128,256]", "140", etc.)
-- REVERT TEXTBOX STYLE TO THE DEFAULT STYLE -----------------------------------
{resetTextStyle}
{resetTextStyleNow}
Information:
- Resets all style properties to the values in the default dialog style, as defined in the hackOptions below.
-- REVERT AN INDIVIDUAL TEXTBOX STYLE PROPERTY TO IT'S DEFAULT VALUE -----------
{resetProperty "styleProperty"}
{resetPropertyNow "styleProperty"}
Information:
- Resets an individual style property to it's default value, as defined in the hackOptions below.
Parameters:
- styleProperty: id/name of a style property, as defined in the hackOptions below. (ex. "borderColor", "textboxWidth", "textSpeed", etc.)
-- SET OR MODIFY THE POSITION AND SIZE OF THE TEXTBOX TO AN ABSOLUTE VALUE -----
{textPosition "x, y, width, minLines, maxLines"}
{textPositionNow "x, y, width, minLines, maxLines"}
Information:
- Repositions and resizes the textbox, at an absolute position based on parameters.
- Calculates the necessary style properties and spacing for the textbox automatically.
Parameters:
- x, y: The x and y coordinates of the top left corner. Measured in bitsy-scale pixels (0-128).
- width: The width of the textbox. Measured in bitsy-scale pixels (0-128).
- minLines: The minimum number of lines to display on the textbox. Height resizes automatically.
- maxLines: The maximum number of lines to display on the textbox. Height resizes automatically.
If any parameter is left blank, the textbox will use it's current values.
If given a +/- value (+8, -40, etc) that value is adjusted relative to it's current value.
----------------------------------------------------------------
DIALOG TAG NOTES:
- Add "Now" to the end of these dialog tags to make the tag trigger mid-dialog, rather than after the current dialog is concluded.
- To reset a property or style to default values, you can also set a style or style property value to "default".
- NOTE: Changing the style after some text is already printed can break existing text.
- Colors are defined as 4 values, [red,green,blue,alpha], each ranging from 0-255.
- Alpha is opacity; colors can be translucent by reducing Alpha below 255. 0 is fully transparent.
Examples:
- {textStyleNow "center"} immediately applies a style defined below, which centers the textbox, without overriding other existing style properties.
- {setTextStyle "vertical"} after the current dialog ends, switches to a vertical textbox style, defined in dialogStyles.
- {textPropertyNow "textSpeed, 25"} immediately reduces the time to print each character to 25ms.
- {resetTextStyle} Once this text is finished, will reset the textbox to the default style.
- {resetPropertyNow "textScale"} Immediately restores the text scale property to the default setting.
- {textPosition "8, 8, 120"} Resizes the textbox to cover the entire screen, with 8 pixels of padding on every side.
TODO: For future versions
- Redraw existing textbox contents in new style, when switching, and then continue.
- Recalculate textbox's center position, considering textbox margins in center calculation?
*/
this.hacks = this.hacks || {};
(function (exports, bitsy) {
'use strict';
var hackOptions = {
// Determines where the textbox is positioned. "shift" moves the textbox based on the player's position.
verticalPosition: 'shift', // options are "top", "center", "bottom", or "shift" (moves based on player position)
horizontalPosition: 'center', // options are "left", "center", "right", or "shift" (moves based on player position)
textboxColor: [0, 0, 0, 255], // Colors text and textbox are drawn, in [R,G,B,A]. TODO: Alpha doesn't presently work!
textboxWidth: 120, // Width of textbox in pixels. Default 104.
textboxMarginX: 4, // Pixels of space outside the left (or right) of the textbox. Default 12.
textboxMarginY: 4, // Pixels of space outside the top (or bottom) of the textbox. Default 12.
textColor: [255, 255, 255, 255], // Default color of text.
textMinLines: 2, // Minimum number of rows for text. Determines starting textbox height. Default 2.
textMaxLines: 2, // Maximum number of rows for text. Determines max textbox height. Default 2.
textScale: 2, // Scaling factor for text. Default 2. Best with 1, 2, or 4.
textSpeed: 50, // Time to print each character, in milliseconds. Default 50.
textPaddingX: 0, // Default horizontal padding around the text.
textPaddingY: 2, // Default vertical padding around the text.
// Color the continue arrow is drawn using, in [R,G,B,A]
arrowColor: [255, 255, 255, 255], // Foreground color of arrow sprite.
arrowBGColor: [0, 0, 0, 255], // Background color. If transparent, draws no BG.
// Position of textbox continue arrow, on bottom of textbox.
arrowAlign: 'right', // Options are: "right", "center", or "left" aligned
// Pixels of padding the arrow is inset from the edge by
arrowInsetX: 12,
arrowInsetY: 0,
// Should match dimensions of the sprite below
arrowWidth: 8, // Width of arrow sprite below, in pixels
arrowHeight: 5, // Height of arrow sprite below, in pixels
arrowScale: 2, // Scaling factor for arrow sprite. Default 2. Best with 1, 2, or 4.
// Pixels defining the textbox continue arrow. 1 draws a pixel in main Color, 0 draws in BG Color.
arrowSprite: [
1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
// Colors the borders are drawn using, in [R,G,B,A].
borderColor: [128, 128, 128, 255], // Foreground color for border tiles.
borderMidColor: [51, 51, 51, 255], // Foreground color used for middle border tiles.
borderBGColor: [0, 0, 0, 255], // Background color the border tiles. If transparent, draws no BG.
// Border is drawn past the edges of the textbox.
// Should match dimensions of the sprite below
borderWidth: 8, // Width of border sprites, in pixels. Default 8.
borderHeight: 8, // Height of border sprites, in pixels. Default 8.
borderScale: 2, // Scaling factor for border sprites. Default 2. Best with 1, 2, or 4.
// Pixels defining the corners and edges the border is drawn with. 1 draws a pixel in foreground color, 0 in BG.
borderUL: [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
],
borderU: [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
borderUR: [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
],
borderL: [
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
],
borderR: [
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
],
borderDL: [
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
borderD: [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
borderDR: [
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
borderM: [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
dialogStyles: {
// You can define alternate Dialog Styles here, which can be switched to in-game from dialog.
// These can be switched between using the (Style "styleName") or (ApplyStyle "styleName") commands.
// These Dialog Styles are meant as examples. Feel free to edit, rename, or remove them.
custom: {
// Copy any hackOptions from above into this section, and modify them, to create a new Style.
},
vanilla: {
// An example style, which emulates the original Bitsy textbox.
verticalPosition: 'shift',
horizontalPosition: 'center',
textboxWidth: 104,
textboxMarginX: 12,
textboxMarginY: 12,
textMinLines: 2,
textMaxLines: 2,
textPaddingX: 8,
textPaddingY: 10,
borderWidth: 0,
borderHeight: 0,
arrowScale: 4,
arrowInsetX: 4,
arrowInsetY: 1,
arrowWidth: 5,
arrowHeight: 3,
arrowSprite: [
1, 1, 1, 1, 1,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0,
],
},
centered: {
// An example style, that centers the textbox as with Starting or Ending text.
verticalPosition: 'center',
horizontalPosition: 'center',
},
vertical: {
// An example style, that positions the textbox vertically, on the left or right side.
verticalPosition: 'center',
horizontalPosition: 'shift',
textboxWidth: 48,
textMinLines: 16,
textMaxLines: 16,
},
corner: {
// An example style, which positions a resizing textbox in the corner opposite the player.
verticalPosition: 'shift',
horizontalPosition: 'shift',
textboxWidth: 64,
textMinLines: 1,
textMaxLines: 8,
},
inverted: {
// An example style, which inverts the normal textbox colors
textboxColor: [255, 255, 255, 255],
textColor: [0, 0, 0, 255],
borderColor: [128, 128, 128, 255],
borderMidColor: [204, 204, 204, 255],
borderBGColor: [255, 255, 255, 255],
arrowColor: [0, 0, 0, 255],
arrowBGColor: [255, 255, 255, 255],
},
smallBorder: {
// An example style, which uses a smaller border with a blue background.
borderWidth: 4,
borderHeight: 4,
textPaddingX: 4,
textPaddingY: 4,
borderColor: [255, 255, 255, 255],
borderBGColor: [51, 153, 204, 255],
arrowBGColor: [51, 153, 204, 255],
borderUL: [
0, 0, 0, 0,
0, 1, 1, 1,
0, 1, 1, 0,
0, 1, 0, 1,
],
borderU: [
0, 0, 0, 0,
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
],
borderUR: [
0, 0, 0, 0,
1, 1, 1, 0,
0, 0, 1, 0,
0, 1, 1, 0,
],
borderL: [
0, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
],
borderR: [
1, 1, 1, 0,
1, 1, 1, 0,
1, 1, 1, 0,
1, 1, 1, 0,
],
borderDL: [
0, 1, 0, 0,
0, 1, 0, 1,
0, 1, 1, 1,
0, 0, 0, 0,
],
borderD: [
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
0, 0, 0, 0,
],
borderDR: [
0, 1, 1, 0,
1, 0, 1, 0,
1, 1, 1, 0,
0, 0, 0, 0,
],
borderM: [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
],
},
},
};
bitsy = bitsy && Object.prototype.hasOwnProperty.call(bitsy, 'default') ? bitsy['default'] : bitsy;
/**
@file utils
@summary miscellaneous bitsy utilities
@author Sean S. LeBlanc
*/
/*
Helper used to replace code in a script tag based on a search regex
To inject code without erasing original string, using capturing groups; e.g.
inject(/(some string)/,'injected before $1 injected after')
*/
function inject(searchRegex, replaceString) {
// find the relevant script tag
var scriptTags = document.getElementsByTagName('script');
var scriptTag;
var code;
for (var i = 0; i < scriptTags.length; ++i) {
scriptTag = scriptTags[i];
var matchesSearch = scriptTag.textContent.search(searchRegex) !== -1;
var isCurrentScript = scriptTag === document.currentScript;
if (matchesSearch && !isCurrentScript) {
code = scriptTag.textContent;
break;
}
}
// error-handling
if (!code) {
throw new Error('Couldn\'t find "' + searchRegex + '" in script tags');
}
// modify the content
code = code.replace(searchRegex, replaceString);
// replace the old script tag with a new one using our modified code
var newScriptTag = document.createElement('script');
newScriptTag.textContent = code;
scriptTag.insertAdjacentElement('afterend', newScriptTag);
scriptTag.remove();
}
/**
* Helper for getting an array with unique elements
* @param {Array} array Original array
* @return {Array} Copy of array, excluding duplicates
*/
function unique(array) {
return array.filter(function (item, idx) {
return array.indexOf(item) === idx;
});
}
/**
* Helper for parsing parameters that may be relative to another value
* e.g.
* - getRelativeNumber('1', 5) -> 1
* - getRelativeNumber('+1', 5) -> 6
* - getRelativeNumber('-1', 5) -> 4
* - getRelativeNumber('', 5) -> 5
* @param {string} value absolute or relative string to parse
* @param {number} relativeTo value to use as fallback if none is provided, and as base for relative value
* @return {number} resulting absolute or relative number
*/
function getRelativeNumber(value, relativeTo) {
var v = (value || value === 0 ? value : relativeTo);
if (typeof v === 'string' && (v.startsWith('+') || v.startsWith('-'))) {
return relativeTo + Number(v);
}
return Number(v);
}
/**
* @param {number} value number to clamp
* @param {number} min minimum
* @param {number} max maximum
* @return min if value < min, max if value > max, value otherwise
*/
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
/**
@file kitsy-script-toolkit
@summary makes it easier and cleaner to run code before and after Bitsy functions or to inject new code into Bitsy script tags
@license WTFPL (do WTF you want)
@requires Bitsy Version: 4.5, 4.6
@author @mildmojo
@description
HOW TO USE:
import {before, after, inject, addDialogTag, addDeferredDialogTag} from "./helpers/kitsy-script-toolkit";
before(targetFuncName, beforeFn);
after(targetFuncName, afterFn);
inject(searchRegex, replaceString);
addDialogTag(tagName, dialogFn);
addDeferredDialogTag(tagName, dialogFn);
For more info, see the documentation at:
/~https://github.com/seleb/bitsy-hacks/wiki/Coding-with-kitsy
*/
// Ex: inject(/(names.sprite.set\( name, id \);)/, '$1console.dir(names)');
function inject$1(searchRegex, replaceString) {
var kitsy = kitsyInit();
if (
!kitsy.queuedInjectScripts.some(function (script) {
return searchRegex.toString() === script.searchRegex.toString() && replaceString === script.replaceString;
})
) {
kitsy.queuedInjectScripts.push({
searchRegex: searchRegex,
replaceString: replaceString,
});
} else {
console.warn('Ignored duplicate inject');
}
}
// Ex: before('load_game', function run() { alert('Loading!'); });
// before('show_text', function run(text) { return text.toUpperCase(); });
// before('show_text', function run(text, done) { done(text.toUpperCase()); });
function before(targetFuncName, beforeFn) {
var kitsy = kitsyInit();
kitsy.queuedBeforeScripts[targetFuncName] = kitsy.queuedBeforeScripts[targetFuncName] || [];
kitsy.queuedBeforeScripts[targetFuncName].push(beforeFn);
}
// Ex: after('load_game', function run() { alert('Loaded!'); });
function after(targetFuncName, afterFn) {
var kitsy = kitsyInit();
kitsy.queuedAfterScripts[targetFuncName] = kitsy.queuedAfterScripts[targetFuncName] || [];
kitsy.queuedAfterScripts[targetFuncName].push(afterFn);
}
function kitsyInit() {
// return already-initialized kitsy
if (bitsy.kitsy) {
return bitsy.kitsy;
}
// Initialize kitsy
bitsy.kitsy = {
queuedInjectScripts: [],
queuedBeforeScripts: {},
queuedAfterScripts: {}
};
var oldStartFunc = bitsy.startExportedGame;
bitsy.startExportedGame = function doAllInjections() {
// Only do this once.
bitsy.startExportedGame = oldStartFunc;
// Rewrite scripts and hook everything up.
doInjects();
applyAllHooks();
// Start the game
bitsy.startExportedGame.apply(this, arguments);
};
return bitsy.kitsy;
}
function doInjects() {
bitsy.kitsy.queuedInjectScripts.forEach(function (injectScript) {
inject(injectScript.searchRegex, injectScript.replaceString);
});
_reinitEngine();
}
function applyAllHooks() {
var allHooks = unique(Object.keys(bitsy.kitsy.queuedBeforeScripts).concat(Object.keys(bitsy.kitsy.queuedAfterScripts)));
allHooks.forEach(applyHook);
}
function applyHook(functionName) {
var functionNameSegments = functionName.split('.');
var obj = bitsy;
while (functionNameSegments.length > 1) {
obj = obj[functionNameSegments.shift()];
}
var lastSegment = functionNameSegments[0];
var superFn = obj[lastSegment];
var superFnLength = superFn ? superFn.length : 0;
var functions = [];
// start with befores
functions = functions.concat(bitsy.kitsy.queuedBeforeScripts[functionName] || []);
// then original
if (superFn) {
functions.push(superFn);
}
// then afters
functions = functions.concat(bitsy.kitsy.queuedAfterScripts[functionName] || []);
// overwrite original with one which will call each in order
obj[lastSegment] = function () {
var returnVal;
var args = [].slice.call(arguments);
var i = 0;
function runBefore() {
// All outta functions? Finish
if (i === functions.length) {
return returnVal;
}
// Update args if provided.
if (arguments.length > 0) {
args = [].slice.call(arguments);
}
if (functions[i].length > superFnLength) {
// Assume funcs that accept more args than the original are
// async and accept a callback as an additional argument.
return functions[i++].apply(this, args.concat(runBefore.bind(this)));
} else {
// run synchronously
returnVal = functions[i++].apply(this, args);
if (returnVal && returnVal.length) {
args = returnVal;
}
return runBefore.apply(this, args);
}
}
return runBefore.apply(this, arguments);
};
}
function _reinitEngine() {
// recreate the script and dialog objects so that they'll be
// referencing the code with injections instead of the original
bitsy.scriptModule = new bitsy.Script();
bitsy.scriptInterpreter = bitsy.scriptModule.CreateInterpreter();
bitsy.dialogModule = new bitsy.Dialog();
bitsy.dialogRenderer = bitsy.dialogModule.CreateRenderer();
bitsy.dialogBuffer = bitsy.dialogModule.CreateBuffer();
}
// Rewrite custom functions' parentheses to curly braces for Bitsy's
// interpreter. Unescape escaped parentheticals, too.
function convertDialogTags(input, tag) {
return input
.replace(new RegExp('\\\\?\\((' + tag + '(\\s+(".*?"|.+?))?)\\\\?\\)', 'g'), function (match, group) {
if (match.substr(0, 1) === '\\') {
return '(' + group + ')'; // Rewrite \(tag "..."|...\) to (tag "..."|...)
}
return '{' + group + '}'; // Rewrite (tag "..."|...) to {tag "..."|...}
});
}
function addDialogFunction(tag, fn) {
var kitsy = kitsyInit();
kitsy.dialogFunctions = kitsy.dialogFunctions || {};
if (kitsy.dialogFunctions[tag]) {
console.warn('The dialog function "' + tag + '" already exists.');
return;
}
// Hook into game load and rewrite custom functions in game data to Bitsy format.
before('parseWorld', function (game_data) {
return [convertDialogTags(game_data, tag)];
});
kitsy.dialogFunctions[tag] = fn;
}
function injectDialogTag(tag, code) {
inject$1(
/(var functionMap = new Map\(\);[^]*?)(this.HasFunction)/m,
'$1\nfunctionMap.set("' + tag + '", ' + code + ');\n$2'
);
}
/**
* Adds a custom dialog tag which executes the provided function.
* For ease-of-use with the bitsy editor, tags can be written as
* (tagname "parameters") in addition to the standard {tagname "parameters"}
*
* Function is executed immediately when the tag is reached.
*
* @param {string} tag Name of tag
* @param {Function} fn Function to execute, with signature `function(environment, parameters, onReturn){}`
* environment: provides access to SetVariable/GetVariable (among other things, see Environment in the bitsy source for more info)
* parameters: array containing parameters as string in first element (i.e. `parameters[0]`)
* onReturn: function to call with return value (just call `onReturn(null);` at the end of your function if your tag doesn't interact with the logic system)
*/
function addDialogTag(tag, fn) {
addDialogFunction(tag, fn);
injectDialogTag(tag, 'kitsy.dialogFunctions["' + tag + '"]');
}
/**
* Adds a custom dialog tag which executes the provided function.
* For ease-of-use with the bitsy editor, tags can be written as
* (tagname "parameters") in addition to the standard {tagname "parameters"}
*
* Function is executed after the dialog box.
*
* @param {string} tag Name of tag
* @param {Function} fn Function to execute, with signature `function(environment, parameters){}`
* environment: provides access to SetVariable/GetVariable (among other things, see Environment in the bitsy source for more info)
* parameters: array containing parameters as string in first element (i.e. `parameters[0]`)
*/
function addDeferredDialogTag(tag, fn) {
addDialogFunction(tag, fn);
bitsy.kitsy.deferredDialogFunctions = bitsy.kitsy.deferredDialogFunctions || {};
var deferred = bitsy.kitsy.deferredDialogFunctions[tag] = [];
injectDialogTag(tag, 'function(e, p, o){ kitsy.deferredDialogFunctions["' + tag + '"].push({e:e,p:p}); o(null); }');
// Hook into the dialog finish event and execute the actual function
after('onExitDialog', function () {
while (deferred.length) {
var args = deferred.shift();
bitsy.kitsy.dialogFunctions[tag](args.e, args.p, args.o);
}
});
// Hook into the game reset and make sure data gets cleared
after('clearGameData', function () {
deferred.length = 0;
});
}
/**
* Adds two custom dialog tags which execute the provided function,
* one with the provided tagname executed after the dialog box,
* and one suffixed with 'Now' executed immediately when the tag is reached.
*
* i.e. helper for the (exit)/(exitNow) pattern.
*
* @param {string} tag Name of tag
* @param {Function} fn Function to execute, with signature `function(environment, parameters){}`
* environment: provides access to SetVariable/GetVariable (among other things, see Environment in the bitsy source for more info)
* parameters: array containing parameters as string in first element (i.e. `parameters[0]`)
*/
function addDualDialogTag(tag, fn) {
addDialogTag(tag + 'Now', function (environment, parameters, onReturn) {
fn(environment, parameters);
onReturn(null);
});
addDeferredDialogTag(tag, fn);
}
/**
* Helper for printing a paragraph break inside of a dialog function.
* Adds the function `AddParagraphBreak` to `DialogBuffer`
*/
inject$1(/(this\.AddLinebreak = )/, 'this.AddParagraphBreak = function() { buffer.push( [[]] ); isActive = true; };\n$1');
/**
📃
@file paragraph-break
@summary Adds paragraph breaks to the dialogue parser
@license WTFPL (do WTF you want)
@version auto
@requires Bitsy Version: 5.0, 5.1
@author Sean S. LeBlanc, David Mowatt
@description
Adds a (p) tag to the dialogue parser that forces the following text to
start on a fresh dialogue screen, eliminating the need to spend hours testing
line lengths or adding multiple line breaks that then have to be reviewed
when you make edits or change the font size.
Note: Bitsy has a built-in implementation of paragraph-break as of 7.0;
before using this, you may want to check if it fulfills your needs.
Usage: (p)
Example: I am a cat(p)and my dialogue contains multitudes
HOW TO USE:
1. Copy-paste this script into a new script tag after the Bitsy source code.
It should appear *before* any other mods that handle loading your game
data so it executes *after* them (last-in first-out).
NOTE: This uses parentheses "()" instead of curly braces "{}" around function
calls because the Bitsy editor's fancy dialog window strips unrecognized
curly-brace functions from dialog text. To keep from losing data, write
these function calls with parentheses like the examples above.
For full editor integration, you'd *probably* also need to paste this
code at the end of the editor's `bitsy.js` file. Untested.
*/
// Adds the actual dialogue tag. No deferred version is required.
addDialogTag('p', function (environment, parameters, onReturn) {
environment.GetDialogBuffer().AddParagraphBreak();
onReturn(null);
});
// End of (p) paragraph break mod
// Make them accessible at a higher scope, so Dialog functions can see them.
var textboxStyler = window.textboxStyler = {
defaultStyle: {
verticalPosition: hackOptions.verticalPosition,
horizontalPosition: hackOptions.horizontalPosition,
textboxColor: hackOptions.textboxColor,
textboxWidth: hackOptions.textboxWidth,
textboxMarginX: hackOptions.textboxMarginX,
textboxMarginY: hackOptions.textboxMarginY,
textColor: hackOptions.textColor,
textMinLines: hackOptions.textMinLines,
textMaxLines: hackOptions.textMaxLines,
textScale: hackOptions.textScale,
textSpeed: hackOptions.textSpeed,
textPaddingX: hackOptions.textPaddingX,
textPaddingY: hackOptions.textPaddingY,
borderColor: hackOptions.borderColor,
borderBGColor: hackOptions.borderBGColor,
borderMidColor: hackOptions.borderMidColor,
borderWidth: hackOptions.borderWidth,
borderHeight: hackOptions.borderHeight,
borderScale: hackOptions.borderScale,
arrowColor: hackOptions.arrowColor,
arrowBGColor: hackOptions.arrowBGColor,
arrowScale: hackOptions.arrowScale,
arrowAlign: hackOptions.arrowAlign,
arrowInsetX: hackOptions.arrowInsetX,
arrowInsetY: hackOptions.arrowInsetY,
arrowWidth: hackOptions.arrowWidth,
arrowHeight: hackOptions.arrowHeight,
arrowSprite: hackOptions.arrowSprite,
borderUL: hackOptions.borderUL,
borderU: hackOptions.borderU,
borderUR: hackOptions.borderUR,
borderL: hackOptions.borderL,
borderR: hackOptions.borderR,
borderDL: hackOptions.borderDL,
borderD: hackOptions.borderD,
borderDR: hackOptions.borderDR,
borderM: hackOptions.borderM,
},
activeStyle: {
verticalPosition: hackOptions.verticalPosition,
horizontalPosition: hackOptions.horizontalPosition,
textboxColor: hackOptions.textboxColor,
textboxWidth: hackOptions.textboxWidth,
textboxMarginX: hackOptions.textboxMarginX,
textboxMarginY: hackOptions.textboxMarginY,
textColor: hackOptions.textColor,
textMinLines: hackOptions.textMinLines,
textMaxLines: hackOptions.textMaxLines,
textScale: hackOptions.textScale,
textSpeed: hackOptions.textSpeed,
textPaddingX: hackOptions.textPaddingX,
textPaddingY: hackOptions.textPaddingY,
borderColor: hackOptions.borderColor,
borderBGColor: hackOptions.borderBGColor,
borderMidColor: hackOptions.borderMidColor,
borderWidth: hackOptions.borderWidth,
borderHeight: hackOptions.borderHeight,
borderScale: hackOptions.borderScale,
arrowColor: hackOptions.arrowColor,
arrowBGColor: hackOptions.arrowBGColor,
arrowScale: hackOptions.arrowScale,
arrowAlign: hackOptions.arrowAlign,
arrowInsetX: hackOptions.arrowInsetX,
arrowInsetY: hackOptions.arrowInsetY,
arrowWidth: hackOptions.arrowWidth,
arrowHeight: hackOptions.arrowHeight,
arrowSprite: hackOptions.arrowSprite,
borderUL: hackOptions.borderUL,
borderU: hackOptions.borderU,
borderUR: hackOptions.borderUR,
borderL: hackOptions.borderL,
borderR: hackOptions.borderR,
borderDL: hackOptions.borderDL,
borderD: hackOptions.borderD,
borderDR: hackOptions.borderDR,
borderM: hackOptions.borderM,
},
styles: hackOptions.dialogStyles,
// Sets activeStyle elements to the new style if defined, or to defaultStyle's elements if not.
style: function (newStyle) {
console.log('SETTING TEXTBOX STYLE TO: ' + newStyle);
// If newStyle doesn't exist, resets to default
if (!textboxStyler.styles[newStyle]) {
textboxStyler.resetStyle();
console.log('UNDEFINED STYLE. RESETTING TO DEFAULT.');
return;
}
textboxStyler.activeStyle = Object.assign({}, textboxStyler.defaultStyle, textboxStyler.styles[newStyle]);
textboxStyler.updateTextbox(); // Updates values used for rendering text.
},
// Applies defined style parameters to the existing style, without changing anything else.
setStyle: function (newStyle) {
console.log('APPLYING STYLE ' + newStyle + ' TO EXISTING TEXTBOX STYLE');
// If newStyle doesn't exist, does nothing.
if (!textboxStyler.styles[newStyle]) {
console.log('UNDEFINED STYLE.');
return;
}
textboxStyler.activeStyle = Object.assign({}, textboxStyler.activeStyle, textboxStyler.styles[newStyle]);
textboxStyler.updateTextbox();
},
// Manually sets a specific property of the active style.
setProperty: function (property, value) {
console.log('APPLYING STYLE PROPERTY: ' + property + ', ' + value);
if (Object.prototype.hasOwnProperty.call(textboxStyler.activeStyle, property)) {
if (!value || value === 'default') {
console.log('UNDEFINED PROPERTY. SETTING TO DEFAULT.');
textboxStyler.activeStyle[property] = Object.assign({}, textboxStyler.defaultStyle)[property];
} else {
textboxStyler.activeStyle[property] = value;
}
}
textboxStyler.updateTextbox();
},
// Resets Active Textbox Style to Default Style
resetStyle: function () {
console.log('RESETTING TO DEFAULT TEXTBOX STYLE');
textboxStyler.activeStyle = Object.assign({}, textboxStyler.defaultStyle);
textboxStyler.updateTextbox();
},
// Resets a Textbox Style Property to it's Default value
resetProperty: function (property) {
console.log('RESETTING STYLE PROPERTY TO DEFAULT: ' + property);
if (Object.prototype.hasOwnProperty.call(textboxStyler.activeStyle, property)) {
textboxStyler.activeStyle[property] = Object.assign({}, textboxStyler.defaultStyle)[property];
} else {
console.log('UNDEFINED PROPERTY.');
return;
}
textboxStyler.updateTextbox();
},
// A command to set or adjust the absolute position, width, and lines of a textbox.
textboxPosition: function (x, y, boxWidth, boxMinLines, boxMaxLines) {
var curX = textboxStyler.activeStyle.textboxMarginX;
var curY = textboxStyler.activeStyle.textboxMarginY;
var curWidth = textboxStyler.activeStyle.textboxWidth;
var curMinLines = textboxStyler.activeStyle.textboxMinLines;
var curMaxLines = textboxStyler.activeStyle.textboxMaxLines;
x = clamp(getRelativeNumber(x, curX), 0, bitsy.width - 1);
y = clamp(getRelativeNumber(y, curY), 0, bitsy.height - 1);
boxWidth = clamp(getRelativeNumber(boxWidth, curWidth), 0, bitsy.width - 1);
boxMinLines = clamp(getRelativeNumber(boxMinLines, curMinLines), 0, bitsy.height - 1);
boxMaxLines = clamp(getRelativeNumber(boxMaxLines, curMaxLines), 0, bitsy.height - 1);
textboxStyler.activeStyle.textboxWidth = boxWidth;
textboxStyler.activeStyle.textboxMinLines = boxMinLines;
textboxStyler.activeStyle.textboxMaxLines = boxMaxLines;
textboxStyler.activeStyle.textboxMarginY = y;
textboxStyler.activeStyle.textboxMarginX = x;
textboxStyler.activeStyle.verticalPosition = 'top';
textboxStyler.activeStyle.horizontalPosition = 'left';
textboxStyler.updateTextbox();
},
// First draft of position. Kept here for future versions.
textboxCornersWIP: function (x1, y1, x2, y2) {
// Trim and sanitize X and Y Positions, and set to current position if omitted.
var curX1 = textboxStyler.activeStyle.textboxMarginX;
var curX2 = curX1 + textboxStyler.activeStyle.textboxWidth;
var curY1 = textboxStyler.activeStyle.textboxMarginY;
var curY2 = curY1 + (textboxStyler.activeStyle.textPaddingY + textboxStyler.activeStyle.borderHeight - 2 + (2 * textboxStyler.activeStyle.textMinLines));
x1 = clamp(getRelativeNumber(x1, curX1), 0, bitsy.width - 1);
x2 = clamp(getRelativeNumber(x2, curX2), 0, bitsy.width - 1);
y1 = clamp(getRelativeNumber(y1, curY1), 0, bitsy.height - 1);
y2 = clamp(getRelativeNumber(y2, curY2), 0, bitsy.height - 1);
console.log('SETTING TEXTBOX CORNERS TO (' + x1 + ',' + y1 + ') and (' + x2 + ',' + y2 + ')');
var topPos = Math.min(y1, y2);
var leftPos = Math.min(x1, x2);
var bottomPos = Math.max(y1, y2);
var rightPos = Math.max(x1, x2);
var width = rightPos - leftPos;
var height = bottomPos - topPos;
var lineCount = Math.floor(height / 8);
textboxStyler.activeStyle.textboxWidth = width;
textboxStyler.activeStyle.textMinLines = Math.max(1, lineCount);
textboxStyler.activeStyle.textMaxLines = Math.max(1, lineCount);
textboxStyler.activeStyle.textPaddingY = (height - (textboxStyler.activeStyle.textMinLines * 8)) / 2;
textboxStyler.activeStyle.textboxMarginY = topPos;
textboxStyler.activeStyle.textboxMarginX = leftPos;
textboxStyler.activeStyle.verticalPosition = 'top';
textboxStyler.activeStyle.horizontalPosition = 'left';