--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.utils;\r
+\r
+import java.io.Serializable;\r
+\r
+import org.simantics.scenegraph.utils.GridUtils;\r
+\r
+\r
+/**\r
+ * Grid configuration.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * @author Marko Luukkainen\r
+ * @author Toni Kalajainen\r
+ */\r
+public final class GridSpacing implements Serializable {\r
+\r
+ private static final long serialVersionUID = 8445639004963828463L;\r
+\r
+ /** When the view scale gets beneath this value a grid should no longer be shown */\r
+ public static final double GRID_MIN_USER_SIZE = 1e-6;\r
+\r
+ public static final GridSpacing SOME_SPACING = GridSpacing.makeGridSpacing(100, 100, 40);\r
+ \r
+ /** The minimum amount of pixel between major grid lines. */\r
+ public double minPixels;\r
+\r
+ /** Grid segment sizes (canvas coordinates) */\r
+ public double segment, segmentExp;\r
+\r
+ /** Conversions between canvas (unit) and control (pixel) */\r
+ public double pixelsPerUnit;\r
+ public double unitsPerPixel;\r
+\r
+ public double pixelsPerSegment;\r
+ \r
+ public GridSpacing(double spacing, double spacingExp, double unitsPerPixel, double minPixels)\r
+ {\r
+ this.unitsPerPixel = unitsPerPixel;\r
+ pixelsPerUnit = 1 / unitsPerPixel;\r
+ this.segment = spacing; \r
+ this.segmentExp = spacingExp;\r
+ pixelsPerSegment = spacing * pixelsPerUnit;\r
+ this.minPixels = minPixels;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj == null) return false;\r
+ if (obj == this) return true;\r
+ if (obj instanceof GridSpacing == false) return false;\r
+ GridSpacing g2 = (GridSpacing)obj;\r
+ if(pixelsPerUnit != g2.pixelsPerUnit) return false;\r
+ if(pixelsPerSegment != g2.pixelsPerSegment) return false;\r
+ if(unitsPerPixel != g2.unitsPerPixel) return false;\r
+ if(segment != g2.segment) return false;\r
+ if(segmentExp != g2.segmentExp) return false;\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Calculates grid cofiguration\r
+ * \r
+ * @return grid configuration \r
+ */\r
+ public static GridSpacing makeGridSpacing(\r
+ double viewboxSize, \r
+ double controlWidth, \r
+ double minPixels)\r
+ {\r
+ if (controlWidth==0) controlWidth = 1;\r
+ if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);\r
+\r
+ double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);\r
+ double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);\r
+ return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);\r
+ }\r
+\r
+ public static GridSpacing makeGridSpacingForTime(\r
+ double viewboxSize, \r
+ double controlWidth, \r
+ double minPixels)\r
+ {\r
+ if (controlWidth==0) controlWidth = 1;\r
+ if (viewboxSize < GRID_MIN_USER_SIZE) viewboxSize = 1;\r
+\r
+ double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);\r
+ double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);\r
+ return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);\r
+ }\r
+ \r
+ private static double[] getGridSpacing(double unitsPerPixel, double minPixels) {\r
+ double minGridSize = minPixels * unitsPerPixel;\r
+ double[] gridSpacing = GridUtils.getEvenGridSpacingWithExp(minGridSize, new double[2]);\r
+ return gridSpacing;\r
+ }\r
+\r
+ /**\r
+ * Snap point to the grid. The snapping is based on grid last paint operation.\r
+ * \r
+ * @param pos\r
+ * @return\r
+ */\r
+ public double snapToGrid(double pos) {\r
+ return GridUtils.snapToGrid(pos, segment);\r
+ }\r
+\r
+}\r