@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
97 lines (96 loc) • 4.52 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';
import Utils from '../utils';
var Panel = /** @class */ (function (_super) {
__extends(Panel, _super);
function Panel(scene, config) {
var _this = _super.call(this, scene, config) || this;
_this._config = config;
_this.Type = 'Panel';
return _this;
}
Panel.prototype.render = function () {
this.drawBackground();
};
Panel.prototype.reDrawBackground = function (config) {
this._config = config;
this.drawBackground();
};
Panel.prototype.drawBackground = function () {
this.RefreshBounds();
this.updateConfigDimensions();
if (this.shouldUseRenderTexture()) {
this.createOrUpdateRenderTexture();
}
else {
this.createOrUpdateNineSlice();
}
this.addChildAt(this.bg, 0);
};
Panel.prototype.updateConfigDimensions = function () {
var _a, _b;
this._config.width = Utils.GetOrDefaultValue((_a = this._config) === null || _a === void 0 ? void 0 : _a.width, this.RealWidth);
this._config.height = Utils.GetOrDefaultValue((_b = this._config) === null || _b === void 0 ? void 0 : _b.height, this.RealHeight);
};
Panel.prototype.shouldUseRenderTexture = function () {
var _a;
return Utils.isNullOrZeroOrEmpty((_a = this._config) === null || _a === void 0 ? void 0 : _a.texture);
};
Panel.prototype.createOrUpdateRenderTexture = function () {
if (this.bg) {
this.bg.destroy();
this.bg = undefined;
}
var _a = this._config, width = _a.width, height = _a.height, _b = _a.borderWidth, borderWidth = _b === void 0 ? 4 : _b, _c = _a.radius, radius = _c === void 0 ? 10 : _c, _d = _a.borderColor, borderColor = _d === void 0 ? 0xff8221 : _d, backgroundColor = _a.backgroundColor, backgroundAlpha = _a.backgroundAlpha;
this.bg = Utils.reDrawRoundedRectRenderTexture(this.scene, this.bg, 0, 0, width, height, borderWidth, radius, borderColor, backgroundColor, backgroundAlpha) || this.bg;
};
Panel.prototype.createOrUpdateNineSlice = function () {
if (this.bg && !(this.bg instanceof Phaser.GameObjects.NineSlice)) {
this.bg.destroy();
this.bg = undefined;
}
if (!this.bg) {
this.createNineSlice();
}
this.updateNineSlice();
};
Panel.prototype.createNineSlice = function () {
var _a = this._config, _b = _a.texture, texture = _b === void 0 ? "" : _b, frame = _a.frame, _c = _a.width, width = _c === void 0 ? 0 : _c, _d = _a.height, height = _d === void 0 ? 0 : _d, leftWidth = _a.leftWidth, rightWidth = _a.rightWidth, topHeight = _a.topHeight, bottomHeight = _a.bottomHeight;
this.bg = this.scene.add.nineslice(0, 0, texture, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight);
};
Panel.prototype.updateNineSlice = function () {
var bg = this.bg;
var _a = this._config, _b = _a.texture, texture = _b === void 0 ? "" : _b, frame = _a.frame, _c = _a.width, width = _c === void 0 ? 0 : _c, _d = _a.height, height = _d === void 0 ? 0 : _d;
bg.setTexture(texture, frame);
bg.setDisplaySize(width, height);
bg.setOrigin(0);
};
Panel.prototype.reSize = function (width, height) {
this._config.width = width;
this._config.height = height;
this.reDrawBackground(this._config);
};
Panel.prototype.destroy = function (fromScene) {
if (this.bg) {
this.bg.destroy();
this.bg = undefined;
}
_super.prototype.destroy.call(this, fromScene);
};
return Panel;
}(Container));
export { Panel };