@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 11.9 kB
JavaScript
var t,i;t=void 0,i=function(){const t=Phaser.Renderer.WebGL.Utils;var i={renderWebGL:function(i,e,s,r){if(e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height){s.addToRenderList(e);var h=e.frame,a=h.width,n=h.height,o=t.getTintAppendFloatAlpha,l=i.pipelines.set(e.pipeline,e),d=l.setTexture2D(h.glTexture,e);i.pipelines.preBatch(e),l.batchTexture(e,h.glTexture,a,n,e.x,e.y,a/e.resolution,n/e.resolution,e.scaleX,e.scaleY,e.rotation,e.flipX,e.flipY,e.scrollFactorX,e.scrollFactorY,e.displayOriginX,e.displayOriginY,0,0,a,n,o(e.tintTopLeft,s.alpha*e._alphaTL),o(e.tintTopRight,s.alpha*e._alphaTR),o(e.tintBottomLeft,s.alpha*e._alphaBL),o(e.tintBottomRight,s.alpha*e._alphaBR),e.tintFill,0,0,s,r,!1,d),i.pipelines.postBatch(e)}},renderCanvas:function(t,i,e,s){i.dirty&&(i.updateTexture(),i.dirty=!1),0!==i.width&&0!==i.height&&(e.addToRenderList(i),t.batchSprite(i,i.frame,e,s))}};const e=Phaser.Display.Color;var s={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,i,e,s,r,h,a,n,o,l){var d=this.scene.sys.textures.getFrame(t,i);if(!d)return this;var u=d.cutWidth,c=d.cutHeight;void 0===e&&(e=0),void 0===s&&(s=0),void 0===r&&(r=u),void 0===h&&(h=c),void 0===a&&(a=0),void 0===n&&(n=0),void 0===o&&(o=u),void 0===l&&(l=c);var v=d.cutX+a,g=d.cutY+n;return this.context.drawImage(d.source.image,v,g,o,l,e,s,r,h),this.dirty=!0,this},getDataURL(t,i){return this.canvas.toDataURL(t,i)},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,i,e,s,r,h){if("number"!=typeof e){var a=e;e=a.red,s=a.green,r=a.blue,h=a.alpha}void 0===h&&(h=0!==e||0!==s||0!==r?255:0);var n=this.context.createImageData(1,1);return n.data[0]=e,n.data[1]=s,n.data[2]=r,n.data[3]=h,this.context.putImageData(n,t,i),this.dirty=!0,this}},r={updateTexture(t,i){if(t){var e=this.resolution;1!==e&&(this.context.save(),this.context.scale(e,e)),i?t.call(i,this.canvas,this.context):t(this.canvas,this.context),1!==e&&this.context.restore()}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.spectorMetadata={textureKey:"Canvas Game Object"}),this.dirty=!1;var s=this.input;return s&&!s.customHitArea&&(s.hitArea.width=this.width,s.hitArea.height=this.height),this},generateTexture(t,i,e,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,i,e,s,r,h,a){var n,o=t.sys.textures,l=t.renderer;void 0===s&&(s=0),void 0===r&&(r=0),void 0===h&&(h=i.width),void 0===a&&(a=i.height);var d=(n=o.exists(e)?o.get(e):o.createCanvas(e,h,a)).getSourceImage();d.width!==h&&(d.width=h),d.height!==a&&(d.height=a);var u=d.getContext("2d",{willReadFrequently:!0});u.clearRect(0,0,h,a),u.drawImage(i,s,r,h,a),l.gl&&n&&l.canvasToTexture(d,n.source[0].glTexture,!0,0)}(this.scene,h,t,i,e,s,r),this},loadTexture(t,i){var e=this.scene.sys.textures.getFrame(t,i);return e?(this.width!==e.cutWidth||this.height!==e.cutHeight?this.setSize(e.cutWidth,e.cutHeight):this.clear(),this.drawFrame(t,i),this.dirty=!0,this):this}},h=!1;!function(t){if(!h){void 0===t&&(t=60);var i=Phaser.VERSION.split("."),e=parseInt(i[0]);if(3===e){var s=parseInt(i[1]);s<t&&console.error(`Minimum supported version : ${e}.${s}`)}else console.error(`Can't supported version : ${e}`);h=!0}}();const a=Phaser.Display.Canvas.CanvasPool,n=Phaser.GameObjects.GameObject,o=Phaser.Utils.String.UUID;class l extends n{constructor(t,i,e,s,r,h){void 0===i&&(i=0),void 0===e&&(e=0),void 0===s&&(s=1),void 0===r&&(r=1),void 0===h&&(h=1),super(t,"rexCanvas"),this.renderer=t.sys.game.renderer,this._width=s,this._height=r,this.resolution=h,s=Math.max(Math.ceil(s*this.resolution),1),r=Math.max(Math.ceil(r*this.resolution),1),this.canvas=a.create(this,s,r),this.context=this.canvas.getContext("2d",{willReadFrequently:!0}),this.dirty=!1,this.setPosition(i,e),this.setOrigin(.5,.5),this.initPipeline(),this.initPostPipeline(!0),this._crop=this.resetCropObject(),this._textureKey=o(),this.texture=t.sys.textures.addCanvas(this._textureKey,this.canvas),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(){a.remove(this.canvas),this.canvas=null,this.context=null;var t=this.texture;t&&t.destroy()}setResolution(t){if(this.resolution===t)return this;this.resolution=t;var i=Math.max(Math.ceil(this.width*t),1),e=Math.max(Math.ceil(this.height*t),1);return this.canvas.width=i,this.canvas.height=e,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,i){return this._width===t&&this._height===i||(this._width=t,this._height=i,this.updateDisplayOrigin(),t=Math.max(Math.ceil(t*this.resolution),1),i=Math.max(Math.ceil(i*this.resolution),1),this.canvas.width=t,this.canvas.height=i,this.frame.setSize(t,i),this.dirty=!0),this}setSize(t,i){return this.setCanvasSize(t,i),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,i){return this.displayWidth=t,this.displayHeight=i,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,i){return this.setSize(t,i),this}}const d=Phaser.GameObjects.Components;Phaser.Class.mixin(l,[d.Alpha,d.BlendMode,d.Crop,d.Depth,d.Flip,d.GetBounds,d.Mask,d.Origin,d.Pipeline,d.PostPipeline,d.ScrollFactor,d.Tint,d.Transform,d.Visible,i,s,r]);const u=Phaser.Utils.String.Pad;var c=function(t,i,e){if(null==t)return t;switch(typeof t){case"string":default:return t;case"number":return`#${u(Math.floor(t).toString(16),6,"0",1)}`;case"function":return t(i,e);case"object":return t.hasOwnProperty("r")?t.hasOwnProperty("a")?`rgba(${t.r},${t.g},${t.b},${t.a})`:`rgb(${t.r},${t.g},${t.b})`:t.hasOwnProperty("h")?t.hasOwnProperty("a")?`hsla(${t.h},${t.s},${t.l},${t.a})`:`hsl(${t.h},${t.s},${t.l})`:t}};const v=Phaser.Utils.Objects.GetValue;let g=class{constructor(t,i,e,s,r){void 0===t&&(t=0),void 0===i&&(i=t),void 0===e&&(e=0),void 0===s&&(s=0),void 0===r&&(r=0),this.cornerRadius={},this._width=0,this._height=0,this.setTo(t,i,e,s,r)}setTo(t,i,e,s,r){return this.setPosition(t,i),this.setRadius(r),this.setSize(e,s),this}setPosition(t,i){return this.x=t,this.y=i,this}setRadius(t){return void 0===t&&(t=0),this.radius=t,this}setSize(t,i){return this.width=t,this.height=i,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 i,e;"number"==typeof t?(i=t,e=t):(i=v(t,"x",0),e=v(t,"y",0));var s=this.cornerRadius;s.tl=y(v(t,"tl",void 0),i,e),s.tr=y(v(t,"tr",void 0),i,e),s.bl=y(v(t,"bl",void 0),i,e),s.br=y(v(t,"br",void 0),i,e)}get radiusTL(){var t=this.cornerRadius.tl;return Math.max(t.x,t.y)}set radiusTL(t){x(this.cornerRadius.tl,t)}get radiusTR(){var t=this.cornerRadius.tr;return Math.max(t.x,t.y)}set radiusTR(t){x(this.cornerRadius.tr,t)}get radiusBL(){var t=this.cornerRadius.bl;return Math.max(t.x,t.y)}set radiusBL(t){x(this.cornerRadius.bl,t)}get radiusBR(){var t=this.cornerRadius.br;return Math.max(t.x,t.y)}set radiusBR(t){x(this.cornerRadius.br,t)}};var y=function(t,i,e){return void 0===t?t={x:i,y:e}:"number"==typeof t&&(t={x:t,y:t}),p(t),t},x=function(t,i){"number"==typeof i?(t.x=i,t.y=i):(t.x=v(i,"x",0),t.y=v(i,"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)};const f=Phaser.Math.DegToRad;var m=function(t){return!t.hasOwnProperty("convex")||t.convex},w=function(t){return t.x>0&&t.y>0},_=function(t,i,e,s,r,h,a,n,o){if(n&&a>h?a-=360:!n&&a<h&&(a+=360),h=f(h),a=f(a),null==o)t.ellipse(i,e,s,r,0,h,a,n);else for(var l,d,u,c=(a-h)/(o+=1),v=0;v<=o;v++)u=h+c*v,l=i+s*Math.cos(u),d=e+r*Math.sin(u),t.lineTo(l,d)},b=function(t,i,e,s,r,h,a,n,o,l,d,u,c){var v;(function(t,i,e,s,r,h,a){var n,o,l,d,u,c=new g(i,e,s,r,h),v=c.minWidth,y=c.minHeight,x=s>=v?1:s/v,p=r>=y?1:r/y,f=c.cornerRadius;t.save(),t.beginPath(),t.translate(i,e),n=f.tl,w(n)?(o=n.x*x,l=n.y*p,m(n)?_(t,o,l,o,l,180,270,!1,a):_(t,0,0,o,l,90,0,!0,a),d=0,u=l):(t.lineTo(0,0),d=0,u=0),n=f.tr,w(n)?(o=n.x*x,l=n.y*p,m(n)?_(t,s-o,l,o,l,270,360,!1,a):_(t,s,0,o,l,180,90,!0,a)):t.lineTo(s,0),n=f.br,w(n)?(o=n.x*x,l=n.y*p,m(n)?_(t,s-o,r-l,o,l,0,90,!1,a):_(t,s,r,o,l,270,180,!0,a)):t.lineTo(s,r),n=f.bl,w(n)?(o=n.x*x,l=n.y*p,m(n)?_(t,o,r-l,o,l,90,180,!1,a):_(t,0,r,o,l,360,270,!0,a)):t.lineTo(0,r),t.lineTo(d,u),t.closePath(),t.restore()}(i,e,s,r,h,a,c),null!=n)&&(null!=d&&((v=u?i.createLinearGradient(0,0,r,0):i.createLinearGradient(0,0,0,h)).addColorStop(0,n),v.addColorStop(1,d),n=v),i.fillStyle=n,i.fill());null!=o&&l>0&&(i.strokeStyle=o,i.lineWidth=l,i.stroke())},T=function(){!function(t,i,e,s,r,h,a,n){if(null!=i||null!=e){var o=t.canvas.width,l=t.canvas.height;null==e&&(s=0);var d=s/2;o=Math.max(1,o-s),l=Math.max(1,l-s),b(t.canvas,t.context,d,d,o,l,r,i,e,s,h,a,n)}}(this,this.fillStyle,this.strokeStyle,this.lineWidth,this.radius,this.fillColor2,this.isHorizontalGradient,this.iteration)};const S=Phaser.Utils.Objects.GetValue;return class extends l{constructor(t,i,e,s,r,h,a,n,o,l,d,u){void 0===i&&(i=0),void 0===e&&(e=0),void 0===s&&(s=1),void 0===r&&(r=s),void 0===h&&(h=0),void 0===u&&(u=1),super(t,i,e,s,r,u),this.type="rexRoundRectangleCanvas";var c=S(h,"radius",h),v=S(h,"iteration",void 0);this.setRadius(c),this.setIteration(v),this.setFillStyle(a,l,d),this.setStrokeStyle(n,o)}get radius(){return this._radius}set radius(t){this.dirty|=this._radius!=t,this._radius=t}setRadius(t){return this.radius=t,this}get iteration(){return this._iteration}set iteration(t){this.dirty|=this._iteration!=t,this._iteration=t}setIteration(t){return this.iteration=t,this}get fillStyle(){return this._fillStyle}set fillStyle(t){t=c(t,this.canvas,this.context),this.dirty|=this._fillStyle!=t,this._fillStyle=t}get fillColor2(){return this._fillColor2}set fillColor2(t){t=c(t,this.canvas,this.context),this.dirty|=this._fillColor2!=t,this._fillColor2=t}get isHorizontalGradient(){return this._isHorizontalGradient}set isHorizontalGradient(t){this.dirty|=this._isHorizontalGradient!=t,this._isHorizontalGradient=t}setFillStyle(t,i,e){return void 0===e&&(e=!0),this.fillStyle=t,this.fillColor2=i,this.isHorizontalGradient=e,this}get strokeStyle(){return this._strokeStyle}set strokeStyle(t){t=c(t,this.canvas,this.context),this.dirty|=this._strokeStyle!=t,this._strokeStyle=t}get lineWidth(){return this._lineWidth}set lineWidth(t){this.dirty|=this._lineWidth!=t,this._lineWidth=t}setStrokeStyle(t,i){return this.strokeStyle=t,this.lineWidth=i,this}updateTexture(){return super.updateTexture((function(){this.clear(),T.call(this)}),this),this}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).rexroundrectanglecanvas=i();