Browse Source

Select the closest edge, not just any nearby (#1922)

codeClimate
root9B 8 years ago
committed by Alexander Wunschik
parent
commit
d5e8a1410e
1 changed files with 23 additions and 6 deletions
  1. +23
    -6
      lib/network/modules/SelectionHandler.js

+ 23
- 6
lib/network/modules/SelectionHandler.js View File

@ -219,15 +219,32 @@ class SelectionHandler {
* @returns {undefined}
*/
getEdgeAt(pointer, returnEdge = true) {
let positionObject = this._pointerToPositionObject(pointer);
let overlappingEdges = this._getAllEdgesOverlappingWith(positionObject);
if (overlappingEdges.length > 0) {
// Iterate over edges, pick closest within 10
var canvasPos = this.canvas.DOMtoCanvas(pointer);
var mindist = 10;
var overlappingEdge = null;
var edges = this.body.edges;
for (var i = 0; i < this.body.edgeIndices.length; i++) {
var edgeId = this.body.edgeIndices[i];
var edge = edges[edgeId];
if (edge.connected) {
var xFrom = edge.from.x;
var yFrom = edge.from.y;
var xTo = edge.to.x;
var yTo = edge.to.y;
var dist = edge.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, canvasPos.x, canvasPos.y);
if(dist < mindist){
overlappingEdge = edgeId;
mindist = dist;
}
}
}
if (overlappingEdge) {
if (returnEdge === true) {
return this.body.edges[overlappingEdges[overlappingEdges.length - 1]];
return this.body.edges[overlappingEdge];
}
else {
return overlappingEdges[overlappingEdges.length - 1];
return overlappingEdge;
}
}
else {

Loading…
Cancel
Save