phaser4-rex-plugins
Version:
2 lines (1 loc) • 9.38 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,l){var d=this.scene.sys.textures.getFrame(t,e);if(!d)return this;var u=d.cutWidth,c=d.cutHeight;void 0===i&&(i=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 g=d.cutX+a,v=d.cutY+n;return this.context.drawImage(d.source.image,g,v,o,l,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,l=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 d=(n=o.exists(i)?o.get(i):o.createCanvas(i,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(e,s,r,h,a),l.gl&&n&&l.canvasToTexture(d,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 l 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 d=Phaser.GameObjects.Components;Phaser.Class.mixin(l,[d.Alpha,d.BlendMode,d.Crop,d.Depth,d.Flip,d.GetBounds,d.Lighting,d.Mask,d.Origin,d.RenderNodes,d.ScrollFactor,d.Tint,d.Transform,d.Visible,t,i,s]);const u=Phaser.Display.Canvas.CanvasPool;var c=function(t){var e=u.create(this);!function(t,e){e.width=t.cutWidth,e.height=t.cutHeight,e.getContext("2d",{willReadFrequently:!0}).drawImage(t.source.image,t.cutX,t.cutY,t.cutWidth,t.cutHeight)}(t,e);var i=this.context.createPattern(e,"repeat");return u.remove(e),i},g=function(){if(this.fillPattern){this.canvas;var t=this.context,e=this.tilePositionX,i=this.tilePositionY,s=this.tileScaleX,r=this.tileScaleY;t.save(),t.scale(s,r),t.translate(-e,-i),t.fillStyle=this.fillPattern,t.fillRect(e,i,this.width/s,this.height/r),t.restore()}};class v extends l{constructor(t,e,i,s,r,h,a){var n=t.sys.textures.get(h),o=n.get(a);o.source.compressionAlgorithm&&(console.warn("RepeatImage cannot use compressed texture"),o=(n=t.sys.textures.get("__MISSING")).get()),"DynamicTexture"===n.type&&(console.warn("RepeatImage cannot use Dynamic Texture"),o=(n=t.sys.textures.get("__MISSING")).get()),s&&r?(s=Math.floor(s),r=Math.floor(r)):(s=o.width,r=o.height),super(t,e,i,s,r),this.type="rexRepeatImage",this._tilePositionX=0,this._tilePositionY=0,this._tileScaleX=1,this._tileScaleY=1,this.fillPattern=null,this.setTexture(h,a)}setTexture(t,e){if(this._textureKey===t&&this._frameName===e)return this;this.dirty=!0,this._textureKey=t,this._frameName=e;var i=this.scene.sys.textures.getFrame(t,e);return i?(this.fillPattern=c.call(this,i),this):(this.fillPattern=null,this)}setFrame(t){return this.setTexture(this._textureKey,t),this}get tilePositionX(){return this._tilePositionX}set tilePositionX(t){this._tilePositionX!==t&&(this.dirty=!0,this._tilePositionX=t)}get tilePositionY(){return this._tilePositionY}set tilePositionY(t){this._tilePositionY!==t&&(this.dirty=!0,this._tilePositionY=t)}setTilePosition(t,e){return this.tilePositionX=t,this.tilePositionY=e,this}get tileScaleX(){return this._tileScaleX}set tileScaleX(t){this._tileScaleX!==t&&(this.dirty=!0,this._tileScaleX=t)}get tileScaleY(){return this._tileScaleY}set tileScaleY(t){this._tileScaleY!==t&&(this.dirty=!0,this._tileScaleY=t)}setTileScale(t,e){return void 0===e&&(e=t),this.tileScaleX=t,this.tileScaleY=e,this}updateTexture(){return this.clear(),g.call(this),super.updateTexture(),this}preDestroy(){this.fillPattern=null,super.preDestroy()}}function f(t,e,i,s,r,h){var a=new v(this.scene,t,e,i,s,r,h);return this.scene.add.existing(a),a}const m=Phaser.Utils.Objects.GetAdvancedValue,x=Phaser.GameObjects.BuildGameObject;function p(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=m(t,"width",void 0),s=m(t,"height",void 0),r=m(t,"key",void 0),h=m(t,"frame",void 0),a=new v(this.scene,0,0,i,s,r,h);return x(this.scene,a,t),a}var y=function(t){return null==t||""===t||0===t.length};class w extends Phaser.Plugins.BasePlugin{constructor(t){super(t),t.registerGameObject("rexRepeatImage",f,p)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(t,e,i,s){if(void 0===s&&(s="."),"object"==typeof t)if(y(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(y(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.RepeatImage",v),w},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexrepeatimageplugin=e();