@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
110 lines (109 loc) • 5.87 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Container } from "./Container";
var ProgressBar = /** @class */ (function (_super) {
__extends(ProgressBar, _super);
function ProgressBar(scene, config) {
var _this = _super.call(this, scene, config) || this;
_this._value = 0;
_this._config = config;
_this.Type = 'ProgressBar';
_this.initializeProgressBar();
return _this;
}
ProgressBar.prototype.initializeProgressBar = function () {
var _a, _b, _c;
if (this.bar) {
this.bar.destroy();
this.bar = undefined;
}
if (this.fill) {
this.fill.destroy();
this.fill = undefined;
}
this.createBar();
this.createFill();
this.setDepth((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.depth) !== null && _b !== void 0 ? _b : 1);
this.value = Math.min(1, Math.max(0, (_c = this._config.process) !== null && _c !== void 0 ? _c : 0));
this.value = Number(this.value.toFixed(2));
this.updateProgress(this.value);
this.setScrollFactor(this._config.isScrollFactor ? 0 : 1);
};
ProgressBar.prototype.createBar = function () {
var _a;
var _b = (_a = this._config.barTexture) !== null && _a !== void 0 ? _a : {}, _c = _b.x, x = _c === void 0 ? 0 : _c, _d = _b.y, y = _d === void 0 ? 0 : _d, _e = _b.key, key = _e === void 0 ? '' : _e, _f = _b.width, width = _f === void 0 ? 0 : _f, _g = _b.height, height = _g === void 0 ? 0 : _g;
this.bar = this.createImage(x, y, key, width, height);
this.addChildAt(this.bar, 0);
};
ProgressBar.prototype.createFill = function () {
var _a;
var _b = (_a = this._config.fillTexture) !== null && _a !== void 0 ? _a : {}, _c = _b.x, x = _c === void 0 ? 0 : _c, _d = _b.y, y = _d === void 0 ? 0 : _d, _e = _b.key, key = _e === void 0 ? '' : _e, _f = _b.width, width = _f === void 0 ? 0 : _f, _g = _b.height, height = _g === void 0 ? 0 : _g;
this.fill = this.createImage(x, y, key, width, height);
this.addChildAt(this.fill, 1);
};
ProgressBar.prototype.createImage = function (x, y, key, width, height) {
var image = this.scene.add.image(x, y, key);
image.setOrigin(0);
image.setDisplaySize(width, height);
return image;
};
ProgressBar.prototype.updateProgress = function (progress) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
var barWidth = (_b = (_a = this._config.barTexture) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
var fillOffset = (_d = (_c = this._config.fillTexture) === null || _c === void 0 ? void 0 : _c.x) !== null && _d !== void 0 ? _d : 0;
var realWidth = barWidth - fillOffset * 2;
if (this._config.flipX) {
// Progress bar from right to left
(_e = this.fill) === null || _e === void 0 ? void 0 : _e.setDisplaySize(progress * realWidth, (_g = (_f = this._config.fillTexture) === null || _f === void 0 ? void 0 : _f.height) !== null && _g !== void 0 ? _g : 0);
var rightEdge = barWidth - fillOffset;
var newX = rightEdge - (progress * realWidth);
(_h = this.fill) === null || _h === void 0 ? void 0 : _h.setPosition(newX, (_k = (_j = this._config.fillTexture) === null || _j === void 0 ? void 0 : _j.y) !== null && _k !== void 0 ? _k : 0);
}
else {
// Progress bar from left to right
(_l = this.fill) === null || _l === void 0 ? void 0 : _l.setDisplaySize(progress * realWidth, (_o = (_m = this._config.fillTexture) === null || _m === void 0 ? void 0 : _m.height) !== null && _o !== void 0 ? _o : 0);
(_p = this.fill) === null || _p === void 0 ? void 0 : _p.setPosition((_r = (_q = this._config.fillTexture) === null || _q === void 0 ? void 0 : _q.x) !== null && _r !== void 0 ? _r : 0, (_t = (_s = this._config.fillTexture) === null || _s === void 0 ? void 0 : _s.y) !== null && _t !== void 0 ? _t : 0);
}
this.updateConfig(this._config);
this.RefreshBounds();
};
Object.defineProperty(ProgressBar.prototype, "value", {
get: function () {
return this._value;
},
set: function (value) {
this._value = value;
},
enumerable: false,
configurable: true
});
ProgressBar.prototype.reDraw = function (newConfig) {
this._config = newConfig;
this._config.barTexture.width = this._config.width;
this._config.barTexture.height = this._config.height;
this.initializeProgressBar();
};
ProgressBar.prototype.destroy = function (fromScene) {
var _a, _b;
(_a = this.bar) === null || _a === void 0 ? void 0 : _a.destroy();
this.bar = undefined;
(_b = this.fill) === null || _b === void 0 ? void 0 : _b.destroy();
this.fill = undefined;
_super.prototype.destroy.call(this, fromScene);
};
return ProgressBar;
}(Container));
export { ProgressBar };