diff --git a/examples/roundrectangleprogress/roundrectangleprogress.bat b/examples/roundrectangleprogress/roundrectangleprogress.bat new file mode 100644 index 0000000000..87d1ef2a2b --- /dev/null +++ b/examples/roundrectangleprogress/roundrectangleprogress.bat @@ -0,0 +1,4 @@ +set main=./examples/roundrectangleprogress/roundrectangleprogress.js +cd .. +cd .. +npm run watch \ No newline at end of file diff --git a/examples/roundrectangleprogress/roundrectangleprogress.js b/examples/roundrectangleprogress/roundrectangleprogress.js new file mode 100644 index 0000000000..c5302e15a4 --- /dev/null +++ b/examples/roundrectangleprogress/roundrectangleprogress.js @@ -0,0 +1,94 @@ +import phaser from 'phaser/src/phaser.js'; +import RoundrRctangleProgressPlugin from '../../plugins/roundrectangleprogress-plugin.js'; +import Dat from '../../plugins/utils/dat.gui/dat.gui.min.js'; + +const COLOR_MAIN = 0x4e342e; +const COLOR_LIGHT = 0x7b5e57; +const COLOR_DARK = 0x260e04; + +class Demo extends Phaser.Scene { + constructor() { + super({ + key: 'examples' + }) + } + + preload() { } + + create() { + //var radius = 30; + var radius = { tl: 0, tr: 0, bl: 30, br: 30 }; + + var bar0 = this.add.rexRoundRectangleProgress({ + x: 200, y: 150, + width: 200, height: 90, + radius: radius, + barColor: COLOR_MAIN, + trackColor: COLOR_DARK, + trackStrokeColor: COLOR_LIGHT, + value: 0.5 + }) + + var bar1 = this.add.rexRoundRectangleProgress({ + x: 500, y: 150, + width: 200, height: 90, + radius: radius, + rtl: true, + barColor: COLOR_MAIN, + trackColor: COLOR_DARK, + trackStrokeColor: COLOR_LIGHT, + value: 0.5 + }) + + var bar2 = this.add.rexRoundRectangleProgress({ + x: 200, y: 400, + width: 90, height: 200, + radius: radius, + orientation: 1, + barColor: COLOR_MAIN, + trackColor: COLOR_DARK, + trackStrokeColor: COLOR_LIGHT, + value: 0.5 + }) + + var bar3 = this.add.rexRoundRectangleProgress({ + x: 500, y: 400, + width: 90, height: 200, + radius: radius, + rtl: true, orientation: 1, + barColor: COLOR_MAIN, + trackColor: COLOR_DARK, + trackStrokeColor: COLOR_LIGHT, + value: 0.5 + }) + + var gui = new Dat.GUI(); + gui.add(bar0, 'value', 0, 1); + gui.add(bar1, 'value', 0, 1); + gui.add(bar2, 'value', 0, 1); + gui.add(bar3, 'value', 0, 1); + } + + update() { } +} + +var config = { + type: Phaser.AUTO, + parent: 'phaser-example', + width: 800, + height: 600, + scale: { + mode: Phaser.Scale.FIT, + autoCenter: Phaser.Scale.CENTER_BOTH, + }, + scene: Demo, + plugins: { + global: [{ + key: 'rexRoundrRctangleProgress', + plugin: RoundrRctangleProgressPlugin, + start: true + }] + } +}; + +var game = new Phaser.Game(config); \ No newline at end of file diff --git a/plugin-list.js b/plugin-list.js index a136272638..bf4d37c6f5 100644 --- a/plugin-list.js +++ b/plugin-list.js @@ -19,6 +19,7 @@ module.exports = { 'customshapesplugin': './plugins/customshapes-plugin.js', 'circularprogressplugin': './plugins/circularprogress-plugin.js', 'lineprogressplugin': './plugins/lineprogress-plugin.js', + 'roundrectangleprogressplugin': './plugins/roundrectangleprogress-plugin.js', 'customprogressplugin': './plugins/customprogress-plugin.js', 'checkboxplugin': './plugins/checkbox-plugin.js', 'toggleswitchplugin': './plugins/toggleswitch-plugin.js', @@ -258,7 +259,8 @@ module.exports = { 'circularprogress': './templates/ui/circularprogress/CircularProgress.js', 'circularprogresscanvas': './templates/ui/circularprogresscanvas/CircularProgressCanvas.js', 'lineprogress': './templates/ui/lineprogress/LineProgress.js', - 'lineprogresscanvas': './templates/ui/lineprogresscanvas/LineProgressCanvas.js', + 'roundrectangleprogress': './templates/ui/roundrectangleprogress/RoundRectangleProgress.js', + 'lineprogresscanvas': './templates/ui/lineprogresscanvas/LineProgressCanvas.js', 'knob': './templates/ui/knob/Knob.js', 'customshapes': './templates/ui/customshapes/CustomShapes.js', 'customprogress': './templates/ui/customprogress/CustomProgress.js', diff --git a/plugins/gameobjects/shape/lineprogress/LineProgress.js b/plugins/gameobjects/shape/lineprogress/LineProgress.js index 5ad4469214..8775af4abb 100644 --- a/plugins/gameobjects/shape/lineprogress/LineProgress.js +++ b/plugins/gameobjects/shape/lineprogress/LineProgress.js @@ -10,24 +10,33 @@ class LineProgress extends ProgressBase(BaseShapes) { constructor(scene, x, y, width, height, barColor, value, config) { if (IsPlainObject(x)) { config = x; - x = GetValue(config, 'x', 0); - y = GetValue(config, 'y', 0); - width = GetValue(config, 'width', 2); - height = GetValue(config, 'height', 2); - barColor = GetValue(config, 'barColor', undefined); - value = GetValue(config, 'value', 0); + + x = config.x; + y = config.y; + width = config.width; + height = config.height; + barColor = config.barColor; + value = config.value; } else if (IsPlainObject(width)) { config = width; - width = GetValue(config, 'width', 2); - height = GetValue(config, 'height', 2); - barColor = GetValue(config, 'barColor', undefined); - value = GetValue(config, 'value', 0); + + width = config.width; + height = config.height; + barColor = config.barColor; + value = config.value; } else if (IsPlainObject(barColor)) { config = barColor; - barColor = GetValue(config, 'barColor', undefined); - value = GetValue(config, 'value', 0); + + barColor = config.barColor; + value = config.value; } + if (x === undefined) { x = 0; } + if (y === undefined) { y = 0; } + if (width === undefined) { width = 2; } + if (height === undefined) { height = width; } + if (value === undefined) { value = 0; } + super(scene, x, y, width, height, config); this.type = 'rexLineProgress'; diff --git a/plugins/gameobjects/shape/lineprogress/UpdateShapes.js b/plugins/gameobjects/shape/lineprogress/UpdateShapes.js index 76ed217633..66f0315f59 100644 --- a/plugins/gameobjects/shape/lineprogress/UpdateShapes.js +++ b/plugins/gameobjects/shape/lineprogress/UpdateShapes.js @@ -2,6 +2,7 @@ var UpdateShapes = function () { var skewX = this.skewX; var width = this.width - Math.abs(skewX); var height = this.height; + var trackFill = this.getShape('trackFill'); trackFill.fillStyle(this.trackColor); if (trackFill.isFilled) { @@ -11,7 +12,6 @@ var UpdateShapes = function () { width, height, // x1, y1 skewX // skewX ) - .close() } var bar = this.getShape('bar'); @@ -32,7 +32,6 @@ var UpdateShapes = function () { barX1, height, // x1, y1 skewX // skew ) - .close() } var trackStroke = this.getShape('trackStroke'); @@ -44,7 +43,6 @@ var UpdateShapes = function () { width, height, // x1, y1 skewX // skewX ) - .end() } } @@ -64,6 +62,8 @@ var BuildRectangle = function (lines, x0, y0, x1, y1, skewX) { .lineTo(x0, y0).lineTo(startX, y0) } + lines.close(); + return lines; } diff --git a/plugins/gameobjects/shape/roundrectangle/RoundRectangle.d.ts b/plugins/gameobjects/shape/roundrectangle/RoundRectangle.d.ts index ae1ca2dc20..ff16b086c6 100644 --- a/plugins/gameobjects/shape/roundrectangle/RoundRectangle.d.ts +++ b/plugins/gameobjects/shape/roundrectangle/RoundRectangle.d.ts @@ -25,9 +25,9 @@ declare namespace RoundRectangle { y?: number, width?: number, height?: number, - radius?: number | RoundRectangle.IRadiusConfig | + radius?: number | IRadiusConfig | ({ - radius?: (number | RoundRectangle.IRadiusConfig), + radius?: (number | IRadiusConfig), iteration?: number }), diff --git a/plugins/gameobjects/shape/roundrectangle/RoundRectangle.js b/plugins/gameobjects/shape/roundrectangle/RoundRectangle.js index c4dbcb1ee1..aa4cfdf7b9 100644 --- a/plugins/gameobjects/shape/roundrectangle/RoundRectangle.js +++ b/plugins/gameobjects/shape/roundrectangle/RoundRectangle.js @@ -1,5 +1,6 @@ import PolygnBase from './PolygnBase.js'; import RoundRectangleGeom from '../../../geom/roundrectangle/RoundRectangle.js'; +import IsArcCorner from '../utils/IsArcCorner.js'; import LineTo from '../../../geom/pathdata/LineTo.js'; import ArcTo from '../../../geom/pathdata/ArcTo.js'; @@ -48,8 +49,7 @@ class RoundRectangle extends PolygnBase { geom.setTo(0, 0, width, height, radius); } - var iteration = GetValue(radiusConfig, 'iteration', undefined); - this.setIteration(iteration); + this.setIteration(GetValue(radiusConfig, 'iteration', undefined)); this.setPosition(x, y); this.setFillStyle(fillColor, fillAlpha); @@ -302,10 +302,6 @@ class RoundRectangle extends PolygnBase { } -var IsArcCorner = function (radius) { - return ((radius.x > 0) && (radius.y > 0)); -} - const ShapeTypeMap = { rectangle: 0, circle: 1 diff --git a/plugins/gameobjects/shape/roundrectangleprogress/Creator.d.ts b/plugins/gameobjects/shape/roundrectangleprogress/Creator.d.ts new file mode 100644 index 0000000000..c5e2d1eb92 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/Creator.d.ts @@ -0,0 +1,12 @@ +import RoundRectangleProgress from './RoundRectangleProgress'; + +export default Creator; + +declare namespace Creator { + interface IConfig extends Phaser.Types.GameObjects.GameObjectConfig { } +} + +declare function Creator( + config?: Creator.IConfig, + addToScene?: boolean, +): RoundRectangleProgress; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/Creator.js b/plugins/gameobjects/shape/roundrectangleprogress/Creator.js new file mode 100644 index 0000000000..3e7a1fd5ca --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/Creator.js @@ -0,0 +1,13 @@ +import RoundRectangleProgress from './RoundRectangleProgress.js'; + +const BuildGameObject = Phaser.GameObjects.BuildGameObject; + +export default function (config, addToScene) { + if (config === undefined) { config = {}; } + if (addToScene !== undefined) { + config.add = addToScene; + } + var gameObject = new RoundRectangleProgress(this.scene, config); + BuildGameObject(this.scene, gameObject, config); + return gameObject; +}; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/Factory.d.ts b/plugins/gameobjects/shape/roundrectangleprogress/Factory.d.ts new file mode 100644 index 0000000000..46cb515f0f --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/Factory.d.ts @@ -0,0 +1,19 @@ +import RoundRectangleProgress from './RoundRectangleProgress'; + +export default function ( + config?: RoundRectangleProgress.IConfig +): RoundRectangleProgress; + +export default function ( + x?: number, y?: number, + width?: number, height?: number, + config?: RoundRectangleProgress.IConfig +): RoundRectangleProgress; + +export default function ( + x?: number, y?: number, + width?: number, height?: number, + barColor?: string | number, + value?: number, + config?: RoundRectangleProgress.IConfig +): RoundRectangleProgress; diff --git a/plugins/gameobjects/shape/roundrectangleprogress/Factory.js b/plugins/gameobjects/shape/roundrectangleprogress/Factory.js new file mode 100644 index 0000000000..a503241f8d --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/Factory.js @@ -0,0 +1,7 @@ +import RoundRectangleProgress from './RoundRectangleProgress.js'; + +export default function (x, y, width, height, barColor, value, config) { + var gameObject = new RoundRectangleProgress(this.scene, x, y, width, height, barColor, value, config); + this.scene.add.existing(gameObject); + return gameObject; +}; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.d.ts b/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.d.ts new file mode 100644 index 0000000000..208e08a612 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.d.ts @@ -0,0 +1,163 @@ +import BaseShapes from '../shapes/BaseShapes'; + +// import * as Phaser from 'phaser'; +export default RoundRectangleProgress; + +declare namespace RoundRectangleProgress { + + type ValueChangeCallbackType = ( + newValue: number, + oldValue: number, + circularProgress: RoundRectangleProgress + ) => void; + + type CornerRadiusType = { + x: number, + y: number, + convex: boolean + }; + + interface IRadiusConfig { + tl?: (number | { x?: number, y?: number }), + tr?: (number | { x?: number, y?: number }), + bl?: (number | { x?: number, y?: number }), + br?: (number | { x?: number, y?: number }), + + x?: number, + y?: number, + } + + type OrientationTypes = 0 | 1 | 'x' | 'y' | 'h' | 'v' | 'horizontal' | 'vertical' | 'left-to-right' | 'top-to-bottom'; + + interface IConfig { + x?: number, y?: number, + width?: number, height?: number, + radius?: number | IRadiusConfig | + ({ + radius?: (number | IRadiusConfig), + iteration?: number + }), + + trackColor?: string | number, + trackStrokeThickness?: number, + trackStrokeColor?: string | number, + barColor?: string | number, + + rtl?: boolean, + orientation?: OrientationTypes, + + value?: number, + + easeValue?: { + duration?: number, + ease?: string + }, + + valuechangeCallback: ValueChangeCallbackType, + } + + namespace Events { + type ValueChangeCallbackType = ( + newValue: number, + oldValue: number, + circularProgress: RoundRectangleProgress + ) => void; + } +} + +declare class RoundRectangleProgress extends BaseShapes { + constructor( + scene: Phaser.Scene, + config?: RoundRectangleProgress.IConfig + ); + + constructor( + scene: Phaser.Scene, + x?: number, y?: number, + width?: number, height?: number, + radiusConfig?: number | RoundRectangleProgress.IRadiusConfig | + ({ + radius?: (number | RoundRectangleProgress.IRadiusConfig), + iteration?: number + }), + config?: RoundRectangleProgress.IConfig + ); + + constructor( + scene: Phaser.Scene, + x?: number, y?: number, + width?: number, height?: number, + radiusConfig?: number | RoundRectangleProgress.IRadiusConfig | + ({ + radius?: (number | RoundRectangleProgress.IRadiusConfig), + iteration?: number + }), + barColor?: string | number, + value?: number, + config?: RoundRectangleProgress.IConfig + ); + + getValue(min?: number, max?: number): number; + setValue(value?: number, min?: number, max?: number): this; + addValue(inc?: number, min?: number, max?: number): this; + value: number; + + easeValueTo(value?: number, min?: number, max?: number): this; + stopEaseValue(): this; + setEaseValueDuration(duration: number): this; + setEaseValueFunction(ease: string): this; + + setTrackColor(color?: number): this; + trackColor: number; + + setTrackStroke( + lineWidth?: number, + color?: number + ): this; + trackStrokeThickness: number; + trackStrokeColor: number; + + setBarColor(color?: number): this; + barColor: number; + + setRTL(enable?: boolean): this; + rtl: boolean; + + setOrientation(orientation: RoundRectangleProgress.OrientationTypes): this; + orientation: number; + + setIteration(iteration: number): this; + iteration: number; + + setRadius( + value: number | RoundRectangleProgress.IRadiusConfig + ): this; + radius: number; + + setRadiusTL( + value: number | RoundRectangleProgress.IRadiusConfig + ): this; + radiusTL: number; + + setRadiusTR( + value: number | RoundRectangleProgress.IRadiusConfig + ): this; + radiusTR: number; + + setRadiusBL( + value: number | RoundRectangleProgress.IRadiusConfig + ): this; + radiusBL: number; + + setRadiusBR( + value: number | RoundRectangleProgress.IRadiusConfig + ): this; + radiusBR: number; + + readonly cornerRadius: { + tl: RoundRectangleProgress.CornerRadiusType, + tr: RoundRectangleProgress.CornerRadiusType, + bl: RoundRectangleProgress.CornerRadiusType, + br: RoundRectangleProgress.CornerRadiusType, + }; +} \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.js b/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.js new file mode 100644 index 0000000000..1fbdb6f80e --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.js @@ -0,0 +1,291 @@ +import BaseShapes from '../shapes/BaseShapes.js'; +import ProgressBase from '../../../utils/progressbase/ProgressBase.js'; +import { Lines } from '../shapes/geoms/index.js'; +import UpdateShapes from './methods/UpdateShapes.js'; +import RoundRectangleGeom from '../../../geom/roundrectangle/RoundRectangle.js'; +import GetOrientationMode from '../../../utils/orientation/GetOrientationMode.js'; + +const GetValue = Phaser.Utils.Objects.GetValue; +const IsPlainObject = Phaser.Utils.Objects.IsPlainObject; + +class RoundRectangleProgress extends ProgressBase(BaseShapes) { + constructor(scene, x, y, width, height, radiusConfig, barColor, value, config) { + if (IsPlainObject(x)) { + config = x; + + x = config.x; + y = config.y; + width = config.width; + height = config.height; + radiusConfig = config.radius; + barColor = config.barColor; + value = config.value; + } else if (IsPlainObject(width)) { + config = width; + + width = config.width; + height = config.height; + radiusConfig = config.radius; + barColor = config.barColor; + value = config.value; + } else if (IsPlainObject(radiusConfig)) { + config = radiusConfig; + + radiusConfig = config.radius; + barColor = config.barColor; + value = config.value; + } + + if (x === undefined) { x = 0; } + if (y === undefined) { y = 0; } + if (width === undefined) { width = 1; } + if (height === undefined) { height = width; } + if (radiusConfig === undefined) { radiusConfig = 0; } + if (value === undefined) { value = 0; } + + super(scene, x, y, width, height, config); + this.type = 'rexRoundRectangleProgress'; + + this.bootProgressBase(config); + + this + .addShape((new Lines()).setName('trackFill')) + .addShape((new Lines()).setName('bar')) + .addShape((new Lines()).setName('trackStroke')) + + this.setTrackColor(GetValue(config, 'trackColor', undefined)); + this.setBarColor(barColor); + this.setTrackStroke(GetValue(config, 'trackStrokeThickness', 2), GetValue(config, 'trackStrokeColor', undefined)); + + this.setOrientation(GetValue(config, 'orientation', 0)); + this.setRTL(GetValue(config, 'rtl', false)); + + this.rrGeom = new RoundRectangleGeom(); // For radiusConfig only + this.setRadius(radiusConfig); + + this.setIteration(GetValue(radiusConfig, 'iteration', undefined)); + + this.setValue(value); + } + + get trackColor() { + return this._trackColor; + } + + set trackColor(value) { + this.dirty = this.dirty || (this._trackColor != value); + this._trackColor = value; + } + + setTrackColor(color) { + this.trackColor = color; + return this; + } + + get trackStrokeColor() { + return this._trackStrokeColor; + } + + set trackStrokeColor(value) { + this.dirty = this.dirty || (this._trackStrokeColor != value); + this._trackStrokeColor = value; + } + + get trackStrokeThickness() { + return this._trackStrokeThickness; + } + + set trackStrokeThickness(value) { + this.dirty = this.dirty || (this._trackStrokeThickness != value); + this._trackStrokeThickness = value; + } + + setTrackStroke(lineWidth, color) { + this.trackStrokeThickness = lineWidth; + this.trackStrokeColor = color; + return this; + } + + get barColor() { + return this._barColor; + } + + set barColor(value) { + this.dirty = this.dirty || (this._barColor != value); + this._barColor = value; + } + + setBarColor(color) { + this.barColor = color; + return this; + } + + get orientation() { + return this._orientation; + } + + set orientation(value) { + value = GetOrientationMode(value); + this.dirty = this.dirty || (this._orientation != value); + this._orientation = value; + } + + setOrientation(value) { + this.orientation = value; + return this; + } + + get rtl() { + return this._rtl; + } + + set rtl(value) { + value = !!value; + this.dirty = this.dirty || (this._rtl != value); + this._rtl = value; + } + + setRTL(enable) { + if (enable === undefined) { + enable = true; + } + this.rtl = enable; + return this; + } + + get radius() { + return this.rrGeom.radius; + } + + set radius(value) { + this.rrGeom.setRadius(value); + this.dirty = true; + } + + get radiusTL() { + return this.rrGeom.radiusTL; + } + + set radiusTL(value) { + this.rrGeom.radiusTL = value; + this.dirty = true; + } + + get radiusTR() { + return this.rrGeom.radiusTR; + } + + set radiusTR(value) { + this.rrGeom.radiusTR = value; + this.dirty = true; + } + + get radiusBL() { + return this.rrGeom.radiusBL; + } + + set radiusBL(value) { + this.rrGeom.radiusBL = value; + this.dirty = true; + } + + get radiusBR() { + return this.rrGeom.radiusBR; + } + + set radiusBR(value) { + this.rrGeom.radiusBR = value; + this.dirty = true; + } + + setRadius(value) { + if (value === undefined) { + value = 0; + } + this.radius = value; + return this; + } + + setRadiusTL(value) { + if (value === undefined) { + value = 0; + } + this.radiusTL = value; + return this; + } + + setRadiusTR(value) { + if (value === undefined) { + value = 0; + } + this.radiusTR = value; + return this; + } + + setRadiusBL(value) { + if (value === undefined) { + value = 0; + } + this.radiusBL = value; + return this; + } + + setRadiusBR(value) { + if (value === undefined) { + value = 0; + } + this.radiusBR = value; + return this; + } + + get cornerRadius() { + return this.rrGeom.cornerRadius; + } + + set cornerRadius(value) { + this.radius = value; + } + + setCornerRadius(value) { + return this.setRadius(value); + } + + get iteration() { + return this._iteration; + } + + set iteration(value) { + // Set iteration first time + if (this._iteration === undefined) { + this._iteration = value; + return; + } + + // Change iteration value + if (this._iteration === value) { + return; + } + + this._iteration = value; + this.dirty = true; + } + + setIteration(iteration) { + if (iteration === undefined) { + iteration = 6; + } + this.iteration = iteration; + return this; + } +} + +var Methods = { + updateShapes: UpdateShapes, +} + +Object.assign( + RoundRectangleProgress.prototype, + Methods, +) + +export default RoundRectangleProgress; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangle.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangle.js new file mode 100644 index 0000000000..6c641c966a --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangle.js @@ -0,0 +1,84 @@ +import IsArcCorner from '../../utils/IsArcCorner.js'; + +var BuildRoundRectangle = function ( + lines, + width, height, cornerRadius, + iteration +) { + + lines + .setIterations(iteration) + .start() + + // Top-left + var radius = cornerRadius.tl; + if (IsArcCorner(radius)) { + if (radius.convex) { + var centerX = radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180, 270, false); + } else { + var centerX = 0; + var centerY = 0; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90, 0, true); + } + } else { + lines.lineTo(0, 0); + } + + // Top-right + var radius = cornerRadius.tr; + if (IsArcCorner(radius)) { + if (radius.convex) { + var centerX = width - radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270, 360, false); + } else { + var centerX = width; + var centerY = 0; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180, 90, true); + } + } else { + lines.lineTo(width, 0); + } + + // Bottom-right + var radius = cornerRadius.br; + if (IsArcCorner(radius)) { + if (radius.convex) { + var centerX = width - radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 0, 90, false); + } else { + var centerX = width; + var centerY = height; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270, 180, true); + } + } else { + lines.lineTo(width, height); + } + + // Bottom-left + var radius = cornerRadius.bl; + if (IsArcCorner(radius)) { + if (radius.convex) { + var centerX = radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90, 180, false); + } else { + var centerX = 0; + var centerY = height; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 360, 270, true); + } + } else { + lines.lineTo(0, height); + } + + lines.close(); + + return lines; +} + + + +export default BuildRoundRectangle; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBar.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBar.js new file mode 100644 index 0000000000..5470cd799f --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBar.js @@ -0,0 +1,38 @@ +import BuildRoundRectangle from './BuildRoundRectangle.js'; +import BuildRoundRectangleBarDirection0 from './BuildRoundRectangleBarDirection0.js'; +import BuildRoundRectangleBarDirection1 from './BuildRoundRectangleBarDirection1.js'; +import BuildRoundRectangleBarDirection2 from './BuildRoundRectangleBarDirection2.js'; +import BuildRoundRectangleBarDirection3 from './BuildRoundRectangleBarDirection3.js'; + +var BuildRoundRectangleBar = function ( + lines, + width, height, cornerRadius, + value, orientation, rtl, + iteration +) { + + lines + .setIterations(iteration) + .start() + + if (value === 0) { + return lines; + } else if (value === 1) { + return BuildRoundRectangle(lines, width, height, cornerRadius, iteration); + } + + var callback; + if (orientation === 0) { + callback = (rtl) ? BuildRoundRectangleBarDirection2 : BuildRoundRectangleBarDirection0; + } else { + callback = (rtl) ? BuildRoundRectangleBarDirection3 : BuildRoundRectangleBarDirection1; + } + + callback(lines, width, height, cornerRadius, value); + + lines.close(); + + return lines; +} + +export default BuildRoundRectangleBar; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection0.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection0.js new file mode 100644 index 0000000000..cd92a7f543 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection0.js @@ -0,0 +1,67 @@ +import IsArcCorner from '../../utils/IsArcCorner.js'; + +var RadToDeg = Phaser.Math.RadToDeg; + +var BuildRoundRectangleBarDirection0 = function ( + lines, + width, height, cornerRadius, + value, +) { + var barWidth = width * value; + + // Top-left + var radius = cornerRadius.tl; + if (IsArcCorner(radius)) { + var theta; + if (barWidth > radius.x) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.x - barWidth) / radius.x)); + } + var centerX = radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180, 180 + theta, false); + } else { + lines.lineTo(0, 0); + } + + // Top-right + var radius = cornerRadius.tr; + if (IsArcCorner(radius) && (barWidth > (width - radius.x))) { + var theta = 90 - RadToDeg(Math.acos((barWidth - (width - radius.x)) / radius.x)); + var centerX = width - radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270, 270 + theta, false); + } else { + lines.lineTo(barWidth, 0); + } + + // Bottom-right + var radius = cornerRadius.br; + if (IsArcCorner(radius) && (barWidth > (width - radius.x))) { + var theta = 90 - RadToDeg(Math.acos((barWidth - (width - radius.x)) / radius.x)); + var centerX = width - radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90 - theta, 90, false); + } else { + lines.lineTo(barWidth, height); + } + + // Bottom-left + var radius = cornerRadius.bl; + if (IsArcCorner(radius)) { + var theta; + if (barWidth > radius.x) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.x - barWidth) / radius.x)); + } + var centerX = radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180 - theta, 180, false); + } else { + lines.lineTo(0, height); + } +} + +export default BuildRoundRectangleBarDirection0; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection1.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection1.js new file mode 100644 index 0000000000..03c43dcd4f --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection1.js @@ -0,0 +1,67 @@ +import IsArcCorner from '../../utils/IsArcCorner.js'; + +var RadToDeg = Phaser.Math.RadToDeg; + +var BuildRoundRectangleBarDirection1 = function ( + lines, + width, height, cornerRadius, + value, +) { + var barHeight = height * value; + + // Top-left + var radius = cornerRadius.tl; + if (IsArcCorner(radius)) { + var theta; + if (barHeight > radius.y) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.y - barHeight) / radius.y)); + } + var centerX = radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270 - theta, 270, false); + } else { + lines.lineTo(0, 0); + } + + // Top-right + var radius = cornerRadius.tr; + if (IsArcCorner(radius)) { + var theta; + if (barHeight > radius.y) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.y - barHeight) / radius.y)); + } + var centerX = width - radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270, 270 + theta, false); + } else { + lines.lineTo(width, 0); + } + + // Bottom-right + var radius = cornerRadius.br; + if (IsArcCorner(radius) && (barHeight > (height - radius.y))) { + var theta = 90 - RadToDeg(Math.acos((barHeight - (height - radius.y)) / radius.y)); + var centerX = width - radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 0, 0 + theta, false); + } else { + lines.lineTo(width, barHeight); + } + + // Bottom-left + var radius = cornerRadius.bl; + if (IsArcCorner(radius) && (barHeight > (height - radius.y))) { + var theta = 90 - RadToDeg(Math.acos((barHeight - (height - radius.y)) / radius.y)); + var centerX = radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180 - theta, 180, false); + } else { + lines.lineTo(0, barHeight); + } +} + +export default BuildRoundRectangleBarDirection1; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection2.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection2.js new file mode 100644 index 0000000000..a61f6b2f80 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection2.js @@ -0,0 +1,68 @@ +import IsArcCorner from '../../utils/IsArcCorner.js'; + +var RadToDeg = Phaser.Math.RadToDeg; + +var BuildRoundRectangleBarDirection2 = function ( + lines, + width, height, cornerRadius, + value, +) { + var barWidth = width * value; + + // Top-right + var radius = cornerRadius.tr; + if (IsArcCorner(radius)) { + var theta; + if (barWidth > radius.x) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.x - barWidth) / radius.x)); + } + var centerX = width - radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 360 - theta, 360, false); + } else { + lines.lineTo(width, 0); + } + + // Bottom-right + var radius = cornerRadius.br; + if (IsArcCorner(radius)) { + var theta; + if (barWidth > radius.x) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.x - barWidth) / radius.x)); + } + var centerX = width - radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 0, 0 + theta, false); + } else { + lines.lineTo(width, height); + } + + // Bottom-left + var radius = cornerRadius.bl; + if (IsArcCorner(radius) && (barWidth > (width - radius.x))) { + var theta = 90 - RadToDeg(Math.acos((barWidth - (width - radius.x)) / radius.x)); + var centerX = radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90, 90 + theta, false); + } else { + lines.lineTo(width - barWidth, height); + } + + // Top-left + var radius = cornerRadius.tl; + if (IsArcCorner(radius) && (barWidth > (width - radius.x))) { + var theta = 90 - RadToDeg(Math.acos((barWidth - (width - radius.x)) / radius.x)); + var centerX = radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 270 - theta, 270, false); + } else { + lines.lineTo(width - barWidth, 0); + } + +} + +export default BuildRoundRectangleBarDirection2; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection3.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection3.js new file mode 100644 index 0000000000..72b5de7014 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/BuildRoundRectangleBarDirection3.js @@ -0,0 +1,66 @@ +import IsArcCorner from '../../utils/IsArcCorner.js'; + +var RadToDeg = Phaser.Math.RadToDeg; + +var BuildRoundRectangleBarDirection3 = function ( + lines, + width, height, cornerRadius, + value, +) { + var barHeight = height * value; + + // Bottom-right + var radius = cornerRadius.br; + if (IsArcCorner(radius)) { + if (barHeight > radius.y) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.y - barHeight) / radius.y)); + } + var centerX = width - radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90 - theta, 90, false); + } else { + lines.lineTo(width, height); + } + + // Bottom-left + var radius = cornerRadius.bl; + if (IsArcCorner(radius)) { + if (barHeight > radius.y) { + theta = 90; + } else { + theta = RadToDeg(Math.acos((radius.y - barHeight) / radius.y)); + } + var centerX = radius.x; + var centerY = height - radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 90, 90 + theta, false); + } else { + lines.lineTo(0, height); + } + + // Top-left + var radius = cornerRadius.tl; + if (IsArcCorner(radius) && (barHeight > (height - radius.y))) { + var theta = 90 - RadToDeg(Math.acos((barHeight - (height - radius.y)) / radius.y)); + var centerX = radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 180, 180 + theta, false); + } else { + lines.lineTo(0, height - barHeight); + } + + // Top-right + var radius = cornerRadius.tr; + if (IsArcCorner(radius) && (barHeight > (height - radius.y))) { + var theta = 90 - RadToDeg(Math.acos((barHeight - (height - radius.y)) / radius.y)); + var centerX = width - radius.x; + var centerY = radius.y; + lines.ellipticalArc(centerX, centerY, radius.x, radius.y, 360 - theta, 360, false); + } else { + lines.lineTo(width, height - barHeight); + } + +} + +export default BuildRoundRectangleBarDirection3; \ No newline at end of file diff --git a/plugins/gameobjects/shape/roundrectangleprogress/methods/UpdateShapes.js b/plugins/gameobjects/shape/roundrectangleprogress/methods/UpdateShapes.js new file mode 100644 index 0000000000..a082aec2d2 --- /dev/null +++ b/plugins/gameobjects/shape/roundrectangleprogress/methods/UpdateShapes.js @@ -0,0 +1,45 @@ +import BuildRoundRectangle from './BuildRoundRectangle.js'; +import BuildRoundRectangleBar from './BuildRoundRectangleBar.js'; + +var UpdateShapes = function () { + var width = this.width; + var height = this.height; + var cornerRadius = this.rrGeom.cornerRadius; + var value = this.value; + var orientation = this.orientation; + var rtl = this.rtl; + var iteration = this.iteration + 1; + + var trackFill = this.getShape('trackFill'); + trackFill.fillStyle(this.trackColor); + if (trackFill.isFilled) { + BuildRoundRectangle( + trackFill, + width, height, cornerRadius, + iteration + ) + } + + var bar = this.getShape('bar'); + bar.fillStyle(this.barColor); + if (bar.isFilled) { + BuildRoundRectangleBar( + bar, + width, height, cornerRadius, + value, orientation, rtl, + iteration + ) + } + + var trackStroke = this.getShape('trackStroke'); + trackStroke.lineStyle(this.trackStrokeThickness, this.trackStrokeColor); + if (trackStroke.isStroked) { + BuildRoundRectangle( + trackStroke, // lines + width, height, cornerRadius, + iteration + ) + } +} + +export default UpdateShapes; \ No newline at end of file diff --git a/plugins/gameobjects/shape/utils/IsArcCorner.js b/plugins/gameobjects/shape/utils/IsArcCorner.js new file mode 100644 index 0000000000..b4878eef9b --- /dev/null +++ b/plugins/gameobjects/shape/utils/IsArcCorner.js @@ -0,0 +1,5 @@ +var IsArcCorner = function (radius) { + return ((radius.x > 0) && (radius.y > 0)); +} + +export default IsArcCorner; \ No newline at end of file diff --git a/plugins/roundrectangleprogress-plugin.d.ts b/plugins/roundrectangleprogress-plugin.d.ts new file mode 100644 index 0000000000..925f9a9210 --- /dev/null +++ b/plugins/roundrectangleprogress-plugin.d.ts @@ -0,0 +1,16 @@ +import Factory from './gameobjects/shape/roundrectangleprogress/Factory'; +import Creator from './gameobjects/shape/roundrectangleprogress/Creator'; + +export default class extends Phaser.Plugins.BasePlugin { } + +declare module 'phaser' { + namespace GameObjects { + interface GameObjectFactory { + rexRoundRectangleProgress: typeof Factory, + } + + interface GameObjectCreator { + rexRoundRectangleProgress: typeof Creator, + } + } +} \ No newline at end of file diff --git a/plugins/roundrectangleprogress-plugin.js b/plugins/roundrectangleprogress-plugin.js new file mode 100644 index 0000000000..018596bdef --- /dev/null +++ b/plugins/roundrectangleprogress-plugin.js @@ -0,0 +1,23 @@ +import Factory from './gameobjects/shape/roundrectangleprogress/Factory.js'; +import Creator from './gameobjects/shape/roundrectangleprogress/Creator.js'; +import RoundRectangleProgress from './gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.js'; +import SetValue from './utils/object/SetValue.js'; + +class RoundRectangleProgressPlugin extends Phaser.Plugins.BasePlugin { + + constructor(pluginManager) { + super(pluginManager); + + // Register our new Game Object type + pluginManager.registerGameObject('rexRoundRectangleProgress', Factory, Creator); + } + + start() { + var eventEmitter = this.game.events; + eventEmitter.on('destroy', this.destroy, this); + } +} + +SetValue(window, 'RexPlugins.GameObjects.RoundRectangleProgress', RoundRectangleProgress); + +export default RoundRectangleProgressPlugin; \ No newline at end of file diff --git a/plugins/roundrectangleprogress.d.ts b/plugins/roundrectangleprogress.d.ts new file mode 100644 index 0000000000..eff12ee190 --- /dev/null +++ b/plugins/roundrectangleprogress.d.ts @@ -0,0 +1,2 @@ +import RoundRectangleProgress from './gameobjects/shape/roundrectangleprogress/RoundRectangleProgress'; +export default RoundRectangleProgress; \ No newline at end of file diff --git a/plugins/roundrectangleprogress.js b/plugins/roundrectangleprogress.js new file mode 100644 index 0000000000..b801beeb7e --- /dev/null +++ b/plugins/roundrectangleprogress.js @@ -0,0 +1,2 @@ +import RoundRectangleProgress from './gameobjects/shape/roundrectangleprogress/RoundRectangleProgress.js'; +export default RoundRectangleProgress; \ No newline at end of file diff --git a/plugins/utils/orientation/GetOrientationMode.js b/plugins/utils/orientation/GetOrientationMode.js new file mode 100644 index 0000000000..a16c97b9f5 --- /dev/null +++ b/plugins/utils/orientation/GetOrientationMode.js @@ -0,0 +1,20 @@ +var OrientationMode = { + x: 0, + h: 0, + horizontal: 0, + 'left-to-right': 0, + + y: 1, + v: 1, + vertical: 1, + 'top-to-bottom': 1 +}; + +var GetOrientationMode = function (orientation) { + if (typeof (orientation) === 'string') { + orientation = OrientationMode[orientation]; + } + return orientation; +} + +export default GetOrientationMode; \ No newline at end of file diff --git a/templates/ui/utils/GetOrientationMode.js b/templates/ui/utils/GetOrientationMode.js index a16c97b9f5..44c453fb67 100644 --- a/templates/ui/utils/GetOrientationMode.js +++ b/templates/ui/utils/GetOrientationMode.js @@ -1,20 +1,2 @@ -var OrientationMode = { - x: 0, - h: 0, - horizontal: 0, - 'left-to-right': 0, - - y: 1, - v: 1, - vertical: 1, - 'top-to-bottom': 1 -}; - -var GetOrientationMode = function (orientation) { - if (typeof (orientation) === 'string') { - orientation = OrientationMode[orientation]; - } - return orientation; -} - +import GetOrientationMode from '../../../plugins/utils/orientation/GetOrientationMode.js'; export default GetOrientationMode; \ No newline at end of file