Ticket #381: selfoverlapping.patch

File selfoverlapping.patch, 2.3 KB (added by xeen, 17 years ago)

Prevents creation of self overlapping ways and stops drawing when ways are closed

  • src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    old new  
    3333import javax.swing.JOptionPane;
    3434
    3535import org.openstreetmap.josm.Main;
     36import org.openstreetmap.josm.actions.mapmode.SelectAction;
    3637import org.openstreetmap.josm.command.AddCommand;
    3738import org.openstreetmap.josm.command.ChangeCommand;
    3839import org.openstreetmap.josm.command.Command;
     
    166167            replacedWays = new ArrayList<Way>();
    167168        boolean newNode = false;
    168169        Node n = null;
     170        boolean wayIsFinished = false;
    169171
    170172        if (!ctrl) {
    171173            n = Main.map.mapView.getNearestNode(mousePos);
     
    279281            // Ok we know now that we'll insert a line segment, but will it connect to an
    280282            // existing way or make a new way of its own? The "alt" modifier means that the
    281283            // user wants a new way.
    282 
    283284            Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
     285
     286            // Don't allow creation of self-overlapping ways
     287            if(way != null) {
     288                int nodeCount=0;
     289                for (Node p : way.nodes)
     290                    if(p.equals(n0)) nodeCount++;
     291                if(nodeCount > 1) way = null;
     292            }
     293
    284294            if (way == null) {
    285295                way = new Way();
    286296                way.nodes.add(n0);
     
    296306                }
    297307            }
    298308
     309            // Connected to a node that's already in the way
     310            if(way != null && way.nodes.contains(n)) {
     311                System.out.println("Stop drawing, node is part of current way");
     312                wayIsFinished = true;
     313                selection.clear();
     314                //Main.map.selectMapMode(new SelectAction(Main.map));
     315            }
     316           
     317            // Add new node to way
    299318            if (way.nodes.get(way.nodes.size() - 1) == n0) {
    300319                way.nodes.add(n);
    301320            } else {
     
    328347        Command c = new SequenceCommand(title, cmds);
    329348
    330349        Main.main.undoRedo.add(c);
    331         lastUsedNode = n;
     350        if(!wayIsFinished) lastUsedNode = n;
    332351        computeHelperLine();
    333352        Main.map.mapView.repaint();
    334353    }