Ignore:
Timestamp:
2006-01-21T19:02:06+01:00 (18 years ago)
Author:
imi
Message:

fixed fancy download world chooser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/MapView.java

    r41 r42  
    1111import java.util.Collection;
    1212import java.util.Collections;
    13 import java.util.HashSet;
    1413import java.util.LinkedList;
    1514
     
    2120import org.openstreetmap.josm.data.GeoPoint;
    2221import org.openstreetmap.josm.data.osm.DataSet;
    23 import org.openstreetmap.josm.data.osm.LineSegment;
    2422import org.openstreetmap.josm.data.osm.Node;
    25 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    26 import org.openstreetmap.josm.data.osm.Track;
    2723import org.openstreetmap.josm.data.projection.Projection;
    2824import org.openstreetmap.josm.gui.layer.Layer;
     
    10399                // initialize the projection if it is the first layer
    104100                if (layers.isEmpty())
    105                         Main.pref.getProjection().init(layer.getBoundsLatLon());
     101                        getProjection().init(layer.getBoundsLatLon());
    106102
    107103                // reinitialize layer's data
    108                 layer.init(Main.pref.getProjection());
     104                layer.init(getProjection());
    109105
    110106                if (layer instanceof OsmDataLayer) {
     
    161157        }
    162158
    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, the
    167          * 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 is
    172          * within 10 pixel to p, this segment is returned, except when
    173          * <code>wholeTrack</code> is <code>true</code>, in which case the
    174          * corresponding Track is returned.
    175          *
    176          * If no line segment is found and the point is within an area, return that
    177          * 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 whole
    183          *                                               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                 // nodes
    191                 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 first
    205                 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 squared
    219                                         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 segments
    231                 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 squared
    240                         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 to
    251          * the mouse. To do this, first the nearest object is
    252          * determined.
    253          *
    254          * If its a node, return all line segments and
    255          * streets the node is part of, as well as all nodes
    256          * (with their line segments and tracks) with the same
    257          * location.
    258          *
    259          * If its a line segment, return all tracks this segment
    260          * belongs to as well as all line segments that are between
    261          * 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 returned
    265          *              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 contains
    280                                 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        
    304159        /**
    305160         * Draw the component.
     
    378233                                // no bounds means standard scale and center
    379234                                center = new GeoPoint(51.526447, -0.14746371);
    380                                 Main.pref.getProjection().latlon2xy(center);
     235                                getProjection().latlon2xy(center);
    381236                                scale = 10;
    382237                        } else {
    383238                                center = bounds.centerXY();
    384                                 Main.pref.getProjection().xy2latlon(center);
     239                                getProjection().xy2latlon(center);
    385240                                double scaleX = (bounds.max.x-bounds.min.x)/w;
    386241                                double scaleY = (bounds.max.y-bounds.min.y)/h;
     
    483338        public void stateChanged(ChangeEvent e) {
    484339                // reset all datasets.
    485                 Projection p = Main.pref.getProjection();
     340                Projection p = getProjection();
    486341                for (Node n : Main.main.ds.nodes)
    487342                        p.latlon2xy(n.coor);
Note: See TracChangeset for help on using the changeset viewer.