phaser4-rex-plugins
Version:
2 lines (1 loc) • 3.09 kB
JavaScript
var e,t;e=void 0,t=function(){const e=Phaser.Game;var t=function(t){return t instanceof e};const i=Phaser.Scene;var s=function(e){return e instanceof i},r=function(e,i,r){if(void 0===r)switch(i){case"image":case"svg":r="textures";break;case"animation":r="json";break;case"tilemapTiledJSON":case"tilemapCSV":r="tilemap";break;case"glsl":r="shader";break;default:r=i}var n;return e=null==(n=e)||"object"!=typeof n?null:t(n)?n:t(n.game)?n.game:s(n)?n.sys.game:s(n.scene)?n.scene.sys.game:void 0,"textures"===r?e.textures:e.cache[r]},n=function(e){return e&&"function"==typeof e},a=function(e,t,i,s,a,o){if(null===a||!1===a);else if(n(a))a();else{var l=r(e,i,a);l.exists(s)&&l.remove(s)}var h=e.load;if(o&&h.once(`filecomplete-${i}-${s}`,(function(e,t,i){o(i)})),n(t))t();else{var f=window.URL.createObjectURL(t);h[i](s,f)}h.start()},o={loadFile:function(e,t,i,s,r){var n=this.scene;return a(n,e,t,i,s,r),this},loadFilePromise:function(e,t,i,s){var r=this.scene;return new Promise((function(n,o){a(r,e,t,i,s,(function(e){n(e)}))}))}},l={setDropEnable(e){return void 0===e&&(e=!0),this.dropEnable=e,this},toggleDropEnable(){return this.dropEnable=!this.dropEnable,this}},h={addFilter(e,t){return this.filters||(this.filters={}),this.filters[e]=t,this},addFilters(e){for(var t in this.filters||(this.filters={}),e)this.filters[t]=e[t];return this}},f={resize:function(e,t){if(this.scene.sys.scale.autoRound&&(e=Math.floor(e),t=Math.floor(t)),this.width===e&&this.height===t)return this;var i=this.node.style;return i.width=`${e}px`,i.height=`${t}px`,this.updateSize(),this},syncTo:function(e){return this.setOrigin(e.originX,e.originY),this.setPosition(e.x,e.y),this.resize(e.displayWidth,e.displayHeight),this}};Object.assign(f,l,h,o);const d={dragenter:"dragenter",dragleave:"dragleave",dragover:"dragover",drop:"drop"},c=Phaser.Utils.Objects.GetValue,u=Phaser.GameObjects.DOMElement,p=Phaser.Utils.Objects.IsPlainObject,v=Phaser.Utils.Objects.GetValue;class g extends u{constructor(e,t,i,s,r,n){p(t)?(t=v(n=t,"x",0),i=v(n,"y",0),s=v(n,"width",0),r=v(n,"height",0)):p(s)&&(s=v(n=s,"width",0),r=v(n,"height",0)),void 0===n&&(n={});var a=document.createElement("div");super(e,t,i,a,v(n,"style",void 0)),this.type="rexFileDropZone",this.resize(s,r),this._files=[],this.setDropEnable(v(n,"dropEnable",!0));var o=v(n,"filters");o&&this.addFilters(o),function(e,t,i,s){var r=c(s,"preventDefault",!1),n=c(s,"preTest");for(let s in i)t.addEventListener(s,(function(t){n&&!n(e,s)||e.emit(i[s],e,t),r&&t.preventDefault()}))}(this,a,d,{preventDefault:!0,preTest:e=>e.dropEnable}),this.on("drop",(function(e,t){this._files=t.dataTransfer.files;var i=this._files;if(i&&this.filters)for(var s in this.filters){for(var r=this.filters[s],n=[],a=0,o=i.length;a<o;a++){var l=i[a];r(l,i)&&n.push(l)}n.length>0&&this.emit(`drop.${s}`,n)}}),this)}get files(){return this._files}}return Object.assign(g.prototype,f),g},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexfiledropzone=t();