forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaption.js
131 lines (119 loc) · 2.92 KB
/
Caption.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
/**
* @typedef {import('./TimedLine.js').TimedLine} TimedLine
* @typedef {import('./renderers/IRenderer.js').IRender} IRender
*/
/**
* @export
* @class Caption
* @property {TimedLine[]} lines
* @property {number} time
* @property {number} lineIndex
* @property {IRender} renderer
*/
export class Caption {
/**
* Creates an instance of Caption.
* @param {TimedLine[]} lines - Array of lines to be used for caption.
* @memberof Caption
*/
constructor(lines) {
this.lines = lines;
// Sort by end time, this ensures proper execution order of lines.
this.lines.sort(function(a, b) {
if (a.endTime < b.endTime) {
return -1;
}
if (a.endTime > b.endTime) {
return 1;
}
return 0;
});
this.reset();
}
/**
* Resets time, lineIndex and content fields.
* @private
* @memberof Caption
*/
reset() {
this.time = 0;
this.lineIndex = 0;
this.renderer = null;
}
/**
* Updates content based on time passed.
* This ~should~ be called every frame that the caption is active.
*
* @param {Number} deltaTime - Time in seconds since last frame.
* @memberof Caption
*/
update(deltaTime) {
const time = this.time + deltaTime * 1000;
if (time === this.time) {
return;
}
this.updateState(time, this.time);
this.time = time;
}
/**
* Handles calling callbacks and updating caption's current state.
* @param {Number} currentTime
* @param {Number} lastTime
* @memberof Caption
*/
updateState(currentTime, lastTime) {
if (this.isFinished()) {
return;
}
if (currentTime > this.lines[this.lineIndex].endTime) {
this.renderer.lineEnd();
}
while (currentTime > this.lines[this.lineIndex].endTime) {
if ((this.lineIndex++, this.isFinished())) {
return;
}
}
const line = this.lines[this.lineIndex];
if (currentTime >= line.startTime && lastTime <= line.startTime) {
this.renderer.lineBegin(line);
return;
}
}
/**
* Checks if caption has completed.
* @returns {Boolean}
* @memberof Caption
*/
isFinished() {
return this.lineIndex >= this.lines.length;
}
/**
* Sets time and line index of caption.
*
* @param {Number} [time=0] - Time in milliseconds.
* @memberof Caption
*/
start(time = 0, renderer = { lineBegin: () => {}, lineEnd: () => {} }) {
this.reset();
this.renderer = renderer;
this.updateTimeIndex(time);
this.updateState(this.time, this.lines[this.lineIndex].startTime - 1);
}
/**
* Updates the current time and index of the caption instance
* @param {Number} [time=0]
* @memberof Caption
*/
updateTimeIndex(time = 0) {
this.time = time;
if (this.isFinished()) {
return;
}
for (let i = this.lines.length - 1; i > -1; i--) {
if (this.lines[i].startTime <= time) {
this.lineIndex = i;
break;
}
}
}
}