UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 12.2 kB
var t,e;t=void 0,e=function(){var t={setEventEmitter(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),this._privateEE=!0===t||void 0===t,this._eventEmitter=this._privateEE?new e:t,this},destroyEventEmitter(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter(){return this._eventEmitter},on(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}};const e=Phaser.Scene;var s=function(t){return t instanceof e};const i=Phaser.Game;var n=function(t){return t instanceof i},r=function(t){return null==t||"object"!=typeof t?null:n(t)?t:n(t.game)?t.game:s(t)?t.sys.game:s(t.scene)?t.scene.sys.game:void 0};const o=Phaser.Utils.Objects.GetValue;class h{constructor(t,e){this.setParent(t),this.isShutdown=!1,this.setEventEmitter(o(e,"eventEmitter",!0)),this.parent&&(this.parent===this.scene?this.scene.sys.events.once("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.once("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.once("destroy",this.onParentDestroy,this))}shutdown(t){this.isShutdown||(this.parent&&(this.parent===this.scene?this.scene.sys.events.off("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.off("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.off("destroy",this.onParentDestroy,this)),this.destroyEventEmitter(),this.parent=void 0,this.scene=void 0,this.game=void 0,this.isShutdown=!0)}destroy(t){this.shutdown(t)}onEnvDestroy(){this.destroy(!0)}onParentDestroy(t,e){this.destroy(e)}setParent(t){var e;return this.parent=t,this.scene=null==(e=t)||"object"!=typeof e?null:s(e)?e:e.scene&&s(e.scene)?e.scene:e.parent&&e.parent.scene&&s(e.parent.scene)?e.parent.scene:null,this.game=r(t),this}}Object.assign(h.prototype,t);const a={maxLength:["maxLength",void 0],minLength:["minLength",void 0],readOnly:["readOnly",!1]},d={direction:["direction",void 0]};var l=function(t,e,s){if("string"==typeof s)t.hasOwnProperty(s)&&(e[s]=t[s]);else{var i=s;if(Array.isArray(i))for(var n=0,r=i.length;n<r;n++)l(t,e,i[n]);else for(var s in i)l(t,e,s)}},u=function(t,e){for(var s=t.scene,i=s.input.cameras.getCamerasBelowPointer(e),n=s.input.manager,r=[t],o=0,h=i.length;o<h;o++)if(n.hitTest(e,r,i[o],c),c.length>0)return c.length=0,!0;return c.length=0,!1},c=[],p=void 0;const v=Phaser.Utils.Objects.GetValue;var f=function(t,e,s){var i,n;for(var r in void 0===s&&(s={}),t)i=t[r],void 0!==(n=v(e,r,i[1]))&&(s[i[0]]=n);return s},m=function(t){t.stopPropagation()},g=function(){return this.close(),this.emit("keydown-ENTER",this.parent,this),this};const y=Phaser.Utils.Array,E=function(t){var e;return t.parentContainer?-1===(e=t.parentContainer.list).indexOf(this)&&t.parentContainer.add(this):t.displayList&&-1===(e=t.displayList.list).indexOf(this)&&t.displayList.add(this),e?(y.MoveBelow(e,this,t),this):this},C=function(t){var e;return t.parentContainer?-1===(e=t.parentContainer.list).indexOf(this)&&(t.isRexContainerLite?t.addToContainer(t.parentContainer):t.parentContainer.add(t)):t.displayList&&-1===(e=t.displayList.list).indexOf(this)&&(t.isRexContainerLite?t.addToLayer(t.displayList):t.displayList.add(t)),e?(y.MoveAbove(e,this,t),this):this};var x=function(){this.isOpened=!0,this.initText(),this.enterCloseEnable&&this.scene.input.keyboard.once("keydown-ENTER",g,this),this.scene.sys.events.on("postupdate",this.updateText,this),this.clickOutSideTarget?(C.call(this.clickOutSideTarget,this.parent),E.call(this.clickOutSideTarget,this.parent),this.clickOutSideTarget.setInteractive().on("pointerdown",this.onClickOutside,this)):this.scene.input.on("pointerdown",this.onClickOutside,this),this.onOpenCallback&&this.onOpenCallback(this.parent,this),this.emit("open",this)},T=function(){this.isOpened=!1,this.updateText(),this.enterCloseEnable&&this.scene.input.keyboard.off("keydown-ENTER",g,this),this.scene.sys.events.off("postupdate",this.updateText,this),this.clickOutSideTarget?this.clickOutSideTarget.disableInteractive().off("pointerdown",this.onClickOutside,this):this.scene.input.off("pointerdown",this.onClickOutside,this),this.onCloseCallback&&this.onCloseCallback(this.parent,this),function(t){if(t){var e=t.parentElement;e&&e.removeChild(t)}}(this.node),this.node=void 0,this.emit("close",this)};const O=Phaser.Utils.Objects.GetValue;var b=function(t,e){var s,i=O(e,"inputType",void 0);void 0===i&&(i=O(e,"type","text")),"textarea"===i?(s=document.createElement("textarea")).style.resize="none":(s=document.createElement("input")).type=i;var n=O(e,"style",void 0),r=s.style;f(d,n,r),r.position="absolute",r.opacity=0,r.pointerEvents="none",r.zIndex=0,r.transform="scale(0)",f(a,e,s),function(t){t.addEventListener("touchstart",m,!1),t.addEventListener("touchmove",m,!1),t.addEventListener("touchend",m,!1),t.addEventListener("mousedown",m,!1),t.addEventListener("mouseup",m,!1),t.addEventListener("mousemove",m,!1)}(s);var o=t.scene.sys.scale;return(o.isFullscreen?o.fullscreenTarget:document.body).appendChild(s),s.addEventListener("focus",(function(e){x.call(t)})),s.addEventListener("blur",(function(e){T.call(t)})),s},L={open:function(){return this.isOpened||this.readOnly||((t=this)!==p&&(void 0!==p&&p.close(),p=t),this.node||(this.node=b(this,this.nodeConfig)),this.setFocus()),this;var t},close:function(){return this.isOpened?(this===p&&(p=void 0),this.setBlur(),this):this}};const _=Phaser.Utils.Objects.GetValue;class k extends h{constructor(t,e){super(t);var s=_(e,"inputType",void 0);void 0===s&&(s=_(e,"type","text")),this.setEnterCloseEnable(_(e,"enterClose","textarea"!==s));var i=_(e,"onOpen",void 0);i||(i=_(e,"onFocus",void 0)),this.onOpenCallback=i,this.clickOutSideTarget=_(e,"clickOutSideTarget",void 0);var n=_(e,"onClose",void 0);n||(n=_(e,"onBlur",void 0)),this.onCloseCallback=n,this.onUpdateCallback=_(e,"onUpdate",void 0),this.isOpened=!1,t.on("pointerdown",(function(){this.open()}),this).setInteractive(),this.nodeConfig=function(t){void 0===t&&(t={});var e={};return l(t,e,"inputType"),l(t,e,"type"),l(t,e,"style"),l(t,e,d),l(t,e,a),e}(e),this.node=void 0}destroy(){this.close(),this.clickOutSideTarget&&this.clickOutSideTarget.destroy(),super.destroy()}onClickOutside(t){(function(t,e,s,i,n){if(e)return!(s&&!s(t,e)||!u(t,e)||i&&!i(t,e));void 0===n&&(n=!1);for(var r=t.scene.input.manager,o=r.pointersTotal,h=r.pointers,a=0;a<o;a++)if(e=h[a],(!s||s(t,e))&&u(t,e)&&(!i||i(t,e)))return!n||e;return!1})(this.parent,t)||this.close()}setEnterCloseEnable(t){return void 0===t&&(t=!0),this.enterCloseEnable=t,this}initText(){}updateText(){}get text(){return this.node?this.node.value:""}set text(t){this.node&&(this.node.value=t)}setText(t){return this.text=t,this}get maxLength(){return this.nodeConfig.maxLength}set maxLength(t){this.nodeConfig.maxLength=t,this.node&&(this.node.maxLength=t)}setMaxLength(t){return this.maxLength=t,this}get minLength(){return this.nodeConfig.minLength}set minLength(t){this.nodeConfig.minLength=t,this.node&&(this.node.minLength=t)}setMinLength(t){return this.minLength=t,this}get placeholder(){return this.node.placeholder}set placeholder(t){this.node&&(this.node.placeholder=t)}setPlaceholder(t){return this.placeholder=t,this}selectText(t,e){return this.node?(void 0===t?this.node.select():this.node.setSelectionRange(t,e),this):this}selectAll(){return this.selectText(),this}get selectionStart(){return this.node?this.node.selectionStart:0}get selectionEnd(){return this.node?this.node.selectionEnd:0}get selectedText(){if(!this.node)return"";var t=this.node;return t.value.substring(t.selectionStart,t.selectionEnd)}get cursorPosition(){return this.node?this.node.selectionStart:0}set cursorPosition(t){this.node&&this.node.setSelectionRange(t,t)}setCursorPosition(t){return void 0===t?t=this.text.length:t<0&&(t=this.text.length+t),this.cursorPosition=t,this}get tooltip(){return this.node?this.node.title:""}set tooltip(t){if(!this.node)return this;this.node.title=t}setTooltip(t){return this.tooltip=t,this}setTextChangedCallback(t){return this.onTextChanged=t,this}get readOnly(){return this.nodeConfig.readOnly}set readOnly(t){this.nodeConfig.readOnly=t,this.node&&(this.node.readOnly=t)}setReadOnly(t){return void 0===t&&(t=!0),this.readOnly=t,this}get spellCheck(){return this.node?this.node.spellcheck:""}set spellCheck(t){this.node&&(this.node.spellcheck=t)}setSpellCheck(t){return this.spellCheck=t,this}get fontColor(){if(this.node)return this.node.style.color}set fontColor(t){this.node&&(this.node.style.color=t)}setFontColor(t){return this.fontColor=t,this}setStyle(t,e){return this.node?(this.node.style[t]=e,this):this}getStyle(t){if(this.node)return this.node.style[t]}scrollToBottom(){return this.node?(this.node.scrollTop=this.node.scrollHeight,this):this}setEnabled(t){return this.node?(void 0===t&&(t=!0),this.node.disabled=!t,this):this}setBlur(){return this.node?(this.node.blur(),this):this}setFocus(){return this.node?(this.node.focus(),this):this}get isFocused(){return this.isOpened}}Object.assign(k.prototype,L);var P=function(t,e,s){t=t.replace(" ","");var i=s.previousText;if(t===i)return t;if(isNaN(t)){s.emit("nan",t,s),t=i;var n=s.cursorPosition-1;s.setText(t),s.setCursorPosition(n)}else s.previousText=t;return t};const w=Phaser.Utils.Objects.GetValue,S=Phaser.Math.Wrap;class F extends k{constructor(t,e){void 0===e&&(e={}),"number"===e.onUpdate&&(e.onUpdate=P),super(t,e),this.setCursor(w(e,"cursor","|")),this.setCursorFlashDuration(w(e,"cursorFlashDuration",1e3)),this.cursorFlashTimer=0}initText(){return this.cursorFlashTimer=0,this.prevCursorPosition=void 0,this.setText(this.parent.text),this.setCursorPosition(),this}updateText(){var t=this.parent,e=this.text;if(this.onUpdateCallback){var s=this.onUpdateCallback(e,t,this);null!=s&&(e=s)}if(this.isOpened&&this.hasCursor){var i=this.cursorPosition;e=e.substring(0,i)+this.cursor+e.substring(i),this.prevCursorPosition!==i&&(this.prevCursorPosition=i)}return t.text!==e&&(t.setText(e),this.emit("textchange",e,t,this)),this}setCursor(t){return this._cursor=t,this.hasCursor=t&&""!==t,t}setCursorFlashDuration(t){return this.cursorFlashDuration=t,this}get cursor(){if(!this._isFocused)return this._cursor;var t;t=this.cursorFlashTimer<this.cursorFlashDuration/2?this._cursor:" ";var e,s=this.cursorFlashTimer+(e=this.scene,r(e).loop.delta);return this.cursorFlashTimer=S(s,0,this.cursorFlashDuration),t}}var j=function(t){return null==t||""===t||0===t.length};class D extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new F(t,e)}}return function(t,e,s,i){if(void 0===i&&(i="."),"object"==typeof t)if(j(e)){if(null==s)return;"object"==typeof s&&(t=s)}else{"string"==typeof e&&(e=e.split(i));var n=e.pop(),r=function(t,e,s){var i=t;if(j(e));else{var n;"string"==typeof e&&(e=e.split("."));for(var r=0,o=e.length;r<o;r++){var h;null!=i[n=e[r]]&&"object"==typeof i[n]||(h=r===o-1?void 0===s?{}:s:{},i[n]=h),i=i[n]}}return i}(t,e);r[n]=s}}(window,"RexPlugins.GameObjects.HiddenInputText",F),D},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexhiddeninputtextplugin=e();