UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 7.94 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 i=function(t){return t instanceof e};const s=Phaser.Game;var r=function(t){return t instanceof s};const n=Phaser.Utils.Objects.GetValue;class h{constructor(t,e){this.setParent(t),this.isShutdown=!1,this.setEventEmitter(n(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:i(e)?e:e.scene&&i(e.scene)?e.scene:e.parent&&e.parent.scene&&i(e.parent.scene)?e.parent.scene:null,this.game=function(t){return null==t||"object"!=typeof t?null:r(t)?t:r(t.game)?t.game:i(t)?t.sys.game:i(t.scene)?t.scene.sys.game:void 0}(t),this}}Object.assign(h.prototype,t);const a=Phaser.Cameras.Scene2D.BaseCamera,o=Phaser.Geom.Rectangle;var c=function(t,e,i){return e instanceof a||(i=e,e=void 0),void 0===i?i=new o:!0===i&&(i=p),e?t.scale.getViewPort(e,i):t.scale.getViewPort(i)},p=new o,l=[Phaser.GameObjects.Image,Phaser.GameObjects.Sprite,Phaser.GameObjects.Mesh,Phaser.GameObjects.Shader,Phaser.GameObjects.Video],v=[Phaser.GameObjects.BitmapText],d=function(t,e,i){if(t&&(void 0!==e||void 0!==i))if(function(t){if(t.resize)return!0;if(!t.setSize)return!1;for(var e=0,i=l.length;e<i;e++){var s=l[e];if(s&&t instanceof s)return!1}return!0}(t)){var s,r;s=void 0===e?t.width:e/t.scaleX,r=void 0===i?t.height:i/t.scaleY,t.resize?t.resize(s,r):t.setSize(s,r)}else{var n=function(t){if(void 0===t.displayWidth)return!1;for(var e=0,i=v.length;e<i;e++){var s=v[e];if(s&&t instanceof s)return!1}return!0}(t);void 0!==e&&(n?t.displayWidth=e:t.scaleX=e/t.width),void 0!==i&&(n?t.displayHeight=i:t.scaleY=i/t.height)}},u=function(t,e,i,s){d(i,t,e)};const m=Phaser.Utils.Objects.GetValue;class g extends h{constructor(t,e){super(t,{eventEmitter:!1}),this.viewport=void 0,this.resetFromJSON(e)}shutdown(t){this.isShutdown||(this.autoAnchor(!1),this.viewport=void 0,this.onUpdateViewportCallback=void 0,this.onUpdateViewportCallbackScope=void 0,this.onResizeCallback=void 0,this.onResizeCallbackScope=void 0,super.shutdown(t))}resetFromJSON(t){void 0===t&&(t={});var e,i,s,r,n,h,a,o,c=this.parent;void 0!==t.x?(e=null,i=t.x):void 0!==t.left?(e=0,i=t.left):void 0!==t.right?(e=1,i=t.right):void 0!==t.centerX&&(e=.5,i=t.centerX),void 0!==t.y?(s=null,r=t.y):void 0!==t.top?(s=0,r=t.top):void 0!==t.bottom?(s=1,r=t.bottom):void 0!==t.centerY&&(s=.5,r=t.centerY),void 0!==i&&(i=i.replace("left","0%").replace("right","100%").replace("center","50%").split("%"),n=parseFloat(i[0])/100,h=""===i[1]?0:parseFloat(i[1])),void 0!==r&&(r=r.replace("top","0%").replace("bottom","100%").replace("center","50%").split("%"),a=parseFloat(r[0])/100,o=""===r[1]?0:parseFloat(r[1]));var p,l,v=t.width;void 0!==v&&(v=v.split("%"),p=parseFloat(v[0])/100,l=""===v[1]?0:parseFloat(v[1]));var d,g,f=t.height;void 0!==f&&(f=f.split("%"),d=parseFloat(f[0])/100,g=""===f[1]?0:parseFloat(f[1]));var E=t.aspectRatio;!0===E&&(E=c.displayWidth/c.displayHeight),this.setAlign(e,s),this.setPercentage(n,a),this.setOffset(h,o),this.setSizePercentage(p,d),this.setSizePadding(l,g),this.setAspectRatio(E);var y=m(t,"onResizeCallback",u),w=m(t,"onResizeCallbackScope");this.setResizeCallback(y,w);var b=m(t,"onUpdateViewportCallback"),_=m(t,"onUpdateViewportCallbackScope");return this.setUpdateViewportCallback(b,_),this.autoAnchor(t.enable),this}autoAnchor(t){return void 0===t&&(t=!0),t=!!t,this.autoAnchorEnable===t||(t?(this.scene.sys.scale.on("resize",this.anchor,this),this.anchor()):this.scene.sys.scale.off("resize",this.anchor,this),this.autoAnchorEnable=t),this}setAlign(t,e){return this.alignX=t,this.alignY=e,this}setPercentage(t,e){return this.percentageX=t,this.percentageY=e,this}setOffset(t,e){return this.offsetX=t,this.offsetY=e,this}setSizePercentage(t,e){return this.percentageWidth=t,this.percentageHeight=e,this}setSizePadding(t,e){return this.paddingWidth=t,this.paddingHeight=e,this}setAspectRatio(t){return this.aspectRatio=t,this}setResizeCallback(t,e){return this.onResizeCallback=t,this.onResizeCallbackScope=e,this}setUpdateViewportCallback(t,e){return this.onUpdateViewportCallback=t,this.onUpdateViewportCallbackScope=e,this}anchor(){return this.updateViewport(),this.updateSize(),this.updatePosition(),this}updateSize(){var t=this.onResizeCallback,e=this.onResizeCallbackScope,i=this.anchorWidth,s=this.anchorHeight;if((void 0!==i||void 0!==s)&&t){var r=this.parent;void 0===i&&(i=this.aspectRatio&&s?this.aspectRatio*s:r.width),void 0===s&&(s=this.aspectRatio&&i?i/this.aspectRatio:r.height),e?t.call(e,i,s,r,this):t(i,s,r,this)}}updatePosition(){var t=this.parent;return null===this.alignX?t.x=this.anchorX:void 0!==this.alignX&&(t.x=this.anchorX+t.displayWidth*(t.originX-this.alignX)),null===this.alignY?t.y=this.anchorY:void 0!==this.alignY&&(t.y=this.anchorY+t.displayHeight*(t.originY-this.alignY)),this}get anchorX(){return this.viewport.x+this.viewport.width*this.percentageX+this.offsetX}get anchorY(){return this.viewport.y+this.viewport.height*this.percentageY+this.offsetY}get anchorWidth(){if(void 0!==this.percentageWidth)return this.viewport.width*this.percentageWidth+this.paddingWidth}get anchorHeight(){if(void 0!==this.percentageHeight)return this.viewport.height*this.percentageHeight+this.paddingHeight}updateViewport(){var t=this.parent.scene.cameras.main;this.viewport=c(this.scene,t,this.viewport);var e=this.viewport,i=this.onUpdateViewportCallback,s=this.onUpdateViewportCallbackScope;i&&(s?i.call(s,e,this.parent,this):i(e,this.parent,this))}}class f extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new g(t,e)}}return f},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexanchorplugin=e();