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=e6b35994e05232dd536af4da224a342c1bd090af;hp=2a0cb1229c783f645d416e28a646af6d22311580;hpb=00e4eca98cef6d77d5023f4b424f9e8da0487463;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 2a0cb122..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,19 +1,27 @@ 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.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; @@ -21,6 +29,7 @@ public class DistrictNetworkVertexElement { public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX"); public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE"); + public static final Key KEY_DN_VERTEX_SYMBOL_NODE = new SceneGraphNodeKey(SVGNode.class, "DN_VERTEX_SYMBOL_NODE"); public static final ElementClass CLASS = ElementClass.compile( @@ -47,7 +56,11 @@ public class DistrictNetworkVertexElement { if (node == null) { node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class); vertexElement.setHint(KEY_DN_VERTEX_NODE, node); + + SVGNode symbol = node.addNode(ElementUtils.generateNodeId(vertexElement), SVGNode.class); + vertexElement.setHint(KEY_DN_VERTEX_SYMBOL_NODE, symbol); } + node.setVertex(vertex); node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE)); @@ -60,12 +73,17 @@ public class DistrictNetworkVertexElement { @Override public void cleanup(IElement edge) { - ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE); + ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_SYMBOL_NODE); + INode node = ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE); + if (node != null) + node.getParent().remove(); + edge.removeHint(KEY_DN_VERTEX_NODE); + edge.removeHint(KEY_DN_VERTEX_SYMBOL_NODE); } } - static final class DNVertexInternalSize implements InternalSize { + static final class DNVertexInternalSize implements InternalSize, Pick { public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize(); @@ -75,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(); + } } }