UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 3.78 kB
var e,t;e=void 0,t=function(e){const t=Phaser.Utils.Objects.GetValue,i=Phaser.Game;var n=function(e){return e instanceof i};const s=Phaser.Scene;var r=function(e){return e instanceof s},o=function(e){return null==e||"object"!=typeof e?null:n(e)?e:n(e.game)?e.game:r(e)?e.sys.game:r(e.scene)?e.scene.sys.game:void 0},l=function({game:e,fileInput:t,closeDelay:i}){return(n=o(e).events,s="focus",new Promise((function(e,t){n.once(s,(function(){e()}))}))).then((function(){return void 0===(e=i)&&(e=0),new Promise((function(i,n){setTimeout((function(){i(t)}),e)}));var e,t})).then((function(){var e={files:t.files};return Promise.resolve(e)}));var n,s};const c=Phaser.Utils.Objects.GetValue,a=Phaser.DOM.RemoveFromDOM;var u=function(e){return e&&"function"==typeof e},h=function(e,t,i,n,s,r){if(null===s||!1===s);else if(u(s))s();else{var l=function(e,t,i){if(void 0===i)switch(t){case"image":case"svg":i="textures";break;case"animation":i="json";break;case"tilemapTiledJSON":case"tilemapCSV":i="tilemap";break;case"glsl":i="shader";break;default:i=t}return e=o(e),"textures"===i?e.textures:e.cache[i]}(e,i,s);l.exists(n)&&l.remove(n)}var c=e.load;if(r&&c.once(`filecomplete-${i}-${n}`,(function(e,t,i){r(i)})),u(t))t();else{var a=window.URL.createObjectURL(t);c[i](n,a)}c.start()},f={loadFile:function(e,t,i,n,s){var r=this.scene;return h(r,e,t,i,n,s),this},loadFilePromise:function(e,t,i,n){var s=this.scene;return new Promise((function(r,o){h(s,e,t,i,n,(function(e){r(e)}))}))}};const p=Phaser.GameObjects.DOMElement,m=Phaser.Utils.Objects.IsPlainObject,d=Phaser.Utils.Objects.GetValue;class v extends p{constructor(e,t,i,n,s,r){m(t)?(t=d(r=t,"x",0),i=d(r,"y",0),n=d(r,"width",0),s=d(r,"height",0)):m(n)&&(n=d(r=n,"width",0),s=d(r,"height",0));var o=document.createElement("input");o.type="file",o.style.display="none";var c=document.createElement("label");c.appendChild(o);var a=d(r,"style",void 0);super(e,t,i,c,a),this.type="rexFileChooser",this.resetFromJSON(r),this.resize(n,s);var u=this;o.onchange=function(){u.emit("change",u)},this.setCloseDelay(d(r,"closeDelay",200)),o.onclick=function(){l({game:e,fileInput:o,closeDelay:u.closeDelay}).then((function(){u.emit("select",u)}))}}resetFromJSON(e){return this.setAccept(d(e,"accept","")),this.setMultiple(d(e,"multiple",!1)),this}setAccept(e){return void 0===e&&(e=""),this.fileInput.setAttribute("accept",e),this}setMultiple(e){return void 0===e&&(e=!0),e?this.fileInput.setAttribute("multiple",""):this.fileInput.removeAttribute("multiple"),this}setCloseDelay(e){return void 0===e&&(e=200),this.closeDelay=e,this}get fileInput(){return this.node.children[0]}open(){return this.fileInput.click(),this}get files(){return this.fileInput.files}setOpenEnable(e){return void 0===e&&(e=!0),this.fileInput.disabled=!e,this}}var y={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(v.prototype,y,f),e.FileChooser=v,e.OpenFileChooser=function(e,i){var n=c(i,"closeDelay",200),s=function(e,i){var n=document.createElement("input");n.type="file";var s=t(i,"accept",""),r=t(i,"multiple",!1);return n.setAttribute("accept",s),r?n.setAttribute("multiple",""):n.removeAttribute("multiple"),n}(0,i);return s.click(),l({game:e,fileInput:s,closeDelay:n}).then((function(e){return a(s),s.remove(),Promise.resolve(e)}))}},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).rexfilechooser={});