UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 4.14 kB
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 r=function(e){return e instanceof i},s=function(e,i,s){if(void 0===s)switch(i){case"image":case"svg":s="textures";break;case"animation":s="json";break;case"tilemapTiledJSON":case"tilemapCSV":s="tilemap";break;case"glsl":s="shader";break;default:s=i}var n;return e=null==(n=e)||"object"!=typeof n?null:t(n)?n:t(n.game)?n.game:r(n)?n.sys.game:r(n.scene)?n.scene.sys.game:void 0,"textures"===s?e.textures:e.cache[s]},n=function(e){return e&&"function"==typeof e},a=function(e,t,i,r,a,o){if(null===a||!1===a);else if(n(a))a();else{var l=s(e,i,a);l.exists(r)&&l.remove(r)}var h=e.load;if(o&&h.once(`filecomplete-${i}-${r}`,(function(e,t,i){o(i)})),n(t))t();else{var f=window.URL.createObjectURL(t);h[i](r,f)}h.start()},o={loadFile:function(e,t,i,r,s){var n=this.scene;return a(n,e,t,i,r,s),this},loadFilePromise:function(e,t,i,r){var s=this.scene;return new Promise((function(n,o){a(s,e,t,i,r,(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 u={dragenter:"dragenter",dragleave:"dragleave",dragover:"dragover",drop:"drop"},c=Phaser.Utils.Objects.GetValue,d=Phaser.GameObjects.DOMElement,p=Phaser.Utils.Objects.IsPlainObject,v=Phaser.Utils.Objects.GetValue;class g extends d{constructor(e,t,i,r,s,n){p(t)?(t=v(n=t,"x",0),i=v(n,"y",0),r=v(n,"width",0),s=v(n,"height",0)):p(r)&&(r=v(n=r,"width",0),s=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(r,s),this._files=[],this.setDropEnable(v(n,"dropEnable",!0));var o=v(n,"filters");o&&this.addFilters(o),function(e,t,i,r){var s=c(r,"preventDefault",!1),n=c(r,"preTest");for(let r in i)t.addEventListener(r,(function(t){n&&!n(e,r)||e.emit(i[r],e,t),s&&t.preventDefault()}))}(this,a,u,{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 r in this.filters){for(var s=this.filters[r],n=[],a=0,o=i.length;a<o;a++){var l=i[a];s(l,i)&&n.push(l)}n.length>0&&this.emit(`drop.${r}`,n)}}),this)}get files(){return this._files}}function b(e,t,i,r,s){var n=new g(this.scene,e,t,i,r,s);return this.scene.add.existing(n),n}Object.assign(g.prototype,f);const m=Phaser.Utils.Objects.GetAdvancedValue,y=Phaser.GameObjects.BuildGameObject;function x(e,t){void 0===e&&(e={}),void 0!==t&&(e.add=t);var i=m(e,"width",void 0),r=m(e,"height",void 0),s=new g(this.scene,0,0,i,r,e);return y(this.scene,s,e),s}var j=function(e){return null!==e&&"object"==typeof e};class O extends Phaser.Plugins.BasePlugin{constructor(e){super(e),e.registerGameObject("rexFileDropZone",b,x)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(e,t,i,r="."){if(!j(e))return e;if(function(e){return null==e}(t)||""===t||Array.isArray(t)&&0===t.length)return e;if("string"==typeof t&&-1===t.indexOf(r)||"number"==typeof t)return e[t]=i,e;var s=function(e,t){return Array.isArray(e)||(e="string"!=typeof e||""===e.trim()?[]:e.split(t).filter(Boolean)),e}(t,r);if(0===s.length)return e;for(var n=e,a=s.length,o=0;o<a-1;o++){var l=s[o],h=n[l];j(h)||(n[l]={}),n=n[l]}n[s[a-1]]=i}(window,"RexPlugins.GameObjects.FileDropZone",g),O},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexfiledropzoneplugin=t();