phaser4-rex-plugins
Version:
2 lines (1 loc) • 8.23 kB
JavaScript
var t,e;t=void 0,e=function(){const t=Phaser.Game;var e=function(e){return e instanceof t};const i=Phaser.Scene;var r,s,h=function(t){return t instanceof i},a=function(t){return null==t||"object"!=typeof t?null:e(t)?t:e(t.game)?t.game:h(t)?t.sys.game:h(t.scene)?t.scene.sys.game:void 0},n=function(t,e,i,h,n){if(void 0===r){var l=(o=t.manager.game,a(o).textures.getFrame("__WHITE"));r=l.cutWidth,s=l.cutHeight}var o;return t.stamp("__WHITE",void 0,e,i,{scaleX:h/r,scaleY:n/s,originX:0,originY:0,erase:!0}),t},l=function(t,e,i,r){this.useDynamicTexture?n(this.texture,t,e,i,r):this.context.clearRect(t,e,i,r)},o=function(t,e,i,r,s){var h=this.context;h.save(),h.translate(t,e),s?r.call(s,this.canvas,h,i):r(this.canvas,h,i),h.restore()},u=function(t,e,i,r,s){var h=this.texture;h.camera.setScroll(-t,-e),s?r.call(s,h,i):r(h,i),h.camera.setScroll(0,0)},c={remove(t){var e=this.getFrameIndex(t);return-1===e||(this.addFrameName(e,void 0),this.texture.remove(t)),this},clear(){for(var t,e=this.frameNames.length;t<e;t++){var i=this.frameNames[t];void 0!==i&&(this.addFrameName(index,void 0),this.texture.remove(i))}return this}},d={draw:function(t,e,i){var r=this.getFrameIndex(t);if(-1===r&&(r=this.getFrameIndex(void 0)),-1===r)return console.warn("Does not have free space."),this;var s=this.getTopLeftPosition(r),h=s.x,a=s.y,n=this.cellPadding,c=h+n,d=a+n;l.call(this,h,a,this.outerCellWidth,this.outerCellHeight);var g={width:this.cellWidth,height:this.cellHeight};return(this.useDynamicTexture?u:o).call(this,c,d,g,e,i),this.texture.add(t,0,c,d,g.width,g.height),this.addFrameName(r,t),this.dirty=!0,this},paste:function(t,e){var i;if(this.useDynamicTexture){var r=function(t){return void 0!==t.displayWidth?t.displayWidth:t.width}(e),s=function(t){return void 0!==t.displayHeight?t.displayHeight:t.height}(e);l=r<=this.cellWidth&&s<=this.cellHeight?1:Math.max(r/this.cellWidth,s/this.cellHeight),i=function(t,i){var h=e.originX,a=e.originY,n=e.scaleX,o=e.scaleY;e.setOrigin(0,0).setScale(l,l),t.draw(e),e.setOrigin(h,a).setScale(n,o),i.width=r/l,i.height=s/l}}else{var h,a,n=e.canvas;if(!n)return console.warn("Can't get canvas of game object."),this;if(r=n.width,s=n.height,r<=this.cellWidth&&s<=this.cellHeight)h=r,a=s;else{var l=Math.max(r/this.cellWidth,s/this.cellHeight);h=r/l,a=s/l}i=function(t,e,i){e.drawImage(n,0,0,h,a),i.width=h,i.height=a}}return this.draw(t,i),this},addEmptyFrame:function(t,e,i){var r;return void 0===e&&(e=this.cellWidth),void 0===i&&(i=this.cellHeight),r=this.useDynamicTexture?function(t,r){r.width=e,r.height=i}:function(t,r,s){s.width=e,s.height=i},this.draw(t,r),this},addToBitmapFont:function(){var t=this.texture.key,e=this.bitmapFontCache.get(t);e||(e={data:{retroFont:!0,font:t,size:this.cellWidth,lineHeight:this.cellHeight,chars:{}},texture:t,frame:null},this.bitmapFontCache.add(t,e));for(var i=e.data.chars,r=this.frameNames,s=0,h=r.length;s<h;s++){var a=r[s];if(void 0!==a){var n=this.texture.get(a),l=n.cutX,o=n.cutY,u=n.cutWidth,c=n.cutHeight;i[a.charCodeAt(0)]={x:l,y:o,width:u,height:c,centerX:l+u/2,centerY:o+c/2,xOffset:0,yOffset:0,xAdvance:u,data:{},kerning:{},u0:n.u0,v0:n.v0,u1:n.u1,v1:n.v1}}}return this}};Object.assign(d,c);const g=Phaser.Utils.Objects.IsPlainObject,m=Phaser.Utils.Objects.GetValue;class v{constructor(t,e,i,r,s,h,n,l){var o,u,c;if(g(e)){var d=e;e=m(d,"key"),i=m(d,"width"),r=m(d,"height"),s=m(d,"cellWidth"),h=m(d,"cellHeight"),c=m(d,"cellPadding",0),o=m(d,"columns"),u=m(d,"rows"),n=m(d,"fillColor"),l=m(d,"useDynamicTexture")}else"boolean"==typeof n&&(l=n,n=void 0);void 0===s&&(s=64),void 0===h&&(h=64),void 0===c&&(c=0),this.scene=t,this.cellWidth=s,this.cellHeight=h,this.cellPadding=c,this.outerCellWidth=s+2*c,this.outerCellHeight=h+2*c,o?i=this.outerCellWidth*o:(void 0===i&&(i=4096),o=Math.floor(i/this.outerCellWidth)),u?r=this.outerCellHeight*u:(void 0===r&&(r=4096),u=Math.floor(r/this.outerCellHeight)),void 0===l&&(l=!1);var v=a(t);if(this.useDynamicTexture=l,this.texture=function(t,e,i,r,s){void 0===s&&(s=!1);var h=(t=a(t)).textures;return h.exists(e)&&h.remove(e),h[s?"addDynamicTexture":"createCanvas"](e,i,r)}(v,e,i,r,l),this.canvas=l?void 0:this.texture.getCanvas(),this.context=l?void 0:this.texture.getContext(),this.bitmapFontCache=v.cache.bitmapFont,void 0!==n)if(l)this.texture.fill(n);else{var f=this.context;f.fillStyle=n,f.fillRect(0,0,this.canvas.width,this.canvas.height)}this.key=e,this.width=i,this.height=r,this.columns=o,this.rows=u,this.totalCount=this.columns*this.rows,this.fillColor=n,this.frameNames=Array(this.totalCount);for(var y=0,x=this.frameNames.length;y<x;y++)this.frameNames[y]=void 0;this.dirty=!1}destroy(){this.scene=void 0,this.texture=void 0,this.canvas=void 0,this.context=void 0,this.frameNames=void 0,this.bitmapFontCache=void 0}getFrameIndex(t){return this.frameNames.indexOf(t)}contains(t){return-1!==this.getFrameIndex(t)}addFrameName(t,e){return this.frameNames[t]=e,this}get isFull(){return-1===this.getFrameIndex(void 0)}getTopLeftPosition(t,e){void 0===e&&(e={});var i=t%this.columns,r=Math.floor(t/this.columns);return e.x=i*(this.cellWidth+2*this.cellPadding),e.y=r*(this.cellHeight+2*this.cellPadding),e}updateTexture(){return this.useDynamicTexture||this.texture.refresh(),this.dirty=!1,this}}Object.assign(v.prototype,d);const f=Phaser.Utils.Objects.IsPlainObject,y=Phaser.Utils.Objects.GetValue;class x{constructor(t,e,i,r,s,h,a,n){var l;this.list=[],f(e)&&(e=y(l=e,"key")),this.keyGenerator=e;var o=this.generateKey();l&&(l.key=o,o=l),this.firstItem=new v(t,o,i,r,s,h,a,n),this.list.push(this.firstItem),this.lastKey=void 0}get scene(){return this.firstItem.scene}get useDynamicTexture(){return this.firstItem.useDynamicTexture}get cellWidth(){return this.firstItem.cellWidth}get cellHeight(){return this.firstItem.cellHeight}get cellPadding(){return this.firstItem.cellPadding}get outerCellWidth(){return this.firstItem.outerCellWidth}get outerCellHeight(){return this.firstItem.outerCellHeight}get width(){return this.firstItem.width}get height(){return this.firstItem.height}get columns(){return this.firstItem.columns}get rows(){return this.firstItem.rows}get totalCount(){return this.firstItem.totalCount}get fillColor(){return this.firstItem.fillColor}generateKey(){var t=this.keyGenerator,e=this.list.length;return"string"==typeof t?`${t}_${e}`:t(e)}getFrameManager(t){for(var e=this.list,i=0,r=this.list.length;i<r;i++){var s=e[i];if(s.contains(t))return s}}getAvailableFrameManager(){for(var t,e=this.list,i=0,r=this.list.length;i<r;i++)if(!(t=e[i]).isFull)return t;return t=new v(this.scene,this.generateKey(),this.width,this.height,this.cellWidth,this.cellHeight,this.fillColor,this.useDynamicTexture),e.push(t),this.lastKey=t.key,t}destroy(){for(var t=this.list,e=0,i=this.list.length;e<i;e++)t[e].destroy();return this}draw(t,e,i){return this.getAvailableFrameManager().draw(t,e,i),this}paste(t,e){return this.getAvailableFrameManager().paste(t,e),this}addEmptyFrame(t,e,i){return this.getAvailableFrameManager().addEmptyFrame(t,e,i),this}updateTexture(){for(var t=this.list,e=0,i=this.list.length;e<i;e++){var r=t[e];r.dirty&&r.updateTexture()}return this}remove(t){for(var e=this.list,i=0,r=this.list.length;i<r;i++){var s=e[i];if(s.contains(t))return s.remove(t),this}return this}clear(){for(var t=this.list,e=0,i=t.length;e<i;e++)t[e].clear();return this}contains(t){for(var e=this.list,i=0,r=e.length;i<r;i++)if(e[i].contains(t))return!0;return!1}getKey(t){var e=this.getFrameManager(t);if(e)return e.key}get keys(){return this.getKeys()}getKeys(){for(var t=[],e=this.list,i=0,r=e.length;i<r;i++)t.push(e[i].key);return t}getTexture(t){var e=this.getFrameManager(t);if(e)return e.texture}getCanvas(t){var e=this.getFrameManager(t);if(e)return e.canvas}getContext(t){var e=this.getFrameManager(t);if(e)return e.context}}class p extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e,i,r,s,h,a,n){return new v(t,e,i,r,s,h,a,n)}addPool(t,e,i,r,s,h,a,n){return new x(t,e,i,r,s,h,a,n)}}return p},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexcanvasframemanagerplugin=e();