]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/snap/GridSnapAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / snap / GridSnapAdvisor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.g2d.snap;\r
13 \r
14 import java.awt.geom.Point2D;\r
15 import java.io.Serializable;\r
16 \r
17 import org.simantics.scl.runtime.function.Function1;\r
18 \r
19 /**\r
20  * @author Tuukka Lehtonen\r
21  */\r
22 public class GridSnapAdvisor implements ISnapAdvisor, Serializable {\r
23 \r
24     private static final long serialVersionUID = 2411311098028229134L;\r
25 \r
26     double resolution;\r
27     Function1<Object, Boolean> enabled;\r
28 \r
29     public GridSnapAdvisor(double resolution) {\r
30         this.resolution = resolution;\r
31     }\r
32 \r
33     public void setEnabledFunction(Function1<Object, Boolean> function) {\r
34         this.enabled = function;\r
35     }\r
36 \r
37     public void setResolution(double resolution) {\r
38         this.resolution = resolution;\r
39     }\r
40     \r
41     public double getResolution() {\r
42         return resolution;\r
43     }\r
44 \r
45     @Override\r
46     public void snap(Point2D point) {\r
47         if (enabled != null && !enabled.apply(null))\r
48             return;\r
49         point.setLocation(\r
50             Math.round(point.getX() / resolution) * resolution, \r
51             Math.round(point.getY() / resolution) * resolution);\r
52     }\r
53 \r
54     @Override\r
55     public void snap(Point2D point, Point2D[] features) {\r
56         if (enabled != null && !enabled.apply(null))\r
57             return;\r
58         double dx = features[0].getX();\r
59         double dy = features[0].getY();\r
60         point.setLocation(\r
61             Math.round((point.getX()-dx) / resolution) * resolution + dx, \r
62             Math.round((point.getY()-dy) / resolution) * resolution + dy);\r
63     }\r
64 \r
65     @Override\r
66     public String toString() {\r
67         return getClass().getSimpleName() + "[resolution=" + resolution + "]";\r
68     }\r
69 \r
70     @Override\r
71     public int hashCode() {\r
72         long temp = Double.doubleToLongBits(resolution);\r
73         return (int) (temp ^ (temp >>> 32));\r
74     }\r
75 \r
76     @Override\r
77     public boolean equals(Object obj) {\r
78         if (this == obj)\r
79             return true;\r
80         if (obj == null)\r
81             return false;\r
82         if (getClass() != obj.getClass())\r
83             return false;\r
84         GridSnapAdvisor other = (GridSnapAdvisor) obj;\r
85         return Double.doubleToLongBits(resolution) != Double.doubleToLongBits(other.resolution);\r
86     }\r
87 \r
88 }\r