]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java
Hover label profile for district elements.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / adapters / DistrictNetworkVertexElement.java
index 7be6a14faa659abb28df213e694b14e9094dd848..1ff5eefa407c7c75a3e978b9531232a23c067bdd 100644 (file)
@@ -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();
+        }
     }
 }