-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalico.js
2669 lines (2282 loc) · 76.1 KB
/
calico.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
/*
CALICO
an engine for ink,
built by elliot herriman
https://twitter.com/elliotherriman
https://elliotherriman.itch.io
*/
// ===================================
// SETUP
// ===================================
// get everything ready before we start playing the story
// test if the browser is compatible with our code
try { new Function("(a = 0) => a"); }
catch (e)
{
// if not, we let the user know that they might not be able to play the game
alert ("It looks like your browser isn't fully compatible with this game. You can try to proceed, but things might not work entirely, or at all.");
}
// -----------------------------------
// default options
// -----------------------------------
// these are the options that get loaded into every story
// you can change them all with tags, or you can change them
// in your project file once you've created your game object,
// via "game.options[option] = value;"
var options =
{
// delay between removing an old passage, and showing the new passage
passagedelay: 200.0,
// delay between each line. if set to 0, all text will appear at once
linedelay: 50.0,
// how long it takes for each line to fully appear
// for fade, usually mostly visible about 50-75% of the way through
showlength: 500.0,
// how long it takes for each line to fully fade out
hidelength: 600.0,
// how long we wait to make a choice clickable after it's fully rendered
suppresschoice: 0.0,
// default file formats that we'll fall back to
// used in certain tags, like #image
defaultimageformat: ".png",
defaultaudioformat: ".mp3",
// default file locations, relative to your project folder
defaultimagelocation: "images/",
defaultaudiolocation: "music/",
// default text animation
textanimation: "fade",
// enable debug mode to print messages to the console
// set to true globally with options.debug = true;
debug: false,
};
// -----------------------------------
// print credits to the dev console
// -----------------------------------
// this is a really dressed up way of including the mandatory software licence
// stuff while also crediting the contributors of the project etc etc
credit({
emoji: "🐈",
name: "Calico",
version: "2.0.1",
description: ["An interactive fiction engine built from patchwork and ink stains.", "Want to write a game like this one? Check out the project at https://elliotherriman.itch.io/calico.", "Trans rights are human rights. 🏳️⚧✨️"],
licences: {
self: "2021 Elliot Herriman",
mit: {
"ink" : "2016 inkle Ltd.",
"inkjs" : "2017 Yannick Lohse"
}
}
});
// ===================================
// STORY CODE
// ===================================
// all the code responsible for controlling the story,
// stored in one handy little (big) class
class Story
{
// immutable list of states the story can exist in
static get states ()
{
if (!this._states)
{
this._states = {
"idle": 0,
"waiting": 1,
"active": 2,
"locked": 3,
};
}
return this._states;
}
// called whenever we create a new story object
constructor(input, innerdiv = "story", outerdiv = "container")
{
// load the ink, and then once that's done,
this.loadInk(input).then((content) =>
{
// create our options, which can be changed later
// (either in your project file, or via ink tags)
this.options = options;
// define our target HTML elements
this.innerdiv = document.getElementById(innerdiv);
this.outerdiv = document.getElementById(outerdiv);
// create the queue for processed elements that we want to render
this.queue = new Queue(this, 0);
// ensure we can refer to this object while we're inside its functions
bindFunctions(this);
// apply all our patches
Patches.apply(this, content);
// apply any external functions
this.bindExternalFunctions(content);
// mark that the engine's idle, and ready to start looping
this.state = Story.states.idle;
this.start();
});
}
// function to load our story into a javascript object
// you can pass this either either a file path, the raw text of a compiled
// ink file, or that raw text parsed into a javascript object
loadInk(input)
{
if (typeof input === "string")
{
// if we tried loading an ink file,
if (input.endsWith(".ink"))
{
// WE CAN DO THIS NOW
return new Promise((resolve, reject) =>
{
// we open up the file,
fetchText(input)
// and with that text,
.then((storyContent) =>
{
// (unless something went wrong,)
if (!storyContent)
{
// (in which case we'll throw an error about here,)
throw throwIntoVoid(console.error, "\"" + input + "\" could not be found.");
}
// anyway, now we have to search our ink for any INCLUDEs
let includeFiles = new Set(Array.from(
storyContent.matchAll(/^\s*INCLUDE (.+\.ink)\s*/gi), m => m["1"]
));
// and iterate through the ones we find,
let includes = {};
let promises = [];
includeFiles.forEach(include =>
{
// create an array of promises (to make sure things don't get too asynchronous)
promises.push(new Promise((resolve, reject) =>
{
fetchText(include).then(text =>
{
// store its contents for later
includes[include] = text;
// and mark that we're done
resolve();
});
}));
});
// then once, everything is done,
return Promise.all(promises).then(() =>
{
// return an object containing everything we need
return {storyContent: storyContent, includes: includes};
});
}).then((inkData) =>
{
// create compiler options so we can set up a filehandler
let compilerOptions = new inkjs.CompilerOptions();
// add our includes to the filehandler
compilerOptions.fileHandler = new inkjs.JsonFileHandler(inkData.includes);
// then we can use it to create the story object
this.ink = new inkjs.Compiler(inkData.storyContent, compilerOptions).Compile();
// and return the compiled ink itself in case we need that
resolve(this.ink.ToJson());
});
});
}
// if we've been handed a string, it might be the story data, or it
// might be a file name that we need to load
else
{
// so we try to load it as if it's story data
try
{
input = JSON.parse(input);
return new Promise((resolve, reject) =>
{
this.ink = new inkjs.Story(input);
resolve(input);
});
}
// and if that breaks, then it was probably a story file
catch (e)
{
// so...
return new Promise((resolve, reject) =>
{
// we open up the file,
fetchText(input)
// and with that text,
.then((storyContent) =>
{
// (unless something went wrong,
if (!storyContent)
{
// (in which case we'll throw an error about here,)
throw throwIntoVoid(console.error, "\"" + input + "\" could not be found.");
}
// then we can use it to create the story object
this.ink = new inkjs.Story(storyContent);
resolve(storyContent);
});
});
}
}
}
// otherwise, if it's already loaded as an object, we load that
else if (input.inkVersion)
{
// and continue with our code once it's loaded
return new Promise((resolve, reject) =>
{
this.ink = new inkjs.Story(input);
resolve(JSON.stringify(input));
});
}
}
// automatically detect and bind external functions in your story
// will work with any function declared at a global level, such as
// ones inside your project file
bindExternalFunctions(content)
{
// match all the external functions in the ink json,
new Set(Array.from(
content.matchAll(/\"x\(\)\":\"(\w+)/gi), m => m["1"]
)).forEach((match) =>
{
// and attempt to bind that to our story. if it doesn't work,
// ink.js will throw an error, so we don't need to handle it here
ExternalFunctions.bind(this, match);
});
}
// starts the story if loaded, or tells it to start once it's loaded
start()
{
// if we're waiting on any patches, then stop
if (this.waiting)
{
return;
}
// if we've marked our story ready,
else if (typeof this.state !== "undefined")
{
// then we notify that the story's ready
notify("story ready", {story: this}, this.outerdiv);
// and start the story
this.continue();
}
// otherwise,
else
{
// we wait until the story's done,
this.outerdiv.addEventListener("story ready", () =>
{
// and then try this again
this.start();
}, {once: true});
}
}
// main story loop
// processes all text, choices, and tags until we reach a point
// where the story expects user input, or has run out of content
continue()
{
// we stop here if the story is already looping
if (this.state != Story.states.idle)
{
return;
}
// otherwise, mark that the story object is now active
this.state = Story.states.active;
this.ink.lastKnownPathString = this.ink.state.currentPathString;
// notify about it
notify("passage start", {story: this}, this.outerdiv);
// process text lines until we reach a choice, or the end of the story
while (this.ink.canContinue)
{
// move the story forwards
this.ink.Continue();
// create the line to store data in
// since it's an object, we can pass it around via events, and
// changing its value there will change it here too. very handy
// for patching in new code
var line = {
text: "",
tags: { before: [], after: [] }
};
// we're going to loop through each item in the output stream,
// which is a list of all the text fragments and tags in this
// line). we mark tags as coming before or after text, style
// each segment of the line, and finally paste it all together
let currentText = "";
let currentTags = [];
// define item iterator here so we don't
// have to recreate it every time
let item;
let stream = this.ink.state.outputStream;
// start by going through each item in the stream (backwards)
for (var i = 0; i < stream.length; i++)
{
// store the item for easy reference
item = stream[i];
if (item._commandType == 24)
{
let result = this.searchStreamForTag(stream, i);
i = result.i;
let currentTag = result.currentTag;
if (currentTag == "unstyled")
{
line.text += currentText;
currentText = "";
}
else if (!currentText.trim())
{
// if so, we sort away our tags
line.tags.before.push(currentTag);
}
else
{
line.tags.after.push(currentTag);
currentTags.push(currentTag);
}
}
// if it's text,
if (item.value)
{
if (currentText.length && currentTags.length)
{
currentText = Parser.process(this, currentText, currentTags);
currentTags = [];
}
currentText += item.value;
}
}
line.text += currentText;
// notify that we've built a line
notify("passage line", {story: this, line: line}, this.outerdiv);
// process all tags that come before any story text
line.tags.before.forEach((tag) => { Tags.process(this, tag, false); });
// if that line has some text,
if (line.text && line.text.trim())
{
// create a paragraph element to display later
var paragraphElement = document.createElement('p');
paragraphElement.classList.add("text");
// set the element's content
paragraphElement.innerHTML = "<span>" + line.text + "</span>";
// and push that element to queue so we can show it later
this.queue.push(paragraphElement);
// notify that we built a line element
notify("passage line element", {story: this, element: paragraphElement, line: line}, this.outerdiv);
}
// process all tags that come after any story text
line.tags.after.forEach((tag) => { Tags.process(this, tag, true); });
}
// process all choices available, adding them to the queue
this.ink.currentChoices.forEach((choice) =>
{
// notify that we're starting to build a choice
notify("passage choice", {story: this, choice: choice}, this.outerdiv);
// create the choice as a paragraph element to show later
var choiceParagraphElement = document.createElement('p');
// add "choice" as a class so we can style it in the css
choiceParagraphElement.classList.add("choice");
// create the link,
var choiceAnchorEl = document.createElement("a");
// and set its text, applying tags and patterns
// BIG IMPORTANT THING TO NOTE: we can't edit the
// innertext or innerhtml of either of these elements
// once any of the event listeners are set, otherwise
// it'll remove those event listeners. you can add to
// them with +=, but no assigning to them with =
choiceAnchorEl.innerHTML = Parser.process(this, choice.text, choice.tags);
// hide the weird click glove cursor until the choice is ready
choiceAnchorEl.style.cursor = "default";
// prevent it from being dragged
choiceAnchorEl.setAttribute('draggable', false);
// and then add the link element to the paragraph element
choiceParagraphElement.appendChild(choiceAnchorEl);
// queue up the choice to show later
this.queue.push(choiceParagraphElement);
// tell people that we've built a choice
notify("passage choice element", {story: this, choice: choice, element: choiceParagraphElement}, this.outerdiv);
// ensure the choice link doesn't work as an actual link to anything
choiceAnchorEl.addEventListener("click", function(event)
{
// this just ignores the default action for the link
// which means... linking to something, it hink/
event.preventDefault();
});
// set up function to continue story when the player clicks a choice
this.queue.onRendered(() =>
{
// wait for a moment before letting the player click the choice
// done to prevent players from skipping past text accidentally
// think it comes from telltale?
setTimeout(() =>
{
// change the mouse cursor to the click-y hand
// when you hover over the link, so the player
// knows it's active (and also a link)
choiceAnchorEl.style.cursor = "pointer";
// function that fires when you click the link,
// telling the story where to go
choiceAnchorEl.onclick = () => { this.choose(choice, choiceAnchorEl); };
// how long we're waiting for (this is the standard pattern
// for timeouts, i won't comment them in future)
}, this.options["suppresschoice"]);
});
});
// now that everything's ready, we render it all!
this.queue.render();
// and we mark that the story is waiting for input
this.state = Story.states.waiting;
}
searchStreamForTag(stream, i)
{
let currentTag = "";
let item;
for (i + 1; i < stream.length; i++)
{
item = stream[i];
if (item._commandType == 25)
{
break;
}
if (item.value)
{
currentTag += item.value;
}
}
return {currentTag: currentTag, i: i};
}
// called once the player chooses a choice, to process that choice
// and clean everything up for the next loop
choose(choice, choiceAnchorEl)
{
if (this.state != Story.states.waiting) return;
this.state = Story.states.locked;
choiceAnchorEl.onclick = null;
choiceAnchorEl.classList.add("chosen");
notify("passage end", {story: this, choice: choice}, this.outerdiv);
this.setHeight();
// tell the story which choice was picked
this.ink.ChooseChoiceIndex(choice.index);
var el = this.innerdiv.querySelector(".choice");
if (!el)
{
// reset our queue
this.queue.reset();
this.state = Story.states.idle;
// and start the loop over
this.continue();
}
else
{
Element.addCallback(el, "onRemove", () =>
{
// reset our queue
this.queue.reset();
this.state = Story.states.idle;
// and start the loop over
this.continue();
});
}
// check ahead to see if we're about to clear, and do so now rather than just
// removing all our elements-- saves an awkward two stage clearing
this.lookAheadAndClear(choice);
}
// glances at the content ahead, and checks if we're about to clear
// if we are, then we'll clear right now, instead of clearing the
// choices and then having to awkwardly wait and then clear the rest
lookAheadAndClear(choice)
{
let stream = this.ink.PointerAtPath(choice.targetPath).container._content;
let item;
for (var i = 0; i < stream.length; i++)
{
item = stream[i];
if (item._commandType == 24)
{
let result = this.searchStreamForTag(stream, i);
if (result.currentTag == "clear")
{
this.clear();
this.queue.reset(0);
return;
}
i = result.i;
}
}
this.removeElements(":scope > .choice");
}
// clears all elements from the story container, and resets the queue
clear(queueDelay = this.options.hidelength, howMany = undefined)
{
// if we don't have anything to clear, we'll just quietly cancel
if (!this.innerdiv.firstElementChild || !this.innerdiv.childNodes.length || this.innerdiv.firstElementChild.state == Element.states.clearing)
{
return;
}
// notify that we're starting a clear
notify("story clearing", {story: this, delay: queueDelay, howMany: howMany}, this.outerdiv);
Element.addCallback(this.innerdiv.firstElementChild, "onRemove", () =>
{
this.scrollUp(false);
});
// remove all elements in story container
this.removeElements(":scope > p");
// then as long as it's not a partial clear,
// (since we only want to clear the queue at the start of a new loop)
if (!howMany)
{
// reset the queue, and set its delay
// (but not if there aren't any elements to clear)
this.queue.reset(this.innerdiv.childNodes.length ? queueDelay : 0, howMany);
}
}
// remove all elements that match with the selector
removeElements(selector, howMany = undefined)
{
// find all matching elements in HTML
var allElements = this.innerdiv.querySelectorAll(selector);
// if howMany doesn't have a value, then we use a fallback value
howMany = howMany || allElements.length;
// loop through the elements
for(var i = howMany - 1; i >= 0; i--)
{
// grab each element,
var el = allElements[i];
// make sure it exists still?
if (!el) continue;
// and remove it
Element.hide(el);
}
return allElements.length;
}
// restart the story without reloading the page
restart()
{
// notify we're restarting
notify("story restarting", {story: this}, this.outerdiv);
// clear everything
this.clear(this.options["hidelength"]);
// reset the story
this.ink.ResetState();
// and start it from the beginning
this.continue();
}
jumpTo(knot)
{
this.clear();
this.ink.ChoosePathString(knot);
this.state = Story.states.idle;
this.continue();
}
// resets the page's scroll position after an optional delay
scrollUp(smooth = true)
{
this.outerdiv.scrollTo({
top: 0,
left: 0,
behavior: (smooth ? "smooth" : "auto")
});
}
// necessary to stop scroll from jumping once the player clicks a choice,
// or removes any elements. height will automatically update after the
// window is resized, or after a new element is added (if there aren't)
// any other elements to add afterwards)
setHeight(el)
{
// get current height
let oldHeight = this.innerdiv.style.height || 0;
// get new height from the bottom of the last story element
// (if there isn't any, then the height is set to the window's)
let bottomElement = el || this.innerdiv.lastElementChild;
// if there are no elements, then we'll just give up now
if (!bottomElement) return;
// calculate the story's height based on the bottom edge
// of the last element in the story div
let newHeight = bottomElement ?
bottomElement.offsetTop + bottomElement.offsetHeight + parseFloat(window.getComputedStyle(bottomElement).marginBottom) : window.innerHeight;
// if new height is less than the window's height,
// then we'll just set it to the window's height
newHeight = Math.max(newHeight, this.outerdiv.scrollTop + window.innerHeight);
// and as long as the height's actually changed,
if (newHeight != this.innerdiv.style.scrollHeight)
{
// we finally update the story container's height
this.innerdiv.style.height = newHeight + "px";
// and notify about this update
notify("story setheight", {story: this, old: oldHeight, new: newHeight}, this.outerdiv);
}
}
// return the story's current state
getState()
{
return this.ink.state;
}
// forces the story to wait before starting, until after every patch
// submitted here tells the story that it's ready. necessary since
// javascript won't wait on some code (especially code that imports or
// downloads files) before continuing-- it'll just keep going, and
// let the files load in their own time. this way, we force the story
// to wait until we've done everything we need to, and *then* we launch it
waitForPatch(name)
{
// create an array if we don't already have one
this.waiting = this.waiting || [];
// and if our patch's name isn't in there yet,
if (this.waiting.indexOf(name) === -1)
{
// we add it
this.waiting.push(name);
}
}
// tell the story that this patch is done, allowing it to start
// if there aren't any other pending patches
patchReady(name)
{
// create an array if we don't already have one
this.waiting = this.waiting || [];
// cancel if our patch's name isn't in there
if (this.waiting.indexOf(name) === -1)
{
return this.start();
}
// remove the patch from the array
removeFromArray(name, this.waiting);
// and if there's nothing left in that list
if (!this.waiting.length)
{
// we null it out so the browser doesn't
// need to keep it in memory,
this.waiting = null;
// and then we try to start our story
this.start();
}
}
}
// ================================================
// QUEUE
// ================================================
// class responsible for storing and rendering all the ink text
// you push your text and choices into here, adding custom classes,
// delays, callbacks, etc. and then you call render() to show it all
class Queue
{
// called when you create an object from this class
constructor(story, initialDelay)
{
// we mark which story this queue belongs to, for convenience
this.story = story;
// do this to set initial values
this.reset(initialDelay);
// determines whether or not the story should keep showing text until
// it finds a choice, or if it should wait for input after every line
// useful for visual novel type games
this.lineByLine = false;
// and then we tell all the functions in this class that "this"
// should apply to the queue object, and not the queue class
bindFunctions(this);
}
// empties out the queue
reset(initialDelay)
{
// clear queue of elements
this.clear();
// clear delay, and set it to a new value
this.setDelay(initialDelay);
// mark down how many elements are on the page right now,
// so we can do a partial clear later
this.pageElements = this.story.innerdiv.childNodes.length;
}
// remove old elements from the queue
clear()
{
notify("queue clear", {queue: this, old: this.contents}, this.story.outerdiv);
// by declaring a new empty array
this.contents = [];
}
// set the initial delay for when we start rendering each line
setDelay(delay = this.story.options["passagedelay"])
{
// if the delay isn't a number,
if (isNaN(delay))
{
// then stop here
return;
}
// notify about it
notify("queue setdelay", {new: delay, old: this.delay, queue: this}, this.story.outerdiv);
// otherwise, update the delay
this.delay = parseFloat(delay);
this.initialDelay = parseFloat(delay);
}
// determine whether we should wait for input after every line, or keep
// rendering until we find a choice
setLineByLine(bool)
{
// notify about this
notify("queue setlinebyline", {value: bool, queue: this}, this.story.outerdiv);
// set value in queue for later elements
this.lineByLine = bool;
// then try to update the latest element,
// so the game won't render past that
this.setProperty("continueAfter", !bool);
}
// helper function to calculate the exact delay between two elements
// if you include the first element, then it will include the queue's
// initial delay-- otherwise, it'll just be the length from the start
// of element one to the start of element two
sumDelay(start = 0, end = this.contents.length - 1)
{
// get the initial delay
var sum = (start == 0 ? this.initialDelay : 0);
// if the start is less than the end, aaaand
// if the last element exists,
if (start < end && this.contents[end])
{
// then we loop through them
for (var i = start; i < end; i++)
{
// and increment the sum
sum += this.contents[i].delay;
}
// aaand return the total
return sum;
}
}
// add a new element to the queue, so we can render it later
// returns index of element in queue in case you need to reference it later?
// not sure why you would, but, y'know, theoretically
push(el)
{
// mark that this element belongs to its queue, in case of multiple
// queues which... i haven't explicitly designed for, but i've avoided
// designing *against*, in case people want to use multiple queues?
el.parent = this;
// mark down the index,
el.index = this.contents.length;
// set the current line delay now in case that value changes later
el.delay = (el.index ? this.story.options["linedelay"] : 0);
// note if we should we render the next line automatically
el.continueAfter = !this.lineByLine;
// add callbacks container
el.callbacks = {};
// also mark the previous and next elements for convenience
if (this.contents.length)
{
this.contents[this.contents.length - 1].next = el;
el.previous = this.contents[this.contents.length - 1];
}
// and finally mark that this element is queued
el.state = Element.states.queued;
// apply any text animation we have
TextAnimation.apply(this.story, el, this.story.options.textanimation);
// add element to the queue for later rendering
this.contents.push(el);
// notify that we've pushed the element
notify("queue push", {element: el, queue: this}, this.story.outerdiv);
}
// adds a new class to the specified element
// index defaults to most recent element in queue
// useful for CSS styling
addClass(className, index = this.contents.length - 1)
{
// make sure the element and class exists
if (this.contents[index] && className)
{
// notify,
notify("element addclass", {element: this.contents[index], class: className, queue: this}, this.story.outerdiv);
// and apply the class
this.contents[index].classList.add(...className);
}
}
// adds a delay before showing the specified element
// index defaults to most recent element in queue
addDelay(delay, index = this.contents.length - 1)
{
// ensure we're actually using a number here
if (isNaN(delay))
{
// if not, cancel by returning
return;
}
// make sure there are elements in the queue
else if (this.contents.length)
{
// if the specified element exists,
if (this.contents[index])
{
// notify,
notify("element adddelay", {element: this.contents[index], delay: delay, story: this.story, queue: this});
// and update the delay
this.contents[index].delay += delay;
}
}
// if not,
else
{
// update our initial queue delay
this.setDelay(this.delay + delay);
}
}
setProperty(property, value, index = this.contents.length - 1)
{
// convert the property to a string, because otherwise the logic
// gets really annoyingly verbose otherwise.
// if you don't know what any of that means then... yeah honestly
// that's fine, don't worry about it. just take pride in the fact
// you're probably much more interesting than me
property = property.toString();
// check that we can actually set that property
if (property && typeof value !== undefined && this.contents[index])
{
// notify,
notify("element setproperty", {element: this.contents[index], story: this.story, property: property, new: value, old: this.contents[index][property], queue: this}, this.story.outerdiv);
// and update
this.contents[index][property] = value;
}
}
// fires a function once the specified element is added to the story div
// index defaults to most recent element in queue
onAdded(callback, index = this.contents.length - 1)
{
// make sure element exists
if (this.contents[index] && this.contents[index].callbacks)
{
// set the callback
Element.addCallback(this.contents[index], "onAdded", callback.bind(this, index));