X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fadapters%2FDistrictNetworkEdgeElement.java;h=9595f232debb6339459ff11203b5e2f56139bed4;hb=a4105d9cb79a428fbe3a4aa918fc6d1527f1988a;hp=cbc6a578638e9a1d679915613af759de27b751be;hpb=4b0e2c52e8124855ae4a4e6c71ddf7ae55df7db5;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java index cbc6a578..9595f232 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; +import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.Collection; import java.util.Collections; @@ -22,7 +23,6 @@ import org.simantics.g2d.element.handler.InternalSize; import org.simantics.g2d.element.handler.Outline; import org.simantics.g2d.element.handler.Pick; import org.simantics.g2d.element.handler.SceneGraph; -import org.simantics.g2d.element.handler.SelectionOutline; import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; @@ -115,48 +115,14 @@ public class DistrictNetworkEdgeElement { } } - private Shape getSelectionShape(IElement e) { - for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) { - Shape shape = so.getSelectionShape(e); - if (shape != null) - return shape; - } - // Using on-diagram coordinates because neither connections nor - // edges have a non-identity transform which means that - // coordinates are always absolute. Therefore branch point - // shape also needs to be calculated in absolute coordinates. - Shape shape = ElementUtils.getElementShapeOrBoundsOnDiagram(e); - return shape; - } - @Override public boolean pickTest(IElement e, Shape s, PickPolicy policy) { DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); if (edge != null) { Rectangle2D bounds = getBounds(s); - switch (policy) { - case PICK_CONTAINED_OBJECTS: - Shape selectionShape = getSelectionShape(e); - return bounds.contains(selectionShape.getBounds2D()); - - case PICK_INTERSECTING_OBJECTS: - double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX(); - Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint()); - double sx = bounds.getCenterX() / MapScalingTransform.getScaleX(); - double sy = bounds.getCenterY() / MapScalingTransform.getScaleY(); - double ssx = ModelledCRS.xToLongitude(sx); - double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system - double distSq = line.ptSegDistSq(ssx, ssy); -// System.out.println("s: " + sx + ", " + sy); -// System.out.println("ss: " + ssx + ", " + ssy); -// System.out.println("p1: " + edge.getStartPoint()); -// System.out.println("p2: " + edge.getEndPoint()); -// System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")"); -// System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance); - if (distSq <= tolerance * tolerance) { - return true; - } + case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds); + case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds); } return false; } @@ -164,6 +130,48 @@ public class DistrictNetworkEdgeElement { return false; } + private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) { + double bminx = bounds.getMinX() / MapScalingTransform.getScaleX(); + double bminy = bounds.getMinY() / MapScalingTransform.getScaleY(); + double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX(); + double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY(); + + double bsminx = ModelledCRS.xToLongitude(bminx); + double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system + double bsmaxx = ModelledCRS.xToLongitude(bmaxx); + double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system + + double boundsMinY = Math.min(bsminy, bsmaxy); + double boundsMaxY = Math.max(bsminy, bsmaxy); + + Point2D start = edge.getStartPoint(); + Point2D end = edge.getEndPoint(); + + double eminx = Math.min(start.getX(), end.getX()); + double eminy = Math.min(start.getY(), end.getY()); + double emaxx = Math.max(start.getX(), end.getX()); + double emaxy = Math.max(start.getY(), end.getY()); + + return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY; + } + + private boolean pickIntersectingObjects(DistrictNetworkEdge edge, Rectangle2D bounds) { + double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX(); + Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint()); + double sx = bounds.getCenterX() / MapScalingTransform.getScaleX(); + double sy = bounds.getCenterY() / MapScalingTransform.getScaleY(); + double ssx = ModelledCRS.xToLongitude(sx); + double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system + double distSq = line.ptSegDistSq(ssx, ssy); +// System.out.println("s: " + sx + ", " + sy); +// System.out.println("ss: " + ssx + ", " + ssy); +// System.out.println("p1: " + edge.getStartPoint()); +// System.out.println("p2: " + edge.getEndPoint()); +// System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")"); +// System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance); + return distSq <= tolerance * tolerance; + } + private Rectangle2D getBounds(Shape shape) { if (shape instanceof Rectangle2D) return (Rectangle2D) shape;