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