@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 8.87 kB
JavaScript
var t,e;t=void 0,e=function(){const t=Phaser.Renderer.WebGL.Utils;var e={renderWebGL:function(e,i,s,r){if(i.dirty&&(i.updateTexture(),i.dirty=!1),0!==i.width&&0!==i.height){s.addToRenderList(i);var a=i.frame,h=a.width,n=a.height,o=t.getTintAppendFloatAlpha,d=e.pipelines.set(i.pipeline,i),c=d.setTexture2D(a.glTexture,i);e.pipelines.preBatch(i),d.batchTexture(i,a.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,r,!1,c),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,r,a,h,n,o,d){var c=this.scene.sys.textures.getFrame(t,e);if(!c)return this;var l=c.cutWidth,u=c.cutHeight;void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=l),void 0===a&&(a=u),void 0===h&&(h=0),void 0===n&&(n=0),void 0===o&&(o=l),void 0===d&&(d=u);var v=c.cutX+h,g=c.cutY+n;return this.context.drawImage(c.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,e,s){void 0===s&&(s=new i);var r=this.context.getImageData(t,e,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}},r={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,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 c=(n=o.exists(i)?o.get(i):o.createCanvas(i,a,h)).getSourceImage();c.width!==a&&(c.width=a),c.height!==h&&(c.height=h);var l=c.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,a,h),l.drawImage(e,s,r,a,h),d.gl&&n&&d.canvasToTexture(c,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}},a=!1;!function(t){if(!a){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}`);a=!0}}();const h=Phaser.Display.Canvas.CanvasPool,n=Phaser.GameObjects.GameObject,o=Phaser.Utils.String.UUID;let d=class extends n{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.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 c=Phaser.GameObjects.Components;Phaser.Class.mixin(d,[c.Alpha,c.BlendMode,c.Crop,c.Depth,c.Flip,c.GetBounds,c.Mask,c.Origin,c.Pipeline,c.PostPipeline,c.ScrollFactor,c.Tint,c.Transform,c.Visible,e,s,r]);var l={loadFromURL(t,e){var i=this,s=new Image;return s.onload=function(){i.width!==s.width||i.height!==s.height?i.resize(s.width,s.height):i.clear(),i.context.drawImage(s,0,0),i.updateTexture(),e&&e(),s.onload=null,s.src="",s.remove()},s.src=t,this},loadFromURLPromise(t){var e=this;return new Promise((function(i,s){e.loadFromURL(t,i)}))},loadFromFile(t,e){var i=URL.createObjectURL(t);return this.loadFromURL(i,(function(){URL.revokeObjectURL(i),e&&e()})),this},loadFromFilePromise(t){var e=this;return new Promise((function(i,s){e.loadFromFile(t,i)}))}};class u extends d{}Object.assign(u.prototype,l);var v=function(t,e){void 0===e&&(e={}),void 0===e.options&&(e.options={});var i=e.options;i.responsive=!1,i.maintainAspectRatio=!1,i.hasOwnProperty("devicePixelRatio")||(i.devicePixelRatio=1);var s=!1;void 0===i.animation?i.animation={}:!1===i.animation&&(s=!0,i.animation={});var r=i.animation;s&&(r.duration=0);var a=r.onProgress;r.onProgress=function(e){a&&a(e),t.needRedraw()};var h=r.onComplete;return r.onComplete=function(e){h&&h(e),t.needRedraw()},e};let g=class extends u{constructor(t,e,i,s,r,a){super(t,e,i,s,r),this.type="rexChart",this.chart=void 0,void 0!==a&&this.setChart(a)}destroy(t){this.scene&&(this.chart&&(this.chart.destroy(),this.chart=void 0),super.destroy(t))}resize(t,e){if(t===this.width&&e===this.height)return this;if(super.resize(t,e),this.chart){var i=this.chart;i.height=this.canvas.height,i.width=this.canvas.width,i.aspectRatio=i.height?i.width/i.height:null,i.update()}return this}};var p={setChart:function(t){return window.Chart?(this.chart&&this.chart.destroy(),this.chart=new Chart(this.context,v(this,t)),this):(console.error("Can not find chartjs! Load chartjs in preload stage.\nscene.load.script('chartjs', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/Chart.min.js');"),this)},getChartDataset:function(t){if(void 0!==this.chart){if("string"!=typeof t)return this.chart.data.datasets[t];for(var e,i=this.chart.data.datasets,s=0,r=i.length;s<r;s++)if((e=i[s]).label===t)return e}},getChartData:function(t,e){var i=this.getChartDataset(t);if(void 0!==i){if("string"==typeof e&&-1===(e=this.chart.data.labels.indexOf(e)))return;return i.data[e]}},setChartData:function(t,e,i){if(void 0===this.chart)return this;var s=this.getChartDataset(t);return"string"==typeof e&&-1===(e=this.chart.data.labels.indexOf(e))||(s.data[e]=i),this},updateChart:function(){return void 0===this.chart||this.chart.update(),this}};return Object.assign(g.prototype,p),g},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexchart=e();