phaser3-rex-plugins
Version:
2 lines (1 loc) • 11.5 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).rextextpageplugin=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 n(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,(s=i.key,r=void 0,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(s,"string"))?r:String(r)),i);}var s,r;}function i(t,e,i){return e&&n(t.prototype,e),i&&n(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(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 r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function a(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 h(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 n,i=r(t);if(e){var s=r(this).constructor;n=Reflect.construct(i,arguments,s);}else n=i.apply(this,arguments);return a(this,n)}}function u(){return u="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var i=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=r(t)););return t}(t,e);if(i){var s=Object.getOwnPropertyDescriptor(i,e);return s.get?s.get.call(arguments.length<3?t:n):s.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):[]}},f=Phaser.Scene,p=function(t){return t instanceof f},l=Phaser.Game,d=function(t){return t instanceof l},g=Phaser.Utils.Objects.GetValue,v=function(){function n(t,i){e(this,n),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(g(i,"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 i(n,[{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 n;return this.parent=e,this.scene=null==(n=e)||"object"!==t(n)?null:p(n)?n:n.scene&&p(n.scene)?n.scene:n.parent&&n.parent.scene&&p(n.parent.scene)?n.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:d(e)?e:d(e.game)?e.game:p(e)?e.sys.game:p(e.scene)?e.scene.sys.game:void 0}(e),this}}]),n}();Object.assign(v.prototype,c);var y=Phaser.GameObjects.Text,m=Phaser.GameObjects.BitmapText,x=function(t){return t instanceof m?2:function(t){return t instanceof y}(t)?0:1},b=function(t,e,n){switch(x(t)){case 0:n=t.getWrappedText(e);break;case 1:n=t.getPenManager(e,n);break;case 2:n=t.maxWidth>0?t.setText(e).getTextBounds().wrappedText.split("\n"):e.split("\n");}return n},P=function(t){return Array.isArray(t)?t=t.join("\n"):"number"==typeof t&&(t=t.toString()),t},E={clearText:function(){return this.sections.length=0,this.pageStartIndexes.length=0,this.lines.length=0,this},appendPage:function(t){var e=this.totalLinesCount;this.sections.push(P(t));t=this.sections.join("\n");this.lines=b(this.parent,t,this.lines);for(var n=this.totalLinesCount-e,i=Math.ceil(n/this.pageLinesCount),s=0;s<i;s++)this.pageStartIndexes.push(e+s*this.pageLinesCount);return this},setText:function(t,e){void 0===e&&(e=!0),e&&this.resetPageIdx(),this.clearText();for(var n=P(t).split(this.pageBreak),i=0,s=n.length;i<s;i++)this.appendPage(n[i]);return this},appendText:function(t){var e=this.content+P(t);return this.setText(e,!1),this}},w=Phaser.Math.Clamp,L={getPage:function(t){return void 0===t&&(t=this.pageIndex),this.setPageIndex(t).getLines(this.startLineIndex,this.endLineIndex)},getNextPage:function(){return this.getPage(this.pageIndex+1)},getPreviousPage:function(){return this.getPage(this.pageIndex-1)},getFirstPage:function(){return this.getPage(0)},getLastPage:function(){return this.getPage(this.lastPageIndex)},resetPageIdx:function(){return this.pageIndex=-1,this},setPageIndex:function(t){return t=w(t,0,this.lastPageIndex),this.pageIndex=t,this.startLineIndex=this.pageStartIndexes[t],this.endLineIndex=this.pageStartIndexes[t+1],this}},_={showPage:function(t){return this.displayText(this.getPage(t)),this},showNextPage:function(){return this.displayText(this.getNextPage()),this},showPreviousPage:function(){return this.displayText(this.getPreviousPage()),this},showFirstPage:function(){return this.displayText(this.getFirstPage()),this},showLastPage:function(){return this.displayText(this.getLastPage()),this},show:function(){return this.displayText(this.getLines()),this},showNextLine:function(){return this.displayText(this.setStartLineIndex(this.startLineIndex+1).getLines()),this},showPreviousLine:function(){return this.displayText(this.setStartLineIndex(this.startLineIndex-1).getLines()),this},displayText:function(t){!function(t,e){switch(x(t)){case 0:var n=(s=t.style).wordWrapWidth,i=s.wordWrapCallback;s.wordWrapWidth=0,s.wordWrapCallback=void 0,t.setText(e),s.wordWrapWidth=n,s.wordWrapCallback=i;break;case 1:var s,r=(s=t.style).wrapMode;s.wrapMode=0,t.setText(e),s.wrapMode=r;break;case 2:var o=t._maxWidth;t._maxWidth=0,t.setText(e),t._maxWidth=o;}}(this.parent,t);}},k={getLines:function(t,e){var n;switch(void 0===t&&(t=this.startLineIndex),void 0===e&&(e=t+this.pageLinesCount),this.textObjectType){case 0:case 2:n=this.lines.slice(t,e).join("\n");break;case 1:var i=this.lines.getLineStartIndex(t),s=this.lines.getLineEndIndex(e-1);n=this.lines.getSliceTagText(i,s,!0);}return n}};Object.assign(k,E,L,_);var I=Phaser.Utils.Objects.GetValue,T=Phaser.Math.Clamp,S=function(t){s(o,t);var n=h(o);function o(t,i){var s;return e(this,o),(s=n.call(this,t,{eventEmitter:!1})).textObjectType=x(s.parent),s.pageStartIndexes=[],s.lines=b(s.parent,""),s.sections=[],s.resetFromJSON(i),s}return i(o,[{key:"resetFromJSON",value:function(t){return this.setMaxLines(I(t,"maxLines",void 0)),this.setPageBreak(I(t,"pageBreak","\f\n")),this.setText(I(t,"text","")),this.setStartLineIndex(I(t,"start",0)),this.setPageIndex(I(t,"page",-1)),this}},{key:"toJSON",value:function(){return {maxLines:this.maxLines,text:this.content,start:this.startLineIndex,page:this.pageIndex,pageBreak:this.pageBreak}}},{key:"shutdown",value:function(t){if(!this.isShutdown){switch(this.textObjectType){case 0:case 2:this.lines.length=0;break;case 1:this.lines.destroy();}this.pageStartIndexes.length=0,this.sections.length=0,this.lines=void 0,this.pageStartIndexes=void 0,this.sections=void 0,u(r(o.prototype),"shutdown",this).call(this,t);}}},{key:"setMaxLines",value:function(t){return this.maxLines=t,this}},{key:"setPageBreak",value:function(t){return this.pageBreak=t,this}},{key:"pageCount",get:function(){return this.pageStartIndexes.length}},{key:"lastPageIndex",get:function(){return this.pageCount-1}},{key:"isFirstPage",get:function(){return this.pageIndex<=0}},{key:"isLastPage",get:function(){return this.pageIndex>=this.pageCount-1}},{key:"totalLinesCount",get:function(){return this.lines?this.lines.length:0}},{key:"startLineIndex",get:function(){return this._startLineIndex},set:function(t){t=T(t,0,this.totalLinesCount-1),this._startLineIndex=t;}},{key:"setStartLineIndex",value:function(t){return this.startLineIndex=t,this}},{key:"pageLinesCount",get:function(){if(void 0!==this.maxLines)return this.maxLines;var t;switch(this.textObjectType){case 0:case 1:var e=this.parent.style.maxLines;t=e>0?e:Math.floor(function(t){var e,n,i;switch(x(t)){case 0:case 1:e=t.height-t.padding.top-t.padding.bottom,n=t.lineSpacing,i=t.style.metrics.fontSize+t.style.strokeThickness;break;case 2:e=t.height,n=0;var s=t.fontSize/t.fontData.size;i=t.fontData.lineHeight*s;}return (e-n)/(i+n)}(this.parent));break;case 2:t=this.totalLinesCount;}return t}},{key:"content",get:function(){return this.sections.join(this.pageBreak)}}]),o}(v);return Object.assign(S.prototype,k),function(t){s(r,Phaser.Plugins.BasePlugin);var n=h(r);function r(t){return e(this,r),n.call(this,t)}return i(r,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}},{key:"add",value:function(t,e){return new S(t,e)}}]),r}()}));