@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
90 lines (89 loc) • 4.54 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 { BaseButton } from "./BaseButton";
import { Text } from ".";
var ImageButton = /** @class */ (function (_super) {
__extends(ImageButton, _super);
function ImageButton(scene, config) {
var _this = this;
var _a, _b;
config.width = (_a = config.width) !== null && _a !== void 0 ? _a : 200;
config.height = (_b = config.height) !== null && _b !== void 0 ? _b : 60;
_this = _super.call(this, scene, config, "ImageButton") || this;
_this._config = config;
_this.reDraw(config);
_this.setEventInteractive();
return _this;
}
ImageButton.prototype.reDraw = function (config) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
this._config = config;
this._config.width = (_a = config.width) !== null && _a !== void 0 ? _a : 200;
this._config.height = (_b = config.height) !== null && _b !== void 0 ? _b : 60;
if (this.image) {
this.image.destroy();
this.image = undefined;
}
if (!this.image)
this.image = this.scene.add.nineslice(0, 0, (_c = this._config.texture) !== null && _c !== void 0 ? _c : "", (_d = this._config.frame) !== null && _d !== void 0 ? _d : 0, this._config.width, this._config.height, (_e = this._config.leftWidth) !== null && _e !== void 0 ? _e : 0, (_f = this._config.rightWidth) !== null && _f !== void 0 ? _f : 0, (_g = this._config.topHeight) !== null && _g !== void 0 ? _g : 0, (_h = this._config.bottomHeight) !== null && _h !== void 0 ? _h : 0);
(_j = this.image) === null || _j === void 0 ? void 0 : _j.setTexture(((_k = this._config.texture) !== null && _k !== void 0 ? _k : ''), this._config.frame);
(_l = this.image) === null || _l === void 0 ? void 0 : _l.setDisplaySize(this._config.width, this._config.height);
(_m = this.image) === null || _m === void 0 ? void 0 : _m.setOrigin(0);
this.addChild(this.image);
this.RefreshBounds();
this.reDrawText(config);
this.updateConfig(config);
this.setDepth((_o = config === null || config === void 0 ? void 0 : config.depth) !== null && _o !== void 0 ? _o : 1);
this.setScrollFactor(this._config.isScrollFactor ? 0 : 1);
};
ImageButton.prototype.reDrawText = function (config) {
var _a, _b, _c, _d, _e;
if (this.text) {
this.text.destroy();
this.text = undefined;
}
if (config.text === undefined || config.text === "" || config.text === null) {
return;
}
this._config = config;
var imageBounds = (_a = this.image) === null || _a === void 0 ? void 0 : _a.getBounds();
var isCenter = false;
if (this._config.textX === undefined && this._config.textY === undefined) {
isCenter = true;
}
var textConfig = {
x: isCenter ? imageBounds.width / 2 : (_b = this._config.textX) !== null && _b !== void 0 ? _b : 0,
y: isCenter ? imageBounds.height / 2 : (_c = this._config.textY) !== null && _c !== void 0 ? _c : 0,
text: (_d = this._config.text) !== null && _d !== void 0 ? _d : "",
textStyle: (_e = this._config.textStyle) !== null && _e !== void 0 ? _e : {},
};
this.text = new Text(this.scene, textConfig);
if (isCenter) {
this.text.text.setOrigin(0.5, 0.5);
}
this.add(this.text);
this.RefreshBounds();
};
ImageButton.prototype.destroy = function (fromScene) {
if (this.image) {
this.image.destroy();
this.image = undefined;
}
_super.prototype.destroy.call(this, fromScene);
};
return ImageButton;
}(BaseButton));
export { ImageButton };