phaser4-rex-plugins
Version:
2 lines (1 loc) • 5.31 kB
JavaScript
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,s=i.length;e<s;e++)i[e]();i=void 0},function(t,e){for(var i=document.getElementsByTagName("script"),s=0,a=i.length;s<a;s++)if(-1!=i[s].src.indexOf(t))return void(e&&e());var r=document.createElement("script");r.setAttribute("src",t),e&&(r.onload=e),document.head.appendChild(r)}("https://www.youtube.com/iframe_api")),i.push(e))},i=[];const s=Phaser.GameObjects.DOMElement,a=Phaser.Utils.Objects.IsPlainObject,r=Phaser.Utils.Objects.GetValue,n=Phaser.Math.Clamp,o=Phaser.Utils.String.UUID;class u extends s{constructor(t,i,s,n,u,h){a(i)?(i=r(h=i,"x",0),s=r(h,"y",0),n=r(h,"width",0),u=r(h,"height",0)):a(n)&&(n=r(h=n,"width",0),u=r(h,"height",0)),void 0===h&&(h={}),super(t,i,s),this.type="rexYoutubePlayer",this.youtubePlayer=void 0,this.videoState=void 0,this.videoId=r(h,"videoId",""),this.loop=r(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:r(h,"autoPlay",!0)?1:0,controls:r(h,"controls",!0)?1:0,disablekb:r(h,"keyboardControl",!0)?0:1,modestbranding:r(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,s=this.paddingCallbacks.length;i<s;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,s,a){var r=new u(this.scene,t,e,i,s,a);return this.scene.add.existing(r),r}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),s=d(t,"height",void 0),a=new u(this.scene,0,0,i,s,t);return y(this.scene,a,t),a}var b=function(t){return null==t||""===t||0===t.length};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,s){if(void 0===s&&(s="."),"object"==typeof t)if(b(e)){if(null==i)return;"object"==typeof i&&(t=i)}else{"string"==typeof e&&(e=e.split(s));var a=e.pop(),r=function(t,e,i){var s=t;if(b(e));else{var a;"string"==typeof e&&(e=e.split("."));for(var r=0,n=e.length;r<n;r++){var o;null!=s[a=e[r]]&&"object"==typeof s[a]||(o=r===n-1?void 0===i?{}:i:{},s[a]=o),s=s[a]}}return s}(t,e);r[a]=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();