phaser3-rex-plugins
Version:
2 lines (1 loc) • 10.7 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).rexalphamaskimageplugin=e();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(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,s=void 0,"symbol"==typeof(s=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"))?s:String(s)),r);}var n,s;}function r(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function n(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 s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function a(t,e){return a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},a(t,e)}function o(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(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=s(t);if(e){var n=s(this).constructor;i=Reflect.construct(r,arguments,n);}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 o(t)}(this,i)}}var u,c=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,s=n.width,a=n.height,o=c.getTintAppendFloatAlpha,h=t.pipelines.set(e.pipeline,e),u=h.setTexture2D(n.glTexture,e);t.pipelines.preBatch(e),h.batchTexture(e,n.glTexture,s,a,e.x,e.y,s/e.resolution,a/e.resolution,e.scaleX,e.scaleY,e.rotation,e.flipX,e.flipY,e.scrollFactorX,e.scrollFactorY,e.displayOriginX,e.displayOriginY,0,0,s,a,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,u),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,s,a,o,h,u){var c=this.scene.sys.textures.getFrame(t,e);if(!c)return this;var l=c.cutWidth,d=c.cutHeight;void 0===i&&(i=0),void 0===r&&(r=0),void 0===n&&(n=l),void 0===s&&(s=d),void 0===a&&(a=0),void 0===o&&(o=0),void 0===h&&(h=l),void 0===u&&(u=d);var f=c.cutX+a,v=c.cutY+o;return this.context.drawImage(c.source.image,f,v,h,u,i,r,n,s),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,s){if("number"!=typeof i){var a=i;i=a.red,r=a.green,n=a.blue,s=a.alpha;}void 0===s&&(s=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]=s,this.context.putImageData(o,t,e),this.dirty=!0,this}},v={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 s=this.canvas;return void 0===r?r=s.width:r*=this.resolution,void 0===n?n=s.height:n*=this.resolution,function(t,e,i,r,n,s,a){var o,h=t.sys.textures,u=t.renderer;void 0===r&&(r=0),void 0===n&&(n=0),void 0===s&&(s=e.width),void 0===a&&(a=e.height);var c=(o=h.exists(i)?h.get(i):h.createCanvas(i,s,a)).getSourceImage();c.width!==s&&(c.width=s),c.height!==a&&(c.height=a);var l=c.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,s,a),l.drawImage(e,r,n,s,a),u.gl&&o&&u.canvasToTexture(c,o.source[0].glTexture,!0,0);}(this.scene,s,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}},p=!1;p||(void 0===u&&(u=60),parseInt(Phaser.VERSION.match(/\.(\d+)\./)[1])<u&&console.error("Minimum supported version : 3.".concat(u)),p=!0);var y=Phaser.Display.Canvas.CanvasPool,g=function(t){n(s,t);var i=h(s);function s(t,r,n,a,h){var u;return e(this,s),void 0===r&&(r=0),void 0===n&&(n=0),void 0===a&&(a=1),void 0===h&&(h=1),(u=i.call(this,t,"rexCanvas")).renderer=t.sys.game.renderer,u.resolution=1,u._width=a,u._height=h,a=Math.max(Math.ceil(a*u.resolution),1),h=Math.max(Math.ceil(h*u.resolution),1),u.canvas=y.create(o(u),a,h),u.context=u.canvas.getContext("2d",{willReadFrequently:!0}),u.dirty=!1,u.setPosition(r,n),u.setOrigin(.5,.5),u.initPipeline(),u.initPostPipeline(!0),u._crop=u.resetCropObject(),u.texture=t.sys.textures.addCanvas(null,u.canvas,!0),u.frame=u.texture.get(),u.frame.source.resolution=u.resolution,u.renderer&&u.renderer.gl&&(u.renderer.deleteTexture(u.frame.source.glTexture),u.frame.source.glTexture=null),u.dirty=!0,u}return r(s,[{key:"preDestroy",value:function(){y.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}}]),s}(Phaser.GameObjects.GameObject),m=Phaser.GameObjects.Components;Phaser.Class.mixin(g,[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,v]);var x=Phaser.Utils.Objects.GetValue,b=function(i){n(a,i);var s=h(a);function a(t,i,r,n,o,h){var u;return e(this,a),(u=s.call(this,t,i,r)).type="rexAlphaMaskImage",u.maskFrame=null,u.setTexture(n,o,h),u}return r(a,[{key:"setTexture",value:function(e,i,r){"object"===t(i)&&(r=i,i=void 0),"string"==typeof r&&(r={mask:{key:r}});var n=x(r,"mask.key"),s=x(r,"mask.frame"),a=x(r,"mask.invertAlpha",!1),o=x(r,"mask.scale"),h=x(r,"backgroundColor");if(n){this._maskKey=n,this._maskFrame=s,this._maskScale=o;var u=n?this.scene.sys.textures.get(n):null;this.maskFrame=u?u.get(s):null;}this._textureKey=e,this._frameName=i;var c=this.maskFrame;if(null===c)return this.loadTexture(e,i),this.dirty=!0,this;var l=null!=h;this.loadTexture(e,i);var d,f,v=this.canvas,p=this.context,y=v.width,g=v.height;p.save(),p.globalCompositeOperation=a?"destination-out":"destination-in",null!=this._maskScale?(d=c.cutWidth*this._maskScale,f=c.cutHeight*this._maskScale):(d=y,f=g);var m=(y-d)/2,b=(g-f)/2;return this.drawFrame(this._maskKey,this._maskFrame,m,b,d,f),p.restore(),l&&(p.save(),p.globalCompositeOperation="destination-over",p.fillStyle=h,p.fillRect(0,0,y,g),p.restore()),this.dirty=!0,this}},{key:"resize",value:function(t,e){return this.setDisplaySize(t,e),this}}]),a}(g);function w(t,e,i,r,n){var s=new b(this.scene,t,e,i,r,n);return this.scene.add.existing(s),s}var k=Phaser.Utils.Objects.GetAdvancedValue,T=Phaser.GameObjects.BuildGameObject;function _(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=k(t,"key",void 0),r=k(t,"frame",void 0),n=new b(this.scene,0,0,i,r,t);return T(this.scene,n,t),n}var P=function(t){return null==t||""===t||0===t.length},O=function(t){n(s,Phaser.Plugins.BasePlugin);var i=h(s);function s(t){var r;return e(this,s),r=i.call(this,t),t.registerGameObject("rexAlphaMaskImage",w,_),r}return r(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}}]),s}();return function(e,i,r,n){if(void 0===n&&(n="."),"object"===t(e)){if(P(i)){if(null==r)return;"object"===t(r)&&(e=r);}else {"string"==typeof i&&(i=i.split(n));var s=i.pop(),a=function(e,i,r){var n=e;if(P(i));else {var s;"string"==typeof i&&(i=i.split("."));for(var a=0,o=i.length;a<o;a++){var h;null!=n[s=i[a]]&&"object"===t(n[s])||(h=a===o-1?void 0===r?{}:r:{},n[s]=h),n=n[s];}}return n}(e,i);a[s]=r;}}}(window,"RexPlugins.GameObjects.AlphaMaskImage",b),O}));