Changeset 19855 in osm for applications/editors/josm


Ignore:
Timestamp:
2010-02-04T12:48:57+01:00 (15 years ago)
Author:
petrdlouhy
Message:

merging can be switched off with ctrl key
other minor fixes

Location:
applications/editors/josm/plugins/tracer/src/tracer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/tracer/src/tracer/TracerAction.java

    r19830 r19855  
    1111import java.awt.Point;
    1212import java.awt.event.ActionEvent;
     13import java.awt.event.InputEvent;
    1314import java.awt.event.KeyEvent;
    1415import java.awt.event.MouseEvent;
     
    2021import java.io.InputStreamReader;
    2122import java.net.URL;
    22 import java.util.HashMap;
    2323import java.util.List;
    24 import java.util.Map;
    2524import org.openstreetmap.josm.Main;
    2625import org.openstreetmap.josm.actions.mapmode.MapMode;
     
    2827import org.openstreetmap.josm.command.ChangeCommand;
    2928import org.openstreetmap.josm.command.Command;
     29import org.openstreetmap.josm.command.DeleteCommand;
    3030import org.openstreetmap.josm.command.SequenceCommand;
    3131import org.openstreetmap.josm.data.coor.LatLon;
     
    5050    protected Thread executeThread;
    5151    protected boolean cancel;
    52     protected Collection<Command> commands = new LinkedList<Command>();
    53     protected Collection<Way> ways = new ArrayList<Way>();
    54 
     52    private boolean ctrl;
     53    private boolean alt;
     54    private boolean shift;
     55   
    5556    public TracerAction(MapFrame mapFrame) {
    5657        super(tr("Tracer"), "tracer-sml", tr("Tracer."), Shortcut.registerShortcut("tools:tracer", tr("Tool: {0}", tr("Tracer")), KeyEvent.VK_T, Shortcut.GROUP_EDIT), mapFrame, getCursor());
     
    107108                }
    108109            };
    109             Thread executeThread = new Thread(tracerTask);
    110             executeThread.start();
     110            Thread executeTraceThread = new Thread(tracerTask);
     111            executeTraceThread.start();
    111112        } catch (Exception e) {
    112113            e.printStackTrace();
     
    152153    }
    153154
    154     private void processnodelist(LatLon pos, LatLon topLeft, LatLon botRight, ProgressMonitor progressMonitor) {
    155         progressMonitor.beginTask(null, 3);
    156         try {
    157             ArrayList<double[]> nodelist = new ArrayList<double[]>();
    158             nodelist = getNodeList(pos);
    159 
    160             if (nodelist.size() == 0) {
    161                 return;
    162             }
    163 
    164             /**
    165              * Turn the arraylist into osm nodes
    166              */
    167             Way way = new Way();
    168             Node n = null;
    169             Node fn = null;
    170 
    171             double eastOffset = 0;
    172             double northOffset = 0;
    173 
    174 
    175             int nodesinway = 0;
    176 
    177             for (int i = 0; i < nodelist.size(); i++) {
     155    private Command connectObjects(Way way){
     156            List<Command> cmds = new LinkedList<Command>();
     157            Way newWay = new Way(way);
     158            for (int i = 0; i < newWay.getNodesCount(); i++) {
     159                Node n = newWay.getNode(i);
    178160                if (cancel) {
    179                     return;
     161                    return null;
    180162                }
    181163
    182164                try {
    183                     LatLon ll = new LatLon(nodelist.get(i)[0] + northOffset, nodelist.get(i)[1] + eastOffset);
     165                    LatLon ll = n.getCoor();
    184166
    185167                    double minDistanceSq = 0.00001;
     
    192174                    Node nearestNode = null;
    193175                    for (Node nn : nodes) {
    194                         if (!nn.isUsable()) {
     176                        if (!nn.isUsable() || way.containsNode(nn) || newWay.containsNode(nn)) {
    195177                            continue;
    196178                        }
    197                         LatLon ll2 = nn.getCoor();
    198                         double dist = ll2.distance(ll);
     179                        double dist = nn.getCoor().distance(ll);
    199180                        if (dist < minDistanceSq) {
    200181                            minDistanceSq = dist;
     
    217198
    218199                        for (Way ww : ways) {
     200                            if (!ww.isUsable() || ww == way || ww == newWay) {
     201                                continue;
     202                            }
    219203                            for (int nindex = 0; nindex < ww.getNodesCount(); nindex++) {
    220204                                Node n1 = ww.getNode(nindex);
     
    231215
    232216                        if (minDist < 0.00001) {
    233                             n = new Node(ll);
    234                             commands.add(new AddCommand(n));
    235                             Way newWay = new Way(nearestWay);
    236                             newWay.addNode(nearestNodeIndex + 1, n);
    237                             commands.add(new ChangeCommand(nearestWay, newWay));
    238                         } else {
    239                             n = new Node(ll);
    240                             commands.add(new AddCommand(n));
     217                            Way newNWay = new Way(nearestWay);
     218                            newNWay.addNode(nearestNodeIndex + 1, n);
     219                            cmds.add(new ChangeCommand(nearestWay, newNWay));
    241220                        }
    242221                    } else {
    243                         n = nearestNode;
    244                     }
    245 
    246                     if (fn == null) {
    247                         fn = n;
     222                          int j = newWay.getNodes().indexOf(n);
     223                          newWay.addNode(j, nearestNode);
     224                          if(j == 0)newWay.addNode(newWay.getNodesCount(), nearestNode);
     225                          newWay.removeNode(n);
     226                          cmds.add(new DeleteCommand(n));
     227                          i--;
    248228                    }
    249229
     
    251231                    ex.printStackTrace();
    252232                }
    253 
    254                 way.addNode(n);
    255                 nodesinway++;
    256             }
    257 /**/
    258             // projdi kazdou novou usecku a zjisti, zda by nemela vest pres existujici body
     233            }
     234            /**/
     235            // projdi kazdou novou usecku a zjisti, zda by nemela vest pres existujici body         
    259236            int i = 0;
    260             while (i < nodelist.size()) {
     237            while (i < newWay.getNodesCount()) {
    261238                if (cancel) {
    262                     return;
     239                    return null;
    263240                }
    264241
     
    266243
    267244                // usecka n1, n2
    268                 double d[] = nodelist.get(i);
    269                 LatLon n1 = new LatLon(d[0], d[1]);
    270                 d = nodelist.get((i + 1) % nodelist.size());
    271                 LatLon n2 = new LatLon(d[0], d[1]);
     245                LatLon n1 = newWay.getNodes().get(i).getCoor();
     246                LatLon n2 = newWay.getNodes().get((i + 1) % newWay.getNodesCount()).getCoor();
    272247
    273248                double minDistanceSq = 0.00001;
     
    281256                Node nearestNode = null;
    282257                for (Node nod : nodes) {
    283                     if (!nod.isUsable()) {
     258                    if (!nod.isUsable() || way.containsNode(nod) || newWay.containsNode(nod)) {
    284259                        continue;
    285260                    }
     
    302277                }
    303278
     279                System.out.println("Nearest_: " + nearestNode);
     280                System.out.println("");
    304281                if (nearestNode == null) {
    305282                    // tato usecka se nerozdeli
     
    308285                } else {
    309286                    // rozdeleni usecky
    310                     nodelist.add(i+1, new double[] { nearestNode.getCoor().getY(), nearestNode.getCoor().getX() });
    311                     way.addNode(i+1, nearestNode);
     287                    newWay.addNode(i+1, nearestNode);
    312288                    continue; // i nezvetsuji, treba bude treba rozdelit usecku znovu
    313289                }
    314290
    315291            }
    316 /**/
     292
     293            cmds.add(new ChangeCommand(way, newWay));
     294
     295            Command cmd = new SequenceCommand(tr("Merge objects nodes"), cmds);
     296            return cmd;
     297    }
     298
     299    private void processnodelist(LatLon pos, LatLon topLeft, LatLon botRight, ProgressMonitor progressMonitor) {
     300        Collection<Command> commands = new LinkedList<Command>();
     301
     302        progressMonitor.beginTask(null, 3);
     303        try {
     304            ArrayList<double[]> nodelist = new ArrayList<double[]>();
     305            nodelist = getNodeList(pos);
     306
     307            if (nodelist.size() == 0) {
     308                return;
     309            }
     310
     311            /**
     312             * Turn the arraylist into osm nodes
     313             */
     314            Way way = new Way();
     315            Node n = null;
     316            Node fn = null;
     317
     318            double eastOffset = 0;
     319            double northOffset = 0;
     320
     321            for (double[] node : nodelist) {
     322                if (cancel) {
     323                    return;
     324                }
     325                LatLon ll = new LatLon(node[0] + northOffset, node[1] + eastOffset);
     326
     327                n = new Node(ll);
     328                if(fn == null) fn = n;
     329                commands.add(new AddCommand(n));
     330                way.addNode(n);
     331            }
     332
    317333            way.put("building", "yes");
    318334            way.put("source", "cuzk:km");
     
    321337
    322338            commands.add(new AddCommand(way));
     339            if(!ctrl) commands.add(connectObjects(way));
    323340
    324341            if (!commands.isEmpty()) {
    325342                Main.main.undoRedo.add(new SequenceCommand(tr("Tracer building"), commands));
    326                 Main.main.getCurrentDataSet().setSelected(ways);
     343                Main.main.getCurrentDataSet().setSelected(way);
    327344            } else {
    328345                System.out.println("Failed");
    329346            }
    330347
    331             commands = new LinkedList<Command>();
    332             ways = new ArrayList<Way>();
    333348        } finally {
    334349            progressMonitor.finishTask();
     
    368383    }
    369384
     385    @Override
    370386    public void mouseClicked(MouseEvent e) {
    371387    }
    372388
     389    @Override
    373390    public void mouseEntered(MouseEvent e) {
    374391    }
    375392
     393    @Override
    376394    public void mouseExited(MouseEvent e) {
    377395    }
    378396
     397    @Override
    379398    public void mousePressed(MouseEvent e) {
    380399        if(!Main.map.mapView.isActiveLayerDrawable())
    381400           return;
    382401
     402        updateKeyModifiers(e);
    383403        if (e.getButton() == MouseEvent.BUTTON1) {
    384404            trace(e.getPoint());
    385405        }
    386406    }
    387 
     407   
     408    private void updateKeyModifiers(MouseEvent e) {
     409        ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
     410        alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
     411        shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
     412    }
     413
     414
     415    @Override
    388416    public void mouseReleased(MouseEvent e) {
    389417    }
  • applications/editors/josm/plugins/tracer/src/tracer/TracerPlugin.java

    r19830 r19855  
    77package tracer;
    88
    9 import static org.openstreetmap.josm.tools.I18n.tr;
    10 
    119import org.openstreetmap.josm.Main;
    1210import org.openstreetmap.josm.gui.MainMenu;
    13 import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
    1411import org.openstreetmap.josm.plugins.Plugin;
    1512import org.openstreetmap.josm.plugins.PluginInformation;
     
    1714public class TracerPlugin extends Plugin {
    1815
    19     public TracerPlugin() {
    20         MainMenu.add(Main.main.menu.toolsMenu, new TracerAction(Main.main.map));
     16    public TracerPlugin(PluginInformation info) {
     17        super(info);
     18        MainMenu.add(Main.main.menu.toolsMenu, new TracerAction(Main.map));
    2119    }
    2220}
Note: See TracChangeset for help on using the changeset viewer.