phaser4-rex-plugins
Version:
2 lines (1 loc) • 12.9 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,a=e.defaultRenderNodes;(r.Submitter||a.Submitter).run(i,e,s,0,r.Texturer||a.Texturer,r.Transformer||a.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,a,h,n,o,d){var l=this.scene.sys.textures.getFrame(t,e);if(!l)return this;var u=l.cutWidth,c=l.cutHeight;void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=u),void 0===a&&(a=c),void 0===h&&(h=0),void 0===n&&(n=0),void 0===o&&(o=u),void 0===d&&(d=c);var v=l.cutX+h,g=l.cutY+n;return this.context.drawImage(l.source.image,v,g,o,d,i,s,r,a),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,a){if("number"!=typeof i){var h=i;i=h.red,s=h.green,r=h.blue,a=h.alpha}void 0===a&&(a=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]=a,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 a=i.width,h=i.height;a===this.frame.width&&h===this.frame.height||(this.frame.setSize(a,h),this.frame.source.updateSize(a,h),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 a=this.canvas;return void 0===s?s=a.width:s*=this.resolution,void 0===r?r=a.height:r*=this.resolution,function(t,e,i,s,r,a,h){var n,o=t.sys.textures,d=t.renderer;void 0===s&&(s=0),void 0===r&&(r=0),void 0===a&&(a=e.width),void 0===h&&(h=e.height);var l=(n=o.exists(i)?o.get(i):o.createCanvas(i,a,h)).getSourceImage();l.width!==a&&(l.width=a),l.height!==h&&(l.height=h);var u=l.getContext("2d",{willReadFrequently:!0});u.clearRect(0,0,a,h),u.drawImage(e,s,r,a,h),d.gl&&n&&d.canvasToTexture(l,n.source[0].glTexture,!0,0)}(this.scene,a,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 a=Phaser.Display.Canvas.CanvasPool,h=Phaser.GameObjects.GameObject,n=Phaser.Utils.String.UUID,o=Phaser.Renderer.WebGL.RenderNodes.Defaults.DefaultImageNodes;class d extends h{constructor(t,e,i,s,r,h){void 0===e&&(e=0),void 0===i&&(i=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.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(){a.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 l=Phaser.GameObjects.Components;Phaser.Class.mixin(d,[l.Alpha,l.BlendMode,l.Crop,l.Depth,l.Flip,l.GetBounds,l.Lighting,l.Mask,l.Origin,l.RenderNodes,l.ScrollFactor,l.Tint,l.Transform,l.Visible,t,i,s]);const u=Phaser.Utils.String.Pad;var c=function(t,e,i){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(e,i);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,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=v(t,"x",0),i=v(t,"y",0));var s=this.cornerRadius;s.tl=y(v(t,"tl",void 0),e,i),s.tr=y(v(t,"tr",void 0),e,i),s.bl=y(v(t,"bl",void 0),e,i),s.br=y(v(t,"br",void 0),e,i)}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,e,i){return void 0===t?t={x:e,y:i}:"number"==typeof t&&(t={x:t,y:t}),f(t),t},x=function(t,e){"number"==typeof e?(t.x=e,t.y=e):(t.x=v(e,"x",0),t.y=v(e,"y",0)),f(t)},f=function(t){t.convex=t.x>=0||t.y>=0,t.x=Math.abs(t.x),t.y=Math.abs(t.y)};const m=Phaser.Math.DegToRad;var p=function(t){return!t.hasOwnProperty("convex")||t.convex},b=function(t){return t.x>0&&t.y>0},w=function(t,e,i,s,r,a,h,n,o){if(n&&h>a?h-=360:!n&&h<a&&(h+=360),a=m(a),h=m(h),null==o)t.ellipse(e,i,s,r,0,a,h,n);else for(var d,l,u,c=(h-a)/(o+=1),v=0;v<=o;v++)u=a+c*v,d=e+s*Math.cos(u),l=i+r*Math.sin(u),t.lineTo(d,l)},S=function(t,e,i,s,r,a,h,n,o,d,l,u,c){var v;(function(t,e,i,s,r,a,h){var n,o,d,l,u,c=new g(e,i,s,r,a),v=c.minWidth,y=c.minHeight,x=s>=v?1:s/v,f=r>=y?1:r/y,m=c.cornerRadius;t.save(),t.beginPath(),t.translate(e,i),n=m.tl,b(n)?(o=n.x*x,d=n.y*f,p(n)?w(t,o,d,o,d,180,270,!1,h):w(t,0,0,o,d,90,0,!0,h),l=0,u=d):(t.lineTo(0,0),l=0,u=0),n=m.tr,b(n)?(o=n.x*x,d=n.y*f,p(n)?w(t,s-o,d,o,d,270,360,!1,h):w(t,s,0,o,d,180,90,!0,h)):t.lineTo(s,0),n=m.br,b(n)?(o=n.x*x,d=n.y*f,p(n)?w(t,s-o,r-d,o,d,0,90,!1,h):w(t,s,r,o,d,270,180,!0,h)):t.lineTo(s,r),n=m.bl,b(n)?(o=n.x*x,d=n.y*f,p(n)?w(t,o,r-d,o,d,90,180,!1,h):w(t,0,r,o,d,360,270,!0,h)):t.lineTo(0,r),t.lineTo(l,u),t.closePath(),t.restore()}(e,i,s,r,a,h,c),null!=n)&&(null!=l&&((v=u?e.createLinearGradient(0,0,r,0):e.createLinearGradient(0,0,0,a)).addColorStop(0,n),v.addColorStop(1,l),n=v),e.fillStyle=n,e.fill());null!=o&&d>0&&(e.strokeStyle=o,e.lineWidth=d,e.stroke())},R=function(){!function(t,e,i,s,r,a,h,n){if(null!=e||null!=i){var o=t.canvas.width,d=t.canvas.height;null==i&&(s=0);var l=s/2;o=Math.max(1,o-s),d=Math.max(1,d-s),S(t.canvas,t.context,l,l,o,d,r,e,i,s,a,h,n)}}(this,this.fillStyle,this.strokeStyle,this.lineWidth,this.radius,this.fillColor2,this.isHorizontalGradient,this.iteration)};const _=Phaser.Utils.Objects.GetValue;class T extends d{constructor(t,e,i,s,r,a,h,n,o,d,l,u){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=1),void 0===r&&(r=s),void 0===a&&(a=0),void 0===u&&(u=1),super(t,e,i,s,r,u),this.type="rexRoundRectangleCanvas";var c=_(a,"radius",a),v=_(a,"iteration",void 0);this.setRadius(c),this.setIteration(v),this.setFillStyle(h,d,l),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,e,i){return void 0===i&&(i=!0),this.fillStyle=t,this.fillColor2=e,this.isHorizontalGradient=i,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,e){return this.strokeStyle=t,this.lineWidth=e,this}updateTexture(){return super.updateTexture((function(){this.clear(),R.call(this)}),this),this}}function M(t,e,i,s,r,a,h,n,o,d){var l=new T(this.scene,t,e,i,s,r,a,h,n,o,d);return this.scene.add.existing(l),l}const P=Phaser.Utils.Objects.GetAdvancedValue,C=Phaser.GameObjects.BuildGameObject;function G(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=P(t,"width",void 0),s=P(t,"height",void 0),r=P(t,"radius",void 0),a=P(t,"fillStyle",void 0),h=P(t,"strokeStyle",void 0),n=P(t,"lineWidth",void 0),o=P(t,"fillColor2",void 0),d=P(t,"isHorizontalGradient",!0),l=P(t,"resolution",1),u=new T(this.scene,0,0,i,s,r,a,h,n,o,d,l);return C(this.scene,u,t),u}var z=function(t){return null==t||""===t||0===t.length};class O extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexRoundRectangleCanvas",M,G)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,s){if(void 0===s&&(s="."),"object"==typeof t)if(z(e)){if(null==i)return;"object"==typeof i&&(t=i)}else{"string"==typeof e&&(e=e.split(s));var r=e.pop(),a=function(t,e,i){var s=t;if(z(e));else{var r;"string"==typeof e&&(e=e.split("."));for(var a=0,h=e.length;a<h;a++){var n;null!=s[r=e[a]]&&"object"==typeof s[r]||(n=a===h-1?void 0===i?{}:i:{},s[r]=n),s=s[r]}}return s}(t,e);a[r]=i}}(window,"RexPlugins.GameObjects.RoundRectangleCanvas",T),O},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexroundrectanglecanvasplugin=e();