phaser4-rex-plugins
Version:
2 lines (1 loc) • 11.2 kB
JavaScript
var t,e;t=void 0,e=function(){var t={renderWebGL:function(t,e,i,s){if(e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height){i.camera.addToRenderList(e);var r=e.customRenderNodes,h=e.defaultRenderNodes;(r.Submitter||h.Submitter).run(i,e,s,0,r.Texturer||h.Texturer,r.Transformer||h.Transformer)}},renderCanvas:function(t,e,i,s){e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height&&(i.addToRenderList(e),t.batchSprite(e,e.frame,i,s))}};const e=Phaser.Display.Color;var i={clear(){return this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},fill(t){return this.context.fillStyle=t,this.context.fillRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},drawFrame(t,e,i,s,r,h,a,n,o,d){var u=this.scene.sys.textures.getFrame(t,e);if(!u)return this;var l=u.cutWidth,c=u.cutHeight;void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=l),void 0===h&&(h=c),void 0===a&&(a=0),void 0===n&&(n=0),void 0===o&&(o=l),void 0===d&&(d=c);var v=u.cutX+a,g=u.cutY+n;return this.context.drawImage(u.source.image,v,g,o,d,i,s,r,h),this.dirty=!0,this},getDataURL(t,e){return this.canvas.toDataURL(t,e)},getPixel(t,i,s){void 0===s&&(s=new e);var r=this.context.getImageData(t,i,1,1);return s.setTo(r.data[0],r.data[1],r.data[2],r.data[3]),s},setPixel(t,e,i,s,r,h){if("number"!=typeof i){var a=i;i=a.red,s=a.green,r=a.blue,h=a.alpha}void 0===h&&(h=0!==i||0!==s||0!==r?255:0);var n=this.context.createImageData(1,1);return n.data[0]=i,n.data[1]=s,n.data[2]=r,n.data[3]=h,this.context.putImageData(n,t,e),this.dirty=!0,this}},s={updateTexture(t,e){var i=this.canvas,s=this.context;if(t){var r=this.resolution;1!==r&&(this.context.save(),this.context.scale(r,r)),e?t.call(e,i,s):t(i,s),1!==r&&this.context.restore()}var h=i.width,a=i.height;h===this.frame.width&&a===this.frame.height||(this.frame.setSize(h,a),this.frame.source.updateSize(h,a),this.frame.updateUVs()),this.renderer&&this.renderer.gl&&(this.frame.source.glTexture=this.renderer.canvasToTexture(i,this.frame.source.glTexture,!0),this.frame.glTexture.spectorMetadata={textureKey:"Canvas Game Object"}),this.dirty=!1;var n=this.input;return n&&!n.customHitArea&&(n.hitArea.width=this.width,n.hitArea.height=this.height),this},generateTexture(t,e,i,s,r){var h=this.canvas;return void 0===s?s=h.width:s*=this.resolution,void 0===r?r=h.height:r*=this.resolution,function(t,e,i,s,r,h,a){var n,o=t.sys.textures,d=t.renderer;void 0===s&&(s=0),void 0===r&&(r=0),void 0===h&&(h=e.width),void 0===a&&(a=e.height);var u=(n=o.exists(i)?o.get(i):o.createCanvas(i,h,a)).getSourceImage();u.width!==h&&(u.width=h),u.height!==a&&(u.height=a);var l=u.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,h,a),l.drawImage(e,s,r,h,a),d.gl&&n&&d.canvasToTexture(u,n.source[0].glTexture,!0,0)}(this.scene,h,t,e,i,s,r),this},loadTexture(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}},r=!1;!function(t){if(!r){void 0===t&&(t=0);var e=Phaser.VERSION.split("."),i=parseInt(e[0]);if(4===i){var s=parseInt(e[1]);s<t&&console.error(`Minimum supported version : ${i}.${s}`)}else console.error(`Can't supported version : ${i}`);r=!0}}();const h=Phaser.Display.Canvas.CanvasPool,a=Phaser.GameObjects.GameObject,n=Phaser.Utils.String.UUID,o=Phaser.Renderer.WebGL.RenderNodes.Defaults.DefaultImageNodes;class d extends a{constructor(t,e,i,s,r,a){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=1),void 0===r&&(r=1),void 0===a&&(a=1),super(t,"rexCanvas"),this.renderer=t.sys.game.renderer,this._width=s,this._height=r,this.resolution=a,s=Math.max(Math.ceil(s*this.resolution),1),r=Math.max(Math.ceil(r*this.resolution),1),this.canvas=h.create(this,s,r),this.dirty=!1,this.setPosition(e,i),this.setOrigin(.5,.5),this.initRenderNodes(this._defaultRenderNodesMap),this._crop=this.resetCropObject(),this._textureKey=n(),this.texture=t.sys.textures.addCanvas(this._textureKey,this.canvas),this.context=this.texture.context,this.frame=this.texture.get(),this.frame.source.resolution=this.resolution,this.renderer&&this.renderer.gl&&(this.renderer.deleteTexture(this.frame.source.glTexture),this.frame.source.glTexture=null),this.dirty=!0}preDestroy(){h.remove(this.canvas),this.canvas=null,this.context=null;var t=this.texture;t&&t.destroy()}get _defaultRenderNodesMap(){return o}setResolution(t){if(this.resolution===t)return this;this.resolution=t;var e=Math.max(Math.ceil(this.width*t),1),i=Math.max(Math.ceil(this.height*t),1);return this.canvas.width=e,this.canvas.height=i,this.frame.source.resolution=t,this.dirty=!0,this}get width(){return this._width}set width(t){this.setSize(t,this._height)}get height(){return this._height}set height(t){this.setSize(this._width,t)}setCanvasSize(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.frame.source.updateSize(t,e),this.frame.updateUVs(),this.dirty=!0),this}setSize(t,e){return this.setCanvasSize(t,e),this}get displayWidth(){return this.scaleX*this._width}set displayWidth(t){this.scaleX=t/this._width}get displayHeight(){return this.scaleY*this._height}set displayHeight(t){this.scaleY=t/this._height}setDisplaySize(t,e){return this.displayWidth=t,this.displayHeight=e,this}getCanvas(t){return t||(this.dirty=!0),this.canvas}getContext(t){return t||(this.dirty=!0),this.context}needRedraw(){return this.dirty=!0,this}resize(t,e){return this.setSize(t,e),this}}const u=Phaser.GameObjects.Components;Phaser.Class.mixin(d,[u.Alpha,u.BlendMode,u.Crop,u.Depth,u.Flip,u.GetBounds,u.Lighting,u.Mask,u.Origin,u.RenderNodes,u.ScrollFactor,u.Tint,u.Transform,u.Visible,t,i,s]);const l=Phaser.Utils.Objects.GetValue;class c{constructor(t,e,i,s,r){void 0===t&&(t=0),void 0===e&&(e=t),void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=0),this.cornerRadius={},this._width=0,this._height=0,this.setTo(t,e,i,s,r)}setTo(t,e,i,s,r){return this.setPosition(t,e),this.setRadius(r),this.setSize(i,s),this}setPosition(t,e){return this.x=t,this.y=e,this}setRadius(t){return void 0===t&&(t=0),this.radius=t,this}setSize(t,e){return this.width=t,this.height=e,this}get minWidth(){var t=this.cornerRadius;return Math.max(t.tl.x+t.tr.x,t.bl.x+t.br.x)}get minHeight(){var t=this.cornerRadius;return Math.max(t.tl.y+t.bl.y,t.tr.y+t.br.y)}get width(){return this._width}set width(t){null==t&&(t=0),this._width=Math.max(t,this.minWidth)}get height(){return this._height}set height(t){null==t&&(t=0),this._height=Math.max(t,this.minHeight)}get radius(){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 radius(t){var e,i;"number"==typeof t?(e=t,i=t):(e=l(t,"x",0),i=l(t,"y",0));var s=this.cornerRadius;s.tl=v(l(t,"tl",void 0),e,i),s.tr=v(l(t,"tr",void 0),e,i),s.bl=v(l(t,"bl",void 0),e,i),s.br=v(l(t,"br",void 0),e,i)}get radiusTL(){var t=this.cornerRadius.tl;return Math.max(t.x,t.y)}set radiusTL(t){g(this.cornerRadius.tl,t)}get radiusTR(){var t=this.cornerRadius.tr;return Math.max(t.x,t.y)}set radiusTR(t){g(this.cornerRadius.tr,t)}get radiusBL(){var t=this.cornerRadius.bl;return Math.max(t.x,t.y)}set radiusBL(t){g(this.cornerRadius.bl,t)}get radiusBR(){var t=this.cornerRadius.br;return Math.max(t.x,t.y)}set radiusBR(t){g(this.cornerRadius.br,t)}}var v=function(t,e,i){return void 0===t?t={x:e,y:i}:"number"==typeof t&&(t={x:t,y:t}),x(t),t},g=function(t,e){"number"==typeof e?(t.x=e,t.y=e):(t.x=l(e,"x",0),t.y=l(e,"y",0)),x(t)},x=function(t){t.convex=t.x>=0||t.y>=0,t.x=Math.abs(t.x),t.y=Math.abs(t.y)};const y=Phaser.Math.DegToRad;var f=function(t){return!t.hasOwnProperty("convex")||t.convex},m=function(t){return t.x>0&&t.y>0},p=function(t,e,i,s,r,h,a,n,o){if(n&&a>h?a-=360:!n&&a<h&&(a+=360),h=y(h),a=y(a),null==o)t.ellipse(e,i,s,r,0,h,a,n);else for(var d,u,l,c=(a-h)/(o+=1),v=0;v<=o;v++)l=h+c*v,d=e+s*Math.cos(l),u=i+r*Math.sin(l),t.lineTo(d,u)};const b=Phaser.Utils.Objects.GetValue;class w extends d{constructor(t,e,i,s,r,h){super(t,e,i),this.type="rexCircleMaskImage",this.setTexture(s,r,h)}setTexture(t,e,i){"object"==typeof e&&(i=e,e=void 0),"string"==typeof i&&(i={maskType:i});var s=b(i,"maskType",0),r=b(i,"backgroundColor",void 0),h=b(i,"strokeColor",void 0),a=b(i,"strokeWidth",null!=h?10:0);if(void 0===s?s=0:"string"==typeof s&&(s=T[s]),this._textureKey=t,this._frameName=e,null===s)return this.loadTexture(t,e),this.dirty=!0,this;var n=this.scene.sys.textures.getFrame(t,e);if(!n)return this;n.cutWidth!==this.width||n.cutHeight!==this.height?this.setCanvasSize(n.cutWidth,n.cutHeight):this.clear();var o=this.canvas,d=this.context,u=o.width,l=o.height;null!=r&&(d.fillStyle=r,d.fillRect(0,0,u,l)),d.save(),d.beginPath();var v=a/2;switch(s){case 1:var g=(y=Math.floor(u/2))-v,x=(w=Math.floor(l/2))-v;d.ellipse(y,w,g,x,0,0,2*Math.PI);break;case 2:!function(t,e,i,s,r,h,a){var n,o,d,u,l,v=new c(e,i,s,r,h),g=v.minWidth,x=v.minHeight,y=s>=g?1:s/g,b=r>=x?1:r/x,w=v.cornerRadius;t.save(),t.beginPath(),t.translate(e,i),n=w.tl,m(n)?(o=n.x*y,d=n.y*b,f(n)?p(t,o,d,o,d,180,270,!1,a):p(t,0,0,o,d,90,0,!0,a),u=0,l=d):(t.lineTo(0,0),u=0,l=0),n=w.tr,m(n)?(o=n.x*y,d=n.y*b,f(n)?p(t,s-o,d,o,d,270,360,!1,a):p(t,s,0,o,d,180,90,!0,a)):t.lineTo(s,0),n=w.br,m(n)?(o=n.x*y,d=n.y*b,f(n)?p(t,s-o,r-d,o,d,0,90,!1,a):p(t,s,r,o,d,270,180,!0,a)):t.lineTo(s,r),n=w.bl,m(n)?(o=n.x*y,d=n.y*b,f(n)?p(t,o,r-d,o,d,90,180,!1,a):p(t,0,r,o,d,360,270,!0,a)):t.lineTo(0,r),t.lineTo(u,l),t.closePath(),t.restore()}(d,v,v,u-a,l-a,b(i,"radius",0),b(i,"iteration",void 0));break;default:var y=Math.floor(u/2),w=Math.floor(l/2),R=Math.min(y,w)-v;d.arc(y,w,R,0,2*Math.PI)}return null!=h&&(d.strokeStyle=h,d.lineWidth=a,d.stroke()),d.clip(),this.loadTexture(t,e),d.restore(),this.dirty=!0,this}resize(t,e){return this.setDisplaySize(t,e),this}}const T={circle:0,ellipse:1,roundRectangle:2};function R(t,e,i,s,r){var h=new w(this.scene,t,e,i,s,r);return this.scene.add.existing(h),h}const M=Phaser.Utils.Objects.GetAdvancedValue,P=Phaser.GameObjects.BuildGameObject;function _(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=M(t,"key",void 0),s=M(t,"frame",void 0),r=new w(this.scene,0,0,i,s,t);return P(this.scene,r,t),r}var S=function(t){return null==t||""===t||0===t.length};class C extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexCircleMaskImage",R,_)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,s){if(void 0===s&&(s="."),"object"==typeof t)if(S(e)){if(null==i)return;"object"==typeof i&&(t=i)}else{"string"==typeof e&&(e=e.split(s));var r=e.pop(),h=function(t,e,i){var s=t;if(S(e));else{var r;"string"==typeof e&&(e=e.split("."));for(var h=0,a=e.length;h<a;h++){var n;null!=s[r=e[h]]&&"object"==typeof s[r]||(n=h===a-1?void 0===i?{}:i:{},s[r]=n),s=s[r]}}return s}(t,e);h[r]=i}}(window,"RexPlugins.GameObjects.CircleMaskImage",w),C},"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();