Changeset 5152 in josm


Ignore:
Timestamp:
Apr 1, 2012 7:11:26 PM (14 months ago)
Author:
simon04
Message:

see #3910 - original patch by Oliver Raupach - add lasso selection mode

Location:
trunk
Files:
3 added
4 edited

Legend:

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

    r5093 r5152  
    8484        rotate("rotate", null), 
    8585        merge("crosshair", null), 
     86        lasso("normal", "rope"), 
    8687        merge_to_node("crosshair", "joinnode"), 
    8788        move(Cursor.MOVE_CURSOR); 
     
    9899        } 
    99100    } 
     101 
     102    private boolean lassoMode = false; 
    100103 
    101104    // Cache previous mouse event (needed when only the modifier keys are 
     
    172175        mv.addMouseListener(this); 
    173176        mv.addMouseMotionListener(this); 
    174         mv.setVirtualNodesEnabled( 
    175                 Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0); 
     177        mv.setVirtualNodesEnabled(Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0); 
    176178        drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true); 
    177179        // This is required to update the cursors when ctrl/shift/alt is pressed 
     
    241243            break; 
    242244        case select: 
    243             c = "rect" + (shift ? "_add" : (ctrl ? "_rm" : "")); 
     245            if (lassoMode) { 
     246                c = "lasso"; 
     247            } else { 
     248                c = "rect" + (shift ? "_add" : (ctrl ? "_rm" : "")); 
     249            } 
    244250            break; 
    245251        } 
     
    727733        case select: 
    728734        default: 
    729             selectionManager.register(mv); 
     735            selectionManager.register(mv, lassoMode); 
    730736            selectionManager.mousePressed(e); 
    731737            break; 
     
    825831    public void selectionEnded(Rectangle r, MouseEvent e) { 
    826832        updateKeyModifiers(e); 
    827         selectPrims(selectionManager.getObjectsInRectangle(r, alt), e, true, true); 
     833        mv.repaint(); 
     834        selectPrims(selectionManager.getSelectedObjects(alt), e, true, true); 
    828835    } 
    829836 
     
    974981        return l instanceof OsmDataLayer; 
    975982    } 
     983 
     984    public void setLassoMode(boolean lassoMode) { 
     985        System.out.println(lassoMode); 
     986        this.selectionManager.setLassoMode(lassoMode); 
     987        this.lassoMode = lassoMode; 
     988    } 
    976989} 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ZoomAction.java

    r4982 r5152  
    6060    @Override public void enterMode() { 
    6161        super.enterMode(); 
    62         selectionManager.register(Main.map.mapView); 
     62        selectionManager.register(Main.map.mapView, false); 
    6363    } 
    6464 
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r5092 r5152  
    4141 
    4242import org.openstreetmap.josm.Main; 
     43import org.openstreetmap.josm.actions.LassoModeAction; 
    4344import org.openstreetmap.josm.actions.mapmode.DeleteAction; 
    4445import org.openstreetmap.josm.actions.mapmode.DrawAction; 
     
    109110 
    110111    // Map modes 
    111     private final MapMode mapModeSelect; 
     112    public final SelectAction mapModeSelect; 
    112113    private final MapMode mapModeDraw; 
    113114    private final MapMode mapModeZoom; 
     
    159160        toolBarActions.setFloatable(false); 
    160161        addMapMode(new IconToggleButton(mapModeSelect = new SelectAction(this))); 
     162        addMapMode(new IconToggleButton(new LassoModeAction(), true)); 
    161163        addMapMode(new IconToggleButton(mapModeDraw = new DrawAction(this))); 
    162164        addMapMode(new IconToggleButton(mapModeZoom = new ZoomAction(this))); 
  • trunk/src/org/openstreetmap/josm/gui/SelectionManager.java

    r3642 r5152  
    66import java.awt.Graphics; 
    77import java.awt.Point; 
     8import java.awt.Polygon; 
    89import java.awt.Rectangle; 
    910import java.awt.event.InputEvent; 
     
    9798    private boolean aspectRatio; 
    9899 
     100    private boolean lassoMode; 
     101    private Polygon lasso = new Polygon(); 
     102 
    99103    /** 
    100104     * Create a new SelectionManager. 
     
    116120     * @param eventSource The emitter of the mouse events. 
    117121     */ 
    118     public void register(NavigatableComponent eventSource) { 
     122    public void register(NavigatableComponent eventSource, boolean lassoMode) { 
     123       this.lassoMode = lassoMode; 
    119124        eventSource.addMouseListener(this); 
    120125        eventSource.addMouseMotionListener(this); 
     
    147152        if (e.getButton() == MouseEvent.BUTTON1) { 
    148153            mousePosStart = mousePos = e.getPoint(); 
     154 
     155            lasso.reset(); 
     156            lasso.addPoint(mousePosStart.x, mousePosStart.y); 
    149157        } 
    150158    } 
     
    160168                mousePosStart = mousePos = e.getPoint(); 
    161169            } 
    162             paintRect(); 
     170            if (!lassoMode) { 
     171                paintRect(); 
     172            } 
    163173        } 
    164174 
    165175        if (buttonPressed == MouseEvent.BUTTON1_DOWN_MASK) { 
    166176            mousePos = e.getPoint(); 
    167             paintRect(); 
     177            if (lassoMode) { 
     178                paintLasso(); 
     179            } else { 
     180                paintRect(); 
     181            } 
    168182        } else if (buttonPressed == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK)) { 
    169183            mousePosStart.x += e.getX()-mousePos.x; 
     
    182196        if (mousePos == null || mousePosStart == null) 
    183197            return; // injected release from outside 
    184  
    185198        // disable the selection rect 
    186         paintRect(); 
    187         Rectangle r = getSelectionRectangle(); 
     199        Rectangle r; 
     200        if (!lassoMode) { 
     201            paintRect(); 
     202            r = getSelectionRectangle(); 
     203 
     204            lasso = rectToPolygon(r); 
     205        } else { 
     206            lasso.addPoint(mousePos.x, mousePos.y); 
     207            r = lasso.getBounds(); 
     208        } 
    188209        mousePosStart = null; 
    189210        mousePos = null; 
     
    207228        Rectangle r = getSelectionRectangle(); 
    208229        g.drawRect(r.x,r.y,r.width,r.height); 
     230    } 
     231 
     232    private void paintLasso() { 
     233        if (mousePos == null || mousePosStart == null || mousePos == mousePosStart) { 
     234            return; 
     235        } 
     236 
     237        Graphics g = nc.getGraphics(); 
     238        g.setColor(Color.WHITE); 
     239 
     240        int lastPosX = lasso.xpoints[lasso.npoints - 1]; 
     241        int lastPosY = lasso.ypoints[lasso.npoints - 1]; 
     242        g.drawLine(lastPosX, lastPosY, mousePos.x, mousePos.y); 
     243 
     244        lasso.addPoint(mousePos.x, mousePos.y); 
    209245    } 
    210246 
     
    261297 
    262298    /** 
    263      * Return a list of all objects in the rectangle, respecting the different 
     299     * Return a list of all objects in the selection, respecting the different 
    264300     * modifier. 
    265      * @param alt Whether the alt key was pressed, which means select all objects 
    266      *      that are touched, instead those which are completly covered. 
    267      */ 
    268     public Collection<OsmPrimitive> getObjectsInRectangle(Rectangle r, boolean alt) { 
     301     * 
     302     * @param alt Whether the alt key was pressed, which means select all 
     303     * objects that are touched, instead those which are completely covered. 
     304     */ 
     305    public Collection<OsmPrimitive> getSelectedObjects(boolean alt) { 
     306 
    269307        Collection<OsmPrimitive> selection = new LinkedList<OsmPrimitive>(); 
    270308 
    271309        // whether user only clicked, not dragged. 
    272         boolean clicked = r.width <= 2 && r.height <= 2; 
    273         Point center = new Point(r.x+r.width/2, r.y+r.height/2); 
     310        boolean clicked = false; 
     311        Rectangle bounding = lasso.getBounds(); 
     312        if (bounding.height <= 2 && bounding.width <= 2) { 
     313            clicked = true; 
     314        } 
    274315 
    275316        if (clicked) { 
     317            Point center = new Point(lasso.xpoints[0], lasso.ypoints[0]); 
    276318            OsmPrimitive osm = nc.getNearestNodeOrWay(center, OsmPrimitive.isSelectablePredicate, false); 
    277319            if (osm != null) { 
     
    281323            // nodes 
    282324            for (Node n : nc.getCurrentDataSet().getNodes()) { 
    283                 if (n.isSelectable() && r.contains(nc.getPoint(n))) { 
     325                if (n.isSelectable() && lasso.contains(nc.getPoint(n))) { 
    284326                    selection.add(n); 
    285327                } 
     
    293335                if (alt) { 
    294336                    for (Node n : w.getNodes()) { 
    295                         if (!n.isIncomplete() && r.contains(nc.getPoint(n))) { 
     337                        if (!n.isIncomplete() && lasso.contains(nc.getPoint(n))) { 
    296338                            selection.add(w); 
    297339                            break; 
     
    301343                    boolean allIn = true; 
    302344                    for (Node n : w.getNodes()) { 
    303                         if (!n.isIncomplete() && !r.contains(nc.getPoint(n))) { 
     345                        if (!n.isIncomplete() && !lasso.contains(nc.getPoint(n))) { 
    304346                            allIn = false; 
    305347                            break; 
     
    315357    } 
    316358 
     359    private Polygon rectToPolygon(Rectangle r) { 
     360        Polygon poly = new Polygon(); 
     361 
     362        poly.addPoint(r.x, r.y); 
     363        poly.addPoint(r.x, r.y + r.height); 
     364        poly.addPoint(r.x + r.width, r.y + r.height); 
     365        poly.addPoint(r.x + r.width, r.y); 
     366 
     367        return poly; 
     368    } 
     369 
     370    public void setLassoMode(boolean lassoMode) { 
     371        this.lassoMode = lassoMode; 
     372    } 
     373 
    317374    public void mouseClicked(MouseEvent e) {} 
    318375    public void mouseEntered(MouseEvent e) {} 
Note: See TracChangeset for help on using the changeset viewer.