]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
Fixed regressions in box selection and element color function validation
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkVertexNode.java
index 58e47f4bcf285dff9f5585aafc7d6a8e5f0eb49e..0cd5ea884fd29058c2e12907eca01112a3e2dbe0 100644 (file)
@@ -41,6 +41,8 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain
     private transient Color dynamicColor;
 
     private Rectangle2D bounds;
+    private transient Point2D point;
+    private transient Rectangle2D rect;
 
     private double nodeSize = 1;
 
@@ -69,44 +71,52 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain
         }
 
         Color oldColor = g2d.getColor();
+        Color newColor = dynamicColor != null ? dynamicColor : color;
+        boolean changeColor = !oldColor.equals(newColor);
 
-        double scaleRecip = 1;
+        double viewScaleRecip = 1;
         if (scaleStroke) {
             double scale = GeometryUtils.getScale(g2d.getTransform());
             scale = Math.max(10000, Math.min(scale, 50000));
-            scaleRecip = 1.0 / scale;
+            viewScaleRecip = 1.0 / scale;
         }
-        scaleRecip = scaleRecip * nodeSize;
+        double scaleRecip = viewScaleRecip * nodeSize;
 
         // Translate lat and lon to X and Y
-        Point2D res = calculatePoint2D(vertex);
-
-        Rectangle2D toDraw;
-        if (hover) {
-            toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
-        } else {
-            toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
-        }
+        Point2D p = point = calculatePoint2D(vertex, point);
+        Rectangle2D toDraw = calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
 
         if (NodeUtil.isSelected(this, 1)) {
+            changeColor = true;
             g2d.setColor(SELECTION_COLOR);
-            BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) scaleRecip*2);
+            BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) viewScaleRecip*5);
             g2d.setStroke(ss);
             g2d.draw(toDraw);
         }
 
         // render
-        g2d.setColor(dynamicColor != null ? dynamicColor : color);
+        if (changeColor)
+            g2d.setColor(newColor);
         g2d.fill(toDraw);
 
         // Reset settings
-        g2d.setColor(oldColor);
+        if (changeColor)
+            g2d.setColor(oldColor);
         if (oaaHint != null)
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
         if (ot != null)
             g2d.setTransform(ot);
     }
 
+    private Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
+        if (result == null)
+            result = new Rectangle2D.Double();
+        double mw = margin.getWidth();
+        double mh = margin.getHeight();
+        result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip);
+        return result;
+    }
+
     @Override
     public Rectangle2D getBounds() {
         return super.getBounds();
@@ -137,19 +147,20 @@ public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPain
     }
 
     private Rectangle2D calculateBounds(Rectangle2D rect) {
-        Point2D calcPoint = calculatePoint2D(vertex);
+        Point2D calcPoint = calculatePoint2D(vertex, point);
         AffineTransform at = getTransform();
         return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
     }
 
-    private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
-        Point2D point= vertex.getPoint();
+    private static Point2D calculatePoint2D(DistrictNetworkVertex vertex, Point2D result) {
+        Point2D point = vertex.getPoint();
         double x = ModelledCRS.longitudeToX(point.getX());
         double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
-
-        // Apply the scaling
-        Point2D res = new Point2D.Double(x, y);
-        return res;
+        if (result == null)
+            result = new Point2D.Double(x, y);
+        else
+            result.setLocation(x, y);
+        return result;
     }
 
     public void setVertex(DistrictNetworkVertex vertex) {