phaser3-rex-plugins
Version:
2 lines (1 loc) • 11.2 kB
JavaScript
!function(t,e){"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();}(undefined,(function(){function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var i=0;i<e.length;i++){var r=e[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(n=r.key,a=void 0,"symbol"==typeof(a=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var r=i.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(n,"string"))?a:String(a)),r);}var n,a;}function i(t,i,r){return i&&e(t.prototype,i),r&&e(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&a(t,e);}function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},n(t)}function a(t,e){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},a(t,e)}function s(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function o(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var i,r=n(t);if(e){var a=n(this).constructor;i=Reflect.construct(r,arguments,a);}else i=r.apply(this,arguments);return function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return s(t)}(this,i)}}function h(){return h="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,i){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=n(t)););return t}(t,e);if(r){var a=Object.getOwnPropertyDescriptor(r,e);return a.get?a.get.call(arguments.length<3?t:i):a.value}},h.apply(this,arguments)}var c,u=Phaser.Renderer.WebGL.Utils,l={renderWebGL:function(t,e,i,r){if(e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height){i.addToRenderList(e);var n=e.frame,a=n.width,s=n.height,o=u.getTintAppendFloatAlpha,h=t.pipelines.set(e.pipeline,e),c=h.setTexture2D(n.glTexture,e);t.pipelines.preBatch(e),h.batchTexture(e,n.glTexture,a,s,e.x,e.y,a/e.resolution,s/e.resolution,e.scaleX,e.scaleY,e.rotation,e.flipX,e.flipY,e.scrollFactorX,e.scrollFactorY,e.displayOriginX,e.displayOriginY,0,0,a,s,o(e.tintTopLeft,i.alpha*e._alphaTL),o(e.tintTopRight,i.alpha*e._alphaTR),o(e.tintBottomLeft,i.alpha*e._alphaBL),o(e.tintBottomRight,i.alpha*e._alphaBR),e.tintFill,0,0,i,r,!1,c),t.pipelines.postBatch(e);}},renderCanvas:function(t,e,i,r){e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height&&(i.addToRenderList(e),t.batchSprite(e,e.frame,i,r));}},d=Phaser.Display.Color,f={clear:function(){return this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},fill:function(t){return this.context.fillStyle=t,this.context.fillRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},drawFrame:function(t,e,i,r,n,a,s,o,h,c){var u=this.scene.sys.textures.getFrame(t,e);if(!u)return this;var l=u.cutWidth,d=u.cutHeight;void 0===i&&(i=0),void 0===r&&(r=0),void 0===n&&(n=l),void 0===a&&(a=d),void 0===s&&(s=0),void 0===o&&(o=0),void 0===h&&(h=l),void 0===c&&(c=d);var f=u.cutX+s,p=u.cutY+o;return this.context.drawImage(u.source.image,f,p,h,c,i,r,n,a),this.dirty=!0,this},getDataURL:function(t,e){return this.canvas.toDataURL(t,e)},getPixel:function(t,e,i){void 0===i&&(i=new d);var r=this.context.getImageData(t,e,1,1);return i.setTo(r.data[0],r.data[1],r.data[2],r.data[3]),i},setPixel:function(t,e,i,r,n,a){if("number"!=typeof i){var s=i;i=s.red,r=s.green,n=s.blue,a=s.alpha;}void 0===a&&(a=0!==i||0!==r||0!==n?255:0);var o=this.context.createImageData(1,1);return o.data[0]=i,o.data[1]=r,o.data[2]=n,o.data[3]=a,this.context.putImageData(o,t,e),this.dirty=!0,this}},p={updateTexture:function(t,e){t&&(e?t.call(e,this.canvas,this.context):t(this.canvas,this.context)),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=this.frame.source.glTexture),this.dirty=!1;var i=this.input;return i&&!i.customHitArea&&(i.hitArea.width=this.width,i.hitArea.height=this.height),this},generateTexture:function(t,e,i,r,n){var a=this.canvas;return void 0===r?r=a.width:r*=this.resolution,void 0===n?n=a.height:n*=this.resolution,function(t,e,i,r,n,a,s){var o,h=t.sys.textures,c=t.renderer;void 0===r&&(r=0),void 0===n&&(n=0),void 0===a&&(a=e.width),void 0===s&&(s=e.height);var u=(o=h.exists(i)?h.get(i):h.createCanvas(i,a,s)).getSourceImage();u.width!==a&&(u.width=a),u.height!==s&&(u.height=s);var l=u.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,a,s),l.drawImage(e,r,n,a,s),c.gl&&o&&c.canvasToTexture(u,o.source[0].glTexture,!0,0);}(this.scene,a,t,e,i,r,n),this},loadTexture:function(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}},v=!1;v||(void 0===c&&(c=60),parseInt(Phaser.VERSION.match(/\.(\d+)\./)[1])<c&&console.error("Minimum supported version : 3.".concat(c)),v=!0);var g=Phaser.Display.Canvas.CanvasPool,y=function(e){r(a,e);var n=o(a);function a(e,i,r,o,h){var c;return t(this,a),void 0===i&&(i=0),void 0===r&&(r=0),void 0===o&&(o=1),void 0===h&&(h=1),(c=n.call(this,e,"rexCanvas")).renderer=e.sys.game.renderer,c.resolution=1,c._width=o,c._height=h,o=Math.max(Math.ceil(o*c.resolution),1),h=Math.max(Math.ceil(h*c.resolution),1),c.canvas=g.create(s(c),o,h),c.context=c.canvas.getContext("2d",{willReadFrequently:!0}),c.dirty=!1,c.setPosition(i,r),c.setOrigin(.5,.5),c.initPipeline(),c.initPostPipeline(!0),c._crop=c.resetCropObject(),c.texture=e.sys.textures.addCanvas(null,c.canvas,!0),c.frame=c.texture.get(),c.frame.source.resolution=c.resolution,c.renderer&&c.renderer.gl&&(c.renderer.deleteTexture(c.frame.source.glTexture),c.frame.source.glTexture=null),c.dirty=!0,c}return i(a,[{key:"preDestroy",value:function(){g.remove(this.canvas),this.texture.destroy(),this.canvas=null,this.context=null;}},{key:"width",get:function(){return this._width},set:function(t){this.setSize(t,this._height);}},{key:"height",get:function(){return this._height},set:function(t){this.setSize(this._width,t);}},{key:"setCanvasSize",value:function(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}},{key:"setSize",value:function(t,e){return this.setCanvasSize(t,e),this}},{key:"displayWidth",get:function(){return this.scaleX*this._width},set:function(t){this.scaleX=t/this._width;}},{key:"displayHeight",get:function(){return this.scaleY*this._height},set:function(t){this.scaleY=t/this._height;}},{key:"setDisplaySize",value:function(t,e){return this.displayWidth=t,this.displayHeight=e,this}},{key:"getCanvas",value:function(t){return t||(this.dirty=!0),this.canvas}},{key:"getContext",value:function(t){return t||(this.dirty=!0),this.context}},{key:"needRedraw",value:function(){return this.dirty=!0,this}},{key:"resize",value:function(t,e){return this.setSize(t,e),this}}]),a}(Phaser.GameObjects.GameObject),m=Phaser.GameObjects.Components;Phaser.Class.mixin(y,[m.Alpha,m.BlendMode,m.Crop,m.Depth,m.Flip,m.GetBounds,m.Mask,m.Origin,m.Pipeline,m.PostPipeline,m.ScrollFactor,m.Tint,m.Transform,m.Visible,l,f,p]);var w={loadFromURL:function(t,e){var i=this,r=new Image;return r.onload=function(){i.width!==r.width||i.height!==r.height?i.resize(r.width,r.height):i.clear(),i.context.drawImage(r,0,0),i.updateTexture(),e&&e(),r.onload=null,r.src="",r.remove();},r.src=t,this},loadFromURLPromise:function(t){var e=this;return new Promise((function(i,r){e.loadFromURL(t,i);}))},loadFromFile:function(t,e){var i=URL.createObjectURL(t);return this.loadFromURL(i,(function(){URL.revokeObjectURL(i),e&&e();})),this},loadFromFilePromise:function(t){var e=this;return new Promise((function(i,r){e.loadFromFile(t,i);}))}},x=function(e){r(a,e);var n=o(a);function a(){return t(this,a),n.apply(this,arguments)}return i(a)}(y);Object.assign(x.prototype,w);var b=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 r=!1;void 0===i.animation?i.animation={}:!1===i.animation&&(r=!0,i.animation={});var n=i.animation;r&&(n.duration=0);var a=n.onProgress;n.onProgress=function(e){a&&a(e),t.needRedraw();};var s=n.onComplete;return n.onComplete=function(e){s&&s(e),t.needRedraw();},e},P=function(e){r(s,e);var a=o(s);function s(e,i,r,n,o,h){var c;return t(this,s),(c=a.call(this,e,i,r,n,o)).type="rexChart",c.chart=void 0,void 0!==h&&c.setChart(h),c}return i(s,[{key:"destroy",value:function(t){this.scene&&(this.chart&&(this.chart.destroy(),this.chart=void 0),h(n(s.prototype),"destroy",this).call(this,t));}},{key:"resize",value:function(t,e){if(t===this.width&&e===this.height)return this;if(h(n(s.prototype),"resize",this).call(this,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}}]),s}(x),R={setChart:function(t){if(!window.Chart){return 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}return this.chart&&this.chart.destroy(),this.chart=new Chart(this.context,b(this,t)),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,r=0,n=i.length;r<n;r++)if((e=i[r]).label===t)return e}},getChartData:function(t,e){var i=this.getChartDataset(t);if(void 0!==i){if("string"==typeof e)if(-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 r=this.getChartDataset(t);if("string"==typeof e&&-1===(e=this.chart.data.labels.indexOf(e)))return this;return r.data[e]=i,this},updateChart:function(){return void 0===this.chart||this.chart.update(),this}};return Object.assign(P.prototype,R),P}));