UNPKG

phaser4-rex-plugins

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