UNPKG

phaser3-rex-plugins

Version:
1,483 lines (1,448 loc) 145 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.rexbbcodetextplugin = 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 _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 _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); } /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2019 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ var Utils = Phaser.Renderer.WebGL.Utils; /** * Renders this Game Object with the WebGL Renderer to the given Camera. * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera. * This method should not be called directly. It is a utility function of the Render module. * * @method Phaser.GameObjects.Text#renderWebGL * @since 3.0.0 * @private * * @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer. * @param {Phaser.GameObjects.Text} src - The Game Object being rendered in this call. * @param {number} interpolationPercentage - Reserved for future use and custom pipelines. * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object. * @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested */ var WebGLRenderer = function WebGLRenderer(renderer, src, camera, parentMatrix) { if (src.width === 0 || src.height === 0) { return; } camera.addToRenderList(src); var frame = src.frame; var width = frame.width; var height = frame.height; var getTint = Utils.getTintAppendFloatAlpha; var pipeline = renderer.pipelines.set(src.pipeline, src); var textureUnit = pipeline.setTexture2D(frame.glTexture, src); renderer.pipelines.preBatch(src); pipeline.batchTexture(src, frame.glTexture, width, height, src.x, src.y, width / src.style.resolution, height / src.style.resolution, src.scaleX, src.scaleY, src.rotation, src.flipX, src.flipY, src.scrollFactorX, src.scrollFactorY, src.displayOriginX, src.displayOriginY, 0, 0, width, height, getTint(src.tintTopLeft, camera.alpha * src._alphaTL), getTint(src.tintTopRight, camera.alpha * src._alphaTR), getTint(src.tintBottomLeft, camera.alpha * src._alphaBL), getTint(src.tintBottomRight, camera.alpha * src._alphaBR), src.tintFill, 0, 0, camera, parentMatrix, false, textureUnit); renderer.pipelines.postBatch(src); }; /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2019 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ /** * Renders this Game Object with the Canvas Renderer to the given Camera. * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera. * This method should not be called directly. It is a utility function of the Render module. * * @method Phaser.GameObjects.Text#renderCanvas * @since 3.0.0 * @private * * @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer. * @param {Phaser.GameObjects.Text} src - The Game Object being rendered in this call. * @param {number} interpolationPercentage - Reserved for future use and custom pipelines. * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object. * @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested */ var CanvasRenderer = function CanvasRenderer(renderer, src, camera, parentMatrix) { if (src.width === 0 || src.height === 0) { return; } camera.addToRenderList(src); renderer.batchSprite(src, src.frame, camera, parentMatrix); }; var Render = { renderWebGL: WebGLRenderer, renderCanvas: CanvasRenderer }; var CanvasPool$2 = Phaser.Display.Canvas.CanvasPool; var MeasureTextMargins = function MeasureTextMargins(textStyle, testString, out) { if (out === undefined) { out = {}; } var canvas = CanvasPool$2.create(this); var context = canvas.getContext('2d', { willReadFrequently: true }); textStyle.syncFont(canvas, context); var metrics = context.measureText(testString); var width = Math.ceil(metrics.width * textStyle.baselineX); var baseline = width; var height = 2 * baseline; baseline = baseline * textStyle.baselineY | 0; canvas.width = width; canvas.height = height; context.fillStyle = '#f00'; context.fillRect(0, 0, width, height); context.font = textStyle._font; context.textBaseline = 'alphabetic'; context.fillStyle = '#000'; context.fillText(textStyle.testString, 0, baseline); out.left = 0; if (width === 0 || height === 0 || !context.getImageData(0, 0, width, height)) { CanvasPool$2.remove(canvas); return out; } var imagedata = context.getImageData(0, 0, width, height).data; var stop = false; for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var idx = (y * width + x) * 4; if (imagedata[idx] !== 255) { out.left = x; stop = true; break; } } if (stop) { break; } } CanvasPool$2.remove(canvas); return out; }; 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 GameObject$1 = Phaser.GameObjects.GameObject; var TextBase = /*#__PURE__*/function (_GameObject) { _inherits(TextBase, _GameObject); var _super = _createSuper(TextBase); function TextBase() { _classCallCheck(this, TextBase); return _super.apply(this, arguments); } _createClass(TextBase, [{ key: "setStyle", value: function setStyle(style) { return this.style.setStyle(style); } }, { key: "setFont", value: function setFont(font) { return this.style.setFont(font); } }, { key: "setFontFamily", value: function setFontFamily(family) { return this.style.setFontFamily(family); } }, { key: "setFontSize", value: function setFontSize(size) { return this.style.setFontSize(size); } }, { key: "setFontStyle", value: function setFontStyle(style) { return this.style.setFontStyle(style); } }, { key: "setTestString", value: function setTestString(string) { return this.style.setTestString(string); } }, { key: "setFixedSize", value: function setFixedSize(width, height) { return this.style.setFixedSize(width, height); } }, { key: "setBackgroundColor", value: function setBackgroundColor(color, color2, isHorizontalGradient) { return this.style.setBackgroundColor(color, color2, isHorizontalGradient); } }, { key: "setBackgroundStrokeColor", value: function setBackgroundStrokeColor(color, lineWidth) { return this.style.setBackgroundStrokeColor(color, lineWidth); } }, { key: "setBackgroundCornerRadius", value: function setBackgroundCornerRadius(radius, iteration) { return this.style.setBackgroundCornerRadius(radius, iteration); } }, { key: "setFill", value: function setFill(color) { return this.style.setFill(color); } }, { key: "setColor", value: function setColor(color) { return this.style.setColor(color); } }, { key: "setStroke", value: function setStroke(color, thickness) { return this.style.setStroke(color, thickness); } }, { key: "setShadow", value: function setShadow(x, y, color, blur, shadowStroke, shadowFill) { return this.style.setShadow(x, y, color, blur, shadowStroke, shadowFill); } }, { key: "setShadowOffset", value: function setShadowOffset(x, y) { return this.style.setShadowOffset(x, y); } }, { key: "setShadowColor", value: function setShadowColor(color) { return this.style.setShadowColor(color); } }, { key: "setShadowBlur", value: function setShadowBlur(blur) { return this.style.setShadowBlur(blur); } }, { key: "setShadowStroke", value: function setShadowStroke(enabled) { return this.style.setShadowStroke(enabled); } }, { key: "setShadowFill", value: function setShadowFill(enabled) { return this.style.setShadowFill(enabled); } }, { key: "setWrapMode", value: function setWrapMode(mode) { return this.style.setWrapMode(mode); } }, { key: "setWrapWidth", value: function setWrapWidth(width) { return this.style.setWrapWidth(width); } // Align with built-in text game object }, { key: "setWordWrapWidth", value: function setWordWrapWidth(width) { return this.style.setWrapWidth(width); } }, { key: "setAlign", value: function setAlign(align) { return this.style.setHAlign(align); } }, { key: "setHAlign", value: function setHAlign(align) { return this.style.setHAlign(align); } }, { key: "setVAlign", value: function setVAlign(align) { return this.style.setVAlign(align); } }, { key: "setLineSpacing", value: function setLineSpacing(value) { return this.style.setLineSpacing(value); } }, { key: "lineSpacing", get: function get() { return this.style.lineSpacing; }, set: function set(value) { this.setLineSpacing(value); } }, { key: "setXOffset", value: function setXOffset(value) { return this.style.setXOffset(value); } }, { key: "setMaxLines", value: function setMaxLines(max) { return this.style.setMaxLines(max); } }, { key: "setResolution", value: function setResolution(value) { return this.style.setResolution(value); } }, { key: "getTextMetrics", value: function getTextMetrics() { return this.style.getTextMetrics(); } }, { key: "setTextMetrics", value: function setTextMetrics(metrics, font) { return this.style.setTextMetrics(metrics, font); } }, { key: "measureTextMargins", value: function measureTextMargins(testString, out) { return MeasureTextMargins(this.style, testString, out); } }]); return TextBase; }(GameObject$1); var Components$1 = Phaser.GameObjects.Components; Phaser.Class.mixin(TextBase, [Components$1.Alpha, Components$1.BlendMode, Components$1.ComputedSize, Components$1.Crop, Components$1.Depth, Components$1.Flip, Components$1.GetBounds, Components$1.Mask, Components$1.Origin, Components$1.Pipeline, Components$1.PostPipeline, Components$1.ScrollFactor, Components$1.Tint, Components$1.Transform, Components$1.Visible, Render]); var Pad = Phaser.Utils.String.Pad; var GetStyle = function GetStyle(style, canvas, context) { if (style == null) { return style; } switch (_typeof(style)) { case 'string': return style; case 'number': return "#".concat(Pad(Math.floor(style).toString(16), 6, '0', 1)); case 'function': return style(canvas, context); case 'object': if (style.hasOwnProperty('r')) { if (style.hasOwnProperty('a')) { // rgba return "rgba(".concat(style.r, ",").concat(style.g, ",").concat(style.b, ",").concat(style.a, ")"); } else { // rgb return "rgb(".concat(style.r, ",").concat(style.g, ",").concat(style.b, ")"); } } else if (style.hasOwnProperty('h')) { if (style.hasOwnProperty('a')) { // hsla return "hsla(".concat(style.h, ",").concat(style.s, ",").concat(style.l, ",").concat(style.a, ")"); } else { // hsl return "hsl(".concat(style.h, ",").concat(style.s, ",").concat(style.l, ")"); } } else { return style; // Not a valid input } default: return style; } }; // Key: [ Object Key, Default Value, postCallback ] var PropertyMap = { // background backgroundColor: ['backgroundColor', null, GetStyle], backgroundColor2: ['backgroundColor2', null, GetStyle], backgroundHorizontalGradient: ['backgroundHorizontalGradient', true, null], backgroundStrokeColor: ['backgroundStrokeColor', null, GetStyle], backgroundStrokeLineWidth: ['backgroundStrokeLineWidth', 2, null], backgroundCornerRadius: ['backgroundCornerRadius', 0, null], backgroundCornerIteration: ['backgroundCornerIteration', null, null], // font fontFamily: ['fontFamily', 'Courier', null], fontSize: ['fontSize', '16px', null], fontStyle: ['fontStyle', '', null], color: ['color', '#fff', GetStyle], stroke: ['stroke', '#fff', GetStyle], strokeThickness: ['strokeThickness', 0, null], shadowOffsetX: ['shadow.offsetX', 0, null], shadowOffsetY: ['shadow.offsetY', 0, null], shadowColor: ['shadow.color', '#000', GetStyle], shadowBlur: ['shadow.blur', 0, null], shadowStroke: ['shadow.stroke', false, null], shadowFill: ['shadow.fill', false, null], // underline underlineColor: ['underline.color', '#000', GetStyle], underlineThickness: ['underline.thickness', 0, null], underlineOffset: ['underline.offset', 0, null], // strikethrough strikethroughColor: ['strikethrough.color', '#000', GetStyle], strikethroughThickness: ['strikethrough.thickness', 0, null], strikethroughOffset: ['strikethrough.offset', 0, null], // align halign: ['halign', 'left', null], valign: ['valign', 'top', null], // size maxLines: ['maxLines', 0, null], fixedWidth: ['fixedWidth', 0, null], fixedHeight: ['fixedHeight', 0, null], resolution: ['resolution', 0, null], lineSpacing: ['lineSpacing', 0, null], xOffset: ['xOffset', 0, null], rtl: ['rtl', false, null], testString: ['testString', '|MÉqgy', null], baselineX: ['baselineX', 1.2, null], baselineY: ['baselineY', 1.4, null], // wrap wrapMode: ['wrap.mode', 0, null], wrapWidth: ['wrap.width', 0, null], wrapCallback: ['wrap.callback', null], wrapCallbackScope: ['wrap.callbackScope', null] }; /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2018 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ var CanvasPool$1 = Phaser.Display.Canvas.CanvasPool; /** * Calculates the ascent, descent and fontSize of a given font style. * * @function Phaser.GameObjects.MeasureText * @since 3.0.0 * * @param {Phaser.GameObjects.Text.TextStyle} textStyle - The TextStyle object to measure. * * @return {object} An object containing the ascent, descent and fontSize of the TextStyle. */ var MeasureText = function MeasureText(textStyle) { // @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered. var canvas = CanvasPool$1.create(this); // @property {HTMLCanvasElement} context - The context of the canvas element that the text is rendered to. var context = canvas.getContext('2d', { willReadFrequently: true }); textStyle.syncFont(canvas, context); var metrics = context.measureText(textStyle.testString); if ('actualBoundingBoxAscent' in metrics) { var ascent = metrics.actualBoundingBoxAscent; var descent = metrics.actualBoundingBoxDescent; var output = { ascent: ascent, descent: descent, fontSize: ascent + descent }; CanvasPool$1.remove(canvas); return output; } var width = Math.ceil(metrics.width * textStyle.baselineX); var baseline = width; var height = 2 * baseline; baseline = baseline * textStyle.baselineY | 0; canvas.width = width; canvas.height = height; context.fillStyle = '#f00'; context.fillRect(0, 0, width, height); context.font = textStyle._font; context.textBaseline = 'alphabetic'; context.fillStyle = '#000'; context.fillText(textStyle.testString, 0, baseline); var output = { ascent: 0, descent: 0, fontSize: 0 }; if (!context.getImageData(0, 0, width, height)) { output.ascent = baseline; output.descent = baseline + 6; output.fontSize = output.ascent + output.descent; CanvasPool$1.remove(canvas); return output; } var imagedata = context.getImageData(0, 0, width, height).data; var pixels = imagedata.length; var line = width * 4; var i; var j; var idx = 0; var stop = false; // ascent. scan from top to bottom until we find a non red pixel for (i = 0; i < baseline; i++) { for (j = 0; j < line; j += 4) { if (imagedata[idx + j] !== 255) { stop = true; break; } } if (!stop) { idx += line; } else { break; } } output.ascent = baseline - i; idx = pixels - line; stop = false; // descent. scan from bottom to top until we find a non red pixel for (i = height; i > baseline; i--) { for (j = 0; j < line; j += 4) { if (imagedata[idx + j] !== 255) { stop = true; break; } } if (!stop) { idx -= line; } else { break; } } output.descent = i - baseline; output.fontSize = output.ascent + output.descent; CanvasPool$1.remove(canvas); return output; }; var CONST = { // new line mode NO_NEWLINE: 0, RAW_NEWLINE: 1, WRAPPED_NEWLINE: 2, // wrap mode NO_WRAP: 0, WORD_WRAP: 1, CHAR_WRAP: 2, // split lines SPLITREGEXP: /(?:\r\n|\r|\n)/ }; var GetAdvancedValue$1 = Phaser.Utils.Objects.GetAdvancedValue; var GetValue$6 = Phaser.Utils.Objects.GetValue; var TextStyle = /*#__PURE__*/function () { function TextStyle(text, style, propertyMap) { _classCallCheck(this, TextStyle); this.parent = text; // parent.updateText() // parent.width, parent.height if (propertyMap === undefined) { propertyMap = PropertyMap; } this.propertyMap = propertyMap; this.backgroundColor; this.backgroundColor2; this.backgroundHorizontalGradient; this.backgroundStrokeColor; this.backgroundStrokeLineWidth; this.backgroundCornerRadius; this.backgroundCornerIteration; this.fontFamily; this.fontSize; this.fontStyle; this.color; this.stroke; this.strokeThickness; this.shadowOffsetX; this.shadowOffsetY; this.shadowColor; this.shadowBlur; this.shadowStroke; this.shadowFill; this.underlineColor; this.underlineThickness; this.underlineOffset; this.strikethroughColor; this.strikethroughThickness; this.strikethroughOffset; this.halign; this.valign; this.maxLines; this.fixedWidth; this.fixedHeight; this.resolution; this.lineSpacing; this.xOffset; this.rtl; this.testString; this.baselineX; this.baselineY; this.wrapMode; this.wrapWidth; this.wrapCallback; this.wrapCallbackScope; this._font; // Set to defaults + user style this.setStyle(style, false, true); } _createClass(TextStyle, [{ key: "isWrapFitMode", get: function get() { return this.fixedWidth > 0 && this.wrapMode !== CONST.NO_WRAP && this.wrapWidth === 0; } }, { key: "setStyle", value: function setStyle(style, updateText, setDefaults) { if (updateText === undefined) { updateText = true; } if (setDefaults === undefined) { setDefaults = false; } if (style && style.hasOwnProperty('wrap')) { var wrap = style.wrap; if (wrap.hasOwnProperty('mode')) { var mode = wrap.mode; if (typeof mode === 'string') { wrap.mode = WRAPMODE[mode]; } } else { if (wrap.hasOwnProperty('width')) { wrap.mode = 1; } } } // default halign of RTL is 'right' if (style && style.rtl && setDefaults && !style.hasOwnProperty('halign')) { style.halign = 'right'; } // Avoid type mutation if (style && style.hasOwnProperty('fontSize') && typeof style.fontSize === 'number') { style.fontSize = style.fontSize.toString() + 'px'; } var propertyMap = this.propertyMap; for (var key in propertyMap) { var prop = propertyMap[key]; // [ Object Key, Default Value, preCallback ] var objKey = prop[0]; var defaultValue = setDefaults ? prop[1] : this[key]; var postCallback = prop[2]; if (key === 'wrapCallback' || key === 'wrapCallbackScope') { // Callback & scope should be set without processing the values this[key] = GetValue$6(style, objKey, defaultValue); } else { var value = GetAdvancedValue$1(style, objKey, defaultValue); if (postCallback) { value = postCallback(value); } this[key] = value; } } // Allow for 'font' override var font = GetValue$6(style, 'font', null); if (font === null) { this._font = this.fontStyle + ' ' + this.fontSize + ' ' + this.fontFamily; } else { this._font = font; } // Allow for 'fill' to be used in place of 'color' var fill = GetValue$6(style, 'fill', null); if (fill !== null) { this.color = GetStyle(fill); } var metrics = GetValue$6(style, 'metrics', false); // Provide optional TextMetrics in the style object to avoid the canvas look-up / scanning // Doing this is reset if you then change the font of this TextStyle after creation if (metrics) { this.metrics = { ascent: GetValue$6(metrics, 'ascent', 0), descent: GetValue$6(metrics, 'descent', 0), fontSize: GetValue$6(metrics, 'fontSize', 0) }; } else if (updateText || !this.metrics) { this.metrics = MeasureText(this); } if (updateText) { return this.parent.updateText(); } else { return this.parent; } } }, { key: "syncFont", value: function syncFont(canvas, context) { context.font = this._font; } }, { key: "syncStyle", value: function syncStyle(canvas, context) { context.textBaseline = 'alphabetic'; context.fillStyle = this.color; context.strokeStyle = this.stroke; context.lineWidth = this.strokeThickness; context.lineCap = 'round'; context.lineJoin = 'round'; } }, { key: "syncShadow", value: function syncShadow(context, enabled) { if (enabled) { context.shadowOffsetX = this.shadowOffsetX; context.shadowOffsetY = this.shadowOffsetY; context.shadowColor = this.shadowColor; context.shadowBlur = this.shadowBlur; } else { context.shadowOffsetX = 0; context.shadowOffsetY = 0; context.shadowColor = 0; context.shadowBlur = 0; } } }, { key: "update", value: function update(recalculateMetrics) { if (recalculateMetrics) { this._font = "".concat(this.fontStyle, " ").concat(this.fontSize, " ").concat(this.fontFamily).trim(); this.metrics = MeasureText(this); } return this.parent.updateText(recalculateMetrics); } }, { key: "buildFont", value: function buildFont() { var newFont = "".concat(this.fontStyle, " ").concat(this.fontSize, " ").concat(this.fontFamily).trim(); if (newFont !== this._font) { this._font = newFont; //this.metrics = MeasureText(this); } return this; } }, { key: "setFont", value: function setFont(font) { if (typeof font === 'string') { this.fontFamily = font; this.fontSize = ''; this.fontStyle = ''; } else { this.fontFamily = GetValue$6(font, 'fontFamily', 'Courier'); this.fontSize = GetValue$6(font, 'fontSize', '16px'); this.fontStyle = GetValue$6(font, 'fontStyle', ''); } return this.update(true); } }, { key: "setFontFamily", value: function setFontFamily(family) { this.fontFamily = family; return this.update(true); } }, { key: "setFontStyle", value: function setFontStyle(style) { this.fontStyle = style; return this.update(true); } }, { key: "setFontSize", value: function setFontSize(size) { if (typeof size === 'number') { size = size.toString() + 'px'; } this.fontSize = size; return this.update(true); } }, { key: "setTestString", value: function setTestString(string) { this.testString = string; return this.update(true); } }, { key: "setFixedSize", value: function setFixedSize(width, height) { this.fixedWidth = width; this.fixedHeight = height; if (width) { this.parent.width = width; } if (height) { this.parent.height = height; } return this.update(this.isWrapFitMode); } }, { key: "setResolution", value: function setResolution(value) { this.resolution = value; return this.update(false); } }, { key: "setLineSpacing", value: function setLineSpacing(value) { this.lineSpacing = value; return this.update(false); } }, { key: "setXOffset", value: function setXOffset(value) { this.xOffset = value; return this.update(false); } }, { key: "setBackgroundColor", value: function setBackgroundColor(color, color2, isHorizontalGradient) { if (isHorizontalGradient === undefined) { isHorizontalGradient = true; } this.backgroundColor = GetStyle(color, this.parent.canvas, this.parent.context); this.backgroundColor2 = GetStyle(color2, this.parent.canvas, this.parent.context); this.backgroundHorizontalGradient = isHorizontalGradient; return this.update(false); } }, { key: "setBackgroundStrokeColor", value: function setBackgroundStrokeColor(color, lineWidth) { this.backgroundStrokeColor = GetStyle(color, this.parent.canvas, this.parent.context); this.backgroundStrokeLineWidth = lineWidth; return this.update(false); } }, { key: "setBackgroundCornerRadius", value: function setBackgroundCornerRadius(radius, iteration) { this.backgroundCornerRadius = radius; this.backgroundCornerIteration = iteration; return this.update(false); } }, { key: "setFill", value: function setFill(color) { this.color = GetStyle(color, this.parent.canvas, this.parent.context); return this.update(false); } }, { key: "setColor", value: function setColor(color) { this.color = GetStyle(color, this.parent.canvas, this.parent.context); return this.update(false); } }, { key: "setStroke", value: function setStroke(color, thickness) { if (color === undefined) { // Reset the stroke to zero (disabling it) this.strokeThickness = 0; } else { if (thickness === undefined) { thickness = this.strokeThickness; } this.stroke = GetStyle(color, this.parent.canvas, this.parent.context); this.strokeThickness = thickness; } return this.update(true); } }, { key: "setShadow", value: function setShadow(x, y, color, blur, shadowStroke, shadowFill) { if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (color === undefined) { color = '#000'; } if (blur === undefined) { blur = 0; } if (shadowStroke === undefined) { shadowStroke = false; } if (shadowFill === undefined) { shadowFill = true; } this.shadowOffsetX = x; this.shadowOffsetY = y; this.shadowColor = GetStyle(color, this.parent.canvas, this.parent.context); this.shadowBlur = blur; this.shadowStroke = shadowStroke; this.shadowFill = shadowFill; return this.update(false); } }, { key: "setShadowOffset", value: function setShadowOffset(x, y) { if (x === undefined) { x = 0; } if (y === undefined) { y = x; } this.shadowOffsetX = x; this.shadowOffsetY = y; return this.update(false); } }, { key: "setShadowColor", value: function setShadowColor(color) { if (color === undefined) { color = '#000'; } this.shadowColor = GetStyle(color, this.parent.canvas, this.parent.context); return this.update(false); } }, { key: "setShadowBlur", value: function setShadowBlur(blur) { if (blur === undefined) { blur = 0; } this.shadowBlur = blur; return this.update(false); } }, { key: "setShadowStroke", value: function setShadowStroke(enabled) { this.shadowStroke = enabled; return this.update(false); } }, { key: "setShadowFill", value: function setShadowFill(enabled) { this.shadowFill = enabled; return this.update(false); } }, { key: "setUnderline", value: function setUnderline(color, thickness, offset) { if (color === undefined) { color = '#000'; } if (thickness === undefined) { thickness = 0; } if (offset === undefined) { offset = 0; } this.underlineColor = GetStyle(color, this.parent.canvas, this.parent.context); this.underlineThickness = thickness; this.underlineOffset = offset; return this.update(false); } }, { key: "setUnderlineColor", value: function setUnderlineColor(color) { if (color === undefined) { color = '#000'; } this.underlineColor = GetStyle(color, this.parent.canvas, this.parent.context); return this.update(false); } }, { key: "setUnderlineThickness", value: function setUnderlineThickness(thickness) { if (thickness === undefined) { thickness = 0; } this.underlineThickness = thickness; return this.update(false); } }, { key: "setUnderlineOffset", value: function setUnderlineOffset(offset) { if (offset === undefined) { offset = 0; } this.underlineOffset = offset; return this.update(false); } }, { key: "setStrikethrough", value: function setStrikethrough(color, thickness, offset) { if (color === undefined) { color = '#000'; } if (thickness === undefined) { thickness = 0; } if (offset === undefined) { offset = 0; } this.strikethroughColor = GetStyle(color, this.parent.canvas, this.parent.context); this.strikethroughThickness = thickness; this.strikethroughOffset = offset; return this.update(false); } }, { key: "setStrikethroughColor", value: function setStrikethroughColor(color) { if (color === undefined) { color = '#000'; } this.strikethroughColor = GetStyle(color, this.parent.canvas, this.parent.context); return this.update(false); } }, { key: "setStrikethroughThickness", value: function setStrikethroughThickness(thickness) { if (thickness === undefined) { thickness = 0; } this.strikethroughThickness = thickness; return this.update(false); } }, { key: "setStrikethroughOffset", value: function setStrikethroughOffset(offset) { if (offset === undefined) { offset = 0; } this.strikethroughOffset = offset; return this.update(false); } }, { key: "setWrapMode", value: function setWrapMode(mode) { if (typeof mode === 'string') { mode = WRAPMODE[mode.toLowerCase()] || 0; } this.wrapMode = mode; return this.update(true); } }, { key: "setWrapWidth", value: function setWrapWidth(width) { this.wrapWidth = width; return this.update(false); } }, { key: "setAlign", value: function setAlign(halign, valign) { if (halign === undefined) { halign = 'left'; } if (valign === undefined) { valign = 'top'; } this.halign = halign; this.valign = valign; return this.update(false); } }, { key: "setHAlign", value: function setHAlign(halign) { if (halign === undefined) { halign = 'left'; } this.halign = halign; return this.update(false); } }, { key: "setVAlign", value: function setVAlign(valign) { if (valign === undefined) { valign = 'top'; } this.valign = valign; return this.update(false); } }, { key: "setMaxLines", value: function setMaxLines(max) { if (max === undefined) { max = 0; } this.maxLines = max; return this.update(false); } }, { key: "getTextMetrics", value: function getTextMetrics() { var metrics = this.metrics; return { ascent: metrics.ascent, descent: metrics.descent, fontSize: metrics.fontSize }; } }, { key: "setTextMetrics", value: function setTextMetrics(metrics, font) { this.metrics.ascent = metrics.ascent; this.metrics.descent = metrics.descent; this.metrics.fontSize = metrics.fontSize; if (font) { if (typeof font === 'string') { this.fontFamily = font; this.fontSize = ''; this.fontStyle = ''; } else { this.fontFamily = GetValue$6(font, 'fontFamily', this.fontFamily); this.fontSize = GetValue$6(font, 'fontSize', this.fontSize); this.fontStyle = GetValue$6(font, 'fontStyle', this.fontStyle); } } return this.parent.updateText(true); } }, { key: "lineHeight", get: function get() { return this.metrics.fontSize + this.strokeThickness + this.lineSpacing; } }, { key: "toJSON", value: function toJSON() { var output = {}; var propertyMap = this.propertyMap; for (var key in propertyMap) { output[key] = this[key]; } output.metrics = this.getTextMetrics(); return output; } }, { key: "destroy", value: function destroy() { this.parent = undefined; } }]); return TextStyle; }(); var WRAPMODE = { none: CONST.NO_WRAP, word: CONST.WORD_WRAP, "char": CONST.CHAR_WRAP, character: CONST.CHAR_WRAP }; var GetValue$5 = Phaser.Utils.Objects.GetValue; var RoundRectangle = /*#__PURE__*/function () { function RoundRectangle(x, y, width, height, radiusConfig) { _classCallCheck(this, RoundRectangle); if (x === undefined) { x = 0; } if (y === undefined) { y = x; } if (width === undefined) { width = 0; } if (height === undefined) { height = 0; } if (radiusConfig === undefined) { radiusConfig = 0; } this.cornerRadius = {}; this._width = 0; this._height = 0; this.setTo(x, y, width, height, radiusConfig); } _createClass(RoundRectangle, [{ key: "setTo", value: function setTo(x, y, width, height, radiusConfig) { this.setPosition(x, y); this.setRadius(radiusConfig); this.setSize(width, height); return this; } }, { key: "setPosition", value: function setPosition(x, y) { this.x = x; this.y = y; return this; } }, { key: "setRadius", value: function setRadius(value) { if (value === undefined) { value = 0; } this.radius = value; return this; } }, { key: "setSize", value: function setSize(width, height) { this.width = width; this.height = height; return this; } }, { key: "minWidth", get: function get() { var radius = this.cornerRadius; return Math.max(radius.tl.x + radius.tr.x, radius.bl.x + radius.br.x); } }, { key: "minHeight", get: function get() { var radius = this.cornerRadius; return Math.max(radius.tl.y + radius.bl.y, radius.tr.y + radius.br.y); } }, { key: "width", get: function get() { return this._width; }, set: function set(value) { if (value == null) { value = 0; } this._width = Math.max(value, this.minWidth); } }, { key: "height", get: function get() { return this._height; }, set: function set(value) { if (value == null) { value = 0; } this._height = Math.max(value, this.minHeight); } }, { key: "radius", get: function get() { var radius = this.cornerRadius; return Math.max(radius.tl.x, radius.tl.y, radius.tr.x, radius.tr.y, radius.bl.x, radius.bl.y, radius.br.x, radius.br.y); }, set: function set(value) { var defaultRadiusX, defaultRadiusY; if (typeof value === 'number') { defaultRadiusX = value; defaultRadiusY = value; } else { defaultRadiusX = GetValue$5(value, 'x', 0); defaultRadiusY = GetValue$5(value, 'y', 0); } var radius = this.cornerRadius; radius.tl = GetRadius(GetValue$5(value, 'tl', undefined), defaultRadiusX, defaultRadiusY); radius.tr = GetRadius(GetValue$5(value, 'tr', undefined), defaultRadiusX, defaultRadiusY); radius.bl = GetRadius(GetValue$5(value, 'bl', undefined), defaultRadiusX, defaultRadiusY); radius.br = GetRadius(GetValue$5(value, 'br', undefined), defaultRadiusX, defaultRadiusY); } }, { key: "radiusTL", get: function get() { var radius = this.cornerRadius.tl; return Math.max(radius.x, radius.y); }, set: function set(value) { SetRadius(this.cornerRadius.tl, value); } }, { key: "radiusTR", get: function get() { var radius = this.cornerRadius.tr; return Math.max(radius.x, radius.y); }, set: function set(value) { SetRadius(this.cornerRadius.tr, value); } }, { key: "radiusBL", get: function get() { var radius = this.cornerRadius.bl; return Math.max(radius.x, radius.y); }, set: function set(value) { SetRadius(this.cornerRadius.bl, value); } }, { key: "radiusBR", get: function get() { var radius = this.cornerRadius.br; return Math.max(radius.x, radius.y); }, set: function set(value) { SetRadius(this.cornerRadius.br, value); } }]); return RoundRectangle; }(); var GetRadius = function GetRadius(radius, defaultRadiusX, defaultRadiusY) { if (radius === undefined) { radius = { x: defaultRadiusX, y: defaultRadiusY }; } else if (typeof radius === 'number') { radius = { x: radius, y: radius }; } SetConvex(radius); return radius; }; var SetRadius = function SetRadius(radius, value) { if (typeof value === 'number') { radius.x = value; radius.y = value; } else { radius.x = GetValue$5(value, 'x', 0); radius.y = GetValue$5(value, 'y', 0); } SetConvex(radius); }; var SetConvex = function SetConvex(radius) { radius.convex = radius.x >= 0 || radius.y >= 0; radius.x = Math.abs(radius.x); radius.y = Math.abs(radius.y); }; var DegToRad = Phaser.Math.DegToRad; var AddRoundRectanglePath = function AddRoundRectanglePath(context, x, y, width, height, radiusConfig, iteration) { var geom = new RoundRectangle(x, y, width, height, radiusConfig), minWidth = geom.minWidth, minHeight = geom.minHeight, scaleRX = width >= minWidth ? 1 : width / minWidth, scaleRY = height >= minHeight ? 1 : height / minHeight; var cornerRadius = geom.cornerRadius; var radius, radiusX, radiusY, centerX, centerY; var startX, startY; context.save(); context.beginPath(); context.translate(x, y); // Top-left radius = cornerRadius.tl; if (IsArcCorner(radius)) { radiusX = radius.x * scaleRX; radiusY = radius.y * scaleRY; if (IsConvexArc(radius)) { centerX = radiusX; centerY = radiusY; ArcTo(context, centerX, centerY, radiusX, radiusY, 180, 270, false, iteration); } else { centerX = 0; centerY = 0; ArcTo(context, centerX, centerY, radiusX, radiusY, 90, 0, true, iteration); } startX