phaser3-rex-plugins
Version:
2 lines (1 loc) • 8.07 kB
JavaScript
!function(t,e){"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();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(r=i.key,o=void 0,"symbol"==typeof(o=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(r,"string"))?o:String(o)),i);}var r,o;}function i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&u(t,e);}function o(t){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},o(t)}function u(t,e){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},u(t,e)}function a(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function s(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var n,i=o(t);if(e){var r=o(this).constructor;n=Reflect.construct(i,arguments,r);}else n=i.apply(this,arguments);return function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return a(t)}(this,n)}}var l=!1,c=function(t){l?t():(window.onYouTubeIframeAPIReady||(window.onYouTubeIframeAPIReady=function(){l=!0;for(var t=0,e=y.length;t<e;t++)y[t]();y=void 0;},function(t,e){for(var n=document.getElementsByTagName("script"),i=0,r=n.length;i<r;i++)if(-1!=n[i].src.indexOf(t))return void(e&&e());var o=document.createElement("script");o.setAttribute("src",t),e&&(o.onload=e),document.head.appendChild(o);}("https://www.youtube.com/iframe_api")),y.push(t));},y=[],h=Phaser.GameObjects.DOMElement,f=Phaser.Utils.Objects.IsPlainObject,d=Phaser.Utils.Objects.GetValue,b=Phaser.Math.Clamp,p=Phaser.Utils.String.UUID,v=function(t){r(o,t);var n=s(o);function o(t,i,r,u,s,l){var y;e(this,o),f(i)?(i=d(l=i,"x",0),r=d(l,"y",0),u=d(l,"width",0),s=d(l,"height",0)):f(u)&&(u=d(l=u,"width",0),s=d(l,"height",0)),void 0===l&&(l={}),(y=n.call(this,t,i,r)).type="rexYoutubePlayer",y.youtubePlayer=void 0,y.videoState=void 0,y.videoId=d(l,"videoId",""),y.loop=d(l,"loop",!1),y.paddingCallbacks=[];var h="YT".concat(p()),b=document.createElement("div");b.id=h,y.setElement(b),y.resize(u,s);var v={autoplay:d(l,"autoPlay",!0)?1:0,controls:d(l,"controls",!0)?1:0,disablekb:d(l,"keyboardControl",!0)?0:1,modestbranding:d(l,"modestBranding",!1)?1:0},m=function(){var t=new YT.Player(h,{videoId:this.videoId,playerVars:v,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 n=0,i=this.paddingCallbacks.length;n<i;n++)this.paddingCallbacks[n]();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(h));}.bind(a(y));return c(m),y}return i(o,[{key:"_runCallback",value:function(t){void 0===this.youtubePlayer?this.paddingCallbacks.push(t):t();}},{key:"videoStateString",get:function(){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"}}},{key:"load",value:function(t,e){void 0===e&&(e=!0);var n=function(){this.youtubePlayer.loadVideoById(t),e?this.youtubePlayer.playVideo():this.youtubePlayer.pauseVideo();}.bind(this);return this._runCallback(n),this}},{key:"play",value:function(){var t=function(){this.youtubePlayer.playVideo();}.bind(this);return this._runCallback(t),this}},{key:"isPlaying",get:function(){return 1===this.videoState}},{key:"pause",value:function(){var t=function(){this.youtubePlayer.pauseVideo();}.bind(this);return this._runCallback(t),this}},{key:"isPaused",get:function(){return 2===this.videoState}},{key:"playbackTime",get:function(){return this.youtubePlayer?this.youtubePlayer.getCurrentTime():0},set:function(t){var e=function(){this.youtubePlayer.seekTo(t);}.bind(this);this._runCallback(e);}},{key:"setPlaybackTime",value:function(t){return this.playbackTime=t,this}},{key:"duration",get:function(){return this.youtubePlayer?this.youtubePlayer.getDuration():0}},{key:"t",get:function(){var t=this.duration;return 0===t?0:this.playbackTime/t},set:function(t){var e=function(){t=b(t,0,1),this.playbackTime=this.duration*b(t,0,1);}.bind(this);this._runCallback(e);}},{key:"setT",value:function(t){return this.t=t,this}},{key:"hasEnded",get:function(){return 0===this.videoState}},{key:"volume",get:function(){return this.youtubePlayer?this.youtubePlayer.getVolume()/100:0},set:function(t){var e=function(){this.youtubePlayer.setVolume(b(100*t,0,100));}.bind(this);this._runCallback(e);}},{key:"setVolume",value:function(t){return this.volume=t,this}},{key:"muted",get:function(){return !!this.youtubePlayer&&this.youtubePlayer.isMuted()},set:function(t){var e=function(){t?this.youtubePlayer.mute():this.youtubePlayer.unMute();}.bind(this);this._runCallback(e);}},{key:"setMute",value:function(t){return void 0===t&&(t=!0),this.muted=t,this}},{key:"setLoop",value:function(t){return void 0===t&&(t=!0),this.loop=t,this}}]),o}(h),m={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 n=this.node.style;return n.width="".concat(t,"px"),n.height="".concat(e,"px"),this.updateSize(),this}};function P(t,e,n,i,r){var o=new v(this.scene,t,e,n,i,r);return this.scene.add.existing(o),o}Object.assign(v.prototype,m);var g=Phaser.Utils.Objects.GetAdvancedValue,k=Phaser.GameObjects.BuildGameObject;function w(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var n=g(t,"width",void 0),i=g(t,"height",void 0),r=new v(this.scene,0,0,n,i,t);return k(this.scene,r,t),r}var S=function(t){return null==t||""===t||0===t.length},j=function(t){r(o,Phaser.Plugins.BasePlugin);var n=s(o);function o(t){var i;return e(this,o),i=n.call(this,t),t.registerGameObject("rexYoutubePlayer",P,w),i}return i(o,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}}]),o}();return function(e,n,i,r){if(void 0===r&&(r="."),"object"===t(e)){if(S(n)){if(null==i)return;"object"===t(i)&&(e=i);}else {"string"==typeof n&&(n=n.split(r));var o=n.pop(),u=function(e,n,i){var r=e;if(S(n));else {var o;"string"==typeof n&&(n=n.split("."));for(var u=0,a=n.length;u<a;u++){var s;null!=r[o=n[u]]&&"object"===t(r[o])||(s=u===a-1?void 0===i?{}:i:{},r[o]=s),r=r[o];}}return r}(e,n);u[o]=i;}}}(window,"RexPlugins.GameObjects.YoutubePlayer",v),j}));