phaser4-rex-plugins
Version:
2 lines (1 loc) • 4.1 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},o=function(e,t,i,s,o,a){if(null===o||!1===o);else if(n(o))o();else{var l=r(e,i,o);l.exists(s)&&l.remove(s)}var h=e.load;if(a&&h.once(`filecomplete-${i}-${s}`,(function(e,t,i){a(i)})),n(t))t();else{var f=window.URL.createObjectURL(t);h[i](s,f)}h.start()},a={loadFile:function(e,t,i,s,r){var n=this.scene;return o(n,e,t,i,s,r),this},loadFilePromise:function(e,t,i,s){var r=this.scene;return new Promise((function(n,a){o(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,a);const c={dragenter:"dragenter",dragleave:"dragleave",dragover:"dragover",drop:"drop"},d=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 o=document.createElement("div");super(e,t,i,o,v(n,"style",void 0)),this.type="rexFileDropZone",this.resize(s,r),this._files=[],this.setDropEnable(v(n,"dropEnable",!0));var a=v(n,"filters");a&&this.addFilters(a),function(e,t,i,s){var r=d(s,"preventDefault",!1),n=d(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,o,c,{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=[],o=0,a=i.length;o<a;o++){var l=i[o];r(l,i)&&n.push(l)}n.length>0&&this.emit(`drop.${s}`,n)}}),this)}get files(){return this._files}}function b(e,t,i,s,r){var n=new g(this.scene,e,t,i,s,r);return this.scene.add.existing(n),n}Object.assign(g.prototype,f);const m=Phaser.Utils.Objects.GetAdvancedValue,y=Phaser.GameObjects.BuildGameObject;function j(e,t){void 0===e&&(e={}),void 0!==t&&(e.add=t);var i=m(e,"width",void 0),s=m(e,"height",void 0),r=new g(this.scene,0,0,i,s,e);return y(this.scene,r,e),r}var x=function(e){return null==e||""===e||0===e.length};class O extends Phaser.Plugins.BasePlugin{constructor(e){super(e),e.registerGameObject("rexFileDropZone",b,j)}start(){this.game.events.on("destroy",this.destroy,this)}}return function(e,t,i,s){if(void 0===s&&(s="."),"object"==typeof e)if(x(t)){if(null==i)return;"object"==typeof i&&(e=i)}else{"string"==typeof t&&(t=t.split(s));var r=t.pop(),n=function(e,t,i){var s=e;if(x(t));else{var r;"string"==typeof t&&(t=t.split("."));for(var n=0,o=t.length;n<o;n++){var a;null!=s[r=t[n]]&&"object"==typeof s[r]||(a=n===o-1?void 0===i?{}:i:{},s[r]=a),s=s[r]}}return s}(e,t);n[r]=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();