UNPKG

phaser3-rex-plugins

Version:
1,580 lines (1,534 loc) 412 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rextextbox = factory()); })(this, (function () { 'use strict'; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); } function set(target, property, value, receiver) { if (typeof Reflect !== "undefined" && Reflect.set) { set = Reflect.set; } else { set = function set(target, property, value, receiver) { var base = _superPropBase(target, property); var desc; if (base) { desc = Object.getOwnPropertyDescriptor(base, property); if (desc.set) { desc.set.call(receiver, value); return true; } else if (!desc.writable) { return false; } } desc = Object.getOwnPropertyDescriptor(receiver, property); if (desc) { if (!desc.writable) { return false; } desc.value = value; Object.defineProperty(receiver, property, desc); } else { _defineProperty(receiver, property, value); } return true; }; } return set(target, property, value, receiver); } function _set(target, property, value, receiver, isStrict) { var s = set(target, property, value, receiver || target); if (!s && isStrict) { throw new TypeError('failed to set property'); } return value; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } var EventEmitterMethods$1 = { setEventEmitter: function setEventEmitter(eventEmitter, EventEmitterClass) { if (EventEmitterClass === undefined) { EventEmitterClass = Phaser.Events.EventEmitter; // Use built-in EventEmitter class by default } this._privateEE = eventEmitter === true || eventEmitter === undefined; this._eventEmitter = this._privateEE ? new EventEmitterClass() : eventEmitter; return this; }, destroyEventEmitter: function destroyEventEmitter() { if (this._eventEmitter && this._privateEE) { this._eventEmitter.shutdown(); } return this; }, getEventEmitter: function getEventEmitter() { return this._eventEmitter; }, on: function on() { if (this._eventEmitter) { this._eventEmitter.on.apply(this._eventEmitter, arguments); } return this; }, once: function once() { if (this._eventEmitter) { this._eventEmitter.once.apply(this._eventEmitter, arguments); } return this; }, off: function off() { if (this._eventEmitter) { this._eventEmitter.off.apply(this._eventEmitter, arguments); } return this; }, emit: function emit(event) { if (this._eventEmitter && event) { this._eventEmitter.emit.apply(this._eventEmitter, arguments); } return this; }, addListener: function addListener() { if (this._eventEmitter) { this._eventEmitter.addListener.apply(this._eventEmitter, arguments); } return this; }, removeListener: function removeListener() { if (this._eventEmitter) { this._eventEmitter.removeListener.apply(this._eventEmitter, arguments); } return this; }, removeAllListeners: function removeAllListeners() { if (this._eventEmitter) { this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments); } return this; }, listenerCount: function listenerCount() { if (this._eventEmitter) { return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments); } return 0; }, listeners: function listeners() { if (this._eventEmitter) { return this._eventEmitter.listeners.apply(this._eventEmitter, arguments); } return []; }, eventNames: function eventNames() { if (this._eventEmitter) { return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments); } return []; } }; var SceneClass = Phaser.Scene; var IsSceneObject = function IsSceneObject(object) { return object instanceof SceneClass; }; var GetSceneObject = function GetSceneObject(object) { if (object == null || _typeof(object) !== 'object') { return null; } else if (IsSceneObject(object)) { // object = scene return object; } else if (object.scene && IsSceneObject(object.scene)) { // object = game object return object.scene; } else if (object.parent && object.parent.scene && IsSceneObject(object.parent.scene)) { // parent = bob object return object.parent.scene; } else { return null; } }; var GameClass = Phaser.Game; var IsGame = function IsGame(object) { return object instanceof GameClass; }; var GetGame = function GetGame(object) { if (object == null || _typeof(object) !== 'object') { return null; } else if (IsGame(object)) { return object; } else if (IsGame(object.game)) { return object.game; } else if (IsSceneObject(object)) { // object = scene object return object.sys.game; } else if (IsSceneObject(object.scene)) { // object = game object return object.scene.sys.game; } }; var GetValue$L = Phaser.Utils.Objects.GetValue; var ComponentBase = /*#__PURE__*/function () { function ComponentBase(parent, config) { _classCallCheck(this, ComponentBase); this.setParent(parent); // gameObject, scene, or game this.isShutdown = false; // Event emitter, default is private event emitter this.setEventEmitter(GetValue$L(config, 'eventEmitter', true)); // Register callback of parent destroy event, also see `shutdown` method if (this.parent) { if (this.parent === this.scene) { // parent is a scene this.scene.sys.events.once('shutdown', this.onEnvDestroy, this); } else if (this.parent === this.game) { // parent is game this.game.events.once('shutdown', this.onEnvDestroy, this); } else if (this.parent.once) { // parent is game object or something else this.parent.once('destroy', this.onParentDestroy, this); } // bob object does not have event emitter } } _createClass(ComponentBase, [{ key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } // parent might not be shutdown yet if (this.parent) { if (this.parent === this.scene) { // parent is a scene this.scene.sys.events.off('shutdown', this.onEnvDestroy, this); } else if (this.parent === this.game) { // parent is game this.game.events.off('shutdown', this.onEnvDestroy, this); } else if (this.parent.once) { // parent is game object or something else this.parent.off('destroy', this.onParentDestroy, this); } // bob object does not have event emitter } this.destroyEventEmitter(); this.parent = undefined; this.scene = undefined; this.game = undefined; this.isShutdown = true; } }, { key: "destroy", value: function destroy(fromScene) { this.shutdown(fromScene); } }, { key: "onEnvDestroy", value: function onEnvDestroy() { this.destroy(true); } }, { key: "onParentDestroy", value: function onParentDestroy(parent, fromScene) { this.destroy(fromScene); } }, { key: "setParent", value: function setParent(parent) { this.parent = parent; // gameObject, scene, or game this.scene = GetSceneObject(parent); this.game = GetGame(parent); return this; } }]); return ComponentBase; }(); Object.assign(ComponentBase.prototype, EventEmitterMethods$1); var TextKlass = Phaser.GameObjects.Text; var IsTextGameObject = function IsTextGameObject(gameObject) { return gameObject instanceof TextKlass; }; var BitmapTextKlass = Phaser.GameObjects.BitmapText; var IsBitmapTextGameObject = function IsBitmapTextGameObject(gameObject) { return gameObject instanceof BitmapTextKlass; }; var TextType = 0; var TagTextType = 1; var BitmapTextType = 2; var GetTextObjectType = function GetTextObjectType(textObject) { var textObjectType; if (IsBitmapTextGameObject(textObject)) { textObjectType = BitmapTextType; } else if (IsTextGameObject(textObject)) { textObjectType = TextType; } else { textObjectType = TagTextType; } return textObjectType; }; var TextToLines = function TextToLines(textObject, text, lines) { var textObjectType = GetTextObjectType(textObject); switch (textObjectType) { case TextType: lines = textObject.getWrappedText(text); // Array of string break; case TagTextType: lines = textObject.getPenManager(text, lines); // Pens-manager break; case BitmapTextType: if (textObject.maxWidth > 0) { lines = textObject.setText(text).getTextBounds().wrappedText.split('\n'); } else { lines = text.split('\n'); } break; } return lines; }; var TextHeightToLinesCount = function TextHeightToLinesCount(textObject) { var textObjectType = GetTextObjectType(textObject); var height, lineSpacing, lineHeight; switch (textObjectType) { case TextType: case TagTextType: height = textObject.height - textObject.padding.top - textObject.padding.bottom; lineSpacing = textObject.lineSpacing; lineHeight = textObject.style.metrics.fontSize + textObject.style.strokeThickness; break; case BitmapTextType: height = textObject.height; lineSpacing = 0; var scale = textObject.fontSize / textObject.fontData.size; lineHeight = textObject.fontData.lineHeight * scale; break; } // height = (lines * (lineHeight + lineSpacing)) - lineSpacing return (height - lineSpacing) / (lineHeight + lineSpacing); }; var GetLines = function GetLines(startLineIndex, endLineIdx) { if (startLineIndex === undefined) { startLineIndex = this.startLineIndex; } if (endLineIdx === undefined) { endLineIdx = startLineIndex + this.pageLinesCount; } var text; switch (this.textObjectType) { case TextType: case BitmapTextType: text = this.lines.slice(startLineIndex, endLineIdx).join('\n'); break; case TagTextType: var startIdx = this.lines.getLineStartIndex(startLineIndex); var endIdx = this.lines.getLineEndIndex(endLineIdx - 1); text = this.lines.getSliceTagText(startIdx, endIdx, true); break; } return text; }; var GetString = function GetString(text) { if (Array.isArray(text)) { text = text.join('\n'); } else if (typeof text === 'number') { text = text.toString(); } return text; }; var SetContentMethods = { clearText: function clearText() { this.sections.length = 0; this.pageStartIndexes.length = 0; this.lines.length = 0; return this; }, appendPage: function appendPage(text) { var pageStartIndex = this.totalLinesCount; this.sections.push(GetString(text)); var text = this.sections.join('\n'); this.lines = TextToLines(this.parent, text, this.lines); var newLinesCount = this.totalLinesCount - pageStartIndex; var pageCount = Math.ceil(newLinesCount / this.pageLinesCount); for (var i = 0; i < pageCount; i++) { this.pageStartIndexes.push(pageStartIndex + i * this.pageLinesCount); } return this; }, setText: function setText(text, resetPageIdx) { if (resetPageIdx === undefined) { resetPageIdx = true; } if (resetPageIdx) { this.resetPageIdx(); } this.clearText(); var sections = GetString(text).split(this.pageBreak); // if (sections[sections.length - 1] === '') { // Last section is an empty string // sections.length -= 1; // } for (var i = 0, cnt = sections.length; i < cnt; i++) { this.appendPage(sections[i]); } return this; }, appendText: function appendText(text) { var content = this.content + GetString(text); this.setText(content, false); return this; } }; var Clamp$2 = Phaser.Math.Clamp; var GetPageMethods = { getPage: function getPage(idx) { if (idx === undefined) { idx = this.pageIndex; } return this.setPageIndex(idx).getLines(this.startLineIndex, this.endLineIndex); }, getNextPage: function getNextPage() { return this.getPage(this.pageIndex + 1); }, getPreviousPage: function getPreviousPage() { return this.getPage(this.pageIndex - 1); }, getFirstPage: function getFirstPage() { return this.getPage(0); }, getLastPage: function getLastPage() { return this.getPage(this.lastPageIndex); }, resetPageIdx: function resetPageIdx() { this.pageIndex = -1; return this; }, setPageIndex: function setPageIndex(idx) { idx = Clamp$2(idx, 0, this.lastPageIndex); this.pageIndex = idx; this.startLineIndex = this.pageStartIndexes[idx]; this.endLineIndex = this.pageStartIndexes[idx + 1]; return this; } }; var SetNoWrapText = function SetNoWrapText(textObject, text) { var textObjectType = GetTextObjectType(textObject); switch (textObjectType) { case TextType: // Store wrap properties var style = textObject.style; var wordWrapWidth = style.wordWrapWidth; var wordWrapCallback = style.wordWrapCallback; // Disable wrap style.wordWrapWidth = 0; style.wordWrapCallback = undefined; // Set text textObject.setText(text); // Restore wrap style.wordWrapWidth = wordWrapWidth; style.wordWrapCallback = wordWrapCallback; break; case TagTextType: // Store wrap properties var style = textObject.style; var wrapMode = style.wrapMode; // Disable wrap style.wrapMode = 0; // Set text textObject.setText(text); // Restore wrap style.wrapMode = wrapMode; break; case BitmapTextType: // Store wrap properties var maxWidth = textObject._maxWidth; // Disable wrap textObject._maxWidth = 0; // Set text textObject.setText(text); // Restore wrap textObject._maxWidth = maxWidth; break; } }; var ShowMethods = { showPage: function showPage(idx) { this.displayText(this.getPage(idx)); return this; }, showNextPage: function showNextPage() { this.displayText(this.getNextPage()); return this; }, showPreviousPage: function showPreviousPage() { this.displayText(this.getPreviousPage()); return this; }, showFirstPage: function showFirstPage() { this.displayText(this.getFirstPage()); return this; }, showLastPage: function showLastPage() { this.displayText(this.getLastPage()); return this; }, show: function show() { this.displayText(this.getLines()); return this; }, showNextLine: function showNextLine() { this.displayText(this.setStartLineIndex(this.startLineIndex + 1).getLines()); return this; }, showPreviousLine: function showPreviousLine() { this.displayText(this.setStartLineIndex(this.startLineIndex - 1).getLines()); return this; }, displayText: function displayText(text) { SetNoWrapText(this.parent, text); } }; var Methods = { getLines: GetLines }; Object.assign(Methods, SetContentMethods, GetPageMethods, ShowMethods); var GetValue$K = Phaser.Utils.Objects.GetValue; var Clamp$1 = Phaser.Math.Clamp; var TextPage = /*#__PURE__*/function (_ComponentBase) { _inherits(TextPage, _ComponentBase); var _super = _createSuper(TextPage); function TextPage(gameObject, config) { var _this; _classCallCheck(this, TextPage); _this = _super.call(this, gameObject, { eventEmitter: false }); // No event emitter // this.parent = gameObject; _this.textObjectType = GetTextObjectType(_this.parent); _this.pageStartIndexes = []; // Text object : array of string // Tag text object : pens-manager // Bitmap text object : array of string _this.lines = TextToLines(_this.parent, ''); _this.sections = []; _this.resetFromJSON(config); return _this; } _createClass(TextPage, [{ key: "resetFromJSON", value: function resetFromJSON(o) { this.setMaxLines(GetValue$K(o, 'maxLines', undefined)); this.setPageBreak(GetValue$K(o, 'pageBreak', '\f\n')); this.setText(GetValue$K(o, 'text', '')); this.setStartLineIndex(GetValue$K(o, 'start', 0)); this.setPageIndex(GetValue$K(o, 'page', -1)); return this; } }, { key: "toJSON", value: function toJSON() { return { maxLines: this.maxLines, text: this.content, start: this.startLineIndex, page: this.pageIndex, pageBreak: this.pageBreak }; } }, { key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } switch (this.textObjectType) { case TextType: this.lines.length = 0; break; case TagTextType: this.lines.destroy(); break; case BitmapTextType: this.lines.length = 0; break; } this.pageStartIndexes.length = 0; this.sections.length = 0; this.lines = undefined; this.pageStartIndexes = undefined; this.sections = undefined; _get(_getPrototypeOf(TextPage.prototype), "shutdown", this).call(this, fromScene); } }, { key: "setMaxLines", value: function setMaxLines(maxLines) { this.maxLines = maxLines; return this; } }, { key: "setPageBreak", value: function setPageBreak(pageBreak) { this.pageBreak = pageBreak; return this; } }, { key: "pageCount", get: function get() { return this.pageStartIndexes.length; } }, { key: "lastPageIndex", get: function get() { return this.pageCount - 1; } }, { key: "isFirstPage", get: function get() { return this.pageIndex <= 0; } }, { key: "isLastPage", get: function get() { return this.pageIndex >= this.pageCount - 1; } }, { key: "totalLinesCount", get: function get() { return this.lines ? this.lines.length : 0; } }, { key: "startLineIndex", get: function get() { return this._startLineIndex; }, set: function set(value) { value = Clamp$1(value, 0, this.totalLinesCount - 1); this._startLineIndex = value; } }, { key: "setStartLineIndex", value: function setStartLineIndex(idx) { this.startLineIndex = idx; return this; } }, { key: "pageLinesCount", get: function get() { if (this.maxLines !== undefined) { return this.maxLines; } else { var count; switch (this.textObjectType) { case TextType: case TagTextType: var maxLines = this.parent.style.maxLines; if (maxLines > 0) { count = maxLines; } else { count = Math.floor(TextHeightToLinesCount(this.parent)); } break; case BitmapTextType: count = this.totalLinesCount; break; } return count; } } }, { key: "content", get: function get() { return this.sections.join(this.pageBreak); } }]); return TextPage; }(ComponentBase); Object.assign(TextPage.prototype, Methods); var GetWrapText = function GetWrapText(textObject, text) { var textObjectType = GetTextObjectType(textObject); switch (textObjectType) { case TextType: textObject.style.syncFont(textObject.canvas, textObject.context); text = textObject.runWordWrap(text); break; case TagTextType: text = textObject.getText(text, undefined, undefined, true); break; case BitmapTextType: text = textObject.setText(text).getTextBounds().wrappedText; break; } return text; }; var GetFastValue = Phaser.Utils.Objects.GetFastValue; var GetValue$J = Phaser.Utils.Objects.GetValue; var TextTyping = /*#__PURE__*/function (_ComponentBase) { _inherits(TextTyping, _ComponentBase); var _super = _createSuper(TextTyping); function TextTyping(gameObject, config) { var _this; _classCallCheck(this, TextTyping); _this = _super.call(this, gameObject, config); // this.parent = gameObject; _this.timer = null; _this.resetFromJSON(config); return _this; } _createClass(TextTyping, [{ key: "resetFromJSON", value: function resetFromJSON(o) { this.setTextWrapEnable(GetValue$J(o, 'wrap', false)); this.setTypeMode(GetValue$J(o, 'typeMode', 0)); this.setTypingSpeed(GetValue$J(o, 'speed', 333)); this.setTextCallback = GetFastValue(o, 'setTextCallback', null); this.setTextCallbackScope = GetFastValue(o, 'setTextCallbackScope', null); this.setTypingContent(GetFastValue(o, 'text', '')); this.typingIdx = GetFastValue(o, 'typingIdx', 0); this.insertIdx = null; this.insertChar = null; var elapsed = GetFastValue(o, 'elapsed', null); if (elapsed !== null) { this.start(undefined, undefined, this.typingIdx, elapsed); } return this; } }, { key: "shutdown", value: function shutdown(fromScene) { // Already shutdown if (this.isShutdown) { return; } this.freeTimer(); _get(_getPrototypeOf(TextTyping.prototype), "shutdown", this).call(this, fromScene); } }, { key: "setTypeMode", value: function setTypeMode(m) { if (typeof m === 'string') { m = TYPEMODE[m]; } this.typeMode = m; return this; } }, { key: "setTypeSpeed", value: function setTypeSpeed(speed) { this.speed = speed; return this; } }, { key: "setTypingSpeed", value: function setTypingSpeed(speed) { this.speed = speed; return this; } }, { key: "setTextWrapEnable", value: function setTextWrapEnable(enable) { if (enable === undefined) { enable = true; } this.textWrapEnable = enable; return this; } }, { key: "text", get: function get() { return this._text; }, set: function set(value) { var text = TransferText(value); if (this.textWrapEnable) { text = GetWrapText(this.parent, text); } this._text = text; } }, { key: "isTyping", get: function get() { return this.getTimer() !== null; } }, { key: "isLastChar", get: function get() { return this.typingIdx === this.textLen; } }, { key: "start", value: function start(text, speed, startIdx, timerStartAt) { if (text !== undefined) { this.setTypingContent(text); } if (speed !== undefined) { this.speed = speed; } if (startIdx === undefined) { startIdx = 0; } this.typingIdx = startIdx + 1; if (this.speed === 0) { this.stop(true); } else { this.setText(''); this.startTimer(timerStartAt); } return this; } }, { key: "appendText", value: function appendText(text) { var newText = this.text.concat(TransferText(text)); if (this.isTyping) { this.setTypingContent(newText); } else { this.start(newText, undefined, this.textLen); } return this; } }, { key: "stop", value: function stop(showAllText) { var timer = this.getTimer(); if (timer) { this.freeTimer(); } if (showAllText) { // Fire 'type' event for remainder characters until lastChar while (!this.isLastChar) { this.getTypingString(this.text, this.typingIdx, this.textLen, this.typeMode); this.emit('typechar', this.insertChar); this.typingIdx++; } // Display all characters on text game object this.setText(this.text); this.emit('type'); this.emit('complete', this, this.parent); } return this; } }, { key: "pause", value: function pause() { var timer = this.getTimer(); if (timer) { timer.paused = true; } return this; } }, { key: "resume", value: function resume() { var timer = this.getTimer(); if (timer) { timer.paused = false; } return this; } }, { key: "setTypingContent", value: function setTypingContent(text) { this.text = text; this.textLen = this.getTextLength(this.text); return this; } }, { key: "onTyping", value: function onTyping() { var newText = this.getTypingString(this.text, this.typingIdx, this.textLen, this.typeMode); this.setText(newText); this.emit('typechar', this.insertChar); this.emit('type'); if (this.isLastChar) { this.freeTimer(); this.emit('complete', this, this.parent); } else { this.timer.delay = this.speed; // delay of next typing this.typingIdx++; } } }, { key: "getTypingString", value: function getTypingString(text, typeIdx, textLen, typeMode) { var result; if (typeMode === 0) { //left-to-right var startIdx = 0; var endIdx = typeIdx; this.insertIdx = endIdx; result = this.getSubString(text, startIdx, endIdx); } else if (typeMode === 1) { //right-to-left var endIdx = textLen; var startIdx = endIdx - typeIdx; this.insertIdx = 0; result = this.getSubString(text, startIdx, endIdx); } else if (typeMode === 2) { //middle-to-sides var midIdx = textLen / 2; var startIdx = Math.floor(midIdx - typeIdx / 2); var endIdx = startIdx + typeIdx; this.insertIdx = typeIdx % 2 ? typeIdx : 0; result = this.getSubString(text, startIdx, endIdx); } else if (typeMode === 3) { //sides-to-middle var lowerLen = Math.floor(typeIdx / 2); var lowerResult; if (lowerLen > 0) { var endIdx = textLen; var startIdx = endIdx - lowerLen; lowerResult = this.getSubString(text, startIdx, endIdx); } else { lowerResult = ""; } var upperLen = typeIdx - lowerLen; var upperResult; if (upperLen > 0) { var startIdx = 0; var endIdx = startIdx + upperLen; this.insertIdx = endIdx; upperResult = this.getSubString(text, startIdx, endIdx); } else { upperResult = ""; this.insertIdx = 0; } result = upperResult + lowerResult; } this.insertChar = result.charAt(this.insertIdx - 1); return result; } }, { key: "startTimer", value: function startTimer(timerStartAt) { if (this.timer) { this.freeTimer(); } var startAt; if (timerStartAt === undefined) { startAt = 0; } else { this.speed; startAt = timerStartAt; } this.timer = this.scene.time.addEvent({ delay: 0.0001, startAt: startAt, loop: true, callback: this.onTyping, callbackScope: this }); // Note: Throw error message if delay is 0 with repeat/loop return this; } }, { key: "getTimer", value: function getTimer() { return this.timer; } }, { key: "freeTimer", value: function freeTimer() { if (this.timer) { this.timer.remove(); this.timer = null; } return this; } }, { key: "setText", value: function setText(text) { if (this.setTextCallback) { if (this.setTextCallbackScope) { text = this.setTextCallback.call(this.setTextCallbackScope, text, this.isLastChar, this.insertIdx); } else { text = this.setTextCallback(text, this.isLastChar, this.insertIdx); } } if (this.textWrapEnable) { SetNoWrapText(this.parent, text); } else { this.parent.setText(text); } } }, { key: "getTextLength", value: function getTextLength(text) { var gameObject = this.parent; var len; if (gameObject.getPlainText) { len = gameObject.getPlainText(text).length; } else { len = text.length; } return len; } }, { key: "getSubString", value: function getSubString(text, startIdx, endIdx) { var gameObject = this.parent; var result; if (gameObject.getSubString) { result = gameObject.getSubString(text, startIdx, endIdx); } else { result = text.slice(startIdx, endIdx); } return result; } }]); return TextTyping; }(ComponentBase); var TransferText = function TransferText(text) { if (Array.isArray(text)) { text = text.join('\n'); } else if (typeof text === 'number') { text = text.toString(); } return text; }; var TYPEMODE = { 'left-to-right': 0, 'right-to-left': 1, 'middle-to-sides': 2, 'sides-to-middle': 3 }; var GetValue$I = Phaser.Utils.Objects.GetValue; var TextBoxBase = function TextBoxBase(GOClass, type) { if (type === undefined) { type = 'rexTextBox'; } var TextBox = /*#__PURE__*/function (_GOClass) { _inherits(TextBox, _GOClass); var _super = _createSuper(TextBox); function TextBox(scene, config) { var _this; _classCallCheck(this, TextBox); _this = _super.call(this, scene, config); _this.type = type; // childrenMap must have 'text' element var text = _this.childrenMap.text; _this.page = new TextPage(text, GetValue$I(config, 'page', undefined)); _this.typing = new TextTyping(text, GetValue$I(config, 'typing', config.type)); _this.typing.on('complete', _this.onPageEnd, _assertThisInitialized(_this)).on('type', _this.onType, _assertThisInitialized(_this)).on('typechar', _this.onTypeChar, _assertThisInitialized(_this)); _this.textWidth = text.width; _this.textHeight = text.height; return _this; } _createClass(TextBox, [{ key: "start", value: function start(text, speed) { this.page.setText(text); if (speed !== undefined) { this.setTypingSpeed(speed); } this.emit('start'); this.typeNextPage(); return this; } }, { key: "typeNextPage", value: function typeNextPage() { if (!this.isLastPage) { var txt = this.page.getNextPage(); this.typing.start(txt); } else { this.emit('complete'); } return this; } }, { key: "pause", value: function pause() { if (this.isTyping) { this.typing.pause(); this.emit('pause'); } return this; } }, { key: "resume", value: function resume() { if (!this.isTyping) { this.emit('resume'); this.typing.resume(); } return this; } }, { key: "stop", value: function stop(showAllText) { this.typing.stop(showAllText); return this; } }, { key: "showLastPage", value: function showLastPage() { this.typing.stop(); this.page.showLastPage(); this.emit('type'); this.onPageEnd(); return this; } }, { key: "setTypeSpeed", value: function setTypeSpeed(speed) { this.typing.setTypingSpeed(speed); return this; } }, { key: "setTypingSpeed", value: function setTypingSpeed(speed) { this.typing.setTypingSpeed(speed); return this; } }, { key: "isTyping", get: function get() { return this.typing.isTyping; } }, { key: "isLastPage", get: function get() { return this.page.isLastPage; } }, { key: "isFirstPage", get: function get() { return this.page.isFirstPage; } }, { key: "pageCount", get: function get() { return this.page.pageCount; } }, { key: "pageIndex", get: function get() { return this.page.pageIndex; } }, { key: "typingSpeed", get: function get() { return this.typing.speed; } }, { key: "onType", value: function onType() { var text = this.childrenMap.text; if (this.textWidth !== text.width || this.textHeight !== text.height) { this.textWidth = text.width; this.textHeight = text.height; this.getTopmostSizer().layout(); } this.emit('type'); } }, { key: "onTypeChar", value: function onTypeChar(_char) { this.emit('typechar', _char); } }, { key: "onPageEnd", value: function onPageEnd() { var isLastPage = this.isLastPage; this.emit('pageend'); /* Might enter this method immediately, if invoking typeNextPage() in this 'pageend' event. */ if (isLastPage) { this.emit('complete'); } } }]); return TextBox; }(GOClass); return TextBox; }; var MinVersion = 60; var IsChecked = false; var CheckP3Version = function CheckP3Version(minVersion) { if (IsChecked) { return; } if (minVersion === undefined) { minVersion = MinVersion; } var currentVersion = parseInt(Phaser.VERSION.match(/\.(\d+)\./)[1]); if (currentVersion < minVersion) { console.error("Minimum supported version : 3.".concat(minVersion)); } IsChecked = true; }; CheckP3Version(); var Zone$1 = Phaser.GameObjects.Zone; var AddItem = Phaser.Utils.Array.Add; var RemoveItem$3 = Phaser.Utils.Array.Remove; var Base$1 = /*#__PURE__*/function (_Zone) { _inherits(Base, _Zone); var _super = _createSuper(Base); function Base(scene, x, y, width, height) { var _this; _classCallCheck(this, Base); if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = 1; } if (height === undefined) { height = 1; } _this = _super.call(this, scene, x, y, width, height); _this.children = []; return _this; } _createClass(Base, [{ key: "destroy", value: function destroy(fromScene) { // This Game Object has already been destroyed if (!this.scene || this.ignoreDestroy) { return; } if (fromScene) { // Stop scene var child; for (var i = this.children.length - 1; i >= 0; i--) { child = this.children[i]; if (!child.parentContainer && // Not in container !child.displayList // Not in scene, neither in layer ) { // Destroy child which is not in scene, container, or layer manually child.destroy(fromScene); } } } // Destroy/remove children this.clear(!fromScene); _get(_getPrototypeOf(Base.prototype), "destroy", this).call(this, fromScene); } }, { key: "contains", value: function contains(gameObject) { return this.children.indexOf(gameObject) !== -1; } }, { key: "add", value: function add(gameObjects) { var parent = this; AddItem(this.children, gameObjects, 0, // Callback of item added function (gameObject) { gameObject.once('destroy', parent.onChildDestroy, parent); }, this); return this; } }, { key: "remove", value: function remove(gameObjects, destroyChild) { var parent = this; RemoveItem$3(this.children, gameObjects, // Callback of item removed function (gameObject) { gameObject.off('destroy', parent.onChildDestroy, parent); if (destroyChild) { gameObject.destroy(); } }); return this; } }, { key: "onChildDestroy", value: function onChildDestroy(child, fromScene) { // Only remove reference this.remove(child, false); } }, { key: "clear", value: function clear(destroyChild) { var parent = this; var gameObject; for (var i = 0, cnt = this.children.length; i < cnt; i++) { gameObject = this.children[i]; gameObject.off('destroy', parent.onChildDestroy, parent); if (destroyChild) { gameObject.destroy(); } } this.children.length = 0; return this; } }]); return Base; }(Zone$1); var Components = Phaser.GameObjects.Components; Phaser.Class.mixin(Base$1, [Components.Alpha, Components.Flip]); var GetParent$1 = function GetParent(gameObject, name) { var parent; if (name === undefined) { if (gameObject.hasOwnProperty('rexContainer')) { parent = gameObject.rexContainer.parent; } } else { parent = GetParent(gameObject); while (parent) { if (parent.name === name) { break; } parent = GetParent(parent); } } return parent; }; var GetTopmostParent$1 = function GetTopmostParent(gameObject) { var parent = GetParent$1(gameObject); while (parent) { gameObject = parent; parent = GetParent$1(parent); } return gameObject; }; var DegToRad$2 = Phaser.Math.DegToRad; var RadToDeg$2 = Phaser.Math.RadToDeg; var GetLocalState = function GetLocalState(gameObject) { if (!gameObject.hasOwnProperty('rexContainer')) { var rexContainer = { parent: null, self: null, layer: null, x: 0, y: 0, syncPosition: true, rotation: 0, syncRotation: true, scaleX: 0, scaleY: 0, syncScale: true, alpha: 0, syncAlpha: true, visible: true, active: true }; Object.defineProperty(rexContainer, 'angle', { get: function get() { return RadToDeg$2(this.rotation); }, set: function set(value) { this.rotation = DegToRad$2(value); } }); Object.defineProperty(rexContainer, 'displayWidth', { get: function get() { return gameObject.width * this.scaleX; }, set: function set(width) { this.scaleX = width / gameObject.width; } }); Object.defineProperty(rexContainer, 'displayHeight', { get: function get() { return gameObject.height * this.scaleY; }, set: function set(height) { this.scaleY = height / gameObject.height; } }); gameObject.rexContainer = rexContainer; } return gameObject.rexContainer; }; var Parent = { setParent: function setParent(gameObject, parent) { if (parent === undefined) { parent = this; } var localState = GetLocalState(gameObject); if (parent) { // Add to parent localState.parent = parent; localState.self = gameObject; } else { // Remove from parent localState.parent = null; localState.self = null; } return this; }, getParent: function getParent(gameObject, name) { if (typeof gameObject === 'string') { name = gameObject; gameObject = undefined; } if (gameObject === undefined) { gameObject = this; } return GetParent$1(gameObject, name); }, getTopmos