import { GameObjectBaseType, GameObjectData, GameResource, } from '@gedit/render-engine'; import { PixiGameScene } from '../../common'; import { PixiGameObjectBase } from '../pixi-base-game-object'; import { setGameObjectBaseData, getVisible, isAndroid } from '../utils'; export interface VideoSchema { autoplay?: boolean; loop?: boolean; muted?: boolean; volume?: number; } export interface VideoPlayOptions { currentTime?: number; volume?: number; loop?: boolean; muted?: boolean; } export interface VideoGameObjectData extends GameObjectData { [GameObjectBaseType.VIDEO]: VideoSchema; } export class VideoGameObject extends PixiGameObjectBase< VideoGameObjectData, PixiGameScene, PIXI.Sprite > { static type = GameObjectBaseType.VIDEO; static needResource = true; video?: HTMLVideoElement; /* onSceneCreated(): void { if (!this.scene.config.isIDE) { this.video.play(); } } */ create(resource: GameResource): void { const texture: PIXI.Texture = this.scene.getTexture(resource.resourceKey); this.video = texture.source.resource; if (this.video && isAndroid && this.scene.videoView) { this.scene.videoView.appendChild(this.video); } this.stop(); this.rendererObject = new PIXI.Sprite(texture); this.scene.container.addChild?.(this.rendererObject); super.create.apply(this, arguments); } stop(): void { if (!this.video) { return; } this.video.currentTime = 0; this.video.pause(); } pause(): void { this.video?.pause(); } play(opts?: VideoPlayOptions): void { if (!this.video) { return; } if (opts) { Object.keys(opts).forEach( (c: 'volume' | 'currentTime' | 'loop' | 'muted') => { // @ts-ignore this.video[c] = opts[c]; } ); } this.video.play(); } dispose(): void { this.rendererObject?.destroy(); this.pause(); super.dispose(); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } update(data: VideoGameObjectData): void { if (this.rendererObject && this.video) { const { video } = data; if (!this.scene.config.isIDE) { if (!!video.autoplay) { this.play(); } else { this.stop(); } this.video.volume = video.volume ?? 1; this.video.muted = !!video.muted; this.video.loop = !!video.loop; } setGameObjectBaseData(this, data, true); this.rendererObject.visible = getVisible(data, this); } super.update(data); } getRendererObject(): PIXI.Sprite | undefined { return this.rendererObject; } }