@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
137 lines (136 loc) • 5.52 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 { Panel } from "./Panel";
var Group = /** @class */ (function (_super) {
__extends(Group, _super);
function Group(scene, x, y) {
if (x === void 0) { x = 0; }
if (y === void 0) { y = 0; }
var _this = _super.call(this, scene, { x: x, y: y }) || this;
scene.add.existing(_this);
_this.parentContainer = _this;
_this.SORT_ASCENDING = -1;
_this.SORT_DESCENDING = 1;
_this.version = 3;
// 模拟 Phaser CE 的 GameObject.alignTo
_this.alignToMapping = {
0: Phaser.Display.Align.To.TopLeft,
1: Phaser.Display.Align.To.TopCenter,
8: Phaser.Display.Align.To.RightCenter,
11: Phaser.Display.Align.To.BottomCenter,
};
_this.worldPosition = { x: _this.x, y: _this.y };
return _this;
}
Object.defineProperty(Group.prototype, "children", {
/** @private 别名以匹配 Phaser CE */
get: function () {
return this.list;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Group.prototype, "realHeight", {
get: function () {
return this.getBounds().height - this.y;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Group.prototype, "realWidth", {
get: function () {
return this.getBounds().width - this.x;
},
enumerable: false,
configurable: true
});
/** 设置组中每个成员的原点 */
Group.prototype.setOrigin = function (x, y) {
if (x === void 0) { x = 0; }
if (y === void 0) { y = 0; }
var node;
for (var i = 0; i < this.children.length; i++) {
node = this.children[i];
if ('setOrigin' in node && typeof node.setOrigin === 'function') {
node.setOrigin(x, y);
}
}
};
Group.prototype.getNodes = function () {
return this.getAll();
};
// 无法自动对齐到容器
Group.prototype.alignToContainerBottom = function (previousNode, child) {
var previousNodeWidth;
var previousNodeHeight;
if ('getBounds' in previousNode && typeof previousNode.getBounds === 'function') {
var bounds = previousNode.getBounds();
previousNodeWidth = bounds.width - this.worldPosition.x;
previousNodeHeight = bounds.height - this.worldPosition.y;
}
else {
previousNodeWidth = previousNode.width || 0;
previousNodeHeight = previousNode.height || 0;
}
var centerX = previousNodeWidth * 0.5;
var bottomY = previousNodeHeight;
var w = child.width || 0;
if ('getBounds' in child && typeof child.getBounds === 'function') {
w = child.getBounds().width;
}
if ('x' in child && 'y' in child) {
child.x = centerX - w * 0.5;
child.y = ('getBounds' in previousNode && typeof previousNode.getBounds === 'function' ? previousNode.getBounds().y : 0) + bottomY;
}
};
/** 容器无法自动对齐 */
Group.prototype.alignContainerToBottom = function (previousNode, child) {
if ('getBounds' in previousNode && typeof previousNode.getBounds === 'function' && 'x' in previousNode && 'x' in child && 'y' in child) {
var bounds = previousNode.getBounds();
var centerX = previousNode.x + bounds.width * 0.5;
child.x = centerX - (child.width || 0) * 0.5;
child.y = bounds.y;
}
};
Group.prototype.alignContainerToRight = function (previousNode, child) {
if ('x' in previousNode && 'x' in child) {
var rightX = previousNode.x + (previousNode.width || 0);
child.x = rightX;
}
};
/** 将子对象对齐到组中的最后一个对象。
* @private
*/
Group.prototype.alignNodeToPrevious = function (child, align, paddingX, paddingY) {
var nodes = this.getNodes();
var previousNode = nodes[nodes.length - 2];
var toGroupAlignFuncs = {
11: this.alignContainerToBottom.bind(this), // 列
8: this.alignContainerToRight.bind(this), // 行
};
var toGroupAlignFunc = toGroupAlignFuncs[align];
if (previousNode instanceof Group) {
this.alignToContainerBottom(previousNode, child);
}
else if (child instanceof Group && previousNode !== undefined) {
toGroupAlignFunc(previousNode, child);
}
else if (previousNode !== undefined) {
this.alignToMapping[align](child, previousNode, paddingX, paddingY);
}
};
return Group;
}(Panel));