UNPKG

@petkoneo/phaser3-rex-plugins

Version:
2 lines (1 loc) 8.55 kB
var t,e;t=void 0,e=function(){const t=Phaser.Renderer.WebGL.Utils;var e={renderWebGL:function(e,i,s,a){if(i.dirty&&(i.updateTexture(),i.dirty=!1),0!==i.width&&0!==i.height){s.addToRenderList(i);var r=i.frame,h=r.width,n=r.height,o=t.getTintAppendFloatAlpha,l=e.pipelines.set(i.pipeline,i),d=l.setTexture2D(r.glTexture,i);e.pipelines.preBatch(i),l.batchTexture(i,r.glTexture,h,n,i.x,i.y,h/i.resolution,n/i.resolution,i.scaleX,i.scaleY,i.rotation,i.flipX,i.flipY,i.scrollFactorX,i.scrollFactorY,i.displayOriginX,i.displayOriginY,0,0,h,n,o(i.tintTopLeft,s.alpha*i._alphaTL),o(i.tintTopRight,s.alpha*i._alphaTR),o(i.tintBottomLeft,s.alpha*i._alphaBL),o(i.tintBottomRight,s.alpha*i._alphaBR),i.tintFill,0,0,s,a,!1,d),e.pipelines.postBatch(i)}},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 i=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,e,i,s,a,r,h,n,o,l){var d=this.scene.sys.textures.getFrame(t,e);if(!d)return this;var c=d.cutWidth,u=d.cutHeight;void 0===i&&(i=0),void 0===s&&(s=0),void 0===a&&(a=c),void 0===r&&(r=u),void 0===h&&(h=0),void 0===n&&(n=0),void 0===o&&(o=c),void 0===l&&(l=u);var v=d.cutX+h,g=d.cutY+n;return this.context.drawImage(d.source.image,v,g,o,l,i,s,a,r),this.dirty=!0,this},getDataURL(t,e){return this.canvas.toDataURL(t,e)},getPixel(t,e,s){void 0===s&&(s=new i);var a=this.context.getImageData(t,e,1,1);return s.setTo(a.data[0],a.data[1],a.data[2],a.data[3]),s},setPixel(t,e,i,s,a,r){if("number"!=typeof i){var h=i;i=h.red,s=h.green,a=h.blue,r=h.alpha}void 0===r&&(r=0!==i||0!==s||0!==a?255:0);var n=this.context.createImageData(1,1);return n.data[0]=i,n.data[1]=s,n.data[2]=a,n.data[3]=r,this.context.putImageData(n,t,e),this.dirty=!0,this}},a={updateTexture(t,e){if(t){var i=this.resolution;1!==i&&(this.context.save(),this.context.scale(i,i)),e?t.call(e,this.canvas,this.context):t(this.canvas,this.context),1!==i&&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,e,i,s,a){var r=this.canvas;return void 0===s?s=r.width:s*=this.resolution,void 0===a?a=r.height:a*=this.resolution,function(t,e,i,s,a,r,h){var n,o=t.sys.textures,l=t.renderer;void 0===s&&(s=0),void 0===a&&(a=0),void 0===r&&(r=e.width),void 0===h&&(h=e.height);var d=(n=o.exists(i)?o.get(i):o.createCanvas(i,r,h)).getSourceImage();d.width!==r&&(d.width=r),d.height!==h&&(d.height=h);var c=d.getContext("2d",{willReadFrequently:!0});c.clearRect(0,0,r,h),c.drawImage(e,s,a,r,h),l.gl&&n&&l.canvasToTexture(d,n.source[0].glTexture,!0,0)}(this.scene,r,t,e,i,s,a),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=60);var e=Phaser.VERSION.split("."),i=parseInt(e[0]);if(3===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,n=Phaser.GameObjects.GameObject,o=Phaser.Utils.String.UUID;class l extends n{constructor(t,e,i,s,a,r){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=1),void 0===a&&(a=1),void 0===r&&(r=1),super(t,"rexCanvas"),this.renderer=t.sys.game.renderer,this._width=s,this._height=a,this.resolution=r,s=Math.max(Math.ceil(s*this.resolution),1),a=Math.max(Math.ceil(a*this.resolution),1),this.canvas=h.create(this,s,a),this.context=this.canvas.getContext("2d",{willReadFrequently:!0}),this.dirty=!1,this.setPosition(e,i),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(){h.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 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.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 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,e,s,a]);const c=Phaser.Utils.Objects.GetValue;class u extends l{constructor(t,e,i,s,a,r){super(t,e,i),this.type="rexAlphaMaskImage",this.maskFrame=null,this.setTexture(s,a,r)}setTexture(t,e,i){"object"==typeof e&&(i=e,e=void 0),"string"==typeof i&&(i={mask:{key:i}});var s=c(i,"mask.key"),a=c(i,"mask.frame"),r=c(i,"mask.invertAlpha",!1),h=c(i,"mask.scale"),n=c(i,"backgroundColor");if(s){this._maskKey=s,this._maskFrame=a,this._maskScale=h;var o=s?this.scene.sys.textures.get(s):null;this.maskFrame=o?o.get(a):null}this._textureKey=t,this._frameName=e;var l=this.maskFrame;if(null===l)return this.loadTexture(t,e),this.dirty=!0,this;var d=null!=n;this.loadTexture(t,e);var u,v,g=this.canvas,p=this.context,m=g.width,x=g.height;p.save(),p.globalCompositeOperation=r?"destination-out":"destination-in",null!=this._maskScale?(u=l.cutWidth*this._maskScale,v=l.cutHeight*this._maskScale):(u=m,v=x);var f=(m-u)/2,y=(x-v)/2;return this.drawFrame(this._maskKey,this._maskFrame,f,y,u,v),p.restore(),d&&(p.save(),p.globalCompositeOperation="destination-over",p.fillStyle=n,p.fillRect(0,0,m,x),p.restore()),this.dirty=!0,this}resize(t,e){return this.setDisplaySize(t,e),this}}function v(t,e,i,s,a){var r=new u(this.scene,t,e,i,s,a);return this.scene.add.existing(r),r}const g=Phaser.Utils.Objects.GetAdvancedValue,p=Phaser.GameObjects.BuildGameObject;function m(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=g(t,"key",void 0),s=g(t,"frame",void 0),a=new u(this.scene,0,0,i,s,t);return p(this.scene,a,t),a}var x=function(t){return null==t||""===t||0===t.length};class f extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexAlphaMaskImage",v,m)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,s){if(void 0===s&&(s="."),"object"==typeof t)if(x(e)){if(null==i)return;"object"==typeof i&&(t=i)}else{"string"==typeof e&&(e=e.split(s));var a=e.pop(),r=function(t,e,i){var s=t;if(x(e));else{var a;"string"==typeof e&&(e=e.split("."));for(var r=0,h=e.length;r<h;r++){var n;null!=s[a=e[r]]&&"object"==typeof s[a]||(n=r===h-1?void 0===i?{}:i:{},s[a]=n),s=s[a]}}return s}(t,e);r[a]=i}}(window,"RexPlugins.GameObjects.AlphaMaskImage",u),f},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexalphamaskimageplugin=e();