@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
331 lines • 13.5 kB
JavaScript
import { EShapeAcceptors, EShapeCapabilities, EShapeCapability, EShapeConnectorLine } from "@wcardinal/wcardinal-ui";
import { Matrix, Point } from "pixi.js";
import { EToolShapeAcceptorEdgeRenderer } from "./e-tool-shape-acceptor-edge-renderer";
var SIZE = 10;
var EToolShapeAcceptorEdge = /** @class */ (function () {
function EToolShapeAcceptorEdge(diagram, canvasMode) {
this._diagram = diagram;
this._canvasMode = canvasMode;
this._renderer = new EToolShapeAcceptorEdgeRenderer(this);
this._work = new Set();
}
EToolShapeAcceptorEdge.prototype.begin = function (canvas, shape) {
this.clear();
this._shape = shape;
if (canvas != null) {
this._renderer.begin(canvas);
}
};
EToolShapeAcceptorEdge.prototype.end = function () {
this._renderer.end();
this.clear();
};
EToolShapeAcceptorEdge.prototype.update = function (type, x, y) {
this._renderer.update(type, x, y);
};
EToolShapeAcceptorEdge.prototype.show = function () {
this._renderer.visible = true;
};
EToolShapeAcceptorEdge.prototype.hide = function () {
this._renderer.visible = false;
};
Object.defineProperty(EToolShapeAcceptorEdge.prototype, "edges", {
get: function () {
var result = this._edges;
if (result == null) {
result = this.newEdges();
this._edges = result;
}
return result;
},
enumerable: false,
configurable: true
});
EToolShapeAcceptorEdge.prototype.newEdges = function () {
var result = new Map();
var layer = this._diagram.layer;
if (layer) {
var layerPositionX = 0;
var layerPositionY = 0;
if (this._canvasMode) {
var layerPosition = layer.transform.position;
layerPositionX = layerPosition.x;
layerPositionY = layerPosition.y;
}
var shape = this._shape;
if (shape != null) {
var shapeParent = shape.parent;
if (shapeParent != null) {
this.addAll(shapeParent.children, layerPositionX, layerPositionY, result);
}
}
else {
this.addAll(layer.children, layerPositionX, layerPositionY, result);
}
}
return result;
};
EToolShapeAcceptorEdge.prototype.addAll = function (shapes, lx, ly, result) {
var _this = this;
var _loop_1 = function (i, imax) {
var shape = shapes[i];
if (!(shape instanceof EShapeConnectorLine)) {
var size = shape.size;
var sx = size.x;
var sy = size.y;
var transform = shape.transform;
var internal = transform.internalTransform;
var a = internal.a;
var b = internal.b;
var c = internal.c;
var d = internal.d;
var pivot = transform.pivot;
var px = pivot.x;
var py = pivot.y;
var matrix_1 = new Matrix(a * sx, b * sx, c * sy, d * sy, internal.tx + lx + (a * px + c * py), internal.ty + ly + (b * px + d * py));
EShapeAcceptors.get(shape.type).each(shape, function (edge, id) {
_this.add(matrix_1, id, edge, shape, result);
});
if (EShapeCapabilities.contains(shape, EShapeCapability.CHILDREN)) {
var children = shape.children;
if (children) {
this_1.addAll(children, lx, ly, result);
}
}
}
};
var this_1 = this;
for (var i = 0, imax = shapes.length; i < imax; ++i) {
_loop_1(i, imax);
}
};
EToolShapeAcceptorEdge.prototype.add = function (matrix, id, acceptorEdge, shape, coordinateToEdges) {
var aes = acceptorEdge.size;
var aesx = aes.x;
var aesy = aes.y;
var aex = acceptorEdge.x;
var aey = acceptorEdge.y;
var a = matrix.a;
var b = matrix.b;
var c = matrix.c;
var d = matrix.d;
var tx = matrix.tx;
var ty = matrix.ty;
var x = a * aex + c * aey + tx;
var y = b * aex + d * aey + ty;
var edge = {
shape: shape,
id: id,
x: null,
y: null,
acceptor: acceptorEdge,
matrix: matrix,
local: new Point(x, y),
center: new Point(x, y),
distance: 0
};
if (aesx === 0 && aesy === 0) {
var gx = this.toGridIndex(x);
var gy = this.toGridIndex(y);
var coordinate = this.toCoordinate(gx, gy);
var edges = coordinateToEdges.get(coordinate);
if (edges == null) {
coordinateToEdges.set(coordinate, [edge]);
}
else {
edges.push(edge);
}
}
else {
var aesxh = aesx * 0.5;
var aesyh = aesy * 0.5;
var aex0 = aex - aesxh;
var aey0 = aey - aesyh;
var aex1 = aex + aesxh;
var aey1 = aey - aesyh;
var aex2 = aex + aesxh;
var aey2 = aey + aesyh;
var aex3 = aex - aesxh;
var aey3 = aey + aesyh;
var x0 = a * aex0 + c * aey0;
var y0 = b * aex0 + d * aey0;
var x1 = a * aex1 + c * aey1;
var y1 = b * aex1 + d * aey1;
var x2 = a * aex2 + c * aey2;
var y2 = b * aex2 + d * aey2;
var x3 = a * aex3 + c * aey3;
var y3 = b * aex3 + d * aey3;
var x4 = Math.min(x0, x1, x2, x3) + tx;
var y4 = Math.min(y0, y1, y2, y3) + ty;
var x5 = Math.max(x0, x1, x2, x3) + tx;
var y5 = Math.max(y0, y1, y2, y3) + ty;
var ix0 = Math.floor(x4 / SIZE);
var iy0 = Math.floor(y4 / SIZE);
var ix1 = Math.ceil(x5 / SIZE);
var iy1 = Math.ceil(y5 / SIZE);
for (var ix = ix0; ix <= ix1; ++ix) {
for (var iy = iy0; iy <= iy1; ++iy) {
var coordinate = this.toCoordinate(ix, iy);
var edges = coordinateToEdges.get(coordinate);
if (edges == null) {
coordinateToEdges.set(coordinate, [edge]);
}
else {
edges.push(edge);
}
}
}
}
};
EToolShapeAcceptorEdge.prototype.toCoordinate = function (ix, iy) {
return (ix % 1024) + (iy % 1024 << 10);
};
EToolShapeAcceptorEdge.prototype.toGridIndex = function (x) {
return Math.round(x / SIZE);
};
EToolShapeAcceptorEdge.prototype.find = function (type, x, y) {
var result = null;
var distance = 0;
var edges = this.edges;
var threshold = SIZE * SIZE;
for (var ix = -SIZE; ix <= SIZE; ix += SIZE) {
for (var iy = -SIZE; iy <= SIZE; iy += SIZE) {
var gix = this.toGridIndex(x + ix);
var giy = this.toGridIndex(y + iy);
var foundEdges = edges.get(this.toCoordinate(gix, giy));
if (foundEdges == null) {
continue;
}
for (var i = 0, imax = foundEdges.length; i < imax; ++i) {
var foundEdge = foundEdges[i];
var acceptorEdge = foundEdge.acceptor;
if ((acceptorEdge.type & type) === 0) {
continue;
}
var size = acceptorEdge.size;
var sx = size.x;
var sy = size.y;
if (sx === 0 && sy === 0) {
var center = foundEdge.center;
var dx = x - center.x;
var dy = y - center.y;
var dd = dx * dx + dy * dy;
if (dd <= threshold && (result == null || dd < distance)) {
result = foundEdge;
distance = dd;
}
}
else {
var matrix = foundEdge.matrix;
var a = matrix.a;
var b = matrix.b;
var c = matrix.c;
var d = matrix.d;
var tx = matrix.tx;
var ty = matrix.ty;
var det = a * d - c * b;
if (0.0000001 < Math.abs(det)) {
var di = 1 / det;
var xtx = x - tx;
var yty = y - ty;
var lx = (+d * xtx - c * yty) * di;
var ly = (-b * xtx + a * yty) * di;
var sxh = sx * 0.5;
var syh = sy * 0.5;
var aex = acceptorEdge.x;
var aey = acceptorEdge.y;
var lx0 = aex - sxh;
var ly0 = aey - syh;
var lx1 = aex + sxh;
var ly1 = aey + syh;
var lx2 = Math.min(lx1, Math.max(lx0, lx));
var ly2 = Math.min(ly1, Math.max(ly0, ly));
var x0 = a * lx2 + c * ly2 + tx;
var y0 = b * lx2 + d * ly2 + ty;
var dx = x - x0;
var dy = y - y0;
var dd = dx * dx + dy * dy;
if (dd <= threshold && (result == null || dd < distance)) {
result = foundEdge;
result.x = lx2;
result.y = ly2;
result.local.set(x0, y0);
distance = dd;
}
}
else {
var center = foundEdge.center;
var dx = x - center.x;
var dy = y - center.y;
var dd = dx * dx + dy * dy;
if (dd <= threshold && (result == null || dd < distance)) {
result = foundEdge;
distance = dd;
}
}
}
}
}
}
return result;
};
EToolShapeAcceptorEdge.prototype.findAll = function (type, x, y, range, limit, result) {
var edges = this.edges;
range = Math.ceil(range / SIZE) * SIZE;
var threshold = range * range;
var work = this._work;
work.clear();
this.findAllS1(type, x, y, 0, 0, edges, threshold, work);
for (var ir = SIZE; ir <= range && work.size < limit; ir += SIZE) {
this.findAllS2(type, x, y, edges, threshold, ir, work);
}
var iresult = -1;
work.forEach(function (found) {
result[++iresult] = found;
});
result.length = work.size;
result.sort(this.compare);
return result;
};
EToolShapeAcceptorEdge.prototype.findAllS2 = function (type, x, y, edges, threshold, radius, result) {
for (var dx = -radius; dx <= radius; dx += SIZE) {
this.findAllS1(type, x, y, dx, -radius, edges, threshold, result);
this.findAllS1(type, x, y, dx, +radius, edges, threshold, result);
}
for (var dy = -radius + SIZE, iymax = radius - SIZE; dy <= iymax; dy += SIZE) {
this.findAllS1(type, x, y, -radius, dy, edges, threshold, result);
this.findAllS1(type, x, y, +radius, dy, edges, threshold, result);
}
};
EToolShapeAcceptorEdge.prototype.findAllS1 = function (type, x, y, dx, dy, edges, threshold, result) {
var gix = this.toGridIndex(x + dx);
var giy = this.toGridIndex(y + dy);
var foundEdges = edges.get(this.toCoordinate(gix, giy));
if (foundEdges) {
for (var i = 0, imax = foundEdges.length; i < imax; ++i) {
var foundEdge = foundEdges[i];
if (foundEdge.acceptor.type & type) {
var foundEdgeLocal = foundEdge.local;
var sx = x - foundEdgeLocal.x;
var sy = y - foundEdgeLocal.y;
var sd = sx * sx + sy * sy;
if (sd <= threshold) {
foundEdge.distance = sd;
result.add(foundEdge);
}
}
}
}
};
EToolShapeAcceptorEdge.prototype.compare = function (a, b) {
var ad = a.distance;
var bd = b.distance;
return ad < bd ? -1 : ad > bd ? +1 : 0;
};
EToolShapeAcceptorEdge.prototype.clear = function () {
this._edges = undefined;
};
return EToolShapeAcceptorEdge;
}());
export { EToolShapeAcceptorEdge };
//# sourceMappingURL=e-tool-shape-acceptor-edge.js.map