UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 9.54 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).rexninepatch=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 r(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(n=i.key,o=void 0,"symbol"==typeof(o=function(t,e){if("object"!=typeof t||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.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)}(n,"string"))?o:String(o)),i);}var n,o;}function i(t,e,i){return e&&r(t.prototype,e),i&&r(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function n(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&&s(t,e);}function o(t){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},o(t)}function s(t,e){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},s(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 c(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 r,i=o(t);if(e){var n=o(this).constructor;r=Reflect.construct(i,arguments,n);}else r=i.apply(this,arguments);return a(this,r)}}function u(){return u="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,r){var i=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=o(t)););return t}(t,e);if(i){var n=Object.getOwnPropertyDescriptor(i,e);return n.get?n.get.call(arguments.length<3?t:r):n.value}},u.apply(this,arguments)}var h=function(t,e,r){return "__BASE"===r?"".concat(t,",").concat(e):"".concat(r,"_").concat(t,",").concat(e)},l=function e(r){var i,n,o;if(null==r||"object"!==t(r))return r;if(i=Array.isArray(r)?[]:{},function(e){if("object"!==t(e)||e.nodeType||e===e.window)return !1;try{if(e.constructor&&!{}.hasOwnProperty.call(e.constructor.prototype,"isPrototypeOf"))return !1}catch(t){return !1}return !0}(r))for(o in r)n=r[o],i[o]=e(n);else i=r;return i},f=Phaser.Utils.Objects.IsPlainObject,d=Phaser.Utils.Objects.GetValue,m=function(t){return "string"==typeof t&&(t=v[t]),t},v={scale:0,repeat:1},y=function(t,e){return 0===t||t===this.columns.count-1||0===e||e===this.rows.count-1},p=function(){},b={_beginDraw:p,_drawImage:p,_drawTileSprite:p,_endDraw:p,setGetFrameNameCallback:function(t){return void 0===t&&(t=h),this.getFrameNameCallback=t,this},setBaseTexture:function(e,r,i,n){Array.isArray(r)&&(n=i,i=r,r=void 0),null==r&&(r="__BASE"),"number"==typeof i&&arguments.length>=6?(i=[arguments[2],void 0,arguments[3]],n=[arguments[4],void 0,arguments[5]]):(i=l(i),n=l(n)),this.textureKey=e,this.baseFrameName=r,this.columns.data=i,this.columns.count=i?i.length:0,this.columns.stretch=0,this.columns.minWidth=0,this.columns.scale=1,this.rows.data=n,this.rows.count=n?n.length:0,this.rows.stretch=0,this.rows.minHeight=0,this.rows.scale=1;var o=this.scene.sys.textures.get(e);if(!o)return this.clear(),this;if(!i||!n)return this.clear(),this;for(var s=o.get(r),a=s.width,c=0,u=0,h=i.length;u<h;u++)void 0===i[u]?c++:"number"==typeof i[u]?a-=i[u]:a-=i[u].width;var f=a/c,d=s.height,m=0;for(u=0,h=n.length;u<h;u++)void 0===n[u]?m++:"number"==typeof n[u]?d-=n[u]:d-=n[u].width;for(var v,y,p,b,g,w=d/m,x=0,P=0,S=0,O=n.length;S<O;S++){void 0===n[S]&&(n[S]=w),"number"==typeof n[S]&&(n[S]={height:n[S],stretch:S%2}),p=(v=n[S]).height,this.rows.stretch+=0|v.stretch,this.rows.minHeight+=v.stretch>0?0:p,x=0;u=0;for(var j=i.length;u<j;u++){if(void 0===i[u]&&(i[u]=f),"number"==typeof i[u]&&(i[u]={width:i[u],stretch:u%2}),b=(y=i[u]).width,0===S&&(this.columns.stretch+=0|y.stretch,this.columns.minWidth+=y.stretch>0?0:b),b>=1&&p>=1){var _=t(g=this.getFrameNameCallback(u,S,r));"string"!==_&&"number"!==_||o.add(g,0,x+s.cutX,P+s.cutY,b,p);}x+=b;}P+=p;}return this.updateTexture(),this},updateTexture:function(){if(this.clear(),void 0===this.textureKey)return this;var t=this.scene.sys.textures.get(this.textureKey);if(!t)return this;var e,r,i,n,o,s,a,c=this.columns.minWidth*this.maxFixedPartScaleX,u=this.rows.minHeight*this.maxFixedPartScaleY,h=this.width-c,l=this.height-u,f=h>=0?this.maxFixedPartScaleX:this.width/c,d=l>=0?this.maxFixedPartScaleY:this.height/u;if(this.preserveRatio){var m=Math.min(f,d);if(f>m){var v=(f-m)*c;h>=0?h+=v:h=v,f=m;}if(d>m){var y=(d-m)*u;l>=0?l+=y:l=y,d=m;}}this.columns.scale=f,this.rows.scale=d,e=h>0&&this.columns.stretch>0?h/this.columns.stretch:0,r=l>0&&this.rows.stretch>0?l/this.rows.stretch:0;var p=0,b=0;this._beginDraw();for(var g=0,w=this.rows.count;g<w;g++){a=0===(o=this.rows.data[g]).stretch?o.height*d:r*o.stretch,p=0;for(var x=0,P=this.columns.count;x<P;x++)s=0===(n=this.columns.data[x]).stretch?n.width*f:e*n.stretch,i=this.getFrameNameCallback(x,g,this.baseFrameName),t.has(i)&&s>0&&a>0&&(0===(0===o.stretch&&0===n.stretch||0===this.getStretchMode(x,g)?0:1)?this._drawImage(this.textureKey,i,p,b,s,a):this._drawTileSprite(this.textureKey,i,p,b,s,a)),p+=s;b+=a;}this._endDraw();},setStretchMode:function(t){return f(t)?(this.stretchMode.edge=m(d(t,"edge",0)),this.stretchMode.internal=m(d(t,"internal",0))):(t=m(t),this.stretchMode.edge=t,this.stretchMode.internal=t),this},getStretchMode:function(t,e){return y.call(this,t,e)?this.stretchMode.edge:this.stretchMode.internal},setPreserveRatio:function(t){return null==t&&(t=!0),this.preserveRatio=t,this},setMaxFixedPartScale:function(t,e){return void 0===e&&(e=t),this.maxFixedPartScaleX=t,this.maxFixedPartScaleY=e,this}},g=Phaser.Utils.Objects.IsPlainObject,w=Phaser.Utils.Objects.GetValue,x=Phaser.Game,P=function(t){return t instanceof x},S=Phaser.Scene,O=function(t){return t instanceof S},j=function(e){return null==e||"object"!==t(e)?null:P(e)?e:P(e.game)?e.game:O(e)?e.sys.game:O(e.scene)?e.scene.sys.game:void 0},_=Phaser.GameObjects,F=void 0,T=function(t,e){if(F||(F={},j(t).events.once("destroy",(function(){for(var t in F)F[t].destroy();F=void 0;}))),!F.hasOwnProperty(e)){var r=j(t).scene.systemScene;(t=new _[e](r)).setOrigin(0),F[e]=t;}return F[e]},k=function(t){n(o,t);var r=c(o);function o(){return e(this,o),r.apply(this,arguments)}return i(o)}(function(t,r){var s=function(t){n(a,t);var s=c(a);function a(t,i,n,o,c,u,h,l,f,d){var m;if(e(this,a),g(i)?(i=w(d=i,"x",0),n=w(d,"y",0),o=w(d,"width",1),c=w(d,"height",1),u=w(d,"key",void 0),h=w(d,"baseFrame",void 0),l=w(d,"columns",void 0),f=w(d,"rows",void 0)):g(o)?(o=w(d=o,"width",1),c=w(d,"height",1),u=w(d,"key",void 0),h=w(d,"baseFrame",void 0),l=w(d,"columns",void 0),f=w(d,"rows",void 0)):g(u)?(u=w(d=u,"key",void 0),h=w(d,"baseFrame",void 0),l=w(d,"columns",void 0),f=w(d,"rows",void 0)):g(h)?(h=w(d=h,"baseFrame",void 0),l=w(d,"columns",void 0),f=w(d,"rows",void 0)):Array.isArray(h)?(d=f,f=l,l=h,h=w(d,"baseFrame",void 0)):g(l)&&(l=w(d=l,"columns",void 0),f=w(d,"rows",void 0)),void 0===l){var v=w(d,"leftWidth",void 0),y=w(d,"rightWidth",void 0);void 0!==v&&void 0!==y&&(l=[v,void 0,y]);}if(void 0===f){var p=w(d,"topHeight",void 0),b=w(d,"bottomHeight",void 0);void 0!==p&&void 0!==b&&(f=[p,void 0,b]);}(m=s.call(this,t)).type=r,m.setPosition(i,n).setSize(o,c).setOrigin(.5,.5),m.columns={},m.rows={},m.stretchMode={},m._tileSprite=void 0,m._image=void 0,m.setGetFrameNameCallback(w(d,"getFrameNameCallback",void 0)),m.setStretchMode(w(d,"stretchMode",0)),m.setPreserveRatio(w(d,"preserveRatio",!0));var x=w(d,"maxFixedPartScale",1),P=w(d,"maxFixedPartScaleX",x),S=w(d,"maxFixedPartScaleY",void 0);return m.setMaxFixedPartScale(P,S),m.setBaseTexture(u,h,l,f),m}return i(a,[{key:"minWidth",get:function(){return this.columns.minWidth}},{key:"minHeight",get:function(){return this.rows.minHeight}},{key:"fixedPartScaleX",get:function(){return this.columns.scale}},{key:"fixedPartScaleY",get:function(){return this.rows.scale}},{key:"resize",value:function(t,e){return this.width===t&&this.height===e||(u(o(a.prototype),"resize",this)?u(o(a.prototype),"resize",this).call(this,t,e):u(o(a.prototype),"setSize",this).call(this,t,e),this.updateTexture()),this}}]),a}(t);return Object.assign(s.prototype,b),s}(Phaser.GameObjects.RenderTexture,"rexNinePatch")),M={_drawImage:function(t,e,r,i,n,o){var s=T(this,"Image").setTexture(t,e).setDisplaySize(n,o);this.draw(s,r,i);},_drawTileSprite:function(t,e,r,i,n,o){var s=T(this,"TileSprite").setTexture(t,e).setSize(n,o);this.draw(s,r,i);}};return Object.assign(k.prototype,M),k}));