@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
110 lines (109 loc) • 4.72 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 { ScrollView } from "./ScrollView";
var ListView = /** @class */ (function (_super) {
__extends(ListView, _super);
function ListView(scene, config) {
var _this = _super.call(this, scene, config) || this;
_this.Type = "ListView";
_this.setItemDatas((config === null || config === void 0 ? void 0 : config.itemDatas) || []);
return _this;
}
ListView.prototype.updateConfig = function (config) {
_super.prototype.updateConfig.call(this, config);
this.setItemDatas((config === null || config === void 0 ? void 0 : config.itemDatas) || []);
};
ListView.prototype.addChild = function (child) {
var _child = _super.prototype.addChild.call(this, child);
var position = this.calculateNextItemPosition();
_child.setPosition(position.x, position.y);
this._lastItem = _child;
return child;
};
ListView.prototype.addItems = function (childConfigs) {
var _a;
for (var _i = 0, childConfigs_1 = childConfigs; _i < childConfigs_1.length; _i++) {
var childConfig = childConfigs_1[_i];
var child = this.scene.getChild({
type: "Container",
width: this.config.width,
height: (_a = this._config) === null || _a === void 0 ? void 0 : _a.itemHeight,
childConfigs: childConfig,
});
this.addChild(child);
}
};
ListView.prototype.calculateNextItemPosition = function () {
if (!this._lastItem)
return { x: 0, y: 0 };
return {
x: this._direction === "x" ? this._lastItem.x + this._lastItem.Width : 0,
y: this._direction === "y" ? this._lastItem.y + this._lastItem.Height : 0,
};
};
ListView.prototype.setChildren = function (childConfigs) {
// ListView has not implemented this method
};
ListView.prototype.setItemDatas = function (itemDatas) {
if (!this._config)
return;
this.config.itemDatas = itemDatas;
this.updateItems();
};
ListView.prototype.reDraw = function (config) {
_super.prototype.reDraw.call(this, config);
this.updateItems();
};
ListView.prototype.updateItems = function () {
var _a, _b, _c, _d;
if (!this._config || !this._content)
return;
var all = (_a = this._content) === null || _a === void 0 ? void 0 : _a.getAll();
if (!all || (all === null || all === void 0 ? void 0 : all.length) < this._config.itemDatas.length) {
(_b = this._content) === null || _b === void 0 ? void 0 : _b.removeAll(true);
this._lastItem = undefined;
for (var i = 0; i < this._config.itemDatas.length; i++) {
var child = this.scene.getChild({
type: "Container",
childConfigs: this._config.itemDatas[i] || [],
width: this.config.width,
height: this._config.itemHeight,
});
this.addChild(child);
}
}
else {
all = this._content.getAll();
for (var i = 0; i < all.length; i++) {
var item = all[i];
if (item && this._config.itemDatas[i]) {
all[i].config.childConfigs =
this._config.itemDatas[i];
all[i].reDraw(all[i].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);
};
ListView.prototype.destroy = function (fromScene) {
this._lastItem = undefined;
this._config = undefined;
_super.prototype.destroy.call(this, fromScene);
};
return ListView;
}(ScrollView));
export { ListView };