@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
157 lines (156 loc) • 6.92 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 __());
};
})();
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 { BaseButton } from "./BaseButton";
var Checkbox = /** @class */ (function (_super) {
__extends(Checkbox, _super);
function Checkbox(scene, config) {
var _this = _super.call(this, scene, config) || this;
_this._value = "";
_this.isChecked = false;
_this.Type = "Checkbox";
_this._config = config;
_this.init();
return _this;
}
Checkbox.prototype.init = function () {
this.reDraw();
this.setEventInteractive();
};
Checkbox.prototype.handleDown = function () {
var _a, _b;
_super.prototype.handleDown.call(this);
this.toggle();
(_b = (_a = this._config).handleSelect) === null || _b === void 0 ? void 0 : _b.call(_a, this);
this.blendMode = "add";
};
Checkbox.prototype.toggle = function () {
this.isChecked = !this.isChecked;
this.reDrawCheckboxButton();
};
Checkbox.prototype.reDraw = function (config) {
var _a, _b, _c, _d;
if (config)
this._config = config;
this._value = (_a = this._config.value) !== null && _a !== void 0 ? _a : "";
this.isChecked = (_b = this._config.isChecked) !== null && _b !== void 0 ? _b : false;
this.reDrawCheckboxButton();
this.reDrawLabel();
this.RefreshBounds();
this._config.width = this.RealWidth;
this._config.height = this.RealHeight;
this.updateConfig(this._config);
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);
};
Checkbox.prototype.reDrawLabel = function () {
var _a, _b, _c, _d;
var text = (_a = this._config.text) !== null && _a !== void 0 ? _a : "MiracleAI";
var style = (_b = this._config.textStyle) !== null && _b !== void 0 ? _b : {};
if (!this.label) {
this.label = this.scene.make.text({ text: text, style: style });
}
else {
this.label.setText(text).setStyle(style);
this.label.setFontStyle((_d = (_c = this._config.textStyle) === null || _c === void 0 ? void 0 : _c.fontStyle) !== null && _d !== void 0 ? _d : '');
}
this.alignLabelWithCheckboxButton();
this.addChild(this.label);
};
Checkbox.prototype.alignLabelWithCheckboxButton = function () {
var _a;
if (!this.label || !this.checkboxButton)
return;
var labelSpace = (_a = this._config.labelSpace) !== null && _a !== void 0 ? _a : 10;
var fontSize = this.getLabelFontSize();
this.label.x = this.checkboxButton.Right + labelSpace;
this.label.y = this.checkboxButton.Top + (this.checkboxButton.RealHeight - fontSize) / 2;
};
Checkbox.prototype.getLabelFontSize = function () {
var _a;
var fontSize = (_a = this._config.textStyle) === null || _a === void 0 ? void 0 : _a.fontSize;
if (typeof fontSize === "string") {
return Number(fontSize.replace("px", ""));
}
return fontSize !== null && fontSize !== void 0 ? fontSize : 16;
};
Checkbox.prototype.reDrawCheckboxButton = function () {
var _this = this;
this.createCheckboxButton();
// 延迟更新遮罩位置,确保Image组件已完全初始化
this.scene.time.delayedCall(0, function () {
var _a, _b;
var bg = (_a = _this.checkboxButton) === null || _a === void 0 ? void 0 : _a.getByName("bg");
bg === null || bg === void 0 ? void 0 : bg.updateMaskShapePos();
var fill = (_b = _this.checkboxButton) === null || _b === void 0 ? void 0 : _b.getByName("fill");
fill === null || fill === void 0 ? void 0 : fill.updateMaskShapePos();
});
};
Checkbox.prototype.createCheckboxButton = function () {
if (this.checkboxButton) {
this.checkboxButton.removeAll(true);
this.checkboxButton.destroy(true);
}
var _a = this._config, _b = _a.iconWidth, iconWidth = _b === void 0 ? 30 : _b, _c = _a.iconHeight, iconHeight = _c === void 0 ? 30 : _c, unCheckedTexture = _a.unCheckedTexture, checkedTexture = _a.checkedTexture, _d = _a.isCircle, isCircle = _d === void 0 ? false : _d;
var bgConfig = { x: 0, y: 0, width: iconWidth, height: iconHeight, key: unCheckedTexture, isCircle: isCircle };
var fillConfig = __assign(__assign({}, bgConfig), { key: checkedTexture });
this.checkboxButton = this.scene.mai3.add.container({});
var bg = this.scene.mai3.add.image(bgConfig);
bg.setName("bg");
this.checkboxButton.add(bg);
var fill = this.scene.mai3.add.image(fillConfig);
fill.setName("fill");
fill.setVisible(this.isChecked);
bg.setVisible(!this.isChecked);
this.checkboxButton.add(fill);
this.checkboxButton.RefreshBounds();
this.add(this.checkboxButton);
};
Object.defineProperty(Checkbox.prototype, "value", {
get: function () {
return this._value;
},
set: function (v) {
this._value = v;
},
enumerable: false,
configurable: true
});
Checkbox.prototype.destroy = function (fromScene) {
if (this.label) {
this.label.destroy(fromScene);
this.label = undefined;
}
if (this.checkboxButton) {
this.checkboxButton.removeAll(true);
this.checkboxButton.destroy(fromScene);
this.checkboxButton = undefined;
}
_super.prototype.destroy.call(this, fromScene);
};
return Checkbox;
}(BaseButton));
export { Checkbox };