X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fsnap%2FGridSnapAdvisor.java;fp=bundles%2Forg.simantics.scenegraph%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fg2d%2Fsnap%2FGridSnapAdvisor.java;h=3b265f3097bd6ff0ecc892bc22d2f4733fbfcf9a;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/snap/GridSnapAdvisor.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/snap/GridSnapAdvisor.java new file mode 100644 index 000000000..3b265f309 --- /dev/null +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/snap/GridSnapAdvisor.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.snap; + +import java.awt.geom.Point2D; +import java.io.Serializable; + +import org.simantics.scl.runtime.function.Function1; + +/** + * @author Tuukka Lehtonen + */ +public class GridSnapAdvisor implements ISnapAdvisor, Serializable { + + private static final long serialVersionUID = 2411311098028229134L; + + double resolution; + Function1 enabled; + + public GridSnapAdvisor(double resolution) { + this.resolution = resolution; + } + + public void setEnabledFunction(Function1 function) { + this.enabled = function; + } + + public void setResolution(double resolution) { + this.resolution = resolution; + } + + public double getResolution() { + return resolution; + } + + @Override + public void snap(Point2D point) { + if (enabled != null && !enabled.apply(null)) + return; + point.setLocation( + Math.round(point.getX() / resolution) * resolution, + Math.round(point.getY() / resolution) * resolution); + } + + @Override + public void snap(Point2D point, Point2D[] features) { + if (enabled != null && !enabled.apply(null)) + return; + double dx = features[0].getX(); + double dy = features[0].getY(); + point.setLocation( + Math.round((point.getX()-dx) / resolution) * resolution + dx, + Math.round((point.getY()-dy) / resolution) * resolution + dy); + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[resolution=" + resolution + "]"; + } + + @Override + public int hashCode() { + long temp = Double.doubleToLongBits(resolution); + return (int) (temp ^ (temp >>> 32)); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GridSnapAdvisor other = (GridSnapAdvisor) obj; + return Double.doubleToLongBits(resolution) != Double.doubleToLongBits(other.resolution); + } + +}