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%2FDistrictNetworkVertexElement.java;h=1ff5eefa407c7c75a3e978b9531232a23c067bdd;hb=0249f20bb127e61c58db4432b8609ff0cb441480;hp=7be6a14faa659abb28df213e694b14e9094dd848;hpb=beab0411489f5730942d98d47dc3faf3107c6c02;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java index 7be6a14f..1ff5eefa 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java @@ -1,15 +1,21 @@ 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; @@ -77,7 +83,7 @@ public class DistrictNetworkVertexElement { } } - static final class DNVertexInternalSize implements InternalSize { + static final class DNVertexInternalSize implements InternalSize, Pick { public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize(); @@ -87,9 +93,42 @@ public class DistrictNetworkVertexElement { 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 x = boundsInLocal.getX(); + double y = boundsInLocal.getY(); + double scaledWidth = boundsInLocal.getWidth(); + double scaledHeight = boundsInLocal.getHeight(); + if (canvasTransform != null) { + scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX(); + scaledHeight= boundsInLocal.getHeight() / canvasTransform.getScaleY(); + } + double width = scaledWidth; + double height = scaledHeight; + 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(); + 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(); + } } }