UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 9.03 kB
!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).rexdragplugin=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,s=void 0,"symbol"==typeof(s=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"))?s:String(s)),i);}var r,s;}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&&o(t,e);}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function a(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 function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function u(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=s(t);if(e){var r=s(this).constructor;n=Reflect.construct(i,arguments,r);}else n=i.apply(this,arguments);return a(this,n)}}function h(){return h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var i=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(i){var r=Object.getOwnPropertyDescriptor(i,e);return r.get?r.get.call(arguments.length<3?t:n):r.value}},h.apply(this,arguments)}var c={setEventEmitter:function(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),this._privateEE=!0===t||void 0===t,this._eventEmitter=this._privateEE?new e:t,this},destroyEventEmitter:function(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter:function(){return this._eventEmitter},on:function(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once:function(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off:function(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit:function(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener:function(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener:function(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners:function(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount:function(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners:function(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames:function(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}},f=Phaser.Scene,l=function(t){return t instanceof f},v=Phaser.Game,p=function(t){return t instanceof v},y=Phaser.Utils.Objects.GetValue,d=function(){function n(t,i){e(this,n),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(y(i,"eventEmitter",!0)),this.parent&&(this.parent===this.scene?this.scene.sys.events.once("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.once("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.once("destroy",this.onParentDestroy,this));}return i(n,[{key:"shutdown",value:function(t){this.isShutdown||(this.parent&&(this.parent===this.scene?this.scene.sys.events.off("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.off("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.off("destroy",this.onParentDestroy,this)),this.destroyEventEmitter(),this.parent=void 0,this.scene=void 0,this.game=void 0,this.isShutdown=!0);}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onEnvDestroy",value:function(){this.destroy(!0);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"setParent",value:function(e){var n;return this.parent=e,this.scene=null==(n=e)||"object"!==t(n)?null:l(n)?n:n.scene&&l(n.scene)?n.scene:n.parent&&n.parent.scene&&l(n.parent.scene)?n.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:p(e)?e:p(e.game)?e.game:l(e)?e.sys.game:l(e.scene)?e.scene.sys.game:void 0}(e),this}}]),n}();Object.assign(d.prototype,c);var m=function(t,e,n,i){if(e)return !(n&&!n(t,e))&&(!!g(t,e)&&!(i&&!i(t,e)));for(var r=t.scene.input.manager,s=r.pointersTotal,o=r.pointers,a=0;a<s;a++)if(e=o[a],(!n||n(t,e))&&g(t,e)&&(!i||i(t,e)))return !0;return !1},g=function(t,e){for(var n=t.scene,i=n.input.cameras.getCamerasBelowPointer(e),r=n.input.manager,s=[t],o=0,a=i.length;o<a;o++)if(r.hitTest(e,s,i[o],E),E.length>0)return E.length=0,!0;return E.length=0,!1},E=[],b=Phaser.Utils.Objects.GetValue,_=Phaser.Math.Distance.Between,x=Phaser.Math.RotateAroundDistance,w=function(t){r(o,t);var n=u(o);function o(t,i){var r;return e(this,o),(r=n.call(this,t,{eventEmitter:!1}))._enable=void 0,t.setInteractive(b(i,"inputConfig",void 0)),r.resetFromJSON(i),r.boot(),r}return i(o,[{key:"resetFromJSON",value:function(t){return this.pointer=void 0,this.setEnable(b(t,"enable",!0)),this.setAxisMode(b(t,"axis",0)),this.setAxisRotation(b(t,"rotation",0)),this}},{key:"toJSON",value:function(){return {enable:this.enable,axis:this.axisMode,rotation:this.axisRotation}}},{key:"boot",value:function(){var t=this.parent;t.on("dragstart",this.onDragStart,this),t.on("drag",this.onDrag,this),t.on("dragend",this.onDragEnd,this);}},{key:"shutdown",value:function(t){this.isShutdown||(this.pointer=void 0,h(s(o.prototype),"shutdown",this).call(this,t));}},{key:"enable",get:function(){return this._enable},set:function(t){if(this._enable!==t)return t||this.dragend(),this._enable=t,this.scene.input.setDraggable(this.parent,t),this}},{key:"setEnable",value:function(t){return void 0===t&&(t=!0),this.enable=t,this}},{key:"toggleEnable",value:function(){return this.setEnable(!this.enable),this}},{key:"setAxisMode",value:function(t){return "string"==typeof t&&(t=D[t]),this.axisMode=t,this}},{key:"setAxisRotation",value:function(t){return this.axisRotation=t,this}},{key:"drag",value:function(){return function(t){for(var e,n=t.scene.input,i=n.manager,r=i.pointersTotal,s=i.pointers,o=0;o<r;o++)if((e=s[o]).primaryDown&&0===n.getDragState(e)&&m(t,e))return n.setDragState(e,1),n._drag[e.id]=[t],0===n.dragDistanceThreshold||0===n.dragTimeThreshold?(n.setDragState(e,3),n.processDragStartList(e)):n.setDragState(e,2),!0}(this.parent),this}},{key:"dragend",value:function(){if(this.isDragging)return this.scene.input.setDragState(this.pointer,5),this}},{key:"onDragStart",value:function(t,e,n){this.isDragging||(this.pointer=t);}},{key:"onDrag",value:function(t,e,n){if(this.pointer===t){var i=this.parent;if(0===this.axisMode)i.x=e,i.y=n;else if(0===this.axisRotation)1===this.axisMode?i.x=e:2===this.axisMode&&(i.y=n);else {var r,s={x:e,y:n};r=_(s.x,s.y,i.x,i.y),s=x(s,i.x,i.y,-this.axisRotation,r),1===this.axisMode?s.y=i.y:2===this.axisMode&&(s.x=i.x),r=_(s.x,s.y,i.x,i.y),s=x(s,i.x,i.y,this.axisRotation,r),i.x=s.x,i.y=s.y;}}}},{key:"onDragEnd",value:function(t,e,n,i){this.pointer===t&&(this.pointer=void 0);}},{key:"isDragging",get:function(){return void 0!==this.pointer}}]),o}(d),D={both:0,"h&v":0,"x&y":0,horizontal:1,h:1,x:1,vertical:2,v:2,y:2};return function(t){r(s,Phaser.Plugins.BasePlugin);var n=u(s);function s(t){return e(this,s),n.call(this,t)}return i(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new w(t,e)}}]),s}()}));