UNPKG

phaser4-rex-plugins

Version:
1,733 lines (1,414 loc) 150 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.rexdynamictextplugin = factory()); })(this, (function () { 'use strict'; // copy from Phaser.GameObjects.Text var WebGLRenderer = function (renderer, src, drawingContext, parentMatrix) { if (src.dirty) { src.updateTexture(); src.dirty = false; } 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 ); }; // copy from Phaser.GameObjects.Text var CanvasRenderer = function (renderer, src, camera, parentMatrix) { if (src.dirty) { src.updateTexture(); src.dirty = false; } 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 Color = Phaser.Display.Color; var CanvasMethods = { clear() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); this.dirty = true; return this; }, fill(color) { this.context.fillStyle = color; this.context.fillRect(0, 0, this.canvas.width, this.canvas.height); this.dirty = true; return this; }, drawFrame(key, frame, dx, dy, dWidth, dHeight, sxOffset, syOffset, sWidth, sHeight) { var textureFrame = this.scene.sys.textures.getFrame(key, frame); if (!textureFrame) { return this; } var frameWidth = textureFrame.cutWidth, frameHeight = textureFrame.cutHeight; if (dx === undefined) { dx = 0; } if (dy === undefined) { dy = 0; } if (dWidth === undefined) { dWidth = frameWidth; } if (dHeight === undefined) { dHeight = frameHeight; } if (sxOffset === undefined) { sxOffset = 0; } if (syOffset === undefined) { syOffset = 0; } if (sWidth === undefined) { sWidth = frameWidth; } if (sHeight === undefined) { sHeight = frameHeight; } var sx = textureFrame.cutX + sxOffset; var sy = textureFrame.cutY + syOffset; this.context.drawImage( textureFrame.source.image, // image sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight ); this.dirty = true; return this; }, getDataURL(type, encoderOptions) { return this.canvas.toDataURL(type, encoderOptions); }, getPixel(x, y, out) { if (out === undefined) { out = new Color(); } var rgb = this.context.getImageData(x, y, 1, 1); out.setTo(rgb.data[0], rgb.data[1], rgb.data[2], rgb.data[3]); return out; }, setPixel(x, y, r, g, b, a) { if (typeof (r) !== 'number') { var color = r; r = color.red; g = color.green; b = color.blue; a = color.alpha; } if (a === undefined) { a = ((r !== 0) || (g !== 0) || (b !== 0)) ? 255 : 0; } var imgData = this.context.createImageData(1, 1); imgData.data[0] = r; imgData.data[1] = g; imgData.data[2] = b; imgData.data[3] = a; this.context.putImageData(imgData, x, y); this.dirty = true; return this; } }; var CopyCanvasToTexture = function (scene, srcCanvas, key, x, y, width, height) { var textures = scene.sys.textures; var renderer = scene.renderer; if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = srcCanvas.width; } if (height === undefined) { height = srcCanvas.height; } var texture; if (textures.exists(key)) { texture = textures.get(key); } else { texture = textures.createCanvas(key, width, height); } var destCanvas = texture.getSourceImage(); if (destCanvas.width !== width) { destCanvas.width = width; } if (destCanvas.height !== height) { destCanvas.height = height; } var destCtx = destCanvas.getContext('2d', { willReadFrequently: true }); destCtx.clearRect(0, 0, width, height); destCtx.drawImage(srcCanvas, x, y, width, height); if (renderer.gl && texture) { renderer.canvasToTexture(destCanvas, texture.source[0].glTexture, true, 0); } }; var TextureMethods = { updateTexture(callback, scope) { var canvas = this.canvas; var context = this.context; if (callback) { var scale = this.resolution; if (scale !== 1) { this.context.save(); this.context.scale(scale, scale); } if (scope) { callback.call(scope, canvas, context); } else { callback(canvas, context); } if (scale !== 1) { this.context.restore(); } } var newWidth = canvas.width, newHeight = canvas.height; if ((newWidth !== this.frame.width) || (newHeight !== this.frame.height)) { this.frame.setSize(newWidth, newHeight); this.frame.source.updateSize(newWidth, newHeight); this.frame.updateUVs(); } if (this.renderer && this.renderer.gl) { this.frame.source.glTexture = this.renderer.canvasToTexture(canvas, this.frame.source.glTexture, true); if (typeof WEBGL_DEBUG) { this.frame.glTexture.spectorMetadata = { textureKey: 'Canvas Game Object' }; } } this.dirty = false; var input = this.input; if (input && !input.customHitArea) { input.hitArea.width = this.width; input.hitArea.height = this.height; } return this; }, generateTexture(key, x, y, width, height) { var srcCanvas = this.canvas; if (width === undefined) { width = srcCanvas.width; } else { width *= this.resolution; } if (height === undefined) { height = srcCanvas.height; } else { height *= this.resolution; } CopyCanvasToTexture(this.scene, srcCanvas, key, x, y, width, height); return this; }, loadTexture(key, frame) { var textureFrame = this.scene.sys.textures.getFrame(key, frame); if (!textureFrame) { return this; } if ((this.width !== textureFrame.cutWidth) || (this.height !== textureFrame.cutHeight)) { this.setSize(textureFrame.cutWidth, textureFrame.cutHeight); } else { this.clear(); } this.drawFrame(key, frame); this.dirty = true; return this; } }; 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 CanvasPool$1 = Phaser.Display.Canvas.CanvasPool; const GameObject$1 = Phaser.GameObjects.GameObject; const UUID = Phaser.Utils.String.UUID; const DefaultImageNodes = Phaser.Renderer.WebGL.RenderNodes.Defaults.DefaultImageNodes; class Canvas extends GameObject$1 { constructor(scene, x, y, width, height, resolution) { if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = 1; } if (height === undefined) { height = 1; } if (resolution === undefined) { resolution = 1; } super(scene, 'rexCanvas'); this.renderer = scene.sys.game.renderer; this._width = width; this._height = height; this.resolution = resolution; width = Math.max(Math.ceil(width * this.resolution), 1); height = Math.max(Math.ceil(height * this.resolution), 1); this.canvas = CanvasPool$1.create(this, width, height); this.dirty = false; this.setPosition(x, y); this.setOrigin(0.5, 0.5); this.initRenderNodes(this._defaultRenderNodesMap); this._crop = this.resetCropObject(); // Create a Texture for this Text object this._textureKey = UUID(); this.texture = scene.sys.textures.addCanvas(this._textureKey, this.canvas); // Set the context to be the CanvasTexture context this.context = this.texture.context; // Get the frame this.frame = this.texture.get(); // Set the resolution this.frame.source.resolution = this.resolution; if (this.renderer && this.renderer.gl) { // Clear the default 1x1 glTexture, as we override it later this.renderer.deleteTexture(this.frame.source.glTexture); this.frame.source.glTexture = null; } this.dirty = true; } preDestroy() { CanvasPool$1.remove(this.canvas); this.canvas = null; this.context = null; var texture = this.texture; if (texture) { texture.destroy(); } } get _defaultRenderNodesMap() { return DefaultImageNodes; } setResolution(resolution) { if (this.resolution === resolution) { return this; } this.resolution = resolution; var width = Math.max(Math.ceil(this.width * resolution), 1); var height = Math.max(Math.ceil(this.height * resolution), 1); this.canvas.width = width; this.canvas.height = height; this.frame.source.resolution = resolution; this.dirty = true; return this; } get width() { return this._width; } set width(value) { this.setSize(value, this._height); } get height() { return this._height; } set height(value) { this.setSize(this._width, value); } setCanvasSize(width, height) { if ((this._width === width) && (this._height === height)) { return this; } this._width = width; this._height = height; this.updateDisplayOrigin(); width = Math.max(Math.ceil(width * this.resolution), 1); height = Math.max(Math.ceil(height * this.resolution), 1); this.canvas.width = width; this.canvas.height = height; this.frame.setSize(width, height); this.frame.source.updateSize(width, height); this.frame.updateUVs(); this.dirty = true; return this; } // setSize might be override setSize(width, height) { this.setCanvasSize(width, height); return this; } get displayWidth() { return this.scaleX * this._width; } set displayWidth(value) { this.scaleX = value / this._width; } get displayHeight() { return this.scaleY * this._height; } set displayHeight(value) { this.scaleY = value / this._height; } setDisplaySize(width, height) { this.displayWidth = width; this.displayHeight = height; return this; } getCanvas(readOnly) { if (!readOnly) { this.dirty = true; } return this.canvas; } getContext(readOnly) { if (!readOnly) { this.dirty = true; } return this.context; } needRedraw() { this.dirty = true; return this; } resize(width, height) { this.setSize(width, height); return this; } } const Components = Phaser.GameObjects.Components; Phaser.Class.mixin(Canvas, [ Components.Alpha, Components.BlendMode, Components.Crop, Components.Depth, Components.Flip, Components.GetBounds, Components.Lighting, Components.Mask, Components.Origin, Components.RenderNodes, Components.ScrollFactor, Components.Tint, Components.Transform, Components.Visible, Render, CanvasMethods, TextureMethods, ] ); const GetValue$a = Phaser.Utils.Objects.GetValue; var GetPadding$1 = function (padding, key) { if (key === undefined) { return padding; } return padding[key]; }; var SetPadding$1 = function (padding, key, value) { if (padding === undefined) { padding = {}; } if (key === undefined) { key = 0; } var keyType = typeof (key); if (keyType === 'string') { padding[key] = value; } else if (keyType === 'number') { padding.left = key; padding.right = key; padding.top = key; padding.bottom = key; } else { padding.left = GetValue$a(key, 'left', 0); padding.right = GetValue$a(key, 'right', 0); padding.top = GetValue$a(key, 'top', 0); padding.bottom = GetValue$a(key, 'bottom', 0); } return padding; }; var GetValue$9 = function (source, key, defaultValue) { if (!source || typeof source === 'number') { return defaultValue; } if (typeof (key) === 'string') { if (source.hasOwnProperty(key)) { return source[key]; } if (key.indexOf('.') !== -1) { key = key.split('.'); } else { return defaultValue; } } var keys = key; var parent = source; var value = defaultValue; // Use for loop here so we can break early for (var i = 0; i < keys.length; i++) { key = keys[i]; if (parent.hasOwnProperty(key)) { // Yes it has a key property, let's carry on down value = parent[key]; parent = value; } else { // Can't go any further, so reset to default value = defaultValue; break; } } return value; }; var Clear = function (obj) { if ((typeof (obj) !== 'object') || (obj === null)) { return obj; } if (Array.isArray(obj)) { obj.length = 0; } else { for (var key in obj) { delete obj[key]; } } return obj; }; var DataMethods = { enableData() { if (this.data === undefined) { this.data = {}; } return this; }, setData(key, value) { this.enableData(); if (arguments.length === 1) { var data = key; for (key in data) { this.data[key] = data[key]; } } else { this.data[key] = value; } return this; }, getData(key, defaultValue) { this.enableData(); return (key === undefined) ? this.data : GetValue$9(this.data, key, defaultValue); }, incData(key, inc, defaultValue) { if (defaultValue === undefined) { defaultValue = 0; } this.enableData(); this.setData(key, this.getData(key, defaultValue) + inc); return this; }, mulData(key, mul, defaultValue) { if (defaultValue === undefined) { defaultValue = 0; } this.enableData(); this.setData(key, this.getData(key, defaultValue) * mul); return this; }, clearData() { if (this.data) { Clear(this.data); } return this; }, }; class Base { constructor(parent, type) { this.setParent(parent); this.type = type; this.renderable = false; this.reset().setActive(); } destroy() { this.parent.removeChild(this); } setParent(parent) { this.parent = parent; return this; } get scene() { return this.parent.scene; } get canvas() { return (this.parent) ? this.parent.canvas : null; } get context() { return (this.parent) ? this.parent.context : null; } setDirty(dirty) { if (dirty && this.parent) { this.parent.dirty = true; } return this; } get active() { return this._active; } set active(value) { this.setDirty(this._active != value); this._active = value; } setActive(active) { if (active === undefined) { active = true; } this.active = active; return this; } modifyPorperties(o) { return this; } // Override onFree() { this.reset().setParent(); } // Override reset() { return this; } // Override render() { } // Override contains(x, y) { return false; } } Object.assign( Base.prototype, DataMethods ); var RenderMethods = { // Override renderContent() { }, // Override render() { if (!this.willRender) { return this; } var context = this.context; context.save(); context.globalAlpha = this.alpha; if (this.toLocalPosition) { var x = this.drawX, y = this.drawY; if (this.autoRound) { x = Math.round(x); y = Math.round(y); } context.translate(x, y); context.scale(this.scaleX, this.scaleY); context.rotate(this.rotation); } if (this.drawBelowCallback) { this.drawBelowCallback(this); } this.renderContent(); if (this.drawAboveCallback) { this.drawAboveCallback(this); } context.restore(); return this; }, }; const RotateAround$1 = Phaser.Math.RotateAround; var CanvasPositionToBobPosition = function (canvasX, canvasY, bob, out) { if (out === undefined) { out = {}; } else if (out === true) { if (globPoint$1 === undefined) { globPoint$1 = {}; } out = globPoint$1; } out.x = (canvasX - bob.drawX) / bob.scaleX; out.y = (canvasY - bob.drawY) / bob.scaleY; if (bob.rotation !== 0) { RotateAround$1(out, 0, 0, -bob.rotation); } return out; }; var globPoint$1; const Rectangle = Phaser.Geom.Rectangle; var Contains = function (canvasX, canvasY) { if ((this.width === 0) || (this.height === 0)) { return false; } var bobPosition = CanvasPositionToBobPosition(canvasX, canvasY, this, true); return GetBobBounds(this).contains(bobPosition.x, bobPosition.y); }; var GetBobBounds = function (bob) { if (bobBounds === undefined) { bobBounds = new Rectangle(); } var x = bob.drawTLX, y = bob.drawTLY; bobBounds.setTo(x, y, (bob.drawTRX - x), (bob.drawBLY - y)); return bobBounds; }; var bobBounds; const RotateAround = Phaser.Math.RotateAround; var BobPositionToCanvasPosition = function (bob, bobX, bobY, out) { if (out === undefined) { out = {}; } else if (out === true) { if (globPoint === undefined) { globPoint = {}; } out = globPoint; } out.x = bobX; out.y = bobY; if (bob.rotation !== 0) { RotateAround(out, 0, 0, bob.rotation); } out.x = (out.x * bob.scaleX) + bob.drawX; out.y = (out.y * bob.scaleY) + bob.drawY; return out; }; var globPoint; const TransformMatrix = Phaser.GameObjects.Components.TransformMatrix; var GameObjectLocalXYToWorldXY = function (gameObject, localX, localY, out) { if (out === undefined) { out = {}; } else if (out === true) { out = globOut; } var px = localX - (gameObject.width * gameObject.originX); var py = localY - (gameObject.height * gameObject.originY); if (tempMatrix === undefined) { tempMatrix = new TransformMatrix(); parentMatrix = new TransformMatrix(); } if (gameObject.parentContainer) { gameObject.getWorldTransformMatrix(tempMatrix, parentMatrix); } else { tempMatrix.applyITRS(gameObject.x, gameObject.y, gameObject.rotation, gameObject.scaleX, gameObject.scaleY); } tempMatrix.transformPoint(px, py, out); return out; }; var tempMatrix, parentMatrix; var globOut = {}; var BobPositionToWorldPosition = function (dynamicText, bob, bobX, bobY, out) { var localXY = BobPositionToCanvasPosition(bob, bobX, bobY, true); var worldXY = GameObjectLocalXYToWorldXY(dynamicText, localXY.x, localXY.y, out); return worldXY; }; var GetBobWorldPosition = function (dynamicText, bob, offsetX, offsetY, out) { if (typeof (offsetX) !== 'number') { out = offsetX; offsetX = 0; offsetY = 0; } var bobX = bob.drawCenterX + offsetX; var bobY = bob.drawCenterY + offsetY; return BobPositionToWorldPosition(dynamicText, bob, bobX, bobY, out); }; var GetWorldPosition = function (offsetX, offsetY, out) { return GetBobWorldPosition(this.parent, this, offsetX, offsetY, out); }; var Methods$1 = { contains: Contains, getWorldPosition: GetWorldPosition, }; Object.assign( Methods$1, RenderMethods ); const DegToRad$1 = Phaser.Math.DegToRad; const RadToDeg = Phaser.Math.RadToDeg; const GetValue$8 = Phaser.Utils.Objects.GetValue; class RenderBase extends Base { constructor(parent, type) { super(parent, type); this.renderable = true; this.scrollFactorX = 1; this.scrollFactorY = 1; this.toLocalPosition = true; this.originX = 0; this.offsetX = 0; // Override this.offsetY = 0; // Override } get visible() { return this._visible; } set visible(value) { this.setDirty(this._visible != value); this._visible = value; } setVisible(visible) { if (visible === undefined) { visible = true; } this.visible = visible; return this; } get alpha() { return this._alpha; } set alpha(value) { this.setDirty(this._alpha != value); this._alpha = value; } setAlpha(alpha) { this.alpha = alpha; return this; } get x() { return this._x; } set x(value) { this.setDirty(this._x != value); this._x = value; } setX(x) { this.x = x; return this; } get y() { return this._y; } set y(value) { this.setDirty(this._y != value); this._y = value; } setY(y) { this.y = y; return this; } setPosition(x, y) { this.x = x; this.y = y; return this; } setInitialPosition(x, y) { this.x0 = x; this.y0 = y; return this; } setScrollFactorX(x) { this.scrollFactorX = x; return this; } setScrollFactorY(y) { this.scrollFactorY = y; return this; } setScrollFactor(x, y) { if (y === undefined) { y = x; } this.scrollFactorX = x; this.scrollFactorY = y; return this; } get rotation() { return this._rotation; } set rotation(value) { this.setDirty(this._rotation != value); this._rotation = value; } setRotation(rotation) { this.rotation = rotation; return this; } get angle() { return RadToDeg(this._rotation); } set angle(value) { this.rotation = DegToRad$1(value); } setAngle(angle) { this.angle = angle; return this; } get scaleX() { return this._scaleX; } set scaleX(value) { this.setDirty(this._scaleX !== value); this._scaleX = value; } setScaleX(scaleX) { this.scaleX = scaleX; return this; } // Override get width() { return 0; } // Override set width(value) { } setWidth(width, keepAspectRatio) { if (keepAspectRatio === undefined) { keepAspectRatio = false; } this.width = width; if (keepAspectRatio) { this.scaleY = this.scaleX; } return this; } get leftSpace() { return this._leftSpace; } set leftSpace(value) { this.setDirty(this._leftSpace !== value); this._leftSpace = value; } setLeftSpace(value) { this.leftSpace = value; return this; } get rightSpace() { return this._rightSpace; } set rightSpace(value) { this.setDirty(this._rightSpace !== value); this._rightSpace = value; } setRightSpace(value) { this.rightSpace = value; return this; } get outerWidth() { return this.width + this.leftSpace + this.rightSpace; } get scaleY() { return this._scaleY; } set scaleY(value) { this.setDirty(this._scaleY !== value); this._scaleY = value; } setScaleY(scaleY) { this.scaleY = scaleY; return this; } // Override get height() { return 0; } // Override set height(value) { } setHeight(height, keepAspectRatio) { if (keepAspectRatio === undefined) { keepAspectRatio = false; } this.height = height; if (keepAspectRatio) { this.scaleX = this.scaleY; } return this; } setScale(scaleX, scaleY) { if (scaleY === undefined) { scaleY = scaleX; } this.scaleX = scaleX; this.scaleY = scaleY; return this; } setOrigin(x) { this.originX = x; return this; } setAlign(align) { this.align = align; return this; } modifyPorperties(o) { if (!o) { return this; } if (o.hasOwnProperty('x')) { this.setX(o.x); } if (o.hasOwnProperty('y')) { this.setY(o.y); } if (o.hasOwnProperty('rotation')) { this.setRotation(o.rotation); } else if (o.hasOwnProperty('angle')) { this.setAngle(o.angle); } if (o.hasOwnProperty('alpha')) { this.setAlpha(o.alpha); } // ScaleX, ScaleY var width = GetValue$8(o, 'width', undefined); var height = GetValue$8(o, 'height', undefined); var scaleX = GetValue$8(o, 'scaleX', undefined); var scaleY = GetValue$8(o, 'scaleY', undefined); if (width !== undefined) { if ((height === undefined) && (scaleY === undefined)) { this.setWidth(width, true); } else { this.setWidth(width); } } else if (scaleX !== undefined) { this.setScaleX(scaleX); } if (height !== undefined) { if ((width === undefined) && (scaleX === undefined)) { this.setHeight(height, true); } else { this.setHeight(height); } } else if (scaleY !== undefined) { this.setScaleY(scaleY); } if (o.hasOwnProperty('leftSpace')) { this.setLeftSpace(o.leftSpace); } if (o.hasOwnProperty('rightSpace')) { this.setRightSpace(o.rightSpace); } if (o.hasOwnProperty('align')) { this.setAlign(o.align); } return this; } setDrawBelowCallback(callback) { this.drawBelowCallback = callback; return this; } setDrawAboveCallback(callback) { this.drawAboveCallback = callback; return this; } reset() { this .setVisible() .setAlpha(1) .setPosition(0, 0) .setRotation(0) .setScale(1, 1) .setLeftSpace(0).setRightSpace(0) .setOrigin(0) .setAlign() .setDrawBelowCallback() .setDrawAboveCallback(); return this; } // Override get willRender() { return this.visible && (this.alpha > 0); } get drawX() { var x = this.x + this.leftSpace + this.offsetX - (this.originX * this.width); return (this.parent._textOX * this.scrollFactorX) + x; } get drawY() { var y = this.y + this.offsetY; return (this.parent._textOY * this.scrollFactorY) + y; } // Override get drawTLX() { return 0; } get drawTLY() { return 0; } get drawBLX() { return 0; } get drawBLY() { return 0; } get drawTRX() { return 0; } get drawTRY() { return 0; } get drawBRX() { return 0; } get drawBRY() { return 0; } get drawCenterX() { return (this.drawTRX + this.drawTLX) / 2; } get drawCenterY() { return (this.drawBLY + this.drawTLY) / 2; } } Object.assign( RenderBase.prototype, Methods$1, ); 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; } }; var GetProperty = function (name, config, defaultConfig) { if (config.hasOwnProperty(name)) { return config[name]; } else { return defaultConfig[name]; } }; const GetValue$7 = 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$7(value, 'x', 0); defaultRadiusY = GetValue$7(value, 'y', 0); } var radius = this.cornerRadius; radius.tl = GetRadius(GetValue$7(value, 'tl', undefined), defaultRadiusX, defaultRadiusY); radius.tr = GetRadius(GetValue$7(value, 'tr', undefined), defaultRadiusX, defaultRadiusY); radius.bl = GetRadius(GetValue$7(value, 'bl', undefined), defaultRadiusX, defaultRadiusY); radius.br = GetRadius(GetValue$7(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$7(value, 'x', 0); radius.y = GetValue$7(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 ); }; const GetValue$6 = Phaser.Utils.Objects.GetValue; class Background extends RenderBase { constructor(parent, config) { super(parent, 'background'); this.setScrollFactor(0); this.setColor( GetValue$6(config, 'color', null), GetValue$6(config, 'color2', null), GetValue$6(config, 'horizontalGradient', true) ); this.setStroke( GetValue$6(config, 'stroke', null), GetValue$6(config, 'strokeThickness', 2) ); this.setCornerRadius( GetValue$6(config, 'cornerRadius', 0), GetValue$6(config, 'cornerIteration', null) ); } set color(value) { value = GetStyle(value, this.canvas, this.context); this.setDirty(this._color != value); this._color = value; } get color() { return this._color; } set color2(value) {