]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
Move remaining profiles to visualisations for perf
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkVertexNode.java
index 99eb3cc117b7fb91cbb8352016a62da7e9836249..b1bb1468dd63653ee1c423a9653fbc307088bc8e 100644 (file)
@@ -4,96 +4,132 @@ 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 java.util.Optional;
 
-import org.simantics.district.network.ModelledCRS;
 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
-import org.simantics.scenegraph.ParentNode;
+import org.simantics.maps.MapScalingTransform;
+import org.simantics.scenegraph.INode;
+import org.simantics.scenegraph.ISelectionPainterNode;
 import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.G2DRenderingHints;
+import org.simantics.scenegraph.g2d.IG2DNode;
+import org.simantics.scenegraph.g2d.nodes.SVGNode;
 import org.simantics.scenegraph.utils.GeometryUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.simantics.scenegraph.utils.NodeUtil;
 
-public class DistrictNetworkVertexNode extends G2DNode {
+public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
+
+    //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
-    
     private static final long serialVersionUID = -2641639101400236719L;
-    private DistrictNetworkVertex vertex;
 
-    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 double left = -15;
+    private static final double top = left;
+    public static final double width = 30;
+    private static final double height = width;
+
+    private static final BasicStroke STROKE           = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
+    private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
+
     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 Stroke stroke = new BasicStroke(2);
-    private boolean scaleStroke = true;
+    private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
+
+    private DistrictNetworkVertex vertex;
+
+    private static boolean scaleStroke = true;
     private boolean hover;
 
     private Color color;
+    private transient Color dynamicColor;
 
     private Rectangle2D bounds;
+    private transient Point2D point;
+    private transient Rectangle2D rect;
+    private transient AffineTransform symbolTransform;
 
-    private Double strokee;
+    private double nodeSize = 1.0;
+
+    private boolean hidden = false;
 
     @Override
     public void init() {
         setZIndex(2);
     }
-    
+
     @Override
     public void render(Graphics2D g2d) {
         AffineTransform ot = null;
         AffineTransform t = getTransform();
+        double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
         if (t != null && !t.isIdentity()) {
-            ot = g2d.getTransform();
-            g2d.transform(getTransform());
-        }
-        
-        Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
-        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
-        
-        Color oldColor = g2d.getColor();
-        Stroke oldStroke = g2d.getStroke();
-        
-        g2d.setColor(color);
-        
-        double scaleRecip = 1;
-        if (scaleStroke) {
-            double scale = GeometryUtils.getScale(g2d.getTransform());
-            double str;
-            if (strokee != null)
-                str = strokee;
-            else
-                str = 1.0;
-            //System.out.println("scale: " + scale);
-            scaleRecip = 1.0 / scale;
+            //ot = g2d.getTransform();
+            ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
+            if (ot == null)
+                ot = g2d.getTransform();
+            g2d.transform(t);
+
+            if (scaleStroke) {
+                AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
+                work.setTransform(ot);
+                work.concatenate(t);
+                viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
+            }
         }
-        scaleRecip = 8.0 * scaleRecip;
-        
+
         // 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 = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
+
+        if (!hidden && nodeSize > 0.0) {
+            Object oaaHint = null;
+            Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
+            if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
+                oaaHint = aaHint;
+                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
+            }
+    
+            Color oldColor = g2d.getColor();
+            Color newColor = dynamicColor != null ? dynamicColor : color;
+            boolean changeColor = !oldColor.equals(newColor);
+    
+            double scaleRecip = viewScaleRecip * nodeSize;
+    
+            Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
+    
+            if (NodeUtil.isSelected(this, 1)) {
+                changeColor = true;
+                g2d.setColor(SELECTION_COLOR);
+                BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
+                g2d.setStroke(ss);
+                g2d.draw(toDraw);
+            }
+    
+            // render
+            if (changeColor)
+                g2d.setColor(newColor);
+            g2d.fill(toDraw);
+            
+            // Reset settings
+            if (changeColor)
+                g2d.setColor(oldColor);
+            if (oaaHint != null)
+                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
+        }
+
+        // Render SVG symbol
+        for (INode nn : getNodes()) {
+            G2DNode g2dNode = (G2DNode)nn;
+            if (nn instanceof SVGNode) {
+                Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
+                symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
+                g2dNode.setTransform(symbolTransform);
+            }
+            
+            g2dNode.render(g2d);
         }
-        // render
-        g2d.fill(toDraw);
-        
-        // Reset stats
-        g2d.setColor(oldColor);
-        g2d.setStroke(oldStroke);
-        
-        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
         
         if (ot != null)
             g2d.setTransform(ot);
@@ -103,12 +139,13 @@ public class DistrictNetworkVertexNode extends G2DNode {
     public Rectangle2D getBounds() {
         return super.getBounds();
     }
-    
+
     @Override
     public Rectangle2D getBoundsInLocal() {
+        updateBounds();
         return bounds;
     }
-    
+
     private void updateBounds() {
         Rectangle2D oldBounds = bounds;
         if (oldBounds == null)
@@ -122,54 +159,112 @@ public class DistrictNetworkVertexNode extends G2DNode {
         // 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(), 1 / at.getScaleX(), 1 / 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;
+    private Rectangle2D calculateBounds(Rectangle2D rect) {
+        Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
+        AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
+        at.concatenate(MapScalingTransform.INSTANCE);
+        double x = calcPoint.getX();
+        double y = calcPoint.getY();
+        double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
+        double widthh = width * scaleRecip * nodeSize;
+        double heighth = height * scaleRecip * nodeSize;
+        if (rect == null)
+            rect = new Rectangle2D.Double();
+        rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
+        return rect;
     }
 
     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;
+
+    public DistrictNetworkVertex getVertex() {
+        return vertex;
+    }
+
+    @Override
+    public boolean hover(boolean hover, boolean isConnectionTool) {
+        // Only react to hover when the connection tool is active
+        boolean doHover = hover && isConnectionTool;
+        boolean changed = this.hover != doHover;
+        this.hover = doHover;
+        
+        for (IG2DNode child : getNodes()) {
+            if (child instanceof HoverSensitiveNode)
+                changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
         }
+        
         return changed;
     }
+    
+    @Override
+    public void setMousePosition(Point2D p) {
+        for (IG2DNode child : getNodes()) {
+            if (child instanceof HoverSensitiveNode)
+                ((HoverSensitiveNode) child).setMousePosition(p);
+        }
+    }
 
     public void setColor(Color color) {
         this.color = color;
     }
-    
+
     public Color getColor() {
         return color;
     }
 
-    @PropertySetter(value = "stroke")
-    public void setStroke(Double stroke) {
-        this.strokee = stroke / 10;
+    @PropertySetter(value = "SVG")
+    public void setSVG(String value) {
+        for (INode nn : this.getNodes())
+            if (nn instanceof SVGNode)
+                ((SVGNode)nn).setData(value);
+    }
+    
+    
+    @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();
+    }
+
+    @PropertySetter(value = "dynamicColor")
+    public void setDynamicColor(Color color) {
+        this.dynamicColor = color;
+    }
+
+    @PropertySetter(value = "hidden")
+    public void setHidden(Boolean value) {
+        this.hidden = value;
+    }
+
+    public void setStaticInformation(Optional<String> staticInformation) {
+        DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
+        Point2D calculatePoint2D = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), null);
+        child.setLocation(calculatePoint2D, new Point2D.Double(1.0, 0.0));
+        if (staticInformation.isPresent()) {
+            child.setInfo(staticInformation.get());
+        } else {
+            child.setInfo(null);
+        }
+    }
+    
+    public void setInSimulation(Optional<Boolean> isInSimulation) {
+        if (!isInSimulation.isPresent()) {
+            removeNode(NotInSimulationNode.NODE_NAME);
+        } else {
+            NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
+            child.setZIndex(1000);
+            child.setIsInSimulation(isInSimulation.get());
+        }
     }
 }