UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 5.35 kB
var t,e;t=void 0,e=function(){var t=!1,e=function(e){t?e():(window.onYouTubeIframeAPIReady||(window.onYouTubeIframeAPIReady=function(){t=!0;for(var e=0,r=i.length;e<r;e++)i[e]();i=void 0},function(t,e){for(var i=document.getElementsByTagName("script"),r=0,s=i.length;r<s;r++)if(-1!=i[r].src.indexOf(t))return void(e&&e());var a=document.createElement("script");a.setAttribute("src",t),e&&(a.onload=e),document.head.appendChild(a)}("https://www.youtube.com/iframe_api")),i.push(e))},i=[];const r=Phaser.GameObjects.DOMElement,s=Phaser.Utils.Objects.IsPlainObject,a=Phaser.Utils.Objects.GetValue,n=Phaser.Math.Clamp,o=Phaser.Utils.String.UUID;class u extends r{constructor(t,i,r,n,u,h){s(i)?(i=a(h=i,"x",0),r=a(h,"y",0),n=a(h,"width",0),u=a(h,"height",0)):s(n)&&(n=a(h=n,"width",0),u=a(h,"height",0)),void 0===h&&(h={}),super(t,i,r),this.type="rexYoutubePlayer",this.youtubePlayer=void 0,this.videoState=void 0,this.videoId=a(h,"videoId",""),this.loop=a(h,"loop",!1),this.paddingCallbacks=[];var l=`YT${o()}`,d=document.createElement("div");d.id=l,this.setElement(d),this.resize(n,u);var y={autoplay:a(h,"autoPlay",!0)?1:0,controls:a(h,"controls",!0)?1:0,disablekb:a(h,"keyboardControl",!0)?0:1,modestbranding:a(h,"modestBranding",!1)?1:0},c=function(){var t=new YT.Player(l,{videoId:this.videoId,playerVars:y,events:{onStateChange:function(t){this.videoState=t.data,this.emit("statechange",this),this.emit(this.videoStateString,this),this.videoState===YT.PlayerState.ENDED&&this.loop&&this.youtubePlayer.playVideo()}.bind(this),onReady:function(e){this.youtubePlayer=t;for(var i=0,r=this.paddingCallbacks.length;i<r;i++)this.paddingCallbacks[i]();this.paddingCallbacks=void 0,this.emit("ready",this)}.bind(this),onError:function(t){this.lastError=t.data,this.emit("error",this,this.lastError)}.bind(this)}});this.setElement(document.getElementById(l))}.bind(this);e(c)}_runCallback(t){void 0===this.youtubePlayer?this.paddingCallbacks.push(t):t()}get videoStateString(){if(void 0===this.videoState||!YT)return"";switch(this.videoState){case-1:return"unstarted";case YT.PlayerState.ENDED:return"ended";case YT.PlayerState.PLAYING:return"playing";case YT.PlayerState.PAUSED:return"pause";case YT.PlayerState.BUFFERING:return"buffering";case YT.PlayerState.CUED:return"cued"}}load(t,e){void 0===e&&(e=!0);var i=function(){this.youtubePlayer.loadVideoById(t),e?this.youtubePlayer.playVideo():this.youtubePlayer.pauseVideo()}.bind(this);return this._runCallback(i),this}play(){var t=function(){this.youtubePlayer.playVideo()}.bind(this);return this._runCallback(t),this}get isPlaying(){return 1===this.videoState}pause(){var t=function(){this.youtubePlayer.pauseVideo()}.bind(this);return this._runCallback(t),this}get isPaused(){return 2===this.videoState}get playbackTime(){return this.youtubePlayer?this.youtubePlayer.getCurrentTime():0}set playbackTime(t){var e=function(){this.youtubePlayer.seekTo(t)}.bind(this);this._runCallback(e)}setPlaybackTime(t){return this.playbackTime=t,this}get duration(){return this.youtubePlayer?this.youtubePlayer.getDuration():0}get t(){var t=this.duration;return 0===t?0:this.playbackTime/t}set t(t){var e=function(){t=n(t,0,1),this.playbackTime=this.duration*n(t,0,1)}.bind(this);this._runCallback(e)}setT(t){return this.t=t,this}get hasEnded(){return 0===this.videoState}get volume(){return this.youtubePlayer?this.youtubePlayer.getVolume()/100:0}set volume(t){var e=function(){this.youtubePlayer.setVolume(n(100*t,0,100))}.bind(this);this._runCallback(e)}setVolume(t){return this.volume=t,this}get muted(){return!!this.youtubePlayer&&this.youtubePlayer.isMuted()}set muted(t){var e=function(){t?this.youtubePlayer.mute():this.youtubePlayer.unMute()}.bind(this);this._runCallback(e)}setMute(t){return void 0===t&&(t=!0),this.muted=t,this}setLoop(t){return void 0===t&&(t=!0),this.loop=t,this}}var h={resize:function(t,e){if(this.scene.sys.scale.autoRound&&(t=Math.floor(t),e=Math.floor(e)),this.width===t&&this.height===e)return this;var i=this.node.style;return i.width=`${t}px`,i.height=`${e}px`,this.updateSize(),this}};function l(t,e,i,r,s){var a=new u(this.scene,t,e,i,r,s);return this.scene.add.existing(a),a}Object.assign(u.prototype,h);const d=Phaser.Utils.Objects.GetAdvancedValue,y=Phaser.GameObjects.BuildGameObject;function c(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=d(t,"width",void 0),r=d(t,"height",void 0),s=new u(this.scene,0,0,i,r,t);return y(this.scene,s,t),s}var b=function(t){return null!==t&&"object"==typeof t};class v extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexYoutubePlayer",l,c)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,r="."){if(!b(t))return t;if(function(t){return null==t}(e)||""===e||Array.isArray(e)&&0===e.length)return t;if("string"==typeof e&&-1===e.indexOf(r)||"number"==typeof e)return t[e]=i,t;var s=function(t,e){return Array.isArray(t)||(t="string"!=typeof t||""===t.trim()?[]:t.split(e).filter(Boolean)),t}(e,r);if(0===s.length)return t;for(var a=t,n=s.length,o=0;o<n-1;o++){var u=s[o],h=a[u];b(h)||(a[u]={}),a=a[u]}a[s[n-1]]=i}(window,"RexPlugins.GameObjects.YoutubePlayer",u),v},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexyoutubeplayerplugin=e();