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=726c0c85d561c2ac85c340bc3d5e6b52e537fbee;hb=e6b35994e05232dd536af4da224a342c1bd090af;hp=d47580074a9d5a3ef23855bc78745d44be726d4d;hpb=db34439af303d45eb67cee78cb3f68c9b6666da4;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 d4758007..726c0c85 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,8 @@ import java.awt.Color; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; +import java.awt.geom.Path2D; +import java.awt.geom.PathIterator; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.Collection; @@ -29,6 +31,7 @@ import org.simantics.g2d.element.handler.impl.SimpleElementLayers; import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.SVGNode; +import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; import org.slf4j.Logger; @@ -36,6 +39,8 @@ import org.slf4j.LoggerFactory; public class DistrictNetworkEdgeElement { + private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkEdgeElement.class); + public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE"); public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE"); public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE"); @@ -60,25 +65,29 @@ public class DistrictNetworkEdgeElement { @Override public void init(IElement edgeElement, G2DParentNode parent) { - DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE); - if (edge == null) { - cleanup(edgeElement); - } else { - DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE); - if (node == null) { - node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class); - edgeElement.setHint(KEY_DN_EDGE_NODE, node); + try { + DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE); + if (edge == null) { + cleanup(edgeElement); + } else { + DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE); + if (node == null) { + node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class); + edgeElement.setHint(KEY_DN_EDGE_NODE, node); + + SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class); + edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol); + } + + node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE)); - SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class); - edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol); + node.setDNEdge(edge); + AffineTransform at = ElementUtils.getTransform(edgeElement); + if (at != null) + node.setTransform(at); } - - node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE)); - - node.setDNEdge(edge); - AffineTransform at = ElementUtils.getTransform(edgeElement); - if (at != null) - node.setTransform(at); + } catch (Exception e) { + LOGGER.error("Could not initialize element {} for parent {}", edgeElement, parent, e); } } @@ -101,11 +110,11 @@ public class DistrictNetworkEdgeElement { @Override public Rectangle2D getBounds(IElement e, Rectangle2D size) { - DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); + DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE); if (size == null) size = new Rectangle2D.Double(); - if (edge != null) - size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D()); + if (edgeNode != null) + size.setFrame(edgeNode.getBoundsInLocal()); else LOGGER.debug("Element {} does not have edge!", e); @@ -114,9 +123,9 @@ public class DistrictNetworkEdgeElement { @Override public Shape getElementShape(IElement e) { - DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); - if (edge != null) { - return DistrictNetworkEdgeNode.calculatePath(edge, null); + DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE); + if (edgeNode != null) { + return edgeNode.getPath(); } else { return getBounds(e, null); } @@ -129,7 +138,7 @@ public class DistrictNetworkEdgeElement { Rectangle2D bounds = getBounds(s); switch (policy) { case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds); - case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds); + case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds); } return false; } @@ -162,21 +171,54 @@ public class DistrictNetworkEdgeElement { 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()); + private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) { + double dx = bounds.getWidth() / MapScalingTransform.getScaleX(); + double dy = bounds.getHeight() / MapScalingTransform.getScaleY(); + + // Half the diagonal + half of the line width + DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE); + AffineTransform at = NodeUtil.getLocalToGlobalTransform(node); + + Path2D path = node.getPath(); + if (path == null) + return false; + + double lineWidth = node.getStrokeWidth(at, true); + double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2; + 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; + + double coords[] = new double[6]; + Point2D prevPoint = new Point2D.Double(), curPoint = new Point2D.Double(); + Line2D line = new Line2D.Double(); + for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) { + int type = it.currentSegment(coords); + switch (type) { + case PathIterator.SEG_MOVETO: + curPoint.setLocation(coords[0], coords[1]); + break; + case PathIterator.SEG_LINETO: + prevPoint.setLocation(curPoint); + curPoint.setLocation(coords[0], coords[1]); + line.setLine(prevPoint, curPoint); + double distSq = line.ptSegDistSq(sx, sy); +// 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; + break; + default: + LOGGER.error("Invalid edge path", new IllegalStateException()); + return false; + } + } + + return false; } private Rectangle2D getBounds(Shape shape) {