X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fnodes%2FRulerNode.java;h=27c1c1fd2bae4c1d2469ea24af1d0259647b152c;hp=ef6f2fc7cdefd62c39a0395dc5044c0563467adf;hb=04f200d2010339b05ba016b6f0c247653f5bdc97;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java index ef6f2fc7c..27c1c1fd2 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java @@ -1,63 +1,63 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.scenegraph.g2d.nodes; -import java.awt.AlphaComposite; -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Font; -import java.awt.FontMetrics; -import java.awt.Graphics2D; -import java.awt.geom.AffineTransform; -import java.awt.geom.Rectangle2D; -import java.util.Locale; - -import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.utils.GridUtils; +import java.awt.AlphaComposite; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; +import java.util.Locale; + +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.utils.GridUtils; 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; - - @SyncField("enabled") - public void setEnabled(Boolean enabled) { - this.enabled = enabled; - } - - @SyncField("gridSize") - public void setGridSize(double gridSize) { - if (gridSize < 1e-6) - gridSize = 1e-6; - this.gridSize = gridSize; - } - + 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; + + @SyncField("enabled") + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + + @SyncField("gridSize") + public void setGridSize(double gridSize) { + if (gridSize < 1e-6) + gridSize = 1e-6; + this.gridSize = gridSize; + } + @Override public void render(Graphics2D g) { - if (!enabled) - return; - + if (!enabled) + return; + AffineTransform tr = g.getTransform(); double scaleX = Math.abs(tr.getScaleX()); double scaleY = Math.abs(tr.getScaleY()); @@ -89,15 +89,15 @@ public class RulerNode extends G2DNode { // stepX and stepY should be something between 50 and 100 double stepX = 50; double stepY = 50; - - stepX = GridUtils.limitedEvenGridSpacing(stepX, scaleX, 100, gridSize, true); - stepY = GridUtils.limitedEvenGridSpacing(stepY, scaleY, 100, gridSize, true); + + stepX = GridUtils.limitedEvenGridSpacing(stepX, scaleX, 100, gridSize, true); + stepY = GridUtils.limitedEvenGridSpacing(stepY, scaleY, 100, gridSize, true); //while(stepX * scaleX > 100) stepX /= 2; //while(stepY * scaleY > 100) stepY /= 2; while(stepX * scaleX < 50) stepX *= 2; while(stepY * scaleY < 50) stepY *= 2; - + stepX *= scaleX; stepY *= scaleY; @@ -107,9 +107,11 @@ public class RulerNode extends G2DNode { double previousText = -100; // Vertical ruler - for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) { + for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) { if(x > 20) { - String str = formatValue((x-offsetX)/scaleX); + double val = (x-offsetX)/scaleX / getTransform().getScaleX(); + double modifiedValue = modifyHorizontalValue(val); + String str = formatValue(modifiedValue); FontMetrics fm = g.getFontMetrics(); Rectangle2D r = fm.getStringBounds(str, g); if((x-r.getWidth()/2) > previousText) { @@ -135,15 +137,14 @@ public class RulerNode extends G2DNode { } } - + // Horizontal ruler previousText = -100; - for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) { - if(y > 20) { - double val = (y-offsetY)/scaleY; - if (MAP_Y_SCALING) - val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val)))); - String str = formatValue(val); + for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) { + if(y > 20) { + double val = (y-offsetY)/scaleY / getTransform().getScaleY(); + double modifiedValue = modifyVerticalValue(val); + String str = formatValue(modifiedValue); FontMetrics fm = g.getFontMetrics(); Rectangle2D r = fm.getStringBounds(str, g); if(y-1+r.getHeight()/2 > previousText) { @@ -173,7 +174,27 @@ public class RulerNode extends G2DNode { } g.setTransform(tr); - } + } + + /** + * A method for subclasses to alter the actual X-value of the ruler + * + * @param value + * @return possibly modified X-value + */ + protected double modifyHorizontalValue(double value) { + return value; + } + + /** + * A method for subclasses to alter the actual Y-value of the ruler + * + * @param value + * @return possibly modified Y-value + */ + protected double modifyVerticalValue(double value) { + return value; + } private static final transient int MAX_DIGITS = 5; private static final transient double EPSILON = 0.01;