Ticket #6323: create_new_mode_6323.patch
| File create_new_mode_6323.patch, 8.6 KB (added by , 15 years ago) |
|---|
-
src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java
### Eclipse Workspace Patch 1.0 #P JOSM
4 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 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; 9 10 import java.awt.Cursor; 10 11 import java.awt.Graphics2D; 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; 16 20 import java.awt.geom.AffineTransform; … … 50 54 */ 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; 56 60 … … 97 101 */ 98 102 private MoveCommand moveCommand; 99 103 104 /** The cursor for the 'create_new' mode. */ 105 private final Cursor cursorCreateNew; 106 100 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 }; 122 123 /** 101 124 * Create a new SelectAction 102 125 * @param mapFrame The MapFrame this action belongs to. 103 126 */ … … 109 132 putValue("help", ht("/Action/Extrude")); 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 114 138 @Override public String getModeHelpText() { … … 116 140 return tr("Move a segment along its normal, then release the mouse button."); 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, Alt-double click to add a new node."); 121 148 } 122 149 123 150 @Override public boolean layerIsSupported(Layer l) { … … 128 155 super.enterMode(); 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 133 164 @Override public void exitMode() { 134 165 Main.map.mapView.removeMouseListener(this); 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 } 139 174 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) { 145 180 if(!Main.map.mapView.isActiveLayerVisible()) … … 158 193 159 194 if ((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) { 160 195 mode = Mode.translate; 196 } else if ((e.getModifiers() & ActionEvent.ALT_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; 163 203 getCurrentDataSet().setSelected(selectedSegment.way); … … 224 264 if (mode == Mode.select) { 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); 230 270 EastNorth mouseEn = Main.map.mapView.getEastNorth(e.getPoint().x, e.getPoint().y); … … 260 300 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) { 266 306 //move nodes to new position … … 292 332 if (mode == Mode.select) { 293 333 // Nothing to be done 294 334 } else { 295 if (mode == Mode.extrude) { 335 if (mode == Mode.create_new) { 336 if( e.getClickCount() == 2 && e.getPoint().equals(initialMousePos) ) { 337 // alt + double click add a new node 338 // Should maybe do the same as in DrawAction and fetch all nearby segments? 339 WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive.isSelectablePredicate); 340 if (ws != null) { 341 Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY())); 342 EastNorth A = ws.getFirstNode().getEastNorth(); 343 EastNorth B = ws.getSecondNode().getEastNorth(); 344 n.setEastNorth(Geometry.closestPointToSegment(A, B, n.getEastNorth())); 345 Way wnew = new Way(ws.way); 346 wnew.addNode(ws.lowerIndex+1, n); 347 SequenceCommand cmds = new SequenceCommand(tr("Add a new node to an existing way"), 348 new AddCommand(n), new ChangeCommand(ws.way, wnew)); 349 Main.main.undoRedo.add(cmds); 350 } 351 } 352 else if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) { 353 // crete a new rectangle 354 Collection<Command> cmds = new LinkedList<Command>(); 355 Node third = new Node(newN2en); 356 Node fourth = new Node(newN1en); 357 Way wnew = new Way(); 358 wnew.addNode(selectedSegment.getFirstNode()); 359 wnew.addNode(selectedSegment.getSecondNode()); 360 wnew.addNode(third); 361 wnew.addNode(fourth); 362 // ... and close the way 363 wnew.addNode(selectedSegment.getFirstNode()); 364 // undo support 365 cmds.add(new AddCommand(third)); 366 cmds.add(new AddCommand(fourth)); 367 cmds.add(new AddCommand(wnew)); 368 Command c = new SequenceCommand(tr("Extrude Way"), cmds); 369 Main.main.undoRedo.add(c); 370 getCurrentDataSet().setSelected(wnew); 371 } 372 } else if (mode == Mode.extrude) { 296 373 if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) { 297 374 // create extrusion 298 375 … … 438 515 Point p3 = mv.getPoint(newN1en); 439 516 Point p4 = mv.getPoint(newN2en); 440 517 441 if (mode == Mode.extrude ) {518 if (mode == Mode.extrude || mode == Mode.create_new) { 442 519 // Draw rectangle around new area. 443 520 GeneralPath b = new GeneralPath(); 444 521 b.moveTo(p1.x, p1.y); b.lineTo(p3.x, p3.y);
