phaser4-rex-plugins
Version:
2 lines (1 loc) • 8.24 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 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===a&&(a=c),void 0===h&&(h=0),void 0===n&&(n=0),void 0===o&&(o=l),void 0===d&&(d=c);var v=u.cutX+h,g=u.cutY+n;return this.context.drawImage(u.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 u=(n=o.exists(i)?o.get(i):o.createCanvas(i,a,h)).getSourceImage();u.width!==a&&(u.width=a),u.height!==h&&(u.height=h);var l=u.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,a,h),l.drawImage(e,s,r,a,h),d.gl&&n&&d.canvasToTexture(u,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 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 extends d{constructor(t,e,i,s,r,a){super(t,e,i),this.type="rexAlphaMaskImage",this.maskFrame=null,this.setTexture(s,r,a)}setTexture(t,e,i){"object"==typeof e&&(i=e,e=void 0),"string"==typeof i&&(i={mask:{key:i}});var s=l(i,"mask.key"),r=l(i,"mask.frame"),a=l(i,"mask.invertAlpha",!1),h=l(i,"mask.scale"),n=l(i,"backgroundColor");if(s){this._maskKey=s,this._maskFrame=r,this._maskScale=h;var o=s?this.scene.sys.textures.get(s):null;this.maskFrame=o?o.get(r):null}this._textureKey=t,this._frameName=e;var d=this.maskFrame;if(null===d)return this.loadTexture(t,e),this.dirty=!0,this;var u=null!=n;this.loadTexture(t,e);var c,v,g=this.canvas,m=this.context,f=g.width,p=g.height;m.save(),m.globalCompositeOperation=a?"destination-out":"destination-in",null!=this._maskScale?(c=d.cutWidth*this._maskScale,v=d.cutHeight*this._maskScale):(c=f,v=p);var x=(f-c)/2,y=(p-v)/2;return this.drawFrame(this._maskKey,this._maskFrame,x,y,c,v),m.restore(),u&&(m.save(),m.globalCompositeOperation="destination-over",m.fillStyle=n,m.fillRect(0,0,f,p),m.restore()),this.dirty=!0,this}resize(t,e){return this.setDisplaySize(t,e),this}}function v(t,e,i,s,r){var a=new c(this.scene,t,e,i,s,r);return this.scene.add.existing(a),a}const g=Phaser.Utils.Objects.GetAdvancedValue,m=Phaser.GameObjects.BuildGameObject;function f(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),r=new c(this.scene,0,0,i,s,t);return m(this.scene,r,t),r}var p=function(t){return null==t||""===t||0===t.length};class x extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexAlphaMaskImage",v,f)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,s){if(void 0===s&&(s="."),"object"==typeof t)if(p(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(p(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.AlphaMaskImage",c),x},"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();