phaser3-rex-plugins
Version:
2 lines (1 loc) • 13.9 kB
JavaScript
!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).rexcirclemaskimageplugin=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 i(t,e){for(var i=0;i<e.length;i++){var r=e[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(n=r.key,s=void 0,"symbol"==typeof(s=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var r=i.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(n,"string"))?s:String(s)),r);}var n,s;}function r(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),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&&a(t,e);}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function a(t,e){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},a(t,e)}function o(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(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 i,r=s(t);if(e){var n=s(this).constructor;i=Reflect.construct(r,arguments,n);}else i=r.apply(this,arguments);return function(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 o(t)}(this,i)}}var u,c=Phaser.Renderer.WebGL.Utils,l={renderWebGL:function(t,e,i,r){if(e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height){i.addToRenderList(e);var n=e.frame,s=n.width,a=n.height,o=c.getTintAppendFloatAlpha,h=t.pipelines.set(e.pipeline,e),u=h.setTexture2D(n.glTexture,e);t.pipelines.preBatch(e),h.batchTexture(e,n.glTexture,s,a,e.x,e.y,s/e.resolution,a/e.resolution,e.scaleX,e.scaleY,e.rotation,e.flipX,e.flipY,e.scrollFactorX,e.scrollFactorY,e.displayOriginX,e.displayOriginY,0,0,s,a,o(e.tintTopLeft,i.alpha*e._alphaTL),o(e.tintTopRight,i.alpha*e._alphaTR),o(e.tintBottomLeft,i.alpha*e._alphaBL),o(e.tintBottomRight,i.alpha*e._alphaBR),e.tintFill,0,0,i,r,!1,u),t.pipelines.postBatch(e);}},renderCanvas:function(t,e,i,r){e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height&&(i.addToRenderList(e),t.batchSprite(e,e.frame,i,r));}},d=Phaser.Display.Color,f={clear:function(){return this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},fill:function(t){return this.context.fillStyle=t,this.context.fillRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},drawFrame:function(t,e,i,r,n,s,a,o,h,u){var c=this.scene.sys.textures.getFrame(t,e);if(!c)return this;var l=c.cutWidth,d=c.cutHeight;void 0===i&&(i=0),void 0===r&&(r=0),void 0===n&&(n=l),void 0===s&&(s=d),void 0===a&&(a=0),void 0===o&&(o=0),void 0===h&&(h=l),void 0===u&&(u=d);var f=c.cutX+a,v=c.cutY+o;return this.context.drawImage(c.source.image,f,v,h,u,i,r,n,s),this.dirty=!0,this},getDataURL:function(t,e){return this.canvas.toDataURL(t,e)},getPixel:function(t,e,i){void 0===i&&(i=new d);var r=this.context.getImageData(t,e,1,1);return i.setTo(r.data[0],r.data[1],r.data[2],r.data[3]),i},setPixel:function(t,e,i,r,n,s){if("number"!=typeof i){var a=i;i=a.red,r=a.green,n=a.blue,s=a.alpha;}void 0===s&&(s=0!==i||0!==r||0!==n?255:0);var o=this.context.createImageData(1,1);return o.data[0]=i,o.data[1]=r,o.data[2]=n,o.data[3]=s,this.context.putImageData(o,t,e),this.dirty=!0,this}},v={updateTexture:function(t,e){t&&(e?t.call(e,this.canvas,this.context):t(this.canvas,this.context)),this.canvas.width===this.frame.width&&this.canvas.height===this.frame.height||this.frame.setSize(this.canvas.width,this.canvas.height),this.renderer&&this.renderer.gl&&(this.frame.source.glTexture=this.renderer.canvasToTexture(this.canvas,this.frame.source.glTexture,!0),this.frame.glTexture=this.frame.source.glTexture),this.dirty=!1;var i=this.input;return i&&!i.customHitArea&&(i.hitArea.width=this.width,i.hitArea.height=this.height),this},generateTexture:function(t,e,i,r,n){var s=this.canvas;return void 0===r?r=s.width:r*=this.resolution,void 0===n?n=s.height:n*=this.resolution,function(t,e,i,r,n,s,a){var o,h=t.sys.textures,u=t.renderer;void 0===r&&(r=0),void 0===n&&(n=0),void 0===s&&(s=e.width),void 0===a&&(a=e.height);var c=(o=h.exists(i)?h.get(i):h.createCanvas(i,s,a)).getSourceImage();c.width!==s&&(c.width=s),c.height!==a&&(c.height=a);var l=c.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,s,a),l.drawImage(e,r,n,s,a),u.gl&&o&&u.canvasToTexture(c,o.source[0].glTexture,!0,0);}(this.scene,s,t,e,i,r,n),this},loadTexture:function(t,e){var i=this.scene.sys.textures.getFrame(t,e);return i?(this.width!==i.cutWidth||this.height!==i.cutHeight?this.setSize(i.cutWidth,i.cutHeight):this.clear(),this.drawFrame(t,e),this.dirty=!0,this):this}},y=!1;y||(void 0===u&&(u=60),parseInt(Phaser.VERSION.match(/\.(\d+)\./)[1])<u&&console.error("Minimum supported version : 3.".concat(u)),y=!0);var p=Phaser.Display.Canvas.CanvasPool,g=function(t){n(s,t);var i=h(s);function s(t,r,n,a,h){var u;return e(this,s),void 0===r&&(r=0),void 0===n&&(n=0),void 0===a&&(a=1),void 0===h&&(h=1),(u=i.call(this,t,"rexCanvas")).renderer=t.sys.game.renderer,u.resolution=1,u._width=a,u._height=h,a=Math.max(Math.ceil(a*u.resolution),1),h=Math.max(Math.ceil(h*u.resolution),1),u.canvas=p.create(o(u),a,h),u.context=u.canvas.getContext("2d",{willReadFrequently:!0}),u.dirty=!1,u.setPosition(r,n),u.setOrigin(.5,.5),u.initPipeline(),u.initPostPipeline(!0),u._crop=u.resetCropObject(),u.texture=t.sys.textures.addCanvas(null,u.canvas,!0),u.frame=u.texture.get(),u.frame.source.resolution=u.resolution,u.renderer&&u.renderer.gl&&(u.renderer.deleteTexture(u.frame.source.glTexture),u.frame.source.glTexture=null),u.dirty=!0,u}return r(s,[{key:"preDestroy",value:function(){p.remove(this.canvas),this.texture.destroy(),this.canvas=null,this.context=null;}},{key:"width",get:function(){return this._width},set:function(t){this.setSize(t,this._height);}},{key:"height",get:function(){return this._height},set:function(t){this.setSize(this._width,t);}},{key:"setCanvasSize",value:function(t,e){return this._width===t&&this._height===e||(this._width=t,this._height=e,this.updateDisplayOrigin(),t=Math.max(Math.ceil(t*this.resolution),1),e=Math.max(Math.ceil(e*this.resolution),1),this.canvas.width=t,this.canvas.height=e,this.frame.setSize(t,e),this.dirty=!0),this}},{key:"setSize",value:function(t,e){return this.setCanvasSize(t,e),this}},{key:"displayWidth",get:function(){return this.scaleX*this._width},set:function(t){this.scaleX=t/this._width;}},{key:"displayHeight",get:function(){return this.scaleY*this._height},set:function(t){this.scaleY=t/this._height;}},{key:"setDisplaySize",value:function(t,e){return this.displayWidth=t,this.displayHeight=e,this}},{key:"getCanvas",value:function(t){return t||(this.dirty=!0),this.canvas}},{key:"getContext",value:function(t){return t||(this.dirty=!0),this.context}},{key:"needRedraw",value:function(){return this.dirty=!0,this}},{key:"resize",value:function(t,e){return this.setSize(t,e),this}}]),s}(Phaser.GameObjects.GameObject),x=Phaser.GameObjects.Components;Phaser.Class.mixin(g,[x.Alpha,x.BlendMode,x.Crop,x.Depth,x.Flip,x.GetBounds,x.Mask,x.Origin,x.Pipeline,x.PostPipeline,x.ScrollFactor,x.Tint,x.Transform,x.Visible,l,f,v]);var m=Phaser.Utils.Objects.GetValue,b=function(){function t(i,r,n,s,a){e(this,t),void 0===i&&(i=0),void 0===r&&(r=i),void 0===n&&(n=0),void 0===s&&(s=0),void 0===a&&(a=0),this.cornerRadius={},this._width=0,this._height=0,this.setTo(i,r,n,s,a);}return r(t,[{key:"setTo",value:function(t,e,i,r,n){return this.setPosition(t,e),this.setRadius(n),this.setSize(i,r),this}},{key:"setPosition",value:function(t,e){return this.x=t,this.y=e,this}},{key:"setRadius",value:function(t){return void 0===t&&(t=0),this.radius=t,this}},{key:"setSize",value:function(t,e){return this.width=t,this.height=e,this}},{key:"minWidth",get:function(){var t=this.cornerRadius;return Math.max(t.tl.x+t.tr.x,t.bl.x+t.br.x)}},{key:"minHeight",get:function(){var t=this.cornerRadius;return Math.max(t.tl.y+t.bl.y,t.tr.y+t.br.y)}},{key:"width",get:function(){return this._width},set:function(t){null==t&&(t=0),this._width=Math.max(t,this.minWidth);}},{key:"height",get:function(){return this._height},set:function(t){null==t&&(t=0),this._height=Math.max(t,this.minHeight);}},{key:"radius",get:function(){var t=this.cornerRadius;return Math.max(t.tl.x,t.tl.y,t.tr.x,t.tr.y,t.bl.x,t.bl.y,t.br.x,t.br.y)},set:function(t){var e,i;"number"==typeof t?(e=t,i=t):(e=m(t,"x",0),i=m(t,"y",0));var r=this.cornerRadius;r.tl=w(m(t,"tl",void 0),e,i),r.tr=w(m(t,"tr",void 0),e,i),r.bl=w(m(t,"bl",void 0),e,i),r.br=w(m(t,"br",void 0),e,i);}},{key:"radiusTL",get:function(){var t=this.cornerRadius.tl;return Math.max(t.x,t.y)},set:function(t){T(this.cornerRadius.tl,t);}},{key:"radiusTR",get:function(){var t=this.cornerRadius.tr;return Math.max(t.x,t.y)},set:function(t){T(this.cornerRadius.tr,t);}},{key:"radiusBL",get:function(){var t=this.cornerRadius.bl;return Math.max(t.x,t.y)},set:function(t){T(this.cornerRadius.bl,t);}},{key:"radiusBR",get:function(){var t=this.cornerRadius.br;return Math.max(t.x,t.y)},set:function(t){T(this.cornerRadius.br,t);}}]),t}(),w=function(t,e,i){return void 0===t?t={x:e,y:i}:"number"==typeof t&&(t={x:t,y:t}),P(t),t},T=function(t,e){"number"==typeof e?(t.x=e,t.y=e):(t.x=m(e,"x",0),t.y=m(e,"y",0)),P(t);},P=function(t){t.convex=t.x>=0||t.y>=0,t.x=Math.abs(t.x),t.y=Math.abs(t.y);},k=Phaser.Math.DegToRad,R=function(t){return !t.hasOwnProperty("convex")||t.convex},M=function(t){return t.x>0&&t.y>0},_=function(t,e,i,r,n,s,a,o,h){if(o&&a>s?a-=360:!o&&a<s&&(a+=360),s=k(s),a=k(a),null==h)t.ellipse(e,i,r,n,0,s,a,o);else for(var u,c,l,d=(a-s)/(h+=1),f=0;f<=h;f++)l=s+d*f,u=e+r*Math.cos(l),c=i+n*Math.sin(l),t.lineTo(u,c);},O=Phaser.Utils.Objects.GetValue,S=function(i){n(a,i);var s=h(a);function a(t,i,r,n,o,h){var u;return e(this,a),(u=s.call(this,t,i,r)).type="rexCircleMaskImage",u.setTexture(n,o,h),u}return r(a,[{key:"setTexture",value:function(e,i,r){"object"===t(i)&&(r=i,i=void 0),"string"==typeof r&&(r={maskType:r});var n=O(r,"maskType",0),s=O(r,"backgroundColor",void 0),a=O(r,"strokeColor",void 0),o=O(r,"strokeWidth",null!=a?10:0);if(void 0===n?n=0:"string"==typeof n&&(n=j[n]),this._textureKey=e,this._frameName=i,null===n)return this.loadTexture(e,i),this.dirty=!0,this;var h=this.scene.sys.textures.getFrame(e,i);if(!h)return this;h.cutWidth!==this.width||h.cutHeight!==this.height?this.setCanvasSize(h.cutWidth,h.cutHeight):this.clear();var u=this.canvas,c=this.context,l=u.width,d=u.height;null!=s&&(c.fillStyle=s,c.fillRect(0,0,l,d)),c.save(),c.beginPath();var f=o/2;switch(n){case 1:var v=(p=Math.floor(l/2))-f,y=(g=Math.floor(d/2))-f;c.ellipse(p,g,v,y,0,0,2*Math.PI);break;case 2:!function(t,e,i,r,n,s,a){var o,h,u,c,l,d=new b(e,i,r,n,s),f=d.minWidth,v=d.minHeight,y=r>=f?1:r/f,p=n>=v?1:n/v,g=d.cornerRadius;t.save(),t.beginPath(),t.translate(e,i),o=g.tl,M(o)?(h=o.x*y,u=o.y*p,R(o)?_(t,h,u,h,u,180,270,!1,a):_(t,0,0,h,u,90,0,!0,a),c=0,l=u):(t.lineTo(0,0),c=0,l=0),o=g.tr,M(o)?(h=o.x*y,u=o.y*p,R(o)?_(t,r-h,u,h,u,270,360,!1,a):_(t,r,0,h,u,180,90,!0,a)):t.lineTo(r,0),o=g.br,M(o)?(h=o.x*y,u=o.y*p,R(o)?_(t,r-h,n-u,h,u,0,90,!1,a):_(t,r,n,h,u,270,180,!0,a)):t.lineTo(r,n),o=g.bl,M(o)?(h=o.x*y,u=o.y*p,R(o)?_(t,h,n-u,h,u,90,180,!1,a):_(t,0,n,h,u,360,270,!0,a)):t.lineTo(0,n),t.lineTo(c,l),t.closePath(),t.restore();}(c,f,f,l-o,d-o,O(r,"radius",0),O(r,"iteration",void 0));break;default:var p=Math.floor(l/2),g=Math.floor(d/2),x=Math.min(p,g)-f;c.arc(p,g,x,0,2*Math.PI);}return null!=a&&(c.strokeStyle=a,c.lineWidth=o,c.stroke()),c.clip(),this.loadTexture(e,i),c.restore(),this.dirty=!0,this}},{key:"resize",value:function(t,e){return this.setDisplaySize(t,e),this}}]),a}(g),j={circle:0,ellipse:1,roundRectangle:2};function C(t,e,i,r,n){var s=new S(this.scene,t,e,i,r,n);return this.scene.add.existing(s),s}var z=Phaser.Utils.Objects.GetAdvancedValue,D=Phaser.GameObjects.BuildGameObject;function B(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=z(t,"key",void 0),r=z(t,"frame",void 0),n=new S(this.scene,0,0,i,r,t);return D(this.scene,n,t),n}var W=function(t){return null==t||""===t||0===t.length},F=function(t){n(s,Phaser.Plugins.BasePlugin);var i=h(s);function s(t){var r;return e(this,s),r=i.call(this,t),t.registerGameObject("rexCircleMaskImage",C,B),r}return r(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}}]),s}();return function(e,i,r,n){if(void 0===n&&(n="."),"object"===t(e)){if(W(i)){if(null==r)return;"object"===t(r)&&(e=r);}else {"string"==typeof i&&(i=i.split(n));var s=i.pop(),a=function(e,i,r){var n=e;if(W(i));else {var s;"string"==typeof i&&(i=i.split("."));for(var a=0,o=i.length;a<o;a++){var h;null!=n[s=i[a]]&&"object"===t(n[s])||(h=a===o-1?void 0===r?{}:r:{},n[s]=h),n=n[s];}}return n}(e,i);a[s]=r;}}}(window,"RexPlugins.GameObjects.CircleMaskImage",S),F}));