UNPKG

@mai3/phaser-sdk

Version:

A UI component library based on the Phaser game engine

139 lines (138 loc) 6.29 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 { Container } from './Container'; import Utils from '../utils'; var defaultStyle = { fontFamily: 'Arial', // 字体 fontSize: '24px', // 字号 color: '#fff', // 颜色 }; var Text = /** @class */ (function (_super) { __extends(Text, _super); function Text(scene, config) { var _this = _super.call(this, scene, config) || this; _this._config = config; _this.Type = 'Text'; _this.reDraw(config); return _this; } Text.prototype.reDraw = function (config) { var _a, _b, _c, _d; this._config = config; var text = (_a = config.text) !== null && _a !== void 0 ? _a : ""; var style = this.getLabelStyle(config); if (this.text) { this.text.destroy(); this.text = undefined; } this.text = this.scene.make.text({ text: text, style: style }); this.add(this.text); this.computedLabelSize(); this.text.setText(text); this.text.setStyle(style); this.text.setFontStyle((_b = config.textStyle) === null || _b === void 0 ? void 0 : _b.fontStyle); this.layout(); this.setEventInteractive(); this.setDepth((_d = (_c = this._config) === null || _c === void 0 ? void 0 : _c.depth) !== null && _d !== void 0 ? _d : 1); this.setScrollFactor(this._config.isScrollFactor ? 0 : 1); }; Text.prototype.layout = function () { var _a; var textAlign = (_a = this._config.textAlign) !== null && _a !== void 0 ? _a : 'left'; if (textAlign === 'left') { var labelY = (this._height - this.text.displayHeight) / 2; this.text.setPosition(0, labelY); } if (textAlign === 'center') { var labelX = (this._width - this.text.displayWidth) / 2; var labelY = (this._height - this.text.displayHeight) / 2; this.text.setPosition(labelX, labelY); } if (textAlign === 'right') { var labelX = this._width - this.text.displayWidth; var labelY = (this._height - this.text.displayHeight) / 2; this.text.setPosition(labelX, labelY); } this.computedLabelSize(); this.updateConfig(this._config); this.RefreshBounds(); }; Text.prototype.computedLabelSize = function () { var _a, _b; var autoWidth = this._config.autoWidth ? true : (this._config.width ? false : true); var autoHeight = this._config.autoWidth ? true : (this._config.height ? false : true); this._width = autoWidth ? (this.scene.scale.width - 20) : ((_a = this._config.width) !== null && _a !== void 0 ? _a : 150); this._width = autoWidth ? this.text.displayWidth : this._width; this._height = autoHeight ? this.text.displayHeight : ((_b = this._config.height) !== null && _b !== void 0 ? _b : this.text.displayHeight); this._config.width = this._width; this._config.height = this._height; }; Text.prototype.getLabelStyle = function (config) { var _a, _b, _c, _d, _e; var textStyle = (_a = config.textStyle) !== null && _a !== void 0 ? _a : defaultStyle; var autoWidth = config.width ? false : true; this._width = autoWidth ? (this.scene.scale.width - 20) : ((_b = config.width) !== null && _b !== void 0 ? _b : 150); var style = __assign(__assign({}, textStyle), { wordWrap: {}, padding: config.padding, backgroundColor: undefined }); var wordWrapWidth = this._width; wordWrapWidth = ((_c = config.padding) === null || _c === void 0 ? void 0 : _c.left) ? (wordWrapWidth - config.padding.left) : wordWrapWidth; wordWrapWidth = ((_d = config.padding) === null || _d === void 0 ? void 0 : _d.right) ? (wordWrapWidth - config.padding.right) : wordWrapWidth; wordWrapWidth = ((_e = config.padding) === null || _e === void 0 ? void 0 : _e.x) ? (this._width - config.padding.x * 2) : wordWrapWidth; style.wordWrap = config.isWordWrap ? { width: wordWrapWidth !== this._width ? wordWrapWidth : this._width, useAdvancedWrap: config.isWordWrap } : {}; return style; }; Object.defineProperty(Text.prototype, "Text", { get: function () { return this.text.text; }, set: function (text) { this._config = Utils.MergeRight(this._config, { text: text }); this.reDraw(this._config); }, enumerable: false, configurable: true }); Text.prototype.setWidth = function (width) { this._config = Utils.MergeRight(this._config, { width: width }); this.reDraw(this._config); }; Text.prototype.setStyle = function (textStyle) { this._config = Utils.MergeRight(this._config, { textStyle: textStyle }); this.reDraw(this._config); }; Text.prototype.destroy = function (fromScene) { if (this.text) { this.text.destroy(); this.text = undefined; } _super.prototype.destroy.call(this, fromScene); }; return Text; }(Container)); export { Text };