From 074c14565cf075b6b7a97cdc11e8e4fa4c5f0529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stevanovi=C4=87?= Date: Mon, 16 Aug 2021 17:52:47 +0200 Subject: [PATCH 1/2] TextBox BitmapText support --- plugins/behaviors/textpage/TextPage.js | 446 ++++++++++-------- .../bitmaptext/IsBitmapTextGameObject.js | 7 + 2 files changed, 255 insertions(+), 198 deletions(-) create mode 100644 plugins/utils/bitmaptext/IsBitmapTextGameObject.js diff --git a/plugins/behaviors/textpage/TextPage.js b/plugins/behaviors/textpage/TextPage.js index edd3e984f4..22af363d99 100644 --- a/plugins/behaviors/textpage/TextPage.js +++ b/plugins/behaviors/textpage/TextPage.js @@ -1,211 +1,261 @@ import GetSceneObject from '../../utils/system/GetSceneObject.js'; import IsTextGameObject from '../../utils/text/IsTextGameObject.js'; +import IsBitmapTextGameObject from '../../utils/bitmaptext/IsBitmapTextGameObject.js'; const GetValue = Phaser.Utils.Objects.GetValue; const Clamp = Phaser.Math.Clamp; +const TextType = 0; +const TagTextType = 1; +const BitmapTextType = 2; class TextPagePlugin { - constructor(gameObject, config) { - this.gameObject = gameObject; - this.scene = GetSceneObject(gameObject); - this.setTextObjectType(); - - this.lines = undefined; // Array (default text object), or pens-manager (tag text object) - this.resetFromJSON(config); - this.boot(); - } - - resetFromJSON(o) { - this.setText(GetValue(o, 'text', '')); - this.setStartIdx(GetValue(o, 'start', 0)); - this.setPageIdx(GetValue(o, 'page', -1)); - return this; - } - - toJSON() { - return { - text: this.text, - start: this.startLineIdx, - page: this.pageIndex, - - pageCount: this.pageCount - - }; - } - - boot() { - this.gameObject.on('destroy', this.destroy, this); - } - - shutdown() { - if (this.lines === undefined) { - // Do nothing - } else if (this.textObjectType === 0) { - this.lines.length = 0; - } else { - this.lines.destroy(); - } - - this.gameObject = undefined; - this.scene = undefined; - - return this; - } - - destroy() { - this.shutdown(); - } - - setTextObjectType() { - this.textObjectType = IsTextGameObject(this.gameObject) ? 0 : 1; - return this; - } - - get isFirstPage() { - return (this.pageIndex <= 0); - } - - get isLastPage() { - return (this.pageIndex >= (this.pageCount - 1)); - } - - setText(text, resetPageIdx) { - if (resetPageIdx === undefined) { - resetPageIdx = true; - } - this.text = transferText(text); - - // Wrap content in lines - if (this.textObjectType === 0) { - this.lines = this.gameObject.getWrappedText(this.text); // lines in array - } else { - this.lines = this.gameObject.getPenManager(this.text, this.lines); // pen manager - } - - this.pageCount = Math.ceil(this.totalLinesCount / this.pageLinesCount); - if (resetPageIdx) { - this.resetPageIdx(); - } - return this; - } - - appendText(text) { - this.setText(this.text.concat(transferText(text))); - return this; - } - - getPage(idx) { - if (idx === undefined) { - idx = this.pageIndex; - } - return this.setPageIdx(idx).getLines(); - } - - getNextPage() { - return this.getPage(this.pageIndex + 1); - } - - getPreviousPage() { - return this.getPage(this.pageIndex - 1); - } - - showPage(idx) { - this.displayText(this.getPage()); - return this; - } - - showNextPage() { - this.displayText(this.getNextPage()); - return this; - } - - showPreviousPage() { - this.displayText(this.getPreviousPage()); - return this; - } - - show() { - this.displayText(this.getLines()); - return this; - } - - showNextLine() { - this.displayText(this.setStartIdx(this.startLineIdx + 1).getLines()); - return this; - } - - showPreviousLine() { - this.displayText(this.setStartIdx(this.startLineIdx - 1).getLines()); - return this; - } - - setStartIdx(idx) { - idx = Clamp(idx, 0, this.totalLinesCount - 1); - this.startLineIdx = idx; - return this; - } - - resetPageIdx() { - this.pageIndex = -1; - } - - setPageIdx(idx) { - idx = Clamp(idx, 0, this.pageCount - 1); - this.pageIndex = idx; - this.setStartIdx(this.pageIndex * this.pageLinesCount); - return this; - } - - get totalLinesCount() { - var count; - if (this.textObjectType === 0) { - count = this.lines.length; - } else { - count = this.lines.linesCount; - } - return count; - } - - get pageLinesCount() { - var count; - var maxLines = this.gameObject.style.maxLines; - if (maxLines > 0) { - count = maxLines; - } else { - count = this.totalLinesCount; - } - return count; - } - - getLines(startLineIdx) { - if (startLineIdx === undefined) { - startLineIdx = this.startLineIdx; - } - var endLineIdx = startLineIdx + this.pageLinesCount; - var text; - if (this.textObjectType === 0) { - text = this.lines.slice(startLineIdx, endLineIdx).join('\n'); - } else { - var startIdx = this.lines.getLineStartIndex(startLineIdx); - var endIdx = this.lines.getLineEndIndex(endLineIdx - 1); - text = this.lines.getSliceTagText(startIdx, endIdx, true); - } - return text; - } - - displayText(text) { - this.gameObject.setText(text); - } + constructor(gameObject, config) { + this.gameObject = gameObject; + this.scene = GetSceneObject(gameObject); + this.setTextObjectType(); + + this.lines = undefined; + // Text object : array of string + // Tag text object : pens-manager + // Bitmap text object : array of string + this.totalLinesCount = 0; + + this.resetFromJSON(config); + this.boot(); + } + + resetFromJSON(o) { + this.setMaxLines(GetValue(o, 'maxLines', undefined)); + this.setText(GetValue(o, 'text', '')); + this.setStartIdx(GetValue(o, 'start', 0)); + this.setPageIdx(GetValue(o, 'page', -1)); + return this; + } + + toJSON() { + return { + maxLines: this.maxLines, + text: this.text, + start: this.startLineIdx, + page: this.pageIndex, + + pageCount: this.pageCount + + }; + } + + boot() { + this.gameObject.on('destroy', this.destroy, this); + } + + shutdown() { + if (this.lines === undefined) { + // Do nothing + } else { + switch (this.textObjectType) { + case TextType: + this.lines.length = 0; + break; + case TagTextType: + this.lines.destroy(); + break; + case BitmapTextType: + this.lines.length = 0; + break; + } + } + + this.gameObject = undefined; + this.scene = undefined; + + return this; + } + + destroy() { + this.shutdown(); + } + + setTextObjectType() { + this.textObjectType = + (IsBitmapTextGameObject(this.gameObject)) ? BitmapTextType : + (IsTextGameObject(this.gameObject)) ? TextType : + TagTextType; + + return this; + } + + get isFirstPage() { + return (this.pageIndex <= 0); + } + + get isLastPage() { + return (this.pageIndex >= (this.pageCount - 1)); + } + + setText(text, resetPageIdx) { + if (resetPageIdx === undefined) { + resetPageIdx = true; + } + this.text = transferText(text); + + // Wrap content in lines + switch (this.textObjectType) { + case TextType: + this.lines = this.gameObject.getWrappedText(this.text); // Array of string + this.totalLinesCount = this.lines.length; + break; + case TagTextType: + this.lines = this.gameObject.getPenManager(this.text, this.lines); // Pens-manager + this.totalLinesCount = this.lines.linesCount; + break; + case BitmapTextType: + this.lines = this.gameObject + .setText(text) + .getTextBounds().wrappedText.split('\n'); + this.totalLinesCount = this.lines.length; + break; + } + + this.pageCount = Math.ceil(this.totalLinesCount / this.pageLinesCount); + if (resetPageIdx) { + this.resetPageIdx(); + } + return this; + } + + setMaxLines(maxLines) { + this.maxLines = maxLines; + return this; + } + + appendText(text) { + this.setText(this.text.concat(transferText(text))); + return this; + } + + getPage(idx) { + if (idx === undefined) { + idx = this.pageIndex; + } + return this.setPageIdx(idx).getLines(); + } + + getNextPage() { + return this.getPage(this.pageIndex + 1); + } + + getPreviousPage() { + return this.getPage(this.pageIndex - 1); + } + + showPage(idx) { + this.displayText(this.getPage()); + return this; + } + + showNextPage() { + this.displayText(this.getNextPage()); + return this; + } + + showPreviousPage() { + this.displayText(this.getPreviousPage()); + return this; + } + + show() { + this.displayText(this.getLines()); + return this; + } + + showNextLine() { + this.displayText(this.setStartIdx(this.startLineIdx + 1).getLines()); + return this; + } + + showPreviousLine() { + this.displayText(this.setStartIdx(this.startLineIdx - 1).getLines()); + return this; + } + + setStartIdx(idx) { + idx = Clamp(idx, 0, this.totalLinesCount - 1); + this.startLineIdx = idx; + return this; + } + + resetPageIdx() { + this.pageIndex = -1; + } + + setPageIdx(idx) { + idx = Clamp(idx, 0, this.pageCount - 1); + this.pageIndex = idx; + this.setStartIdx(this.pageIndex * this.pageLinesCount); + return this; + } + + get pageLinesCount() { + if (this.maxLines !== undefined) { + return this.maxLines; + + } else { + var count; + switch (this.textObjectType) { + case TextType: + case TagTextType: + var maxLines = this.gameObject.style.maxLines; + if (maxLines > 0) { + count = maxLines; + } else { + count = this.totalLinesCount; + } + break; + case BitmapTextType: + count = this.totalLinesCount; + break; + } + return count; + + } + } + + getLines(startLineIdx) { + if (startLineIdx === undefined) { + startLineIdx = this.startLineIdx; + } + var endLineIdx = startLineIdx + this.pageLinesCount; + var text; + switch (this.textObjectType) { + case TextType: + text = this.lines.slice(startLineIdx, endLineIdx).join('\n'); + break; + case TagTextType: + var startIdx = this.lines.getLineStartIndex(startLineIdx); + var endIdx = this.lines.getLineEndIndex(endLineIdx - 1); + text = this.lines.getSliceTagText(startIdx, endIdx, true); + break; + case BitmapTextType: + text = this.lines.slice(startLineIdx, endLineIdx).join('\n'); + break; + } + + return text; + } + + displayText(text) { + this.gameObject.setText(text); + } } var transferText = function (text) { - if (Array.isArray(text)) { - text = text.join('\n'); - } else if (typeof (text) === 'number') { - text = text.toString(); - } - return text; + if (Array.isArray(text)) { + text = text.join('\n'); + } else if (typeof (text) === 'number') { + text = text.toString(); + } + return text; } -export default TextPagePlugin; \ No newline at end of file +export default TextPagePlugin; diff --git a/plugins/utils/bitmaptext/IsBitmapTextGameObject.js b/plugins/utils/bitmaptext/IsBitmapTextGameObject.js new file mode 100644 index 0000000000..608746c4cb --- /dev/null +++ b/plugins/utils/bitmaptext/IsBitmapTextGameObject.js @@ -0,0 +1,7 @@ +const BitmapTextKlass = Phaser.GameObjects.BitmapText; + +var IsBitmapTextGameObject = function (gameObject) { + return (gameObject instanceof BitmapTextKlass); +} + +export default IsBitmapTextGameObject; \ No newline at end of file From cb3963bdab14acd5729c4da47d3a766459078b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stevanovi=C4=87?= Date: Thu, 3 Nov 2022 21:25:28 +0100 Subject: [PATCH 2/2] Add dialog endScale config --- plugins/popup.js | 6 +++--- templates/ui/basesizer/ScaleMethods.js | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/popup.js b/plugins/popup.js index 4692e4e3cd..5db2b883c1 100644 --- a/plugins/popup.js +++ b/plugins/popup.js @@ -1,6 +1,6 @@ import Scale from './scale.js'; -var PopUp = function (gameObject, duration, orientation, ease, scale) { +var PopUp = function (gameObject, duration, orientation, ease, scale, endScale) { defaultConfig.mode = 0; switch (orientation) { case 0: @@ -19,7 +19,7 @@ var PopUp = function (gameObject, duration, orientation, ease, scale) { defaultConfig.start = 0; break; } - defaultConfig.end = 1; + defaultConfig.end = endScale ?? 1; defaultConfig.duration = duration; defaultConfig.ease = (ease === undefined) ? 'Cubic' : ease; @@ -35,4 +35,4 @@ var PopUp = function (gameObject, duration, orientation, ease, scale) { var defaultConfig = {}; // reuse this config -export default PopUp; \ No newline at end of file +export default PopUp; diff --git a/templates/ui/basesizer/ScaleMethods.js b/templates/ui/basesizer/ScaleMethods.js index c10e17e743..b098d76812 100644 --- a/templates/ui/basesizer/ScaleMethods.js +++ b/templates/ui/basesizer/ScaleMethods.js @@ -5,15 +5,16 @@ import { WaitComplete } from '../utils/WaitEvent.js' const IsPlainObject = Phaser.Utils.Objects.IsPlainObject; export default { - popUp(duration, orientation, ease) { + popUp(duration, orientation, ease, endScale) { if (IsPlainObject(duration)) { var config = duration; duration = config.duration; orientation = config.orientation; ease = config.ease; + endScale = config.endScale; } - this._scale = PopUp(this, duration, orientation, ease, this._scale); + this._scale = PopUp(this, duration, orientation, ease, this._scale, endScale); this._scale.once('complete', function () { this.emit('popup.complete', this); }, this); @@ -73,4 +74,4 @@ export default { } -} \ No newline at end of file +}