UNPKG

@mai3/phaser-sdk

Version:

A UI component library based on the Phaser game engine

159 lines (158 loc) 7.08 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 __()); }; })(); import { Checkbox } from './Checkbox'; import { BaseButton } from "./BaseButton"; var CheckboxGroup = /** @class */ (function (_super) { __extends(CheckboxGroup, _super); function CheckboxGroup(scene, config) { var _this = _super.call(this, scene, config) || this; _this._checkboxes = []; _this._selectedValues = []; _this._selectedIndexes = []; _this._checkboxGroupWidth = 0; _this._checkboxGroupHeight = 0; _this._checkboxConfigs = []; _this._config = config; _this.Type = 'CheckboxGroup'; _this._initCheckboxGroup(); return _this; } CheckboxGroup.prototype._initCheckboxGroup = function () { var _a; this._setDefaultConfig(); this._createCheckboxes(); this.updateConfig(this._config); this.RefreshBounds(); this.setDepth((_a = this._config.depth) !== null && _a !== void 0 ? _a : 1); this.setScrollFactor(this._config.isScrollFactor ? 0 : 1); }; CheckboxGroup.prototype._setDefaultConfig = function () { var _a, _b, _c; this._config.orientation = (_a = this._config.orientation) !== null && _a !== void 0 ? _a : 'horizontal'; this._config.defaultSelectedIndex = (_b = this._config.defaultSelectedIndex) !== null && _b !== void 0 ? _b : -1; this._config.labelSpace = (_c = this._config.labelSpace) !== null && _c !== void 0 ? _c : 20; }; CheckboxGroup.prototype._createCheckboxes = function () { var _this = this; var _a; var nextX = 0; var nextY = 0; var items = (_a = this._config.items) !== null && _a !== void 0 ? _a : []; items.forEach(function (item, index) { var _a; var checkbox = _this._createCheckbox(item, index, nextX, nextY); _this.addChild(checkbox); _this._checkboxes.push(checkbox); // const config = this._checkboxConfigs[index]; // config.x = checkbox.x; // config.y = checkbox.y; // checkbox.reDraw(config); _a = _this._updateNextPosition(checkbox), nextX = _a[0], nextY = _a[1]; _this._updateGroupSize(checkbox); }); }; CheckboxGroup.prototype._createCheckbox = function (item, index, x, y) { var ckbConfig = { x: x, y: y, text: item.text, value: item.value, isChecked: this._config.defaultSelectedIndex === index, labelSpace: this._config.labelSpace, textStyle: this._config.textStyle, handleSelect: this._handleCheckClick.bind(this), iconWidth: this._config.iconWidth, iconHeight: this._config.iconHeight, unCheckedTexture: this._config.unCheckedTexture, checkedTexture: this._config.checkedTexture, isCircle: this._config.isCircle, }; this._checkboxConfigs.push(ckbConfig); return new Checkbox(this.scene, ckbConfig); }; CheckboxGroup.prototype._updateNextPosition = function (checkbox) { return this._config.orientation === 'horizontal' ? [checkbox.Right + this._config.labelSpace, checkbox.y] : [checkbox.x, checkbox.Bottom + this._config.labelSpace]; }; CheckboxGroup.prototype._updateGroupSize = function (checkbox) { this._config.width = this._config.orientation === 'horizontal' ? Math.max(checkbox.Right, this._checkboxGroupWidth) : Math.max(checkbox.RealWidth, this._checkboxGroupWidth); this._config.height = this._config.orientation === 'horizontal' ? Math.max(checkbox.RealHeight, this._checkboxGroupHeight) : Math.max(checkbox.Bottom, this._checkboxGroupHeight); this._checkboxGroupWidth = this._config.width; this._checkboxGroupHeight = this._config.height; }; CheckboxGroup.prototype._handleCheckClick = function (ckb) { var _a; this._selectedValues = []; this._selectedIndexes = []; var multiSelect = (_a = this._config.multiSelect) !== null && _a !== void 0 ? _a : false; if (ckb.isChecked && !multiSelect) { this._handleSingleSelect(ckb); } else if (multiSelect) { this._handleMultiSelect(); } if (this._config.handleSelect) { this._config.handleSelect(this, this._selectedValues, this._selectedIndexes); } this.emit('change', this._selectedValues); }; CheckboxGroup.prototype._handleSingleSelect = function (ckb) { var _this = this; this._checkboxes.forEach(function (checkbox, index) { if (checkbox.isChecked && checkbox.value !== ckb.value) { checkbox.isChecked = false; var ckbItem = _this._checkboxConfigs[index]; ckbItem.isChecked = false; checkbox.reDraw(ckbItem); } else if (checkbox.isChecked && checkbox.value === ckb.value) { _this._selectedIndexes.push(index); _this._selectedValues.push(ckb.value); } }); }; CheckboxGroup.prototype._handleMultiSelect = function () { var _this = this; this._checkboxes.forEach(function (checkbox, index) { if (checkbox.isChecked) { _this._selectedIndexes.push(index); _this._selectedValues.push(checkbox.value); } }); }; CheckboxGroup.prototype.reDraw = function (config) { this._config = config; this._checkboxes.forEach(function (checkbox) { return checkbox.destroy(); }); this._checkboxes = []; this._selectedValues = []; this._selectedIndexes = []; this._checkboxGroupWidth = 0; this._checkboxGroupHeight = 0; this._initCheckboxGroup(); }; CheckboxGroup.prototype.destroy = function (fromScene) { this._checkboxes.forEach(function (checkbox) { return checkbox.destroy(); }); this._checkboxes = []; this._selectedValues = []; this._selectedIndexes = []; this._checkboxGroupWidth = 0; this._checkboxGroupHeight = 0; _super.prototype.destroy.call(this, fromScene); }; return CheckboxGroup; }(BaseButton)); export { CheckboxGroup };