phaser4-rex-plugins
Version:
2 lines (1 loc) • 8.08 kB
JavaScript
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};const r=Phaser.Utils.Objects.GetValue;class h{constructor(t,e){this.setParent(t),this.isShutdown=!1,this.setEventEmitter(r(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=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}(t),this}}Object.assign(h.prototype,t);var a=function(t){return null==t?t="":Array.isArray(t)?t=t.join("\n"):"number"==typeof t&&(t=t.toString()),t};const o=Phaser.GameObjects.Text,p=Phaser.GameObjects.BitmapText;var l=function(t){return t instanceof p?2:function(t){return t instanceof o}(t)?0:1},d=function(t,e){switch(l(t)){case 0:var s=(n=t.style).wordWrapWidth,i=n.wordWrapCallback;n.wordWrapWidth=0,n.wordWrapCallback=void 0,t.setText(e),n.wordWrapWidth=s,n.wordWrapCallback=i;break;case 1:var n,r=(n=t.style).wrapMode;n.wrapMode=0,t.setText(e),n.wrapMode=r;break;case 2:var h=t._maxWidth;t._maxWidth=0,t.setText(e),t._maxWidth=h}},c={setText(t){this.setTextCallback&&(t=this.setTextCallbackScope?this.setTextCallback.call(this.setTextCallbackScope,t,this.isLastChar,this.insertIndex):this.setTextCallback(t,this.isLastChar,this.insertIndex)),this.textWrapEnable?d(this.parent,t):this.parent.setText(t)},appendText(t){var e=this.text.concat(a(t));return this.isTyping?this.setTypingContent(e):this.start(e,void 0,this.textLength),this}},u=function(t,e){return t.getPlainText&&(e=t.getPlainText(e)),e},v=function(t,e){for(var s=void 0,i=0;i<e&&-1!==(s=t.indexOf("\n",s+1));i++);return s},m=function(t,e,s,i){return t.getSubString?t.getSubString(e,s,i):e.slice(s,i)},x=function(t,e,s,i){var n,r=this.parent;if(0===i){var h=0,a=e;this.insertIndex=a,n=m(r,t,h,a)}else if(1===i)h=(a=s)-e,this.insertIndex=0,n=m(r,t,h,a);else if(2===i){var o=s/2;a=(h=Math.floor(o-e/2))+e,this.insertIndex=e%2?e:0,n=m(r,t,h,a)}else if(3===i){var p,l=Math.floor(e/2);p=l>0?m(r,t,h=(a=s)-l,a):"";var d,c=e-l;c>0?(a=(h=0)+c,this.insertIndex=a,d=m(r,t,h,a)):(d="",this.insertIndex=0),n=d+p}return this.insertChar=n.charAt(this.insertIndex-1),n},y={start:function(t,e,s,i){return void 0!==t&&this.setTypingContent(t),void 0!==e&&(this.speed=e),void 0===s&&(s=0),this.typingIndex=s+1,0===this.speed?this.stop(!0):(this.setText(""),this.startTimer(i)),this},startFromLine:function(t,e,s,i,n){var r;if(e>0){void 0===i&&(i=0);var h=u(this.parent,t);r=v(h,e)+i}return this.start(t,s,r,n)},stop:function(t){if(this.getTimer()&&this.freeTimer(),t){for(;!this.isLastChar;)x.call(this,this.text,this.typingIndex,this.textLength,this.typeMode),this.emit("typechar",this.insertChar),this.typingIndex++;this.setText(this.text),this.emit("type"),this.emit("complete",this,this.parent)}return this},pause:function(){var t=this.getTimer();return t&&(t.paused=!0),this},resumeTyping:function(){var t=this.getTimer();return t&&(t.paused=!1),this}};Object.assign(y,c);const g=Phaser.Utils.Objects.GetFastValue,f=Phaser.Utils.Objects.GetValue;class E extends h{constructor(t,e){super(t,e),this.timer=null,this.resetFromJSON(e)}resetFromJSON(t){this.setTextWrapEnable(f(t,"wrap",!1)),this.setTypeMode(f(t,"typeMode",0)),this.setTypingSpeed(f(t,"speed",333)),this.setTextCallback=g(t,"setTextCallback",null),this.setTextCallbackScope=g(t,"setTextCallbackScope",null),this.setTypingContent(g(t,"text","")),this.typingIndex=g(t,"typingIndex",0),this.insertIndex=null,this.insertChar=null;var e=g(t,"elapsed",null);return null!==e&&this.start(void 0,void 0,this.typingIndex,e),this}shutdown(t){this.isShutdown||(this.freeTimer(),super.shutdown(t))}setTypeMode(t){return"string"==typeof t&&(t=T[t]),this.typeMode=t,this}setTypeSpeed(t){return this.speed=t,this}setTypingSpeed(t){return this.speed=t,this}setTextWrapEnable(t){return void 0===t&&(t=!0),this.textWrapEnable=t,this}set text(t){var e=a(t);this.textWrapEnable&&(e=function(t,e){switch(l(t)){case 0:t.style.syncFont(t.canvas,t.context),e=t.runWordWrap(e);break;case 1:e=t.getText(e,void 0,void 0,!0);break;case 2:e=t.setText(e).getTextBounds().wrappedText}return e}(this.parent,e)),this._text=e}get text(){return this._text}get isTyping(){return null!==this.getTimer()}get isLastChar(){return this.typingIndex===this.textLength}setTypingContent(t){return this.text=t,this.textLength=u(this.parent,this.text).length,this}onTyping(){var t=x.call(this,this.text,this.typingIndex,this.textLength,this.typeMode);this.setText(t),this.emit("typechar",this.insertChar),this.emit("type"),this.isLastChar?(this.freeTimer(),this.scene.sys.events.once("preupdate",(function(){this.emit("complete",this,this.parent)}),this)):(this.timer.delay=this.speed,this.typingIndex++)}startTimer(t){var e;return this.timer&&this.freeTimer(),void 0===t?e=0:(this.speed,e=t),this.timer=this.scene.time.addEvent({delay:1e-4,startAt:e,loop:!0,callback:this.onTyping,callbackScope:this}),this}getTimer(){return this.timer}freeTimer(){return this.timer&&(this.timer.remove(),this.timer=null),this}setText(t){this.setTextCallback&&(t=this.setTextCallbackScope?this.setTextCallback.call(this.setTextCallbackScope,t,this.isLastChar,this.insertIndex):this.setTextCallback(t,this.isLastChar,this.insertIndex)),this.textWrapEnable?d(this.parent,t):this.parent.setText(t)}}const T={"left-to-right":0,"right-to-left":1,"middle-to-sides":2,"sides-to-middle":3};Object.assign(E.prototype,y);class b extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e){return new E(t,e)}}return b},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rextexttypingplugin=e();