@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
208 lines • 7.89 kB
JavaScript
import { __extends } from "tslib";
import { DCommandBase, EShapeCapabilities, EShapeCapability, EShapeConnectorLine, EShapeConnectors, EShapeGroup, EShapeLockPart, EShapeResourceManagerDeserializationMode } from "@wcardinal/wcardinal-ui";
import { UtilShapeDeleter } from "../util/util-shape-deleter";
import { UtilShapeSearch } from "../util/util-shape-search";
var ECommandShapeGroup = /** @class */ (function (_super) {
__extends(ECommandShapeGroup, _super);
function ECommandShapeGroup(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(ECommandShapeGroup.prototype, "parent", {
get: function () {
return this._parent;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ECommandShapeGroup.prototype, "selection", {
get: function () {
return this._selection;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ECommandShapeGroup.prototype, "before", {
get: function () {
return this._before;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ECommandShapeGroup.prototype, "deleteds", {
get: function () {
return this._deleteds;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ECommandShapeGroup.prototype, "indices", {
get: function () {
return this._indices;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ECommandShapeGroup.prototype, "createds", {
get: function () {
return this._createds;
},
enumerable: false,
configurable: true
});
ECommandShapeGroup.prototype.execute = function () {
// Save the current selection
var selection = this._selection;
this._before = selection.store();
selection.lock();
// Unselect shapes that do not have the grouping capability
var shapes = selection.get();
for (var i = shapes.length - 1; 0 <= i; --i) {
var shape = shapes[i];
if (!EShapeCapabilities.contains(shape, EShapeCapability.GROUPING)) {
shape.selected = false;
shapes.splice(i, 1);
}
}
// Delete selected shapes
var parent = this._parent;
var deletedSet = new Set();
var deleteds = UtilShapeDeleter.delete(parent, shapes, true, deletedSet) || [];
this._deleteds = deleteds;
for (var i = 0, imax = deleteds.length; i < imax; ++i) {
deleteds[i].reference += 1;
}
// Create a new group
var modifier = selection.modifier;
var x = modifier.position.x + modifier.width * 0.5;
var y = modifier.position.y + modifier.height * 0.5;
var w = modifier.width;
var h = modifier.height;
var group = new EShapeGroup(EShapeResourceManagerDeserializationMode.EDITOR);
group.transform.position.set(x, y);
group.size.set(w, h);
group.attach(parent);
var groupChildren = group.children;
for (var i = 0, imax = deleteds.length; i < imax; ++i) {
var clone = deleteds[i].clone();
clone.lock(EShapeLockPart.ALL);
var clonePosition = clone.transform.position;
clonePosition.set(clonePosition.x - x, clonePosition.y - y);
if (clone instanceof EShapeConnectorLine) {
var edge = clone.edge;
var tail = edge.tail;
var tailAcceptor = tail.acceptor;
var tailAcceptorShape = tailAcceptor.shape;
var tailLocal = tail.local;
if (tailAcceptorShape == null) {
tailLocal.set(tailLocal.x - x, tailLocal.y - y);
}
else if (!deletedSet.has(tailAcceptorShape)) {
tailAcceptor.shape = null;
tailLocal.set(tailLocal.x - x, tailLocal.y - y);
}
var head = edge.head;
var headAcceptor = head.acceptor;
var headAcceptorShape = headAcceptor.shape;
var headLocal = head.local;
if (headAcceptorShape == null) {
headLocal.set(headLocal.x - x, headLocal.y - y);
}
else if (!deletedSet.has(headAcceptorShape)) {
headAcceptor.shape = null;
headLocal.set(headLocal.x - x, headLocal.y - y);
}
}
clone.parent = group;
groupChildren.push(clone);
}
EShapeConnectors.moveAll(deleteds, groupChildren, deleteds, groupChildren);
for (var i = 0, imax = groupChildren.length; i < imax; ++i) {
groupChildren[i].unlock(EShapeLockPart.ALL, true);
}
group.onChildTransformChange();
group.toDirty();
group.onAttach();
group.reference += 1;
this._createds = [group];
deletedSet.clear();
// Indices
this._indices = UtilShapeSearch.toIndices(deleteds);
// Select a group
selection.clearAndAddAll(this._createds);
selection.update("TREE");
selection.unlock();
return true;
};
ECommandShapeGroup.prototype.redo = function () {
// Delete shapes
var selection = this._selection;
selection.lock();
selection.clearAndAddAll(this._deleteds);
selection.delete(false);
// Add a group and select it
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.update("TREE");
selection.unlock();
return true;
};
ECommandShapeGroup.prototype.undo = function () {
// Delete a group
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;
};
ECommandShapeGroup.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 ECommandShapeGroup;
}(DCommandBase));
export { ECommandShapeGroup };
//# sourceMappingURL=e-command-shape-group.js.map