@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
157 lines • 6.2 kB
JavaScript
import { __extends } from "tslib";
import { EShapeButton, EShapeCopyPart, EShapeCorner, EShapeGroupShadowed, EShapeLockPart } from "@wcardinal/wcardinal-ui";
import { EShapeButtonLayerButtonValueContainer } from "./e-shape-button-layer-button-value-container";
import { EShapeButtonLayerIds } from "./e-shape-button-layer-ids";
var EShapeButtonLayer = /** @class */ (function (_super) {
__extends(EShapeButtonLayer, _super);
function EShapeButtonLayer(mode, type) {
if (type === void 0) { type = EShapeButtonLayerIds.ID; }
var _this = _super.call(this, mode, type) || this;
_this._button = new EShapeButtonLayerButtonValueContainer(function () {
_this.onButtonValueChange();
});
_this._corner = EShapeCorner.ALL;
return _this;
}
Object.defineProperty(EShapeButtonLayer.prototype, "button", {
get: function () {
return this._button;
},
enumerable: false,
configurable: true
});
Object.defineProperty(EShapeButtonLayer.prototype, "corner", {
get: function () {
return this._corner;
},
set: function (corner) {
if (this._corner !== corner) {
this._corner = corner;
this.updateUploaded();
}
},
enumerable: false,
configurable: true
});
EShapeButtonLayer.prototype.onButtonValueChange = function () {
this.newButtons();
this.layoutButtons();
};
EShapeButtonLayer.prototype.onSizeChange = function () {
_super.prototype.onSizeChange.call(this);
this.layoutButtons();
};
EShapeButtonLayer.prototype.newButtons = function () {
var shapeButton = this.button;
var buttonCount = shapeButton.size();
var values = shapeButton.values;
var children = this.children;
var childrenLength = children.length;
// Create / delete children
if (childrenLength < buttonCount) {
var master = 0 < childrenLength ? children[childrenLength - 1] : null;
for (var i = childrenLength; i < buttonCount; ++i) {
var button = new EShapeButton();
var value = values[i];
if (master != null) {
button.copy(master);
button.text.value = value.label;
}
else {
button.stroke.set(false, 0x3399ff, 1);
button.fill.set(true, 0x3399ff, 1);
button.text.set(value.label, 0xffffff, 1);
button.state.isFocusable = true;
button.interactive = true;
}
if (0 < button.action.size()) {
button.action.set(0, value.action);
}
else {
button.action.add(value.action);
}
button.isToggle = true;
button.isGrouped = true;
button.attach(this);
}
}
else if (buttonCount < childrenLength) {
for (var i = childrenLength - 1; buttonCount <= i; --i) {
children[i].detach();
}
}
// Update corner, action and state
var corner = this.corner;
for (var i = 0; i < buttonCount; ++i) {
var child = children[i];
var value = values[i];
child.corner = this.toCorner(i, buttonCount, corner);
child.text.value = value.label;
if (0 < child.action.size()) {
child.action.set(0, value.action);
}
else {
child.action.add(value.action);
}
child.state.isActive = value.isActive;
}
};
EShapeButtonLayer.prototype.layoutButtons = function () {
var button = this.button;
var buttonValues = button.values;
var buttonValuesLength = buttonValues.length;
var buttonMargin = button.margin;
var size = this.size;
var sizeX = size.x;
var sizeY = size.y;
var children = this.children;
var childrenLength = children.length;
// Total weight
var totalWeight = 0;
for (var i = 0; i < buttonValuesLength; ++i) {
totalWeight += buttonValues[i].weight;
}
totalWeight = Math.max(0.0001, totalWeight);
var buttonSpace = Math.max(0, sizeX - (buttonValuesLength - 1) * buttonMargin);
var buttonSpaceFactor = buttonSpace / totalWeight;
// Update
var x = -sizeX * 0.5;
for (var i = 0; i < childrenLength; ++i) {
var buttonValue = buttonValues[i];
var width = buttonValue.weight * buttonSpaceFactor;
var child = children[i];
child.lock(EShapeLockPart.ALL);
child.size.set(width, sizeY);
child.transform.position.set(x + width * 0.5, 0);
child.unlock(EShapeLockPart.ALL, true);
x += width + buttonMargin;
}
};
EShapeButtonLayer.prototype.toCorner = function (index, size, corner) {
var result = EShapeCorner.NONE;
if (index === 0) {
result |= EShapeCorner.LEFT & corner;
}
if (index === size - 1) {
result |= EShapeCorner.RIGHT & corner;
}
return result;
};
EShapeButtonLayer.prototype.copy = function (source, part) {
if (part === void 0) { part = EShapeCopyPart.ALL; }
var result = _super.prototype.copy.call(this, source, part);
if (source instanceof EShapeButtonLayer) {
this._button.copy(source.button);
}
return result;
};
EShapeButtonLayer.prototype.serialize = function (manager) {
var result = _super.prototype.serialize.call(this, manager);
var resource = result[15];
result[15] = manager.addResource("[".concat(this._button.serialize(manager), ",").concat(resource, "]"));
return result;
};
return EShapeButtonLayer;
}(EShapeGroupShadowed));
export { EShapeButtonLayer };
//# sourceMappingURL=e-shape-button-layer.js.map