UNPKG

@teampanfu/phaser-nineslice

Version:
1 lines 13.4 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.NineSlice=e():t.NineSlice=e()}(self,(()=>(()=>{"use strict";var t={d:(e,o)=>{for(var r in o)t.o(o,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:o[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function o(t){return o="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},o(t)}function r(t,e){for(var o=0;o<e.length;o++){var r=e[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function i(){return i="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,o){var r=n(t,e);if(r){var i=Object.getOwnPropertyDescriptor(r,e);return i.get?i.get.call(arguments.length<3?t:o):i.value}},i.apply(this,arguments)}function n(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=f(t)););return t}function a(t,e){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},a(t,e)}function h(t,e){if(e&&("object"===o(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 f(t){return f=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},f(t)}t.r(e),t.d(e,{NineSlice:()=>u,Plugin:()=>w});var c=function(t){return{topLeft:{x:t.topLeft.width,y:t.topLeft.height},topRight:{x:t.topRight.width,y:t.topRight.height},bottomLeft:{x:t.bottomLeft.width,y:t.bottomLeft.height},bottomRight:{x:t.bottomRight.width,y:t.bottomRight.height}}},u=function(t){!function(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&&a(t,e)}(l,Phaser.GameObjects.RenderTexture);var e,o,n,u,s=(n=l,u=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}}(),function(){var t,e=f(n);if(u){var o=f(this).constructor;t=Reflect.construct(e,arguments,o)}else t=e.apply(this,arguments);return h(this,t)});function l(t,e,o){var r;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,l);var i=o.x,n=o.y,a=o.width,h=o.height;(r=s.call(this,t,i,n,a||32,h||32)).sliceConfig=function(t){if("number"==typeof t.sourceLayout.width){var e=t.sourceLayout,o=e.width,r=e.height;t.sourceLayout={topLeft:{width:o,height:r}}}var i=t.sourceLayout;i.topRight=i.topRight||i.topLeft,i.bottomLeft=i.bottomLeft||i.topLeft,i.bottomRight=i.bottomRight||i.topLeft;var n=Math.max(i.topLeft.height,i.topRight.height),a=Math.max(i.topRight.width,i.bottomRight.width),h=Math.max(i.bottomLeft.height,i.bottomRight.height),f=Math.max(i.topLeft.width,i.bottomLeft.width);return t.safeOffsets||(t.safeOffsets={top:n,right:a,bottom:h,left:f}),t}(e);var f=r.sliceConfig,c=f.sourceKey,u=f.sourceFrame;if(!c)throw new Error("NineSlice requires a texture key to be specified.");if(r.isSpriteTexture=!0,r.sourceTexture=t.sys.textures.get(c),!r.sourceTexture||"__MISSING"===r.sourceTexture.key)throw new Error("Specified texture key '".concat(c,"' not found."));r.frameCache={};var p=u||"__BASE";return r.sourceFrame=r.sourceTexture.get(p),r.initFrames(),r.initialized=!0,r.resize(a,h),r}return e=l,o=[{key:"x",get:function(){return this._x},set:function(t){this._x=t,this._debugGraphic&&(this._debugGraphic.clear(),this.updateSafeBounds(),this.drawFrames())}},{key:"y",get:function(){return this._y},set:function(t){this._y=t,this._debugGraphic&&(this._debugGraphic.clear(),this.updateSafeBounds(),this.drawFrames())}},{key:"drawFrames",value:function(){var t=this;if(this.initialized){this._debugGraphic&&this._debugGraphic.lineStyle(1,16711680);var e=c(this.sliceConfig.sourceLayout),o=this.frameCache,r=function(e,o,r,i,n){if(i>0&&n>0){if(t._debugGraphic){var a,h,f=t.x+o+((null===(a=t.parentContainer)||void 0===a?void 0:a.x)||0),c=t.y+r+((null===(h=t.parentContainer)||void 0===h?void 0:h.y)||0);t._debugGraphic.strokeRect(f,c,i,n)}var u=t.scene.make.image({key:t.sourceTexture.key,frame:e.name,x:0,y:0}),s=i/e.width,l=n/e.height;u.setOrigin(0).setScale(s,l),t.draw(u,o,r),u.destroy()}};this.clear();var i=Math.min(e.topLeft.x,e.bottomLeft.x),n=Math.min(e.topRight.x,e.bottomRight.x),a=Math.min(e.topLeft.y,e.topRight.y),h=Math.min(e.bottomLeft.y,e.bottomRight.y);r(o.center,i,a,this.width-i-n,this.height-a-h),r(o.topMiddle,e.topLeft.x,0,this.width-e.topLeft.x-e.topRight.x,o.topMiddle.height),r(o.bottomMiddle,e.bottomLeft.x,this.height-o.bottomMiddle.height,this.width-e.bottomLeft.x-e.bottomRight.x,o.bottomMiddle.height),r(o.leftMiddle,0,e.topLeft.y,o.leftMiddle.width,this.height-e.topLeft.y-e.bottomLeft.y),r(o.rightMiddle,this.width-e.topRight.x,e.topRight.y,o.rightMiddle.width,this.height-e.topRight.y-e.bottomRight.y),r(o.topLeft,0,0,e.topLeft.x,e.topLeft.y),r(o.topRight,this.width-e.topRight.x,0,e.topRight.x,e.topRight.y),r(o.bottomLeft,0,this.height-e.bottomLeft.y,e.bottomLeft.x,e.bottomLeft.y),r(o.bottomRight,this.width-e.bottomRight.x,this.height-e.bottomRight.y,e.bottomRight.x,e.bottomRight.y)}}},{key:"enableDebugDraw",value:function(){var t=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];this._debugGraphic&&this._debugGraphic.clear(),t?this._debugGraphic=this.scene.add.graphics(this.x,this.y):(this._debugGraphic.destroy(!0),this._debugGraphic=null),this.drawFrames(),this.updateSafeBounds()}},{key:"initFrames",value:function(){var t=this,e=this.sourceTexture,o=this.sourceFrame.width,r=this.sourceFrame.height,i=this.sourceFrame.cutX,n=this.sourceFrame.cutY,a=function(o,r,a,h,f){var c="".concat(t.sliceConfig.sourceKey,"_").concat(t.sliceConfig.sourceFrame,"/").concat(o);e.has(c)?t.frameCache[o]=e.frames[c]:t.frameCache[o]=e.add(c,0,i+r,n+a,h,f)},h=c(this.sliceConfig.sourceLayout);a("topLeft",0,0,h.topLeft.x,h.topLeft.y),a("topRight",o-h.topRight.x,0,h.topRight.x,h.topRight.y),a("bottomRight",o-h.bottomRight.x,r-h.bottomRight.y,h.bottomRight.x,h.bottomRight.y),a("bottomLeft",0,r-h.bottomLeft.y,h.bottomLeft.x,h.bottomLeft.y),a("topMiddle",h.topLeft.x,0,o-(h.topLeft.x+h.topRight.x),Math.max(h.topLeft.y,h.topRight.y)),a("bottomMiddle",h.bottomLeft.x,r-Math.max(h.bottomLeft.y,h.bottomRight.y),o-(h.bottomLeft.x+h.bottomRight.x),Math.max(h.bottomLeft.y,h.bottomRight.y)),a("leftMiddle",0,h.topLeft.y,Math.max(h.topLeft.x,h.bottomLeft.x),r-(h.topLeft.y+h.bottomLeft.y)),a("rightMiddle",o-Math.max(h.topRight.x,h.bottomRight.x),h.topRight.y,Math.max(h.topRight.x,h.bottomRight.x),r-h.topRight.y-h.bottomRight.y);var f=Math.min(h.topLeft.x,h.bottomLeft.x),u=Math.min(h.topLeft.y,h.topRight.y);a("center",f,u,o-f-Math.min(h.topRight.x,h.bottomRight.x),r-u-Math.min(h.bottomRight.y,h.bottomLeft.y))}},{key:"resize",value:function(t,e){if(this.initialized){var o=this.sliceConfig.safeOffsets,r=o.left+o.right,n=o.top+o.bottom;(t<r||e<n)&&console.error("Attempted to set the NineSlice size below the minimum. (".concat(r,"x").concat(n,")")),i(f(l.prototype),"resize",this).call(this,t,e),this.drawFrames(),this.updateSafeBounds()}}},{key:"updateSafeBounds",value:function(){if(this.initialized&&this._debugGraphic){this._safeBounds||(this._safeBounds=new Phaser.Geom.Rectangle);var t=this.sliceConfig.safeOffsets,e=t.top,o=t.right,r=t.bottom,i=t.left,n=this.x+i,a=this.y+e,h=this.width-(i+o),f=this.height-(e+r),c=this._safeBounds,u=c.x,s=c.y,l=c.width,p=c.height;if(n!==u||a!==s||h!==l||f!==p){var y,b,d=n+((null===(y=this.parentContainer)||void 0===y?void 0:y.x)||0),g=a+((null===(b=this.parentContainer)||void 0===b?void 0:b.y)||0);this._safeBounds.setTo(d,g,h,f)}this._debugGraphic.lineStyle(1,65280),this._debugGraphic.strokeRectShape(this._safeBounds)}}}],o&&r(e.prototype,o),Object.defineProperty(e,"prototype",{writable:!1}),l}();function s(t){return s="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},s(t)}function l(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var r,i,n=[],a=!0,h=!1;try{for(o=o.call(t);!(a=(r=o.next()).done)&&(n.push(r.value),!e||n.length!==e);a=!0);}catch(t){h=!0,i=t}finally{try{a||null==o.return||o.return()}finally{if(h)throw i}}return n}}(t,e)||function(t,e){if(t){if("string"==typeof t)return p(t,e);var o=Object.prototype.toString.call(t).slice(8,-1);return"Object"===o&&t.constructor&&(o=t.constructor.name),"Map"===o||"Set"===o?Array.from(t):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?p(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,r=new Array(e);o<e;o++)r[o]=t[o];return r}var y=function(t){switch(t.length){case 1:return[t[0],t[0],t[0],t[0]];case 2:return[t[0],t[1],t[0],t[1]];case 3:return[t[0],t[1],t[2],t[1]];case 4:return t}throw new Error("Received ".concat(arr.length," offset values, expected 1 to 4."))};function b(t,e){if(2===e.length)return new u(this.scene,e);if(e.length<6)throw new Error("Expected at least 6 arguments to NineSlice creator, received ".concat(e.length,"."));e.length>7&&console.warn("Expected less than 7 arguments to NineSlice creator, received ".concat(e.length,"."));var o=l(e,6),r=o[0],i=o[1],n=o[2],a=o[3],h=o[4],f=o[5],c={},p={x:r,y:i,width:n,height:a};if("string"==typeof h)c.sourceKey=h;else{var b=h.key,d=h.frame;c.sourceKey=b,"string"==typeof d&&(c.sourceFrame=d)}if("number"==typeof f)c.sourceLayout={width:f,height:f};else if(Array.isArray(f)){var g=l(y(f),4),m=g[0],x=g[1],v=g[2],w=g[3];c.sourceLayout={topLeft:{width:w,height:m},topRight:{width:x,height:m},bottomLeft:{width:w,height:v},bottomRight:{width:x,height:v}}}else c.sourceLayout=f;if(e.length>6){var R=e[6];if("number"==typeof R)c.safeOffsets={top:R,right:R,bottom:R,left:R};else{if(!Array.isArray(R))throw new Error("Expected type number or array for argument 7, got ".concat(s(R)));var L=l(y(R),4),S=L[0],O=L[1],_=L[2],j=L[3];c.safeOffsets={top:S,right:O,bottom:_,left:j}}}return new u(t,c,p)}function d(t){return d="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},d(t)}function g(t,e){for(var o=0;o<e.length;o++){var r=e[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function m(t,e){return m=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},m(t,e)}function x(t,e){if(e&&("object"===d(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 v(t){return v=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},v(t)}var w=function(t){!function(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&&m(t,e)}(a,Phaser.Plugins.BasePlugin);var e,o,r,i,n=(r=a,i=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}}(),function(){var t,e=v(r);if(i){var o=v(this).constructor;t=Reflect.construct(e,arguments,o)}else t=e.apply(this,arguments);return x(this,t)});function a(t){var e;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,a),e=n.call(this,t),t.registerGameObject("nineslice",e.addNineSlice,e.makeNineSlice),e}return e=a,o=[{key:"addNineSlice",value:function(){for(var t=arguments.length,e=new Array(t),o=0;o<t;o++)e[o]=arguments[o];var r=new b(this.scene,e);return this.displayList.add(r),r}},{key:"makeNineSlice",value:function(){for(var t=arguments.length,e=new Array(t),o=0;o<t;o++)e[o]=arguments[o];return new b(this.scene,e)}}],o&&g(e.prototype,o),Object.defineProperty(e,"prototype",{writable:!1}),a}();return e})()));