Changeset 2374


Ignore:
Timestamp:
03/01/07 18:12:51 (13 years ago)
Author:
sorber
Message:

Select and drag individual polyline nodes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/overlays/PolylineTool.java

    r2333 r2374  
    2525 
    2626import java.awt.event.InputEvent; 
     27import java.util.Vector; 
    2728import loci.visbio.data.TransformEvent; 
    2829import loci.visbio.util.DisplayUtil; 
     
    4243  /** Curve currently being drawn or modified. */ 
    4344  protected OverlayPolyline line; 
     45 
     46  /** Curve close to the mouse when drawing is not occurring */ 
     47  protected OverlayPolyline near; 
     48 
     49  /** Nearest node on Polyline near */ 
     50  protected int nearNode; 
    4451 
    4552  /** Whether the active node of the polyline is anchored. */ 
     
    6875    nearHead = false; 
    6976    departed = false; 
     77    unsetMouseOverNode(); 
    7078  } 
    7179 
     
    8795    
    8896    if (line == null) { 
    89       line =  new OverlayPolyline(overlay, dx, dy, dx, dy); 
    90       configureOverlay(line); 
    91       overlay.addObject(line, pos); 
     97      if (!isMouseOverNode()) { 
     98        line =  new OverlayPolyline(overlay, dx, dy, dx, dy); 
     99        configureOverlay(line); 
     100        overlay.addObject(line, pos); 
     101      } 
    92102    } 
    93103    else { 
     
    129139  public void mouseDrag(DisplayEvent e, int px, int py, 
    130140    float dx, float dy, int[] pos, int mods) { 
     141    // System.out.println("mouseDrag"); // TEMP 
     142    if (isMouseOverNode()) { 
     143      near.setNodeCoords(nearNode, dx, dy); 
     144      overlay.notifyListeners(new TransformEvent(overlay)); 
     145    }  
     146    else { 
     147      mouseMoved(e, px, py, dx, dy, pos, mods); 
     148    } 
    131149  }  
    132150 
     
    134152  public void mouseUp(DisplayEvent e, int px, int py,  
    135153      float dx, float dy, int[] pos, int mods) { 
     154    if (isMouseOverNode()) { 
     155      // System.out.println("mouseUp"); // TEMP 
     156      near.updateBoundingBox(); 
     157      near.computeGridParameters(); 
     158      unsetMouseOverNode(); 
     159    } 
    136160  } 
    137161 
     
    184208        line.turnOffHighlighting(); 
    185209      } 
    186  
    187       overlay.notifyListeners(new TransformEvent(overlay)); 
    188     } 
     210    } 
     211    else { 
     212      // find out if you're near a node 
     213      OverlayObject[] objects = overlay.getObjects(); 
     214      double threshold = 2.0; 
     215      int[] objNode =  getNearestNode(display, objects, px, py, threshold); 
     216 
     217      unHighlightAllPolylines(objects); 
     218      if (objNode != null) { 
     219        int obj = objNode[0]; 
     220        int node = objNode[1]; 
     221        //System.out.println("near node " + node + " of object " + obj); // TEMP  
     222        OverlayPolyline pln = (OverlayPolyline) objects[obj]; 
     223        pln.setHighlightNode(node); 
     224        setMouseOverNode(pln, node); 
     225      } 
     226      else if (isMouseOverNode()) { 
     227        unsetMouseOverNode(); 
     228      } 
     229    } 
     230    overlay.notifyListeners(new TransformEvent(overlay)); 
    189231  } 
    190232 
    191233  // -- Helper methods --  
     234   
     235  private void unsetMouseOverNode() { 
     236    near = null; 
     237    nearNode = -1; 
     238  } 
     239   
     240  private void setMouseOverNode(OverlayPolyline pln, int node) { 
     241    near = pln; 
     242    nearNode = node; 
     243  } 
     244 
     245  private boolean isMouseOverNode() { return (near != null); } 
     246   
     247  /** Unhighlights all polylines in a list of OverlayObjects */ 
     248  private void unHighlightAllPolylines(OverlayObject[] objects) { 
     249    for (int i=0; i<objects.length; i++) { 
     250      if (objects[i] instanceof OverlayPolyline)  
     251        ((OverlayPolyline) objects[i]).turnOffHighlighting(); 
     252    } 
     253  } 
     254 
     255  /** Finds nearest (subject to a threshold) node of all polylines 
     256   *  in a list of OverlayObjects. 
     257   *  @param objects An array of OverlayObjects on this display 
     258   *  Returns an array int[2], with item 0 the index of the nearest polyline 
     259   *  in the objects array, item 1 the index of the nearest node in the nearest 
     260   *  polyline */ 
     261  private int[] getNearestNode(DisplayImpl display, 
     262      OverlayObject[] objects, int px, int py, double threshold) { 
     263    Vector polylines = new Vector(); 
     264    Vector indices = new Vector(); 
     265    double[] p = {(double) px, (double) py}; 
     266 
     267    for (int i=0; i<objects.length; i++) { 
     268      if (objects[i] instanceof OverlayPolyline) { 
     269        polylines.add(objects[i]); 
     270        indices.add(new Integer(i)); 
     271      } 
     272    } 
     273 
     274    int nearestPline = -1; 
     275    int nearestNode = -1; 
     276    double minDist = Double.POSITIVE_INFINITY; 
     277    for (int i=0; i<polylines.size(); i++) { 
     278      OverlayPolyline pln = (OverlayPolyline) polylines.get(i); 
     279      for (int j=0; j<pln.getNumNodes(); j++) { 
     280        float[] c = pln.getNodeCoords(j); 
     281        double[] cDbl = {c[0], c[1]}; // auto cast 
     282        int[] cPxl = DisplayUtil.domainToPixel(display, cDbl); 
     283        double[] cPxlDbl = {(double) cPxl[0], (double) cPxl[1]}; 
     284        double dist = MathUtil.getDistance (cPxlDbl, p); 
     285        if (dist < minDist && dist < threshold) { 
     286          minDist = dist; 
     287          nearestPline = ((Integer) indices.get(i)).intValue(); 
     288          nearestNode = j; 
     289        } 
     290      } 
     291    } 
     292 
     293    if (nearestPline == -1) return null; 
     294    else return new int[]{nearestPline, nearestNode}; 
     295  } 
     296   
     297 
    192298   
    193299  /** Ends drawing of the current line */ 
     
    199305      line.computeLength(); 
    200306      line.setDrawing(false); 
    201       line.setSelected(true); 
     307      line.setSelected(false); 
    202308      line = null; 
    203309    } 
Note: See TracChangeset for help on using the changeset viewer.