Changeset 4142 in josm


Ignore:
Timestamp:
2011-06-18T22:35:49+02:00 (13 years ago)
Author:
stoecker
Message:

fix #6323 - patch by olejorgenb and Hojoe (modified and bug fixed by me) - add new node and new segment creation to extrude mode

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java

    r4126 r4142  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.awt.AWTEvent;
    78import java.awt.BasicStroke;
    89import java.awt.Color;
     
    1112import java.awt.Point;
    1213import java.awt.Rectangle;
     14import java.awt.Toolkit;
     15import java.awt.event.AWTEventListener;
    1316import java.awt.event.ActionEvent;
     17import java.awt.event.InputEvent;
    1418import java.awt.event.KeyEvent;
    1519import java.awt.event.MouseEvent;
     
    5155public class ExtrudeAction extends MapMode implements MapViewPaintable {
    5256
    53     enum Mode { extrude, translate, select }
     57    enum Mode { extrude, translate, select, create_new }
    5458
    5559    private Mode mode = Mode.select;
     
    97101     */
    98102    private MoveCommand moveCommand;
     103
     104    /** The cursor for the 'create_new' mode. */
     105    private final Cursor cursorCreateNew;
     106
     107    /**
     108     * This listener is used to indicate the 'create_new' mode, if the Alt modifier is pressed.
     109     */
     110    private final AWTEventListener altKeyListener = new AWTEventListener() {
     111        @Override
     112        public void eventDispatched(AWTEvent e) {
     113            if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isActiveLayerDrawable())
     114                return;
     115            InputEvent ie = (InputEvent) e;
     116            boolean alt = (ie.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
     117            if(mode == Mode.select) {
     118                Main.map.mapView.setNewCursor(alt ? cursorCreateNew : cursor, this);
     119            }
     120        }
     121    };
    99122
    100123    /**
     
    110133        initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200);
    111134        selectedColor = PaintColors.SELECTED.get();
     135        cursorCreateNew = ImageProvider.getCursor("normal", "rectangle_plus");
    112136    }
    113137
     
    117141        else if (mode == Mode.extrude)
    118142            return tr("Draw a rectangle of the desired size, then release the mouse button.");
     143        else if (mode == Mode.create_new)
     144            return tr("Draw a rectangle of the desired size, then release the mouse button.");
    119145        else
    120             return tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal.");
     146            return tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal, " +
     147            "Alt-drag to create a new rectangle, double click to add a new node.");
    121148    }
    122149
     
    129156        Main.map.mapView.addMouseListener(this);
    130157        Main.map.mapView.addMouseMotionListener(this);
     158        try {
     159            Toolkit.getDefaultToolkit().addAWTEventListener(altKeyListener, AWTEvent.KEY_EVENT_MASK);
     160        } catch (SecurityException ex) {
     161        }
    131162    }
    132163
     
    135166        Main.map.mapView.removeMouseMotionListener(this);
    136167        Main.map.mapView.removeTemporaryLayer(this);
     168        try {
     169            Toolkit.getDefaultToolkit().removeAWTEventListener(altKeyListener);
     170        } catch (SecurityException ex) {
     171        }
    137172        super.exitMode();
    138173    }
     
    140175    /**
    141176     * If the left mouse button is pressed over a segment, switch
    142      * to either extrude or translate mode depending on whether Ctrl is held.
     177     * to either extrude, translate or create_new mode depending on whether Ctrl or Alt is held.
    143178     */
    144179    @Override public void mousePressed(MouseEvent e) {
     
    159194            if ((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) {
    160195                mode = Mode.translate;
     196            } else if ((e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0) {
     197                mode = Mode.create_new;
     198                // create a new segment and then select and extrude the new segment
     199                getCurrentDataSet().setSelected(selectedSegment.way);
     200                alwaysCreateNodes = true;
    161201            } else {
    162202                mode = Mode.extrude;
     
    225265            // Just sit tight and wait for mouse to be released.
    226266        } else {
    227             //move and extrude mode - move the selected segment
     267            //move, create new and extrude mode - move the selected segment
    228268
    229269            EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y);
     
    261301            Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this);
    262302
    263             if (mode == Mode.extrude) {
     303            if (mode == Mode.extrude || mode == Mode.create_new) {
    264304                //nothing here
    265305            } else if (mode == Mode.translate) {
     
    293333            // Nothing to be done
    294334        } else {
    295             if (mode == Mode.extrude) {
     335            if (mode == Mode.create_new) {
    296336                if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) {
     337                    // crete a new rectangle
     338                    Collection<Command> cmds = new LinkedList<Command>();
     339                    Node third = new Node(newN2en);
     340                    Node fourth = new Node(newN1en);
     341                    Way wnew = new Way();
     342                    wnew.addNode(selectedSegment.getFirstNode());
     343                    wnew.addNode(selectedSegment.getSecondNode());
     344                    wnew.addNode(third);
     345                    wnew.addNode(fourth);
     346                    // ... and close the way
     347                    wnew.addNode(selectedSegment.getFirstNode());
     348                    // undo support
     349                    cmds.add(new AddCommand(third));
     350                    cmds.add(new AddCommand(fourth));
     351                    cmds.add(new AddCommand(wnew));
     352                    Command c = new SequenceCommand(tr("Extrude Way"), cmds);
     353                    Main.main.undoRedo.add(c);
     354                    getCurrentDataSet().setSelected(wnew);
     355                }
     356            } else if (mode == Mode.extrude) {
     357                if( e.getClickCount() == 2 && e.getPoint().equals(initialMousePos) ) {
     358                    // double click add a new node
     359                    // Should maybe do the same as in DrawAction and fetch all nearby segments?
     360                    WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive.isSelectablePredicate);
     361                    if (ws != null) {
     362                        Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
     363                        EastNorth A = ws.getFirstNode().getEastNorth();
     364                        EastNorth B = ws.getSecondNode().getEastNorth();
     365                        n.setEastNorth(Geometry.closestPointToSegment(A, B, n.getEastNorth()));
     366                        Way wnew = new Way(ws.way);
     367                        wnew.addNode(ws.lowerIndex+1, n);
     368                        SequenceCommand cmds = new SequenceCommand(tr("Add a new node to an existing way"),
     369                                new AddCommand(n), new ChangeCommand(ws.way, wnew));
     370                        Main.main.undoRedo.add(cmds);
     371                    }
     372                }
     373                else if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) {
    297374                    // create extrusion
    298375
     
    349426            }
    350427
     428            boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
    351429            // Switch back into select mode
    352             Main.map.mapView.setNewCursor(cursor, this);
     430            Main.map.mapView.setNewCursor(alt ? cursorCreateNew : cursor, this);
    353431            Main.map.mapView.removeTemporaryLayer(this);
    354432            selectedSegment = null;
     
    439517                Point p4 = mv.getPoint(newN2en);
    440518
    441                 if (mode == Mode.extrude) {
     519                if (mode == Mode.extrude || mode == Mode.create_new) {
    442520                    // Draw rectangle around new area.
    443521                    GeneralPath b = new GeneralPath();
Note: See TracChangeset for help on using the changeset viewer.