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