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=cbc6a578638e9a1d679915613af759de27b751be;hb=29914be09d4a237840e5c793bdb562ec83093b8d;hp=db730b1e6fafcec231b54ecdde6afe73d36e0d57;hpb=10fe42db158693adffc526131d37f58cbd5780b2;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 db730b1e..cbc6a578 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 @@ -1,23 +1,32 @@ package org.simantics.district.network.ui.adapters; import java.awt.Color; +import java.awt.Shape; import java.awt.geom.AffineTransform; +import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.Collection; import java.util.Collections; +import org.simantics.district.network.ModelledCRS; import org.simantics.district.network.ui.DistrictNetworkEdge; import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode; import org.simantics.g2d.connection.handler.ConnectionHandler; +import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy; import org.simantics.g2d.diagram.handler.Topology.Connection; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.SceneGraphNodeKey; 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; +import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; @@ -28,7 +37,7 @@ public class DistrictNetworkEdgeElement { 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 ElementClass CLASS = ElementClass.compile( DefaultTransform.INSTANCE, @@ -36,14 +45,16 @@ public class DistrictNetworkEdgeElement { DNEdgeSceneGraph.INSTANCE, DNEdgeConnectionHandler.INSTANCE, SimpleElementLayers.INSTANCE, + // TODO: do we need this and does it work? + ConnectionSelectionOutline.INSTANCE, DistrictNetworkAdditionalColor.INSTANCE ).setId(DistrictNetworkEdgeElement.class.getSimpleName()); - + static final class DNEdgeSceneGraph implements SceneGraph { - + public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph(); - private static final long serialVersionUID = 8894367073815556871L; + private static final long serialVersionUID = 68135568495835923L; @Override public void init(IElement edgeElement, G2DParentNode parent) { @@ -72,13 +83,13 @@ public class DistrictNetworkEdgeElement { edge.removeHint(KEY_DN_EDGE_NODE); } } - - static final class DNEdgeInternalSize implements InternalSize { + + static final class DNEdgeInternalSize implements InternalSize, Outline, Pick { private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class); - - private static final long serialVersionUID = -2725017034692179676L; - + + private static final long serialVersionUID = -7346653820911240628L; + public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize(); @Override @@ -87,18 +98,84 @@ public class DistrictNetworkEdgeElement { if (size == null) size = new Rectangle2D.Double(); if (edge != null) - size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge).getBounds2D()); + size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D()); else LOGGER.debug("Element {} does not have edge!", e); return size; } + + @Override + public Shape getElementShape(IElement e) { + DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); + if (edge != null) { + return DistrictNetworkEdgeNode.calculatePath(edge, null); + } else { + return getBounds(e, null); + } + } + + 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; + } + } + return false; + } + + return false; + } + + private Rectangle2D getBounds(Shape shape) { + if (shape instanceof Rectangle2D) + return (Rectangle2D) shape; + return shape.getBounds2D(); + } + } - + static class DNEdgeConnectionHandler implements ConnectionHandler { - private static final long serialVersionUID = -410377314637446238L; - + private static final long serialVersionUID = -6882671891381761687L; + public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler(); @Override @@ -121,4 +198,5 @@ public class DistrictNetworkEdgeElement { return Collections.emptyList(); } } + }