UNPKG

@mai3/phaser-sdk

Version:

A UI component library based on the Phaser game engine

163 lines (162 loc) 7.54 kB
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 __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import Utils from '../utils'; import { Panel } from './Panel'; var defaultStyle = { fontFamily: 'Arial', fontSize: '24px', color: '#fff', }; var Label = /** @class */ (function (_super) { __extends(Label, _super); function Label(scene, config) { var _this = this; var _a, _b; var padding = Utils.getPadding(config.padding); var width = padding.left + padding.right + ((_a = config.width) !== null && _a !== void 0 ? _a : 0); var height = padding.top + padding.bottom + ((_b = config.height) !== null && _b !== void 0 ? _b : 0); _this = _super.call(this, scene, __assign(__assign({}, config), { width: width, height: height })) || this; _this._config = config; _this.Type = 'Label'; _this.scene = scene; _this.reDraw(config); return _this; } Label.prototype.reDraw = function (config) { var _a, _b; this._config = config; this.validateConfig(); this.drawText(); this.reDrawBackground(this._config); this.RefreshBounds(); this.updateConfig(this._config); this.setDepth((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.depth) !== null && _b !== void 0 ? _b : 1); this.setScrollFactor(this._config.isScrollFactor ? 0 : 1); }; Label.prototype.validateConfig = function () { if (!this._config.autoWidth && !this._config.width) { throw new Error('Label ERROR: Label width must be specified when autoWidth is false'); } if (this._config.autoWidth && this._config.isWordWrap) { throw new Error('Label ERROR: Label cannot be word wrapped when autoWidth is true'); } }; Label.prototype.drawText = function () { var _a = this._config, _b = _a.text, text = _b === void 0 ? "" : _b, _c = _a.textAlign, textAlign = _c === void 0 ? 'left' : _c; var style = this.getLabelStyle(); var padding = Utils.getPadding(this._config.padding); this.createOrUpdateLabel(text, style); this.computedLabelSize(); this.setLabelPosition(textAlign, padding); // console.log('config: ', this._config); }; Label.prototype.createOrUpdateLabel = function (text, style) { var _a; if (!this.label) { this.label = this.scene.make.text({}); this.addChildAt(this.label, 1); } this.label.setText(text).setStyle(style); this.label.setFontStyle((_a = this._config.textStyle) === null || _a === void 0 ? void 0 : _a.fontStyle); }; Label.prototype.computedLabelSize = function () { var _a, _b, _c, _d, _e, _f, _g, _h; var _j = this._config, _k = _j.autoWidth, autoWidth = _k === void 0 ? false : _k, _l = _j.autoHeight, autoHeight = _l === void 0 ? false : _l; var padding = Utils.getPadding(this._config.padding); this._width = autoWidth ? ((_b = (_a = this.label) === null || _a === void 0 ? void 0 : _a.displayWidth) !== null && _b !== void 0 ? _b : 0) + padding.left + padding.right : (_c = this._config.width) !== null && _c !== void 0 ? _c : 0; this._height = autoHeight ? ((_e = (_d = this.label) === null || _d === void 0 ? void 0 : _d.displayHeight) !== null && _e !== void 0 ? _e : 30) + padding.top + padding.bottom : (_f = this._config.height) !== null && _f !== void 0 ? _f : ((_h = (_g = this.label) === null || _g === void 0 ? void 0 : _g.displayHeight) !== null && _h !== void 0 ? _h : 30) + padding.top + padding.bottom; this._config.width = this._width; this._config.height = this._height; // console.log(`this.label?.displayWidth: ${this.label?.displayWidth}, width: ${this._width}`); }; Label.prototype.setLabelPosition = function (textAlign, padding) { var labelY = padding.top || (this._height - this.label.displayHeight) / 2; var labelX = padding.left; switch (textAlign) { case 'center': labelX = (this._width - this.label.displayWidth) / 2; break; case 'right': labelX = this._width - this.label.displayWidth - padding.right; break; } this.label.setPosition(labelX, labelY); }; Label.prototype.getLabelStyle = function () { var _a, _b; var textStyle = (_a = this._config.textStyle) !== null && _a !== void 0 ? _a : defaultStyle; var padding = Utils.getPadding(this._config.padding); var style = __assign(__assign({}, textStyle), { wordWrap: {}, padding: padding }); if (this._config.isWordWrap && !this._config.autoWidth) { style.wordWrap = { width: ((_b = this._config.width) !== null && _b !== void 0 ? _b : 0) - padding.left - padding.right, useAdvancedWrap: true }; } return style; }; Object.defineProperty(Label.prototype, "Text", { get: function () { var _a, _b; return (_b = (_a = this.label) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : ''; }, set: function (text) { this.reDraw(__assign(__assign({}, this._config), { text: text })); }, enumerable: false, configurable: true }); Label.prototype.setWidth = function (width) { this.reDraw(__assign(__assign({}, this._config), { width: width })); }; Label.prototype.setStyle = function (textStyle) { this.reDraw(__assign(__assign({}, this._config), { textStyle: textStyle })); }; Object.defineProperty(Label.prototype, "TextWidth", { get: function () { var _a, _b; return (_b = (_a = this.label) === null || _a === void 0 ? void 0 : _a.displayWidth) !== null && _b !== void 0 ? _b : 0; }, enumerable: false, configurable: true }); Object.defineProperty(Label.prototype, "Label", { get: function () { return this.label; }, enumerable: false, configurable: true }); Label.prototype.destroy = function (fromScene) { var _a; (_a = this.label) === null || _a === void 0 ? void 0 : _a.destroy(); this.label = undefined; _super.prototype.destroy.call(this, fromScene); }; return Label; }(Panel)); export { Label };