-
Notifications
You must be signed in to change notification settings - Fork 964
/
Copy pathvideoruntimeobject.ts
449 lines (403 loc) · 12 KB
/
videoruntimeobject.ts
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
namespace gdjs {
/** The initial properties for {@link gdjs.VideoRuntimeObject} */
export type VideoObjectDataType = {
/** The base parameters of the video */
content: {
/** The opacity of the video */
opacity: number;
/** Does the video loops itself? */
loop: boolean;
/** The volume of the video */
volume: number;
/** Name of the resource corresponding to the video */
videoResource: string;
};
};
export type VideoObjectData = ObjectData & VideoObjectDataType;
export type VideoNetworkSyncDataType = {
op: float;
wid: float;
hei: float;
// We don't sync volume, as it's probably a user setting?
pla: boolean;
loop: boolean;
ct: float;
ps: number;
};
export type VideoNetworkSyncData = ObjectNetworkSyncData &
VideoNetworkSyncDataType;
/**
* An object displaying a video on screen.
*
* For the same video resource, only one video is being created in memory (
* as a HTMLVideoElement). This means that two objects displaying the same
* video will have the same state for this video (paused/playing, current time,
* volume, etc...).
*/
export class VideoRuntimeObject
extends gdjs.RuntimeObject
implements gdjs.OpacityHandler
{
_opacity: float;
_loop: boolean;
_volume: float;
_videoResource: string;
// Use a boolean to track if the video was paused because we
// navigated to another scene, and so should resume if we're back.
_pausedAsScenePaused: boolean = false;
_renderer: gdjs.VideoRuntimeObjectRenderer;
_playbackSpeed: any;
/**
* @param instanceContainer The scene the object belongs to.
* @param videoObjectData The data defining the object
*/
constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
videoObjectData: VideoObjectData
) {
super(instanceContainer, videoObjectData);
this._opacity = videoObjectData.content.opacity;
this._loop = videoObjectData.content.loop;
this._volume = videoObjectData.content.volume;
this._videoResource = videoObjectData.content.videoResource;
this._renderer = new gdjs.VideoRuntimeObjectRenderer(
this,
instanceContainer
);
// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
this.onCreated();
}
getRendererObject() {
return this._renderer.getRendererObject();
}
updateFromObjectData(
oldObjectData: VideoObjectData,
newObjectData: VideoObjectData
): boolean {
if (oldObjectData.content.opacity !== newObjectData.content.opacity) {
this.setOpacity(newObjectData.content.opacity);
}
if (oldObjectData.content.loop !== newObjectData.content.loop) {
this.setLoop(newObjectData.content.loop);
}
if (oldObjectData.content.volume !== newObjectData.content.volume) {
this.setVolume(newObjectData.content.volume);
}
if (
oldObjectData.content.videoResource !==
newObjectData.content.videoResource
) {
return false;
}
return true;
}
getNetworkSyncData(): VideoNetworkSyncData {
return {
...super.getNetworkSyncData(),
op: this._opacity,
wid: this.getWidth(),
hei: this.getHeight(),
pla: this.isPlayed(),
loop: this.isLooped(),
ct: this.getCurrentTime(),
ps: this.getPlaybackSpeed(),
};
}
updateFromNetworkSyncData(
syncData: VideoNetworkSyncData,
options?: { skipMultiplayerInstructions: boolean }
): void {
super.updateFromNetworkSyncData(syncData, options);
if (this._opacity !== undefined && this._opacity && syncData.op) {
this.setOpacity(syncData.op);
}
if (this.getWidth() !== undefined && this.getWidth() !== syncData.wid) {
this.setWidth(syncData.wid);
}
if (this.getHeight() !== undefined && this.getHeight() !== syncData.hei) {
this.setHeight(syncData.hei);
}
if (syncData.pla !== undefined && this.isPlayed() !== syncData.pla) {
syncData.pla ? this.play() : this.pause();
}
if (syncData.loop !== undefined && this.isLooped() !== syncData.loop) {
this.setLoop(syncData.loop);
}
// We don't update the current time too regularly, only if it's off by a lot.
if (
syncData.ct !== undefined &&
Math.abs(this.getCurrentTime() - syncData.ct) > 3 // More than 3 seconds off
) {
this.setCurrentTime(syncData.ct);
}
if (
syncData.ps !== undefined &&
this.getPlaybackSpeed() !== syncData.ps
) {
this.setPlaybackSpeed(syncData.ps);
}
}
/**
* Initialize the extra parameters that could be set for an instance.
* @param initialInstanceData The initial instance data
*/
extraInitializationFromInitialInstance(initialInstanceData: InstanceData) {
if (initialInstanceData.customSize) {
this.setWidth(initialInstanceData.width);
this.setHeight(initialInstanceData.height);
}
if (initialInstanceData.opacity !== undefined) {
this.setOpacity(initialInstanceData.opacity);
}
}
onDestroyed(): void {
super.onDestroyed();
this._renderer.onDestroy();
}
update(instanceContainer: gdjs.RuntimeInstanceContainer): void {
this._renderer.ensureUpToDate();
}
/**
* Set object position on X axis.
* @param x The new position X of the object.
*/
setX(x: float): void {
super.setX(x);
this._renderer.updatePosition();
}
/**
* Set object position on Y axis.
* @param y The new position Y of the object.
*/
setY(y: float): void {
super.setY(y);
this._renderer.updatePosition();
}
/**
* Set the angle of the object.
* @param angle The new angle of the object.
*/
setAngle(angle: float): void {
super.setAngle(angle);
this._renderer.updateAngle();
}
/**
* Set object opacity.
* @param opacity The new opacity of the object (0-255).
*/
setOpacity(opacity: float): void {
this._opacity = opacity;
this._renderer.updateOpacity();
}
/**
* Get object opacity.
* @returns The current opacity
*/
getOpacity(): number {
return this._opacity;
}
/**
* Set the width of the video.
* @param width The new width in pixels.
*/
setWidth(width: float): void {
if (this._renderer.getWidth() === width) return;
this._renderer.setWidth(width);
this.invalidateHitboxes();
}
/**
* Set the height of the video.
* @param height The new height in pixels.
*/
setHeight(height: float): void {
if (this._renderer.getHeight() === height) return;
this._renderer.setHeight(height);
this.invalidateHitboxes();
}
/**
* Get the width of the video object.
* @returns The current width of the object
*/
getWidth(): float {
return this._renderer.getWidth();
}
/**
* Get the height of the video object.
* @returns The current height of the object
*/
getHeight(): float {
return this._renderer.getHeight();
}
/**
* Play the video.
*/
play(): void {
this._renderer.play();
}
/**
* Pause the video.
*/
pause(): void {
this._renderer.pause();
}
/**
* Set the state looped of the video.
* @param enable true to loop the video
*/
setLoop(enable: boolean): void {
this._renderer.setLoop(enable);
}
/**
* Set the state muted of the video.
* @param enable The new state.
*/
mute(enable: boolean) {
this._renderer.setMute(enable);
}
/**
* Return the state muted of video object.
* @returns Is the video muted?
*/
isMuted(): boolean {
return this._renderer.isMuted();
}
/**
* Set the volume of the video object.
* @param volume The new volume, between 0 and 100.
*/
setVolume(volume: number): void {
this._volume =
gdjs.evtTools.common.clamp(
gdjs.evtTools.common.normalize(volume, 0, 100),
0,
1
) * 100;
this._renderer.updateVolume();
}
/**
* Get the volume of the video object.
* @returns The current video's volume, between 0 and 100.
*/
getVolume(): number {
return (
gdjs.evtTools.common.normalize(this._renderer.getVolume(), 0, 1) * 100
);
}
/**
* Check if the video is being played.
* @returns Is the video being played?
*/
isPlayed(): boolean {
return this._renderer.isPlayed();
}
/**
* Check if the video is paused.
* @returns Is the video being paused?
*/
isPaused(): boolean {
return !this._renderer.isPlayed();
}
/**
* Check if the video is looping.
* @returns Is the video looping?
*/
isLooped(): boolean {
return this._renderer.isLooped();
}
/**
* Return the total time of the video.
* @returns The duration of the video
*/
getDuration(): number {
return this._renderer.getDuration();
}
/**
* Check if the video has ended.
* @returns Has the video ended?
*/
isEnded(): boolean {
return this._renderer.isEnded();
}
/**
* Set the new time of the video object.
* @param time The new time.
*/
setCurrentTime(time: float): void {
this._renderer.setCurrentTime(time);
}
/**
* Get the current time of the video object.
* @returns The current time of the video
*/
getCurrentTime(): float {
return this._renderer.getCurrentTime();
}
/**
* Set the new playback speed of the video object.
* @param playbackSpeed The new playback speed.
*/
setPlaybackSpeed(playbackSpeed: number): void {
this._playbackSpeed = gdjs.evtTools.common.clamp(playbackSpeed, 0.5, 2);
this._renderer.setPlaybackSpeed(this._playbackSpeed);
}
/**
* Get the playback speed of the video object.
* @returns The current playback speed of the video.
*/
getPlaybackSpeed(): number {
return this._renderer.getPlaybackSpeed();
}
}
gdjs.registerObject('Video::VideoObject', gdjs.VideoRuntimeObject);
/**
* When a scene is unloaded, pause any video being run.
* TODO: Investigate how to dispose the video source?
*/
gdjs.registerRuntimeSceneUnloadedCallback(function (runtimeScene) {
// Manually find all the gdjs.VideoRuntimeObject living on the scene,
// and pause them.
const instances = runtimeScene.getAdhocListOfAllInstances();
for (let i = 0; i < instances.length; ++i) {
const obj = instances[i];
if (obj instanceof gdjs.VideoRuntimeObject) {
if (obj.isPlayed()) {
obj.pause();
}
}
}
});
/**
* When a scene is paused, pause any video being run.
*/
gdjs.registerRuntimeScenePausedCallback(function (runtimeScene) {
// Manually find all the gdjs.VideoRuntimeObject living on the scene,
// and pause them.
const instances = runtimeScene.getAdhocListOfAllInstances();
for (let i = 0; i < instances.length; ++i) {
const obj = instances[i];
if (obj instanceof gdjs.VideoRuntimeObject) {
if (obj.isPlayed()) {
obj.pause();
obj._pausedAsScenePaused = true;
}
}
}
});
// Flag it to be started again when scene is resumed.
/**
* When a scene is resumed, resume any video previously paused.
*/
gdjs.registerRuntimeSceneResumedCallback(function (runtimeScene) {
// Manually find all the gdjs.VideoRuntimeObject living on the scene,
// and play them if they have been previously paused.
const instances = runtimeScene.getAdhocListOfAllInstances();
for (let i = 0; i < instances.length; ++i) {
const obj = instances[i];
if (obj instanceof gdjs.VideoRuntimeObject) {
if (obj._pausedAsScenePaused) {
obj.play();
}
}
}
});
}