UNPKG

@wcardinal/wcardinal-geditor

Version:

WebGL-based graphic editor, tester and viewer for supervisory systems

246 lines 9.99 kB
import { __extends } from "tslib"; import { DCommandBase, EShapeCapabilities, EShapeCapability, EShapeConnectorLine, EShapeConnectors, EShapeGroup, EShapeLockPart } from "@wcardinal/wcardinal-ui"; import { UtilShapeTransforms } from "../util/util-shape-transforms"; import { UtilShapeDeleter } from "../util/util-shape-deleter"; import { UtilShapeSearch } from "../util/util-shape-search"; var ECommandShapeUngroup = /** @class */ (function (_super) { __extends(ECommandShapeUngroup, _super); function ECommandShapeUngroup(parent, selection) { var _this = _super.call(this) || this; _this._parent = parent; _this._selection = selection; _this._before = []; _this._deleteds = []; _this._indices = []; _this._createds = []; return _this; } Object.defineProperty(ECommandShapeUngroup.prototype, "parent", { get: function () { return this._parent; }, enumerable: false, configurable: true }); Object.defineProperty(ECommandShapeUngroup.prototype, "selection", { get: function () { return this._selection; }, enumerable: false, configurable: true }); Object.defineProperty(ECommandShapeUngroup.prototype, "before", { get: function () { return this._before; }, enumerable: false, configurable: true }); Object.defineProperty(ECommandShapeUngroup.prototype, "deleteds", { get: function () { return this._deleteds; }, enumerable: false, configurable: true }); Object.defineProperty(ECommandShapeUngroup.prototype, "indices", { get: function () { return this._indices; }, enumerable: false, configurable: true }); Object.defineProperty(ECommandShapeUngroup.prototype, "createds", { get: function () { return this._createds; }, enumerable: false, configurable: true }); ECommandShapeUngroup.prototype.execute = function () { // Save the current selection var selection = this._selection; selection.lock(); this._before = selection.store(); // Unselect non-group shapes var shapes = selection.get(); for (var i = shapes.length - 1; 0 <= i; --i) { var shape = shapes[i]; if (!this.isUngroupable(shape)) { shape.selected = false; shapes.splice(i, 1); } } // Delete groups var parent = this._parent; var deleteds = UtilShapeDeleter.delete(parent, shapes, true) || []; this._deleteds = deleteds; for (var i = 0, imax = deleteds.length; i < imax; ++i) { deleteds[i].reference += 1; } // Create shapes var createds = []; for (var i = 0, imax = deleteds.length; i < imax; ++i) { var target = deleteds[i]; var targetLocalTransform = target.transform.localTransform; var a = targetLocalTransform.a; var b = targetLocalTransform.b; var c = targetLocalTransform.c; var d = targetLocalTransform.d; var tx = targetLocalTransform.tx; var ty = targetLocalTransform.ty; var targetChildren = target.children; var clones = []; for (var j = 0, jmax = targetChildren.length; j < jmax; ++j) { var clone = targetChildren[j].clone(); clone.lock(EShapeLockPart.ALL); clone.parent = null; clone.updateTransform(); var localTransform = clone.transform.localTransform; localTransform.prepend(targetLocalTransform); var capability = EShapeCapability.NONE; if (EShapeCapabilities.contains(clone, EShapeCapability.POSITION)) { capability |= EShapeCapability.POSITION; } if (EShapeCapabilities.contains(clone, EShapeCapability.ROTATION)) { capability |= EShapeCapability.ROTATION; } if (EShapeCapabilities.contains(clone, EShapeCapability.SKEW)) { capability |= EShapeCapability.SKEW; } if (capability !== EShapeCapability.NONE) { UtilShapeTransforms.applyLocal(clone, localTransform, capability); } if (clone instanceof EShapeConnectorLine) { var edge = clone.edge; var tail = edge.tail; if (tail.acceptor.shape == null) { // Local var tailLocal = tail.local; var tailLocalX = tailLocal.x; var tailLocalY = tailLocal.y; tailLocal.set(a * tailLocalX + c * tailLocalY + tx, b * tailLocalX + d * tailLocalY + ty); // Normal var tailNormal = tail.normal; var tailNormalX = tailNormal.x; var tailNormalY = tailNormal.y; var ndx = a * tailNormalX + c * tailNormalY; var ndy = b * tailNormalX + d * tailNormalY; var nd = ndx * ndx + ndy * ndy; if (0.000001 < nd) { var f = Math.sqrt(1 / nd); tailNormal.set(ndx * f, ndy * f); } } var head = edge.head; if (head.acceptor.shape == null) { // Local var headLocal = head.local; var headLocalX = headLocal.x; var headLocalY = headLocal.y; headLocal.set(a * headLocalX + c * headLocalY + tx, b * headLocalX + d * headLocalY + ty); // Normal var headNormal = head.normal; var headNormalX = headNormal.x; var headNormalY = headNormal.y; var ndx = a * headNormalX + c * headNormalY; var ndy = b * headNormalX + d * headNormalY; var nd = ndx * ndx + ndy * ndy; if (0.000001 < nd) { var f = Math.sqrt(1 / nd); headNormal.set(ndx * f, ndy * f); } } } clones.push(clone); } EShapeConnectors.moveAll(targetChildren, clones, targetChildren, clones); for (var j = 0, jmax = clones.length; j < jmax; ++j) { var clone = clones[j]; clone.unlock(EShapeLockPart.ALL, true); clone.attach(parent); clone.reference += 1; createds.push(clone); } } this._createds = createds; // Indices this._indices = UtilShapeSearch.toIndices(deleteds); // Select created shapes selection.clearAndAddAll(createds); selection.update("TREE"); selection.unlock(); return true; }; ECommandShapeUngroup.prototype.isUngroupable = function (shape) { return (shape instanceof EShapeGroup && EShapeCapabilities.contains(shape, EShapeCapability.CHILDREN) && EShapeCapabilities.contains(shape, EShapeCapability.UNGROUPING)); }; ECommandShapeUngroup.prototype.redo = function () { // Delete shapes var selection = this._selection; selection.lock(); selection.clearAndAddAll(this._deleteds); selection.delete(false); // Add created shapes var parent = this._parent; var createds = this._createds; for (var i = 0, imax = createds.length; i < imax; ++i) { createds[i].attach(parent); } // Select created shapes selection.clearAndAddAll(createds); selection.update("TREE"); selection.unlock(); return true; }; ECommandShapeUngroup.prototype.undo = function () { // Delete created shapes var parent = this._parent; var selection = this._selection; selection.lock(); var createds = this._createds; for (var i = createds.length - 1; 0 <= i; --i) { createds[i].detach(); } // Restore deleted shapes var deleteds = this._deleteds; var indices = this._indices; for (var i = 0, imax = deleteds.length; i < imax; ++i) { deleteds[i].attach(parent, indices[i]); } // Restore the selection this._selection.restore(this._before); selection.update("TREE"); selection.unlock(); return true; }; ECommandShapeUngroup.prototype.destroy = function () { // Stored selection this._before.length = 0; // Deleted shapes var deleteds = this._deleteds; for (var i = 0, imax = deleteds.length; i < imax; ++i) { var deleted = deleteds[i]; deleted.reference -= 1; if (deleted.parent == null && deleted.reference <= 0) { deleted.destroy(); } } deleteds.length = 0; // Created shapes var createds = this._createds; for (var i = 0, imax = createds.length; i < imax; ++i) { var created = createds[i]; created.reference -= 1; if (created.parent == null && created.reference <= 0) { created.destroy(); } } createds.length = 0; }; return ECommandShapeUngroup; }(DCommandBase)); export { ECommandShapeUngroup }; //# sourceMappingURL=e-command-shape-ungroup.js.map