]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
First draft of vertex size adjusting district network diagram profiles
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkVertexNode.java
index a126ee5942a841f50a2108684a364a78052a4554..1dd0afe5c5e52a30b773262f85207d79066a9c9b 100644 (file)
 package org.simantics.district.network.ui.nodes;
 
-import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
-import java.awt.Stroke;
 import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
+import org.simantics.district.network.ModelledCRS;
 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
 import org.simantics.scenegraph.g2d.G2DNode;
 import org.simantics.scenegraph.utils.GeometryUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class DistrictNetworkVertexNode extends G2DNode {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
+
     private static final long serialVersionUID = -2641639101400236719L;
     private DistrictNetworkVertex vertex;
-    
-    private static final double scale = 1;
-    
-    private double left = -0.5;
-    private double top = -0.5;
-    private double width = 1;
-    private double height = 1;
-    
-    private Stroke stroke = new BasicStroke(2);
+
+    private static final double left = -0.25;
+    private static final double top = -0.25;
+    private static final double width = 0.5;
+    private static final double height = 0.5;
+
+    private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
+    private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
+
     private boolean scaleStroke = true;
+    private boolean hover;
+
+    private Color color;
+
+    private Rectangle2D bounds;
+
+    private double nodeSize = 1;
 
     @Override
     public void init() {
         setZIndex(2);
     }
-    
+
     @Override
     public void render(Graphics2D g2d) {
+        if (nodeSize <= 0.0)
+            return;
+
         AffineTransform ot = null;
         AffineTransform t = getTransform();
         if (t != null && !t.isIdentity()) {
             ot = g2d.getTransform();
             g2d.transform(getTransform());
         }
-        
+
+        Object oaaHint = null;
         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
-        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
-        
+        if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
+            oaaHint = aaHint;
+            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
+        }
+
         Color oldColor = g2d.getColor();
-        Stroke oldStroke = g2d.getStroke();
-        
-        g2d.setColor(Color.RED);
-        if (stroke != null) {
-            if (scaleStroke && stroke instanceof BasicStroke) {
-                BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
-                g2d.setStroke(bs);
-            } else {
-                g2d.setStroke(stroke);
-            }
+        g2d.setColor(color);
+
+        double scaleRecip = 1;
+        if (scaleStroke) {
+            double scale = GeometryUtils.getScale(g2d.getTransform());
+            scale = Math.max(10000, Math.min(scale, 50000));
+            scaleRecip = 1.0 / scale;
+        }
+        scaleRecip = scaleRecip * 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);
         }
         // render
-        
-        Rectangle2D.Double rect = new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale);
-        g2d.draw(rect);
-        
-        // Reset stats
+        g2d.fill(toDraw);
+
+        // Reset settings
         g2d.setColor(oldColor);
-        g2d.setStroke(oldStroke);
-        
-        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
-        
+        if (oaaHint != null)
+           g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
         if (ot != null)
             g2d.setTransform(ot);
     }
 
+    @Override
+    public Rectangle2D getBounds() {
+        return super.getBounds();
+    }
+
     @Override
     public Rectangle2D getBoundsInLocal() {
-        return new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale);
+        return bounds;
+    }
+
+    private void updateBounds() {
+        Rectangle2D oldBounds = bounds;
+        if (oldBounds == null)
+            oldBounds = new Rectangle2D.Double();
+        bounds = calculateBounds(oldBounds);
+    }
+
+    @Override
+    public void setTransform(AffineTransform transform) {
+        super.setTransform(transform);
+        // Update bounds
+        updateBounds();
+    }
+
+    @Override
+    public AffineTransform getTransform() {
+        return super.getTransform();
+    }
+
+    private Rectangle2D calculateBounds(Rectangle2D rect) {
+        Point2D calcPoint = calculatePoint2D(vertex);
+        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();
+        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;
     }
 
     public void setVertex(DistrictNetworkVertex vertex) {
         this.vertex = vertex;
+        updateBounds();
+    }
+
+    public boolean hover(boolean hover) {
+//        if (hover && LOGGER.isDebugEnabled())
+//            LOGGER.debug("Hovering " + this);
+        boolean changed = false;
+        if (this.hover != hover) {
+            this.hover = hover;
+            changed = true;
+        }
+        return changed;
+    }
+
+    public void setColor(Color color) {
+        this.color = color;
+    }
+
+    public Color getColor() {
+        return color;
+    }
+
+    @PropertySetter(value = "size")
+    public void setSize(Double size) {
+        boolean changed = false;
+        if (size != null) {
+            changed = size != this.nodeSize;
+            this.nodeSize = size;
+        } else {
+            changed = this.nodeSize != 1.0;
+            this.nodeSize = 1.0;
+        }
+        if (changed)
+            updateBounds();
     }
 
 }