Changeset 4142 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java
r4126 r4142 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 import java.awt.AWTEvent; 7 8 import java.awt.BasicStroke; 8 9 import java.awt.Color; … … 11 12 import java.awt.Point; 12 13 import java.awt.Rectangle; 14 import java.awt.Toolkit; 15 import java.awt.event.AWTEventListener; 13 16 import java.awt.event.ActionEvent; 17 import java.awt.event.InputEvent; 14 18 import java.awt.event.KeyEvent; 15 19 import java.awt.event.MouseEvent; … … 51 55 public class ExtrudeAction extends MapMode implements MapViewPaintable { 52 56 53 enum Mode { extrude, translate, select }57 enum Mode { extrude, translate, select, create_new } 54 58 55 59 private Mode mode = Mode.select; … … 97 101 */ 98 102 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 }; 99 122 100 123 /** … … 110 133 initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200); 111 134 selectedColor = PaintColors.SELECTED.get(); 135 cursorCreateNew = ImageProvider.getCursor("normal", "rectangle_plus"); 112 136 } 113 137 … … 117 141 else if (mode == Mode.extrude) 118 142 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."); 119 145 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."); 121 148 } 122 149 … … 129 156 Main.map.mapView.addMouseListener(this); 130 157 Main.map.mapView.addMouseMotionListener(this); 158 try { 159 Toolkit.getDefaultToolkit().addAWTEventListener(altKeyListener, AWTEvent.KEY_EVENT_MASK); 160 } catch (SecurityException ex) { 161 } 131 162 } 132 163 … … 135 166 Main.map.mapView.removeMouseMotionListener(this); 136 167 Main.map.mapView.removeTemporaryLayer(this); 168 try { 169 Toolkit.getDefaultToolkit().removeAWTEventListener(altKeyListener); 170 } catch (SecurityException ex) { 171 } 137 172 super.exitMode(); 138 173 } … … 140 175 /** 141 176 * If the left mouse button is pressed over a segment, switch 142 * to either extrude or translate mode depending on whether Ctrlis held.177 * to either extrude, translate or create_new mode depending on whether Ctrl or Alt is held. 143 178 */ 144 179 @Override public void mousePressed(MouseEvent e) { … … 159 194 if ((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) { 160 195 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; 161 201 } else { 162 202 mode = Mode.extrude; … … 225 265 // Just sit tight and wait for mouse to be released. 226 266 } else { 227 //move and extrude mode - move the selected segment267 //move, create new and extrude mode - move the selected segment 228 268 229 269 EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y); … … 261 301 Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this); 262 302 263 if (mode == Mode.extrude ) {303 if (mode == Mode.extrude || mode == Mode.create_new) { 264 304 //nothing here 265 305 } else if (mode == Mode.translate) { … … 293 333 // Nothing to be done 294 334 } else { 295 if (mode == Mode. extrude) {335 if (mode == Mode.create_new) { 296 336 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) { 297 374 // create extrusion 298 375 … … 349 426 } 350 427 428 boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0; 351 429 // Switch back into select mode 352 Main.map.mapView.setNewCursor( cursor, this);430 Main.map.mapView.setNewCursor(alt ? cursorCreateNew : cursor, this); 353 431 Main.map.mapView.removeTemporaryLayer(this); 354 432 selectedSegment = null; … … 439 517 Point p4 = mv.getPoint(newN2en); 440 518 441 if (mode == Mode.extrude ) {519 if (mode == Mode.extrude || mode == Mode.create_new) { 442 520 // Draw rectangle around new area. 443 521 GeneralPath b = new GeneralPath();
Note:
See TracChangeset
for help on using the changeset viewer.