]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java
HiDPI scaling for diagram ruler and model activity tracker
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / RulerNode.java
index 7e962976e52f1b67ec5f489af713c25357e90a2b..785c246b4e20679396f5cf9b5282e100d512455f 100644 (file)
@@ -22,6 +22,7 @@ import java.awt.geom.Rectangle2D;
 import java.util.Locale;
 
 import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.scenegraph.utils.DPIUtil;
 import org.simantics.scenegraph.utils.GridUtils;
 
 public class RulerNode extends G2DNode {
@@ -30,17 +31,14 @@ public class RulerNode extends G2DNode {
      */
     private static final long  serialVersionUID  = 2490944880914577411L;
 
-    /**
-     * FIXME: this is a hack for the map UI that has to be solved some other way.
-     */
-    private static final boolean MAP_Y_SCALING = false;
-
     private static final Color GRAY              = new Color(100, 100, 100);
 
     protected Boolean          enabled           = true;
 
     protected double           gridSize          = 1.0;
 
+    protected double           rulerSize         = 20;
+
     @SyncField("enabled")
     public void setEnabled(Boolean enabled) {
         this.enabled = enabled;
@@ -53,11 +51,16 @@ public class RulerNode extends G2DNode {
         this.gridSize = gridSize;
     }
 
+    @SyncField("rulerSize")
+    public void setRulerSize(double rulerSize) {
+        this.rulerSize = rulerSize;
+    }
+
     @Override
     public void render(Graphics2D g) {
         if (!enabled)
             return;
-        
+
         AffineTransform tr = g.getTransform();
         double scaleX = Math.abs(tr.getScaleX());
         double scaleY = Math.abs(tr.getScaleY());
@@ -69,7 +72,7 @@ public class RulerNode extends G2DNode {
         double offsetY = tr.getTranslateY();
         g.setTransform(new AffineTransform());
 
-        Font rulerFont = new Font("Tahoma", Font.PLAIN, 9);
+        Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
 
         //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g.setStroke(new BasicStroke(1));
@@ -80,10 +83,15 @@ public class RulerNode extends G2DNode {
 
         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
 
-        Rectangle2D vertical = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMinY()+20);
+        int rulerPixelSize = (int) DPIUtil.upscale(rulerSize);
+        int u7 = DPIUtil.upscale(7);
+        int u4 = DPIUtil.upscale(4);
+        int u2 = DPIUtil.upscale(2);
+
+        Rectangle2D vertical = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMinY()+rulerPixelSize);
         g.fill(vertical);
 
-        Rectangle2D horizontal = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY()+20, bounds.getMinX()+20, bounds.getMaxY());
+        Rectangle2D horizontal = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY()+rulerPixelSize, bounds.getMinX()+rulerPixelSize, bounds.getMaxY());
         g.fill(horizontal);
 
         // stepX and stepY should be something between 50 and 100
@@ -103,16 +111,16 @@ public class RulerNode extends G2DNode {
 
         g.setColor(GRAY);
         g.setFont(rulerFont);
+        FontMetrics fm = g.getFontMetrics();
 
         double previousText = -100;
 
         // Vertical ruler
         for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) {
-            if(x > 20) {
+            if(x > rulerPixelSize) {
                 double val = (x-offsetX)/scaleX / getTransform().getScaleX();
                 double modifiedValue = modifyHorizontalValue(val);
                 String str = formatValue(modifiedValue, getMaxDigits());
-                FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if((x-r.getWidth()/2) > previousText) {
                     g.setColor(Color.BLACK);
@@ -121,31 +129,29 @@ public class RulerNode extends G2DNode {
                 }
 
                 g.setColor(GRAY);
-                g.drawLine((int)x, (int)bounds.getMinY()+12, (int)x, (int)bounds.getMinY()+19);
+                g.drawLine((int)x, (int)bounds.getMinY()+rulerPixelSize-1-u7, (int)x, (int)bounds.getMinY()+rulerPixelSize-1);
             }
             if(stepX/5 > 2) {
                 for(double x2 = x+stepX/5; x2 < x+stepX; x2+=stepX/5) {
-                    if(x2 > 20) {
-                        g.drawLine((int)x2, (int)bounds.getMinY()+15, (int)x2, (int)bounds.getMinY()+19);
+                    if(x2 > rulerPixelSize) {
+                        g.drawLine((int)x2, (int)bounds.getMinY()+rulerPixelSize-1-u4, (int)x2, (int)bounds.getMinY()+rulerPixelSize-1);
                     }
                 }
                 for(double x2 = x+stepX/10; x2 < x+stepX; x2+=stepX/5) {
-                    if(x2 > 20) {
-                        g.drawLine((int)x2, (int)bounds.getMinY()+17, (int)x2, (int)bounds.getMinY()+19);
+                    if(x2 > rulerPixelSize) {
+                        g.drawLine((int)x2, (int)bounds.getMinY()+rulerPixelSize-1-u2, (int)x2, (int)bounds.getMinY()+rulerPixelSize-1);
                     }
                 }
-
             }
         }
 
         // Horizontal ruler
         previousText = -100;
         for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) {
-            if(y > 20) {
+            if(y > rulerPixelSize) {
                 double val = (y-offsetY)/scaleY / getTransform().getScaleY();
                 double modifiedValue = modifyVerticalValue(val);
                 String str = formatValue(modifiedValue, getMaxDigits());
-                FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if(y-1+r.getHeight()/2 > previousText) {
                     g.setColor(Color.BLACK);
@@ -157,17 +163,17 @@ public class RulerNode extends G2DNode {
                     previousText = y-1+r.getHeight();
                 }
                 g.setColor(GRAY);
-                g.drawLine((int)bounds.getMinX()+12, (int)y, (int)bounds.getMinX()+19, (int)y);
+                g.drawLine((int)bounds.getMinX()+rulerPixelSize-1-u7, (int)y, (int)bounds.getMinX()+rulerPixelSize-1, (int)y);
             }
             if(stepY/5 > 2) {
                 for(double y2 = y+stepY/5; y2 < y+stepY; y2+=stepY/5) {
-                    if(y2 > 20) {
-                        g.drawLine((int)bounds.getMinX()+15, (int)y2, (int)bounds.getMinX()+19, (int)y2);
+                    if(y2 > rulerPixelSize) {
+                        g.drawLine((int)bounds.getMinX()+rulerPixelSize-1-u4, (int)y2, (int)bounds.getMinX()+rulerPixelSize-1, (int)y2);
                     }
                 }
                 for(double y2 = y+stepY/10; y2 < y+stepY; y2+=stepY/5) {
-                    if(y2 > 20) {
-                        g.drawLine((int)bounds.getMinX()+17, (int)y2, (int)bounds.getMinX()+19, (int)y2);
+                    if(y2 > rulerPixelSize) {
+                        g.drawLine((int)bounds.getMinX()+rulerPixelSize-1-u2, (int)y2, (int)bounds.getMinX()+rulerPixelSize-1, (int)y2);
                     }
                 }
             }