phaser4-rex-plugins
Version:
1,636 lines (1,319 loc) • 159 kB
JavaScript
(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';
var WebGLRenderer = function (renderer, src, drawingContext, parentMatrix) {
if ((src.width === 0) || (src.height === 0)) {
return;
}
drawingContext.camera.addToRenderList(src);
var customRenderNodes = src.customRenderNodes;
var defaultRenderNodes = src.defaultRenderNodes;
(customRenderNodes.Submitter || defaultRenderNodes.Submitter).run(
drawingContext,
src,
parentMatrix,
0,
customRenderNodes.Texturer || defaultRenderNodes.Texturer,
customRenderNodes.Transformer || defaultRenderNodes.Transformer
);
};
var CanvasRenderer = function (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
};
const CanvasPool$3 = Phaser.Display.Canvas.CanvasPool;
var MeasureTextMargins = function (textStyle, testString, out) {
if (out === undefined) {
out = {};
}
var canvas = CanvasPool$3.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$3.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$3.remove(canvas);
return out;
};
const MainVersionNumber = 4;
const SubVersionNumber = 0;
var IsChecked = false;
var CheckP3Version = function (minVersion) {
if (IsChecked) {
return;
}
if (minVersion === undefined) {
minVersion = SubVersionNumber;
}
var version = Phaser.VERSION.split('.');
var mainVersion = parseInt(version[0]);
if (mainVersion === MainVersionNumber) {
var subVersion = parseInt(version[1]);
if (subVersion < minVersion) {
console.error(`Minimum supported version : ${mainVersion}.${subVersion}`);
}
} else {
console.error(`Can't supported version : ${mainVersion}`);
}
IsChecked = true;
};
CheckP3Version();
const GameObject$1 = Phaser.GameObjects.GameObject;
class TextBase extends GameObject$1 {
setStyle(style) {
return this.style.setStyle(style);
}
setFont(font) {
return this.style.setFont(font);
}
setFontFamily(family) {
return this.style.setFontFamily(family);
}
setFontSize(size) {
return this.style.setFontSize(size);
}
setFontStyle(style) {
return this.style.setFontStyle(style);
}
setTestString(string) {
return this.style.setTestString(string);
}
setFixedSize(width, height) {
return this.style.setFixedSize(width, height);
}
setBackgroundColor(color, color2, isHorizontalGradient) {
return this.style.setBackgroundColor(color, color2, isHorizontalGradient);
}
setBackgroundStrokeColor(color, lineWidth) {
return this.style.setBackgroundStrokeColor(color, lineWidth);
}
setBackgroundCornerRadius(radius, iteration) {
return this.style.setBackgroundCornerRadius(radius, iteration);
}
setFill(color) {
return this.style.setFill(color);
}
setColor(color) {
return this.style.setColor(color);
}
setStroke(color, thickness) {
return this.style.setStroke(color, thickness);
}
setShadow(x, y, color, blur, shadowStroke, shadowFill) {
return this.style.setShadow(x, y, color, blur, shadowStroke, shadowFill);
}
setShadowOffset(x, y) {
return this.style.setShadowOffset(x, y);
}
setShadowColor(color) {
return this.style.setShadowColor(color);
}
setShadowBlur(blur) {
return this.style.setShadowBlur(blur);
}
setShadowStroke(enabled) {
return this.style.setShadowStroke(enabled);
}
setShadowFill(enabled) {
return this.style.setShadowFill(enabled);
}
setUnderline(color, thickness, offset) {
return this.style.setUnderline(color, thickness, offset);
}
setUnderlineColor(color) {
return this.style.setUnderlineColor(color);
}
setUnderlineThickness(thickness) {
return this.style.setUnderlineThickness(thickness);
}
setUnderlineOffset(offset) {
return this.style.setUnderlineOffset(offset);
}
setStrikethrough(color, thickness, offset) {
return this.style.setStrikethrough(color, thickness, offset);
}
setStrikethroughColor(color) {
return this.style.setStrikethroughColor(color);
}
setStrikethroughThickness(thickness) {
return this.style.setStrikethroughThickness(thickness);
}
setStrikethroughOffset(offset) {
return this.style.setStrikethroughOffset(offset);
}
setWrapMode(mode) {
return this.style.setWrapMode(mode);
}
setWrapWidth(width) {
return this.style.setWrapWidth(width);
}
// Align with built-in text game object
setWordWrapWidth(width) {
return this.style.setWrapWidth(width);
}
setAlign(align) {
return this.style.setHAlign(align);
}
setHAlign(align) {
return this.style.setHAlign(align);
}
setVAlign(align) {
return this.style.setVAlign(align);
}
get lineSpacing() {
return this.style.lineSpacing;
}
set lineSpacing(value) {
this.style.lineSpacing = value;
}
setLineSpacing(value) {
this.style.lineSpacing = value;
this.updateText(true);
return this;
}
get letterSpacing() {
return this.style.letterSpacing;
}
set letterSpacing(value) {
this.style.letterSpacing = value;
}
setLetterSpacing(value) {
this.style.letterSpacing = value;
this.updateText(true);
return this;
}
setXOffset(value) {
return this.style.setXOffset(value);
}
setMaxLines(max) {
return this.style.setMaxLines(max);
}
setResolution(value) {
return this.style.setResolution(value);
}
getTextMetrics() {
return this.style.getTextMetrics();
}
setTextMetrics(metrics, font) {
return this.style.setTextMetrics(metrics, font);
}
measureTextMargins(testString, out) {
return MeasureTextMargins(this.style, testString, out);
}
}
const 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.Lighting,
Components$1.Mask,
Components$1.Origin,
Components$1.RenderNodes,
Components$1.ScrollFactor,
Components$1.Tint,
Components$1.Transform,
Components$1.Visible,
Render
]
);
const Pad = Phaser.Utils.String.Pad;
var GetStyle = function (style, canvas, context) {
if (style == null) {
return style;
}
switch (typeof (style)) {
case 'string': return style;
case 'number': return `#${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(${style.r},${style.g},${style.b},${style.a})`;
} else { // rgb
return `rgb(${style.r},${style.g},${style.b})`;
}
} else if (style.hasOwnProperty('h')) {
if (style.hasOwnProperty('a')) { // hsla
return `hsla(${style.h},${style.s},${style.l},${style.a})`;
} else { // hsl
return `hsl(${style.h},${style.s},${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],
letterSpacing: ['letterSpacing', 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}
*/
const CanvasPool$2 = 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 (textStyle) {
// @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered.
var canvas = CanvasPool$2.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$2.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$2.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$2.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,
MIX_WRAP: 3,
// split lines
SPLITREGEXP: /(?:\r\n|\r|\n)/
};
const WRAPMODE = {
none: CONST.NO_WRAP,
word: CONST.WORD_WRAP,
char: CONST.CHAR_WRAP,
character: CONST.CHAR_WRAP,
mix: CONST.MIX_WRAP
};
const GetAdvancedValue$1 = Phaser.Utils.Objects.GetAdvancedValue;
const GetValue$6 = Phaser.Utils.Objects.GetValue;
class TextStyle {
constructor(text, style, propertyMap) {
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.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);
}
get isWrapFitMode() {
return (this.fixedWidth > 0) && (this.wrapMode !== CONST.NO_WRAP) && (this.wrapWidth === 0);
}
setStyle(style, updateText, setDefaults) {
if (updateText === undefined) {
updateText = true;
}
if (setDefaults === undefined) {
setDefaults = false;
}
// Compatible with Text game object
if (style && style.hasOwnProperty('wordWrap')) {
var wordWrap = style.wordWrap;
if (wordWrap.hasOwnProperty('width')) {
style.wrap = {
mode: 'word',
width: wordWrap.width,
};
}
}
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;
}
}
syncFont(canvas, context) {
context.font = this._font;
}
syncStyle(canvas, context) {
context.textBaseline = 'alphabetic';
context.fillStyle = this.color;
context.strokeStyle = this.stroke;
context.lineWidth = this.strokeThickness;
context.lineCap = 'round';
context.lineJoin = 'round';
}
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;
}
}
update(recalculateMetrics) {
if (recalculateMetrics) {
this._font = `${this.fontStyle} ${this.fontSize} ${this.fontFamily}`.trim();
this.metrics = MeasureText(this);
}
return this.parent.updateText(recalculateMetrics);
}
buildFont() {
var newFont = `${this.fontStyle} ${this.fontSize} ${this.fontFamily}`.trim();
if (newFont !== this._font) {
this._font = newFont;
//this.metrics = MeasureText(this);
}
return this;
}
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);
}
setFontFamily(family) {
this.fontFamily = family;
return this.update(true);
}
setFontStyle(style) {
this.fontStyle = style;
return this.update(true);
}
setFontSize(size) {
if (typeof size === 'number') {
size = size.toString() + 'px';
}
this.fontSize = size;
return this.update(true);
}
setTestString(string) {
this.testString = string;
return this.update(true);
}
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);
}
setResolution(value) {
this.resolution = value;
return this.update(false);
}
setXOffset(value) {
this.xOffset = value;
return this.update(false);
}
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);
}
setBackgroundStrokeColor(color, lineWidth) {
this.backgroundStrokeColor = GetStyle(color, this.parent.canvas, this.parent.context);
this.backgroundStrokeLineWidth = lineWidth;
return this.update(false);
}
setBackgroundCornerRadius(radius, iteration) {
this.backgroundCornerRadius = radius;
this.backgroundCornerIteration = iteration;
return this.update(false);
}
setFill(color) {
this.color = GetStyle(color, this.parent.canvas, this.parent.context);
return this.update(false);
}
setColor(color) {
this.color = GetStyle(color, this.parent.canvas, this.parent.context);
return this.update(false);
}
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);
}
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);
}
setShadowOffset(x, y) {
if (x === undefined) {
x = 0;
}
if (y === undefined) {
y = x;
}
this.shadowOffsetX = x;
this.shadowOffsetY = y;
return this.update(false);
}
setShadowColor(color) {
if (color === undefined) {
color = '#000';
}
this.shadowColor = GetStyle(color, this.parent.canvas, this.parent.context);
return this.update(false);
}
setShadowBlur(blur) {
if (blur === undefined) {
blur = 0;
}
this.shadowBlur = blur;
return this.update(false);
}
setShadowStroke(enabled) {
this.shadowStroke = enabled;
return this.update(false);
}
setShadowFill(enabled) {
this.shadowFill = enabled;
return this.update(false);
}
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);
}
setUnderlineColor(color) {
if (color === undefined) {
color = '#000';
}
this.underlineColor = GetStyle(color, this.parent.canvas, this.parent.context);
return this.update(false);
}
setUnderlineThickness(thickness) {
if (thickness === undefined) {
thickness = 0;
}
this.underlineThickness = thickness;
return this.update(false);
}
setUnderlineOffset(offset) {
if (offset === undefined) {
offset = 0;
}
this.underlineOffset = offset;
return this.update(false);
}
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);
}
setStrikethroughColor(color) {
if (color === undefined) {
color = '#000';
}
this.strikethroughColor = GetStyle(color, this.parent.canvas, this.parent.context);
return this.update(false);
}
setStrikethroughThickness(thickness) {
if (thickness === undefined) {
thickness = 0;
}
this.strikethroughThickness = thickness;
return this.update(false);
}
setStrikethroughOffset(offset) {
if (offset === undefined) {
offset = 0;
}
this.strikethroughOffset = offset;
return this.update(false);
}
setWrapMode(mode) {
if (typeof mode === 'string') {
mode = WRAPMODE[mode.toLowerCase()] || 0;
}
this.wrapMode = mode;
return this.update(true);
}
setWrapWidth(width) {
this.wrapWidth = width;
return this.update(false);
}
setAlign(halign, valign) {
if (halign === undefined) {
halign = 'left';
}
if (valign === undefined) {
valign = 'top';
}
this.halign = halign;
this.valign = valign;
return this.update(false);
}
setHAlign(halign) {
if (halign === undefined) {
halign = 'left';
}
this.halign = halign;
return this.update(false);
}
setVAlign(valign) {
if (valign === undefined) {
valign = 'top';
}
this.valign = valign;
return this.update(false);
}
setMaxLines(max) {
if (max === undefined) {
max = 0;
}
this.maxLines = max;
return this.update(false);
}
getTextMetrics() {
var metrics = this.metrics;
return {
ascent: metrics.ascent,
descent: metrics.descent,
fontSize: metrics.fontSize
};
}
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);
}
get lineHeight() {
return this.metrics.fontSize + this.parent.lineSpacing;
}
toJSON() {
var output = {};
var propertyMap = this.propertyMap;
for (var key in propertyMap) {
output[key] = this[key];
}
output.metrics = this.getTextMetrics();
return output;
}
destroy() {
this.parent = undefined;
}
}
var GetString = function (value) {
if (value == null) {
value = '';
} else if (Array.isArray(value)) {
value = value.join('\n');
} else if (typeof (value) === 'number') {
value = value.toString();
}
return value;
};
const GetValue$5 = Phaser.Utils.Objects.GetValue;
class RoundRectangle {
constructor(x, y, width, height, radiusConfig) {
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);
}
setTo(x, y, width, height, radiusConfig) {
this.setPosition(x, y);
this.setRadius(radiusConfig);
this.setSize(width, height);
return this;
}
setPosition(x, y) {
this.x = x;
this.y = y;
return this;
}
setRadius(value) {
if (value === undefined) {
value = 0;
}
this.radius = value;
return this;
}
setSize(width, height) {
this.width = width;
this.height = height;
return this;
}
get minWidth() {
var radius = this.cornerRadius;
return Math.max(radius.tl.x + radius.tr.x, radius.bl.x + radius.br.x);
}
get minHeight() {
var radius = this.cornerRadius;
return Math.max(radius.tl.y + radius.bl.y, radius.tr.y + radius.br.y);
}
get width() {
return this._width;
}
set width(value) {
if (value == null) {
value = 0;
}
this._width = Math.max(value, this.minWidth);
}
get height() {
return this._height;
}
set height(value) {
if (value == null) {
value = 0;
}
this._height = Math.max(value, this.minHeight);
}
get radius() {
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 radius(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);
}
get radiusTL() {
var radius = this.cornerRadius.tl;
return Math.max(radius.x, radius.y);
}
set radiusTL(value) {
SetRadius(this.cornerRadius.tl, value);
}
get radiusTR() {
var radius = this.cornerRadius.tr;
return Math.max(radius.x, radius.y);
}
set radiusTR(value) {
SetRadius(this.cornerRadius.tr, value);
}
get radiusBL() {
var radius = this.cornerRadius.bl;
return Math.max(radius.x, radius.y);
}
set radiusBL(value) {
SetRadius(this.cornerRadius.bl, value);
}
get radiusBR() {
var radius = this.cornerRadius.br;
return Math.max(radius.x, radius.y);
}
set radiusBR(value) {
SetRadius(this.cornerRadius.br, value);
}
}
var GetRadius = function (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 (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 (radius) {
radius.convex = (radius.x >= 0) || (radius.y >= 0);
radius.x = Math.abs(radius.x);
radius.y = Math.abs(radius.y);
};
const DegToRad = Phaser.Math.DegToRad;
var AddRoundRectanglePath = function (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 = 0;
startY = radiusY;
} else {
context.lineTo(0, 0);
startX = 0;
startY = 0;
}
// Top-right
radius = cornerRadius.tr;
if (IsArcCorner(radius)) {
radiusX = radius.x * scaleRX;
radiusY = radius.y * scaleRY;
if (IsConvexArc(radius)) {
centerX = width - radiusX;
centerY = radiusY;
ArcTo(context, centerX, centerY, radiusX, radiusY, 270, 360, false, iteration);
} else {
centerX = width;
centerY = 0;
ArcTo(context, centerX, centerY, radiusX, radiusY, 180, 90, true, iteration);
}
} else {
context.lineTo(width, 0);
}
// Bottom-right
radius = cornerRadius.br;
if (IsArcCorner(radius)) {
radiusX = radius.x * scaleRX;
radiusY = radius.y * scaleRY;
if (IsConvexArc(radius)) {
centerX = width - radiusX;
centerY = height - radiusY;
ArcTo(context, centerX, centerY, radiusX, radiusY, 0, 90, false, iteration);
} else {
centerX = width;
centerY = height;
ArcTo(context, centerX, centerY, radiusX, radiusY, 270, 180, true, iteration);
}
} else {
context.lineTo(width, height);
}
// Bottom-left
radius = cornerRadius.bl;
if (IsArcCorner(radius)) {
radiusX = radius.x * scaleRX;
radiusY = radius.y * scaleRY;
if (IsConvexArc(radius)) {
centerX = radiusX;
centerY = height - radiusY;
ArcTo(context, centerX, centerY, radiusX, radiusY, 90, 180, false, iteration);
} else {
centerX = 0;
centerY = height;
ArcTo(context, centerX, centerY, radiusX, radiusY, 360, 270, true, iteration);
}
} else {
context.lineTo(0, height);
}
context.lineTo(startX, startY);
context.closePath();
context.restore();
};
var IsConvexArc = function (radius) {
return (!radius.hasOwnProperty('convex')) || // radius does not have convex property
radius.convex;
};
var IsArcCorner = function (radius) {
return ((radius.x > 0) && (radius.y > 0));
};
var ArcTo = function (
context,
centerX, centerY,
radiusX, radiusY,
startAngle, endAngle,
antiClockWise,
iteration
) {
// startAngle, endAngle: 0 ~ 360
if (antiClockWise && (endAngle > startAngle)) {
endAngle -= 360;
} else if (!antiClockWise && (endAngle < startAngle)) {
endAngle += 360;
}
startAngle = DegToRad(startAngle);
endAngle = DegToRad(endAngle);
if (iteration == null) { // undefined, or null
context.ellipse(centerX, centerY, radiusX, radiusY, 0, startAngle, endAngle, antiClockWise);
} else {
iteration += 1;
var x, y, angle;
var step = (endAngle - startAngle) / iteration;
for (var i = 0; i <= iteration; i++) {
angle = startAngle + (step * i);
x = centerX + (radiusX * Math.cos(angle));
y = centerY + (radiusY * Math.sin(angle));
context.lineTo(x, y);
}
}
};
var DrawRoundRectangle = function (
canvas, context,
x, y,
width, height, radiusConfig,
fillStyle, strokeStyle, lineWidth, fillColor2, isHorizontalGradient,
iteration
) {
AddRoundRectanglePath(context, x, y, width, height, radiusConfig, iteration);
if (fillStyle != null) {
if (fillColor2 != null) {
var grd;
if (isHorizontalGradient) {
grd = context.createLinearGradient(0, 0, width, 0);
} else {
grd = context.createLinearGradient(0, 0, 0, height);
}
grd.addColorStop(0, fillStyle);
grd.addColorStop(1, fillColor2);
fillStyle = grd;
}
context.fillStyle = fillStyle;
context.fill();
}
if ((strokeStyle != null) && (lineWidth > 0)) {
context.strokeStyle = strokeStyle;
context.lineWidth = lineWidth;
context.stroke();
}
};
var DrawRoundRectangleBackground = function (
canvasObject,
color,
strokeColor, strokeLineWidth,
radius,
color2, isHorizontalGradient,
iteration
) {
if ((color == null) && (strokeColor == null)) {
return;
}
var width = canvasObject.canvas.width,
height = canvasObject.canvas.height;
if (strokeColor == null) {
strokeLineWidth = 0;
}
var x = strokeLineWidth / 2;
width = Math.max(1, width - strokeLineWidth); // Min width is 1
height = Math.max(1, height - strokeLineWidth); // Min height is 1
DrawRoundRectangle(canvasObject.canvas, canvasObject.context,
x, x,
width, height,
radius,
color,
strokeColor, strokeLineWidth,
color2, isHorizontalGradient,
iteration
);
};
var DrawMethods = {
draw(startX, startY, textWidth, textHeight) {
var penManager = this.penManager;
this.hitAreaManager.clear();
var context = this.context;
context.save();
var defaultStyle = this.defaultStyle;
this.clear();
DrawRoundRectangleBackground(
this,
defaultStyle.backgroundColor,
defaultStyle.backgroundStrokeColor,
defaultStyle.backgroundStrokeLineWidth,
defaultStyle.backgroundCornerRadius,
defaultStyle.backgroundColor2,
defaultStyle.backgroundHorizontalGradient,
defaultStyle.backgroundCornerIteration
);
// draw lines
startX += this.startXOffset;
startY += this.startYOffset;
var defaultHalign = defaultStyle.halign,
valign = defaultStyle.valign;
var lineWidth, lineHeight = defaultStyle.lineHeight;
var lines = penManager.lines;
var total