]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java
Use detailed geometry in drawing info labels, symbols and picking.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkStaticInfoNode.java
index 94b4e89613363f31027683067b3ae49d9910e45e..4ce35a27344450de02942cf3607036829f64f20c 100644 (file)
@@ -2,36 +2,38 @@ package org.simantics.district.network.ui.nodes;
 
 import java.awt.Color;
 import java.awt.Font;
+import java.awt.FontMetrics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.geom.AffineTransform;
-import java.awt.geom.NoninvertibleTransformException;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
 import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle;
-import org.simantics.maps.MapScalingTransform;
 import org.simantics.scenegraph.ParentNode;
 import org.simantics.scenegraph.g2d.G2DNode;
-import org.simantics.scenegraph.g2d.G2DParentNode;
 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.utils.DPIUtil;
 import org.simantics.scenegraph.utils.NodeUtil;
 
 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
 
-       private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
-       
-       private static final long serialVersionUID = 1L;
+       private static final long serialVersionUID = -1723122278582600873L;
+
+       private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10));
 
        private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0);
 
        public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
-       
+
        String info = null;
        Point2D origin = new Point2D.Double();
        Point2D direction = UNIT_X;
-       
+
+       private DistrictNetworkEdgeNode edgeNode = null;
+
+       private int prevZoomLevel = -1;
+
        @Override
        public void render(Graphics2D g) {
                ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
@@ -46,27 +48,25 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo
        public void renderDeferred(Graphics2D g) {
                if (info == null || "".equals(info))
                        return;
-               
-               AffineTransform oldTransform = g.getTransform();
-               
-               // There has to be a better way to calculate the zoom level in this context...
-               AffineTransform baseTransform;
-               try {
-                       baseTransform = ((G2DParentNode)getParent()).getTransform().createInverse();
-               } catch (NoninvertibleTransformException e) {
-                       baseTransform = new AffineTransform();
-               }
-               
-               baseTransform.preConcatenate(oldTransform);
-               int zoomLevel = MapScalingTransform.zoomLevel(baseTransform);
+               int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
                if (zoomLevel < 17)
                        return;
-               
+
+               AffineTransform oldTransform = g.getTransform();
+
                Font oldFont = g.getFont();
                Color oldColor = g.getColor();
                Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-               
+
+               if (edgeNode != null && zoomLevel != prevZoomLevel) {
+                       origin.setLocation(edgeNode.getCenterPoint(zoomLevel));
+                       Point2D dir = edgeNode.getDirection(zoomLevel);
+                       double s = dir.getX() >= 0.0 ? 1.0 : -1.0;
+                       direction.setLocation(s * dir.getX(), s * dir.getY());
+               }
+               prevZoomLevel = zoomLevel;
+
                doRender(g);
 
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
@@ -77,22 +77,24 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo
 
        private void doRender(Graphics2D g) {
                g.translate(origin.getX(), origin.getY());
-               double scale = 1.5 / g.getTransform().getScaleX();
+               double scale = 1.0 / g.getTransform().getScaleX();
                g.scale(scale, scale);
 
                g.setFont(FONT);
-               int width1 = g.getFontMetrics().stringWidth(info);
-//             int height = g.getFontMetrics().getHeight();
-               
+               FontMetrics fm = g.getFontMetrics();
+               int width = fm.stringWidth(info);
+               int ascent = fm.getMaxAscent();
+
                g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
-               g.translate(0, 10);
-               
+               g.translate(0, ascent);
+
+//             int height = fm.getHeight();
 //             g.setColor(Color.WHITE);
-//             g.fillRect(-width1/2 - 5, -height-2, width1+10, height+4);
-               
+//             g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2);
                g.setColor(Color.BLACK);
-               
-               g.drawString(info, -width1/2, 0);
+//             g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2);
+
+               g.drawString(info, -width/2, 0);
        }
 
        @Override
@@ -108,4 +110,8 @@ public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNo
        public void setInfo(String info) {
                this.info = info;
        }
+
+       public void setEdgeNode(DistrictNetworkEdgeNode n) {
+               this.edgeNode = n;
+       }
 }