mai3-phaser-sdk
Version:
A UI component library based on the Phaser game engine
90 lines (89 loc) • 4.85 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;
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 = (_c = this._config.value) !== null && _c !== void 0 ? _c : 0;
};
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, frame = _b.frame, width = _b.width, height = _b.height, leftWidth = _b.leftWidth, rightWidth = _b.rightWidth, topHeight = _b.topHeight, bottomHeight = _b.bottomHeight;
this.bar = this.createNineSlice(x, y, key, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight);
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, frame = _b.frame, width = _b.width, height = _b.height, leftWidth = _b.leftWidth, rightWidth = _b.rightWidth, topHeight = _b.topHeight, bottomHeight = _b.bottomHeight;
this.fill = this.createNineSlice(x, y, key, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight);
this.addChildAt(this.fill, 1);
};
ProgressBar.prototype.createNineSlice = function (x, y, key, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight) {
var nineSlice = this.scene.add.nineslice(x, y, key, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight);
nineSlice.setOrigin(0);
return nineSlice;
};
ProgressBar.prototype.updateProgress = function (progress) {
var _a, _b, _c, _d, _e, _f;
var barWidth = (_b = (_a = this._config.barTexture) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
var fillWidth = (_d = (_c = this._config.fillTexture) === null || _c === void 0 ? void 0 : _c.x) !== null && _d !== void 0 ? _d : 0;
var realWidth = barWidth - fillWidth * 2;
(_e = this.fill) === null || _e === void 0 ? void 0 : _e.setSize(progress * realWidth, this.fill.height);
this._config.width = realWidth;
this._config.height = (_f = this.fill) === null || _f === void 0 ? void 0 : _f.height;
};
Object.defineProperty(ProgressBar.prototype, "value", {
get: function () {
return this._value;
},
set: function (value) {
this._value = value;
this.updateProgress(value);
},
enumerable: false,
configurable: true
});
ProgressBar.prototype.reDraw = function (newConfig) {
var _a, _b;
this._config = newConfig;
(_a = this.bar) === null || _a === void 0 ? void 0 : _a.destroy();
(_b = this.fill) === null || _b === void 0 ? void 0 : _b.destroy();
this.removeAll();
this.initializeProgressBar();
this.updateProgress(this._value);
this.updateConfig(this._config);
};
ProgressBar.prototype.destroy = function (fromScene) {
var _a, _b;
(_a = this.bar) === null || _a === void 0 ? void 0 : _a.destroy();
(_b = this.fill) === null || _b === void 0 ? void 0 : _b.destroy();
_super.prototype.destroy.call(this, fromScene);
};
return ProgressBar;
}(Container));
export { ProgressBar };