Changeset 42 in josm for src/org/openstreetmap/josm/gui/MapView.java
- Timestamp:
- 2006-01-21T19:02:06+01:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/MapView.java
r41 r42 11 11 import java.util.Collection; 12 12 import java.util.Collections; 13 import java.util.HashSet;14 13 import java.util.LinkedList; 15 14 … … 21 20 import org.openstreetmap.josm.data.GeoPoint; 22 21 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.data.osm.LineSegment;24 22 import org.openstreetmap.josm.data.osm.Node; 25 import org.openstreetmap.josm.data.osm.OsmPrimitive;26 import org.openstreetmap.josm.data.osm.Track;27 23 import org.openstreetmap.josm.data.projection.Projection; 28 24 import org.openstreetmap.josm.gui.layer.Layer; … … 103 99 // initialize the projection if it is the first layer 104 100 if (layers.isEmpty()) 105 Main.pref.getProjection().init(layer.getBoundsLatLon());101 getProjection().init(layer.getBoundsLatLon()); 106 102 107 103 // reinitialize layer's data 108 layer.init( Main.pref.getProjection());104 layer.init(getProjection()); 109 105 110 106 if (layer instanceof OsmDataLayer) { … … 161 157 } 162 158 163 /**164 * Return the object, that is nearest to the given screen point.165 *166 * First, a node will be searched. If a node within 10 pixel is found, the167 * nearest node is returned.168 *169 * If no node is found, search for pending line segments.170 *171 * If no such line segment is found, and a non-pending line segment is172 * within 10 pixel to p, this segment is returned, except when173 * <code>wholeTrack</code> is <code>true</code>, in which case the174 * corresponding Track is returned.175 *176 * If no line segment is found and the point is within an area, return that177 * area.178 *179 * If no area is found, return <code>null</code>.180 *181 * @param p The point on screen.182 * @param lsInsteadTrack Whether the line segment (true) or only the whole183 * track should be returned.184 * @return The primitive, that is nearest to the point p.185 */186 public OsmPrimitive getNearest(Point p, boolean lsInsteadTrack) {187 double minDistanceSq = Double.MAX_VALUE;188 OsmPrimitive minPrimitive = null;189 190 // nodes191 for (Node n : Main.main.ds.nodes) {192 if (n.isDeleted())193 continue;194 Point sp = getScreenPoint(n.coor);195 double dist = p.distanceSq(sp);196 if (minDistanceSq > dist && dist < 100) {197 minDistanceSq = p.distanceSq(sp);198 minPrimitive = n;199 }200 }201 if (minPrimitive != null)202 return minPrimitive;203 204 // for whole tracks, try the tracks first205 minDistanceSq = Double.MAX_VALUE;206 if (!lsInsteadTrack) {207 for (Track t : Main.main.ds.tracks) {208 if (t.isDeleted())209 continue;210 for (LineSegment ls : t.segments) {211 if (ls.isDeleted())212 continue;213 Point A = getScreenPoint(ls.start.coor);214 Point B = getScreenPoint(ls.end.coor);215 double c = A.distanceSq(B);216 double a = p.distanceSq(B);217 double b = p.distanceSq(A);218 double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared219 if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {220 minDistanceSq = perDist;221 minPrimitive = t;222 }223 }224 }225 if (minPrimitive != null)226 return minPrimitive;227 }228 229 minDistanceSq = Double.MAX_VALUE;230 // line segments231 for (LineSegment ls : Main.main.ds.lineSegments) {232 if (ls.isDeleted())233 continue;234 Point A = getScreenPoint(ls.start.coor);235 Point B = getScreenPoint(ls.end.coor);236 double c = A.distanceSq(B);237 double a = p.distanceSq(B);238 double b = p.distanceSq(A);239 double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared240 if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {241 minDistanceSq = perDist;242 minPrimitive = ls;243 }244 }245 246 return minPrimitive;247 }248 249 /**250 * @return A list of all objects that are nearest to251 * the mouse. To do this, first the nearest object is252 * determined.253 *254 * If its a node, return all line segments and255 * streets the node is part of, as well as all nodes256 * (with their line segments and tracks) with the same257 * location.258 *259 * If its a line segment, return all tracks this segment260 * belongs to as well as all line segments that are between261 * the same nodes (in both direction) with all their tracks.262 *263 * @return A collection of all items or <code>null</code>264 * if no item under or near the point. The returned265 * list is never empty.266 */267 public Collection<OsmPrimitive> getAllNearest(Point p) {268 OsmPrimitive osm = getNearest(p, true);269 if (osm == null)270 return null;271 Collection<OsmPrimitive> c = new HashSet<OsmPrimitive>();272 c.add(osm);273 if (osm instanceof Node) {274 Node node = (Node)osm;275 for (Node n : Main.main.ds.nodes)276 if (!n.isDeleted() && n.coor.equalsLatLon(node.coor))277 c.add(n);278 for (LineSegment ls : Main.main.ds.lineSegments)279 // line segments never match nodes, so they are skipped by contains280 if (!ls.isDeleted() && (c.contains(ls.start) || c.contains(ls.end)))281 c.add(ls);282 }283 if (osm instanceof LineSegment) {284 LineSegment line = (LineSegment)osm;285 for (LineSegment ls : Main.main.ds.lineSegments)286 if (!ls.isDeleted() && ls.equalPlace(line))287 c.add(ls);288 }289 if (osm instanceof Node || osm instanceof LineSegment) {290 for (Track t : Main.main.ds.tracks) {291 if (t.isDeleted())292 continue;293 for (LineSegment ls : t.segments) {294 if (!ls.isDeleted() && c.contains(ls)) {295 c.add(t);296 break;297 }298 }299 }300 }301 return c;302 }303 304 159 /** 305 160 * Draw the component. … … 378 233 // no bounds means standard scale and center 379 234 center = new GeoPoint(51.526447, -0.14746371); 380 Main.pref.getProjection().latlon2xy(center);235 getProjection().latlon2xy(center); 381 236 scale = 10; 382 237 } else { 383 238 center = bounds.centerXY(); 384 Main.pref.getProjection().xy2latlon(center);239 getProjection().xy2latlon(center); 385 240 double scaleX = (bounds.max.x-bounds.min.x)/w; 386 241 double scaleY = (bounds.max.y-bounds.min.y)/h; … … 483 338 public void stateChanged(ChangeEvent e) { 484 339 // reset all datasets. 485 Projection p = Main.pref.getProjection();340 Projection p = getProjection(); 486 341 for (Node n : Main.main.ds.nodes) 487 342 p.latlon2xy(n.coor);
Note:
See TracChangeset
for help on using the changeset viewer.