phaser4-rex-plugins
Version:
2 lines (1 loc) • 4.79 kB
JavaScript
var e,t;e=void 0,t=function(){const e=Phaser.Utils.Objects.GetValue,t=Phaser.Game;var i=function(e){return e instanceof t};const n=Phaser.Scene;var s=function(e){return e instanceof n},r=function(e){return null==e||"object"!=typeof e?null:i(e)?e:i(e.game)?e.game:s(e)?e.sys.game:s(e.scene)?e.scene.sys.game:void 0},o=function({game:e,fileInput:t,closeDelay:i}){return(n=r(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 l=Phaser.Utils.Objects.GetValue,c=Phaser.DOM.RemoveFromDOM;var a=function(t,i){var n=l(i,"closeDelay",200),s=function(t,i){var n=document.createElement("input");n.type="file";var s=e(i,"accept",""),r=e(i,"multiple",!1);return n.setAttribute("accept",s),r?n.setAttribute("multiple",""):n.removeAttribute("multiple"),n}(0,i);return s.click(),o({game:t,fileInput:s,closeDelay:n}).then((function(e){return c(s),s.remove(),Promise.resolve(e)}))},u=function(e){return e&&"function"==typeof e},h=function(e,t,i,n,s,o){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=r(e),"textures"===i?e.textures:e.cache[i]}(e,i,s);l.exists(n)&&l.remove(n)}var c=e.load;if(o&&c.once(`filecomplete-${i}-${n}`,(function(e,t,i){o(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,d=Phaser.Utils.Objects.IsPlainObject,v=Phaser.Utils.Objects.GetValue;class m extends p{constructor(e,t,i,n,s,r){d(t)?(t=v(r=t,"x",0),i=v(r,"y",0),n=v(r,"width",0),s=v(r,"height",0)):d(n)&&(n=v(r=n,"width",0),s=v(r,"height",0));var l=document.createElement("input");l.type="file",l.style.display="none";var c=document.createElement("label");c.appendChild(l);var a=v(r,"style",void 0);super(e,t,i,c,a),this.type="rexFileChooser",this.resetFromJSON(r),this.resize(n,s);var u=this;l.onchange=function(){u.emit("change",u)},this.setCloseDelay(v(r,"closeDelay",200)),l.onclick=function(){o({game:e,fileInput:l,closeDelay:u.closeDelay}).then((function(){u.emit("select",u)}))}}resetFromJSON(e){return this.setAccept(v(e,"accept","")),this.setMultiple(v(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 g={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}};function y(e,t,i,n,s){var r=new m(this.scene,e,t,i,n,s);return this.scene.add.existing(r),r}Object.assign(m.prototype,g,f);const b=Phaser.Utils.Objects.GetAdvancedValue,P=Phaser.GameObjects.BuildGameObject;function O(e,t){void 0===e&&(e={}),void 0!==t&&(e.add=t);var i=b(e,"width",void 0),n=b(e,"height",void 0),s=new m(this.scene,0,0,i,n,e);return P(this.scene,s,e),s}var j=function(e){return null==e||""===e||0===e.length};class x extends Phaser.Plugins.BasePlugin{constructor(e){super(e),e.registerGameObject("rexFileChooser",y,O)}start(){this.game.events.on("destroy",this.destroy,this)}open(e){return a(this.game,e)}}return function(e,t,i,n){if(void 0===n&&(n="."),"object"==typeof e)if(j(t)){if(null==i)return;"object"==typeof i&&(e=i)}else{"string"==typeof t&&(t=t.split(n));var s=t.pop(),r=function(e,t,i){var n=e;if(j(t));else{var s;"string"==typeof t&&(t=t.split("."));for(var r=0,o=t.length;r<o;r++){var l;null!=n[s=t[r]]&&"object"==typeof n[s]||(l=r===o-1?void 0===i?{}:i:{},n[s]=l),n=n[s]}}return n}(e,t);r[s]=i}}(window,"RexPlugins.GameObjects.FileChooser",m),x},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).rexfilechooserplugin=t();