phaser3-rex-plugins
Version:
2 lines (1 loc) • 11 kB
JavaScript
!function(t,e){"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();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,(r=n.key,s=void 0,"symbol"==typeof(s=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var n=i.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(r,"string"))?s:String(s)),n);}var r,s;}function n(t,e,n){return e&&i(t.prototype,e),n&&i(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e);}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function h(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function a(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var i,n=s(t);if(e){var r=s(this).constructor;i=Reflect.construct(n,arguments,r);}else i=n.apply(this,arguments);return h(this,i)}}function u(){return u="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,i){var n=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(n){var r=Object.getOwnPropertyDescriptor(n,e);return r.get?r.get.call(arguments.length<3?t:i):r.value}},u.apply(this,arguments)}var c={setEventEmitter:function(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:function(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter:function(){return this._eventEmitter},on:function(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once:function(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off:function(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit:function(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener:function(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener:function(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners:function(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount:function(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners:function(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames:function(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}},l=Phaser.Scene,p=function(t){return t instanceof l},f=Phaser.Game,y=function(t){return t instanceof f},v=Phaser.Utils.Objects.GetValue,d=function(){function i(t,n){e(this,i),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(v(n,"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));}return n(i,[{key:"shutdown",value:function(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);}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onEnvDestroy",value:function(){this.destroy(!0);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"setParent",value:function(e){var i;return this.parent=e,this.scene=null==(i=e)||"object"!==t(i)?null:p(i)?i:i.scene&&p(i.scene)?i.scene:i.parent&&i.parent.scene&&p(i.parent.scene)?i.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:y(e)?e:y(e.game)?e.game:p(e)?e.sys.game:p(e.scene)?e.scene.sys.game:void 0}(e),this}}]),i}();Object.assign(d.prototype,c);var m=Phaser.GameObjects.Text,g=Phaser.GameObjects.BitmapText,b=function(t){return t instanceof g?2:function(t){return t instanceof m}(t)?0:1},x=Phaser.Utils.Objects.GetFastValue,E=Phaser.Utils.Objects.GetValue,T=function(t){r(o,t);var i=a(o);function o(t,n){var r;return e(this,o),(r=i.call(this,t,n)).timer=null,r.resetFromJSON(n),r}return n(o,[{key:"resetFromJSON",value:function(t){this.setTextWrapEnable(E(t,"wrap",!1)),this.setTypeMode(E(t,"typeMode",0)),this.setTypingSpeed(E(t,"speed",333)),this.setTextCallback=x(t,"setTextCallback",null),this.setTextCallbackScope=x(t,"setTextCallbackScope",null),this.setTypingContent(x(t,"text","")),this.typingIdx=x(t,"typingIdx",0),this.insertIdx=null,this.insertChar=null;var e=x(t,"elapsed",null);return null!==e&&this.start(void 0,void 0,this.typingIdx,e),this}},{key:"shutdown",value:function(t){this.isShutdown||(this.freeTimer(),u(s(o.prototype),"shutdown",this).call(this,t));}},{key:"setTypeMode",value:function(t){return "string"==typeof t&&(t=k[t]),this.typeMode=t,this}},{key:"setTypeSpeed",value:function(t){return this.speed=t,this}},{key:"setTypingSpeed",value:function(t){return this.speed=t,this}},{key:"setTextWrapEnable",value:function(t){return void 0===t&&(t=!0),this.textWrapEnable=t,this}},{key:"text",get:function(){return this._text},set:function(t){var e=_(t);this.textWrapEnable&&(e=function(t,e){switch(b(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;}},{key:"isTyping",get:function(){return null!==this.getTimer()}},{key:"isLastChar",get:function(){return this.typingIdx===this.textLen}},{key:"start",value:function(t,e,i,n){return void 0!==t&&this.setTypingContent(t),void 0!==e&&(this.speed=e),void 0===i&&(i=0),this.typingIdx=i+1,0===this.speed?this.stop(!0):(this.setText(""),this.startTimer(n)),this}},{key:"appendText",value:function(t){var e=this.text.concat(_(t));return this.isTyping?this.setTypingContent(e):this.start(e,void 0,this.textLen),this}},{key:"stop",value:function(t){if(this.getTimer()&&this.freeTimer(),t){for(;!this.isLastChar;)this.getTypingString(this.text,this.typingIdx,this.textLen,this.typeMode),this.emit("typechar",this.insertChar),this.typingIdx++;this.setText(this.text),this.emit("type"),this.emit("complete",this,this.parent);}return this}},{key:"pause",value:function(){var t=this.getTimer();return t&&(t.paused=!0),this}},{key:"resume",value:function(){var t=this.getTimer();return t&&(t.paused=!1),this}},{key:"setTypingContent",value:function(t){return this.text=t,this.textLen=this.getTextLength(this.text),this}},{key:"onTyping",value:function(){var t=this.getTypingString(this.text,this.typingIdx,this.textLen,this.typeMode);this.setText(t),this.emit("typechar",this.insertChar),this.emit("type"),this.isLastChar?(this.freeTimer(),this.emit("complete",this,this.parent)):(this.timer.delay=this.speed,this.typingIdx++);}},{key:"getTypingString",value:function(t,e,i,n){var r;if(0===n){var s=0,o=e;this.insertIdx=o,r=this.getSubString(t,s,o);}else if(1===n){s=(o=i)-e;this.insertIdx=0,r=this.getSubString(t,s,o);}else if(2===n){var h=i/2;o=(s=Math.floor(h-e/2))+e;this.insertIdx=e%2?e:0,r=this.getSubString(t,s,o);}else if(3===n){var a,u=Math.floor(e/2);if(u>0){s=(o=i)-u;a=this.getSubString(t,s,o);}else a="";var c,l=e-u;if(l>0){o=(s=0)+l;this.insertIdx=o,c=this.getSubString(t,s,o);}else c="",this.insertIdx=0;r=c+a;}return this.insertChar=r.charAt(this.insertIdx-1),r}},{key:"startTimer",value:function(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}},{key:"getTimer",value:function(){return this.timer}},{key:"freeTimer",value:function(){return this.timer&&(this.timer.remove(),this.timer=null),this}},{key:"setText",value:function(t){this.setTextCallback&&(t=this.setTextCallbackScope?this.setTextCallback.call(this.setTextCallbackScope,t,this.isLastChar,this.insertIdx):this.setTextCallback(t,this.isLastChar,this.insertIdx)),this.textWrapEnable?function(t,e){switch(b(t)){case 0:var i=(r=t.style).wordWrapWidth,n=r.wordWrapCallback;r.wordWrapWidth=0,r.wordWrapCallback=void 0,t.setText(e),r.wordWrapWidth=i,r.wordWrapCallback=n;break;case 1:var r,s=(r=t.style).wrapMode;r.wrapMode=0,t.setText(e),r.wrapMode=s;break;case 2:var o=t._maxWidth;t._maxWidth=0,t.setText(e),t._maxWidth=o;}}(this.parent,t):this.parent.setText(t);}},{key:"getTextLength",value:function(t){var e=this.parent;return e.getPlainText?e.getPlainText(t).length:t.length}},{key:"getSubString",value:function(t,e,i){var n=this.parent;return n.getSubString?n.getSubString(t,e,i):t.slice(e,i)}}]),o}(d),_=function(t){return Array.isArray(t)?t=t.join("\n"):"number"==typeof t&&(t=t.toString()),t},k={"left-to-right":0,"right-to-left":1,"middle-to-sides":2,"sides-to-middle":3};return function(t){r(s,Phaser.Plugins.BasePlugin);var i=a(s);function s(t){return e(this,s),i.call(this,t)}return n(s,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new T(t,e)}}]),s}()}));