package org.simantics.district.network.ui.adapters;
import java.awt.Color;
+import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
+import org.simantics.diagram.elements.DiagramNodeUtil;
import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
+import org.simantics.g2d.canvas.Hints;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
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.Pick;
import org.simantics.g2d.element.handler.SceneGraph;
import org.simantics.g2d.element.handler.impl.DefaultTransform;
import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
import org.simantics.scenegraph.INode;
import org.simantics.scenegraph.g2d.G2DParentNode;
import org.simantics.scenegraph.g2d.nodes.SVGNode;
+import org.simantics.scenegraph.utils.GeometryUtils;
import org.simantics.utils.datastructures.hints.IHintContext.Key;
import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
}
}
- static final class DNVertexInternalSize implements InternalSize {
+ static final class DNVertexInternalSize implements InternalSize, Pick {
public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
public Rectangle2D getBounds(IElement e, Rectangle2D size) {
DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE);
Rectangle2D boundsInLocal = node.getBoundsInLocal();
- size.setFrame(boundsInLocal);
+ ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node);
+ AffineTransform canvasTransform = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
+ // for some reason PickContextImpl expands the rectangle by 0.001 (too much) - let's counter it
+ double counterExpansion = 0.001;
+ double x = boundsInLocal.getX() + counterExpansion;
+ double y = boundsInLocal.getY() + counterExpansion;
+ double scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX() / 10000.0;
+ double scaledHeight = boundsInLocal.getHeight() / canvasTransform.getScaleY() / 10000.0;
+ double width = scaledWidth - 2*counterExpansion;
+ double height = scaledHeight - 2*counterExpansion;
+ size.setFrame(x, y, width, height);
+
return size;
}
+
+ @Override
+ public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
+ DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE);
+ Rectangle2D boundsInLocal = node.getBounds();
+ ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node);
+ AffineTransform canvasTransform = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
+ Rectangle2D scaledBounds = new Rectangle2D.Double(boundsInLocal.getX(), boundsInLocal.getY(), boundsInLocal.getWidth() / canvasTransform.getScaleX() * 2, boundsInLocal.getHeight() / canvasTransform.getScaleY() * 2);
+ Rectangle2D bounds = getBounds(s);
+ switch (policy) {
+ case PICK_CONTAINED_OBJECTS:
+ return org.simantics.g2d.utils.GeometryUtils.contains(bounds, boundsInLocal);
+ case PICK_INTERSECTING_OBJECTS:
+ return org.simantics.g2d.utils.GeometryUtils.intersects(boundsInLocal, bounds);
+ }
+ return false;
+ }
+ private Rectangle2D getBounds(Shape shape) {
+ if (shape instanceof Rectangle2D)
+ return (Rectangle2D) shape;
+ return shape.getBounds2D();
+ }
}
}