]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java
Improve picking to prefer vertices and with a more useful pick area
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / NetworkDrawingParticipant.java
index e5ce207e51af69f3760724a92c5697aa3748d591..4ff231034935f30c3da0d694cde4debc56198e4e 100644 (file)
@@ -6,7 +6,6 @@ import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import org.simantics.db.Resource;
@@ -15,6 +14,7 @@ import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
 import org.simantics.district.network.ui.nodes.NetworkDrawingNode;
+import org.simantics.district.network.ui.participants.DNPickSorter;
 import org.simantics.district.network.ui.participants.DynamicVisualisationContributionsParticipant;
 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
@@ -43,7 +43,7 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
 
     /**
      * Holds the current element for which hover information is shown.
-     * This is just to optimize the 
+     * This exists only to optimize the hover updating procedure.
      */
     private IElement currentHoverElement;
 
@@ -68,31 +68,8 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
         PickRequest req = new PickRequest(getPickRect(canvasPos, viewTransform)).context(getContext());
         List<IElement> pickables = new ArrayList<>();
         pick.pick(diagram, req, pickables);
-        
-
-        Comparator<IElement> nearestVerticesFirst = (IElement e1, IElement e2) -> {
-            // If there are any vertices in the elements, prefer those primarily.
-            DistrictNetworkVertexNode v1 = e1.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
-            DistrictNetworkVertexNode v2 = e2.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
-
-            // Don't reorder edges
-            if ((v1 == null && v2 == null))
-                return 0;
-
-            // Sort vertices in nearest first order
-            if (v1 != null && v2 != null) {
-                Rectangle2D b1 = v1.getBounds();
-                Rectangle2D b2 = v2.getBounds();
-                double dist1 = canvasPos.distanceSq(b1.getCenterX(), b1.getCenterY());
-                double dist2 = canvasPos.distanceSq(b2.getCenterX(), b2.getCenterY());
-                return dist1 < dist2 ? -1 : dist1 > dist2 ? 1 : 0;
-            }
-
-            // Always vertices before edges
-            return v1 != null && v2 == null ? -1 : 1;
-        };
 
-        Collections.sort(pickables, nearestVerticesFirst);
+        Collections.sort(pickables, DNPickSorter.nearestVerticesFirst(false, DNPickSorter.centerDistSq(canvasPos.getX(), canvasPos.getY())));
 
         updateHoveredElement(pickables, true, isConnectionTool, viewTransform);
         // Will repaint once the async hover info calculation is ready, no need to do it here